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.
This opts in for the more concise syntax introduced in C++17: namespace
a::b { ... }.
Usage of this is especially useful in codebases where existing forward
declarations in nested namespaces already use this form: so the IWYU
suggestion for new forward declarations can be consistent with the
existing ones.
fix_includes.py already handled this, but add a test to maintain this
behavior, too.
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.
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!
assertListEqual is available in Python's unittest from 2.7 and onwards,
and provides much nicer diff output.
assertTrue is the same as assert_ but without causing a
DeprecationWarning.
assertSetEqual gives better diff output than a simple assertEqual.
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.
correct' here data, when a file was reported on more than
once.
R=dsturtevant
DELTA=57 (53 added, 3 deleted, 1 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=3314
#define line when it was really just a normal #define line.
As a result, we were treating the line as not really
contentful, when it is. Now we correctly identify #define
lines, and insert stuff into the right place.
R=dsturtevant
DELTA=39 (39 added, 0 deleted, 0 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=3286
edit it, even if includes need to be resorted, blank lines
inserted, etc.
This is entirely a policy decision: our policy used to be
otherwise (as demonstrated by the test), but it turned out to
be more annoying than helpful.
R=wan
DELTA=18 (13 added, 1 deleted, 4 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=2495
fix_includes.py, not just iwyu. Otherwise, fix_includes was
reordering the files incorrectly.
R=nilton
DELTA=99 (96 added, 0 deleted, 3 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=2494
Write forward-declaration namespaces in IWYU format
(compact single-line format), not "normalized" format.
Add a user-switch to trigger this (non-default) option.
This CL does not add tests for the new flag. But it does fix tests that are affected by the addition of a new flag.
Also fixed one failing testcase from a previous checkin.
R=wan,csilvers
DELTA=33 (23 added, 3 deleted, 7 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1853
header guard to be a 'contentful' line, and thus were
unwilling to put forward-declares after it in some situations
(particularly inside namespaces). This affected every .h file
at google, basically!
R=wan
DELTA=62 (59 added, 0 deleted, 3 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1850
all of them. After this small fix is in, I'll look to revamp
the entire routine to make it smarter.
Also cleaned up missing r's in front of regexp strings.
R=wan,dsturtevant
DELTA=75 (46 added, 12 deleted, 17 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1751
Remove duplicate forward declare suggestions. The problem arises if
two analyses of the same file suggest forward declarations with
differently-named template arguments.
R=csilvers
DELTA=54 (48 added, 0 deleted, 6 changed)
Change on 2011-05-04 10:05:05-07:00 by csilvers
While exploring another bug regarding default template
parameters, I ran across this one, where we wrongly required a
full type when none was available. This bug, at least, is
easy to fix.
R=dsturtevant
DELTA=44 (36 added, 0 deleted, 8 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1737
is specified (default, yes), and if all files to modify were read-only,
create a checkout cl and put the files into it. This may not be so appropriate for opensource.
R=wan,csilvers
DELTA=236 (191 added, 0 deleted, 45 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1724
instructions on how the user may test the changes IWYU
introduced.
R=csilvers
DELTA=144 (125 added, 0 deleted, 19 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1717
during the fixit as most of the fixit will run in phase one now.
The --nosafe_headers mode remains to get the full advantage of IWYU on any
particular code.
R=wan,csilvers
DELTA=37 (0 added, 31 deleted, 6 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1610
the beginning of the line but didn't go to the end. I thought
this wouldn't happen in google code, but it does in macros:
#define FOO \
/* doing something cool now */ \
...
Rather than just check for the special case with backslash
after the comment, I "correctly" deal with all cases where a
c-style comment-line doesn't take the entire line. I put
"correctly" in quotes because I always call such lines
'contentful' lines, when they could also be forward-declare
lines. I'm ok with that.
While testing, discovered an off-by-one error, which I now fix
and test for as well.
R=wan
DELTA=60 (56 added, 0 deleted, 4 changed)
Revision created by MOE tool push_codebase.
MOE_MIGRATION=1609