The `python` command may not exist on a system with Python 3 installed.
See https://peps.python.org/pep-0394/ for a discussion of the commands
expected to be installed.
Since Python2 was officially sunsetted almost three years ago
(https://www.python.org/doc/sunset-python-2/), IWYU should prefer
compatibility with newer systems over older ones.
Python code in scripts is still Python3/Python2-compatible, but we will no
longer make an effort to preserve Python2 support over time.
Fixes#1096.
Fixes#75 and fixes#494.
Add `--update_comments` option to make IWYU always print the 'why'
#include comments, regardless of whether any include lines need to be
added or removed. With this change, IWYU will update the "// for xyz"
comments as the code changes, preventing the comments from becoming
stale.
fix_includes.py could be over-zealous and delete forward declarations of
nested classes if they happened to share a name with another nested
class in the same file. This was because the analysis of top-level
spans detected #ifdefs and namespaces, but not classes as things which
made code non-top-level.
Luckily, the IWYU output makes it obvious when a forward declaration is
for a nested class. Take advantage of that to fix this bug.
Also, add a regression test.
A diff of the original and change file should show only changes related
to the includes and/or forward declarations. In files that have line
separators that do not match the FileInfo.linesep, those line will be
changed. So instead of stripping a file's line separators, reuse them.
For any new line, use the FileInfo.linesep.
Also, do not add a new line separator at the end of a file.
When multiple forward declarations were present of the form:
template<typename T>
class Foo;
fix_includes.py would consider the multiple 'template<typename T>'
lines to be duplicates and delete all but one.
Fix that, and add a test for it.
fix_includes, when sorting headers, will put main-CU #include lines
first.
It should respect 'IWYU pragma: associated' as a way for the user to
specify what the main-CU files are when its heuristics fail.
Make it do so, and test it.
Enable specifiying a base dir relative to which the IWYU output should
be interpreted.
With the path normalization that happens when interpreting IWYU output,
we now need to normalize command-line arguments as well, in order for
filtering to work correctly.
Let test framework simulate a cwd to be able to test this.
Current default behavior is to re-order modified lines relative to lines
of a similar type (all forward decls get sorted, all project headers get
sorted, etc).
Make two changes:
* Add options to enable/disable line re-ordering
* Default to no re-ordering; to get the original behavior, the user must
specify --reorder
Original patch by Phantal with some cleanup by Kim Gräsman.
The fix_includes.py script has logic to append new forward declares at
the namespace level if possible. This logic only tries on the first
namespace encountered, however, so projects with a lot of namespaces
require a lot of manual tidying up.
Add multi-level detection so nested namespaces have includes
automatically inserted without creating new namespace blocks when it is
possible to do so cleanly. If any issues are encountered when
evaluating namespaces, new forward declares will be added at the top
level.
Ifdeffed code will also be ignored when trying to evaluate namespaces.
Signed-off-by: Christian Venegas <cvenegas@esri.com>
The fix_includes.py script was originally written to work for namespaces
with { on the same line as the namespace. For styles that do not follow
this format, namespace detection does not work properly as the line
numbers and regex statement are not able to be evaluated.
Add logic to detect Allman namespaces and shift line numbers
appropriately, so that the following namespace formatting can be
handled:
namespace A
{
class Foo;
}
Also detect mixed same-line and Allman formatting, e.g.
namespace A { namespace B { namespace C
{
class Foo;
}
}
}
With the addition of Allman namespace detection, mixed namespace
detection has also been added to cover all namespace styles.
Signed-off-by: Christian Venegas <cvenegas@esri.com>
This uses a few simple heuristics to detect file encoding before
rewriting file contents.
All file I/O is now binary, and decoding/encoding is explicit based on
detected encoding.
Cover a few common encodings (utf-8, ascii, windows-1250, windows-1252),
and make it easy to add more encodings should it be necessary.
Should make fix_includes.py behave better under Python3 with
non-ASCII-encoded files.
- Inline _WriteFileContentsToFileObject
- Rename _WriteFileContents -> _WriteFile
- Use context manager for file closing
- Use an explicit stub method instead of lambda
No functional change.
Now that version control operations are no longer happening as part of
the fixup, we can simplify FixManyFiles significantly, and inline most
of GetFixedFile into a new FixOneFile function.
No functional change intended.
The script had rudimentary support for checking out files from Google's
version control system, which complicated testing and implementation
somewhat.
Now that this is not primarily a Google tool and most version control
systems do not use the read-only flag for preventing concurrent edits,
remove this code to simplify.
No functional change intended, unless you were using this support.
`fix_includes.py --dry_run` generated an invalid diff with an additional
newline between the @@ hunk header and the actual diff hunk.
This patch fixes that and adds test coverage.
Patch by Philip Pfaffe, thanks!
Python 3 randomizes sort order in dicts and sets, to the point where we
can't test the outputs deterministically. Implement a naive ordered set,
and use that where applicable.
Also switch one dict to OrderedDict for the same reason.