Commit Graph

72 Commits

Author SHA1 Message Date
Seth R. Johnson e4dd555000
Use python3 shebang for all python scripts
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.
2022-12-16 21:03:39 +01:00
Kim Gräsman cb4d34477a [fix_includes] Recognize namespace aliases
... as different from Allman-style namespace definitions.

Fixes #1022.
2022-05-24 06:17:30 +02:00
Salman Javed 75226c1ce8 Add `--update_comments` option
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.
2021-10-01 06:31:27 +02:00
John Bytheway f3bec0d16b Prevent deleting duplicate nested forward decls
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.
2020-03-22 20:40:58 +01:00
Uladzislau Paulovich 9e19ec7fae Ignore file extension case when detecting main CU 2019-12-02 19:37:14 +01:00
Uladzislau Paulovich 74ad1cbe39 Support hpp extension for main CU files in fix_includes.py 2019-11-28 07:34:32 +01:00
Miklos Vajna 353a6da9b7 Add new --cxx17ns option
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.
2019-02-06 19:55:19 +01:00
Tom Rix 8b63d319e9 Reduce spurious line separator changes.
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.
2019-01-30 21:06:46 +01:00
John Bytheway 6e4330dfcf Better fix_includes handling of template lines
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.
2019-01-30 20:50:20 +01:00
John Bytheway fff6dadc08 In fix_includes, treat associated as main-CU
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.
2019-01-05 21:43:05 +01:00
trix 7eaed91303 Use utf-8-sig encoding with utf-8 bom files 2018-12-11 22:28:00 +01:00
Philip Pfaffe 0a54b0ef99 [iwyu_tool] Add --basedir argument
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.
2018-09-29 18:35:42 +02:00
Kim Grasman c3b5264ca5 [fix_includes] Remove unused command-line flags
Neither of invoking_command_line or find_affected_targets appear to have
any purpose.

No functional change intended.
2018-08-19 14:51:57 +02:00
Phantal 5266ab8f5a [fix_includes] Add option to disable line re-ordering
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.
2018-08-19 10:43:08 +02:00
Christian Venegas b290cf5915 [fix_includes] Enhance to understand multi-level namespaces
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>
2018-08-18 11:56:27 +02:00
Kim Grasman 7dfb021310 Fix up indentation in fix_includes_test.py
No functional change.
2018-08-10 13:05:04 +02:00
Christian Venegas 0ac92dbbf3 Enhance fix_includes.py to understand Allman namespaces
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>
2018-07-10 19:27:44 +02:00
Victor Poughon 67b6ef4918 Add --only_re option to fix_includes.py 2018-07-08 15:31:42 +02:00
Kim Grasman 5082fddccb Detect and preserve original file encoding
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.
2018-04-08 19:03:35 +02:00
Kim Grasman eee0b0fc51 Make FileInfo available to both read/write of files 2018-04-08 19:03:35 +02:00
Kim Grasman cdb8ac8c1f Introduce FileInfo object for file encoding details
Currently only handles line separators with a new, simpler
implementation.
2018-04-08 19:03:35 +02:00
Kim Grasman 4bfe5d6e39 Simplify file writing
- Inline _WriteFileContentsToFileObject
- Rename _WriteFileContents -> _WriteFile
- Use context manager for file closing
- Use an explicit stub method instead of lambda

No functional change.
2018-03-19 21:21:09 +01:00
Kim Grasman 0ac36dadb8 Remove Google VCS support from fix_includes.py
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.
2018-03-19 21:21:09 +01:00
Kim Gräsman 37670ddaff Make fix_includes.py dry-run return proper exit code
This makes the script match the documentation better, as proposed by
issue #479.
2017-09-17 10:34:28 +02:00
Kim Grasman 6e28b990ba Add .H as a known header extension
Both in IWYU and fix_includes.py.

Fix issue #452.
2017-07-15 19:46:51 +02:00
Kim Grasman ee47cdff67 Make fix_includes understand Windows path names
This is based on a patch from @xuzhen1994, with a test case to reproduce
the original problem.
2017-07-04 22:06:08 +03:00
Philip Pfaffe 4d0ef26f1f Fix #280: unified diff output
`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!
2016-03-15 20:44:04 +01:00
Kim Gräsman 2aed859a08 Change shebang so that python is picked up from the PATH. 2015-05-11 20:39:32 +00:00
Kim Gräsman 187dc4e302 Use recommended unittest assertions.
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.
2015-05-11 20:39:15 +00:00
Kim Gräsman 496eb11b2c Add OrderedSet and use it where tests mind sorting
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.
2015-05-11 20:38:54 +00:00
Kim Gräsman ab485e3db3 Python 3-friendly dict items iteration. 2015-05-11 20:36:48 +00:00
Kim Gräsman 5a8d57b3cb Python 3-friendly StringIO. 2015-05-11 20:36:31 +00:00
Kim Gräsman d3c31d2d3d Python 3-compatible print. 2015-05-11 20:35:50 +00:00
Kim Gräsman c315b49aea Fix issue 165: #pragma once vs. #include ordering
Detect #pragma once in content and make sure it stays before
#includes.

Patch by Chris Glover, thanks!
2015-01-30 20:27:19 +00:00
csilvers+iwyu 0add290002 fix_includes was not correctly merging in 'includes are all
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
2011-09-23 17:18:46 +00:00
csilvers+iwyu 44a28faccc fix_includes was sometimes saying a line was a header-guard
#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
2011-09-20 11:44:55 +00:00
csilvers+iwyu 5b8cd16e26 If iwyu reports that a file has no substantive changes, don't
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
2011-07-07 17:02:40 +00:00
csilvers+iwyu 772f955c1e I have to put the new 'is associated header file' logic in
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
2011-07-07 17:02:06 +00:00
csilvers+iwyu 58f5d1c29a Added unit-tests for the namespace-format changes.
R=wan,csilvers,dsturtevant
DELTA=159  (157 added, 0 deleted, 2 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=1855
2011-05-12 23:12:33 +00:00
csilvers+iwyu 102f3014f9 Suggested feature for IWYU:
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
2011-05-12 23:11:08 +00:00
csilvers+iwyu a1118a7ef0 Fix a bug where we considered the #define that's part of the
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
2011-05-12 23:09:36 +00:00
csilvers+iwyu b174e75769 Remove a test that doesn't make sense outside google.
R=dsturtevant
DELTA=2  (2 added, 0 deleted, 0 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=1754
2011-05-04 21:32:54 +00:00
csilvers+iwyu 9c7ae87ba5 This resolves some of the bugs with merging namespaces but not
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
2011-05-04 20:52:05 +00:00
csilvers+iwyu debefe0e5d Change on 2011-05-04 06:41:44-07:00 by dsturtevant
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
2011-05-04 18:33:53 +00:00
csilvers+iwyu 7888430742 Revamp dependency checking.
R=wan,csilvers
DELTA=43  (19 added, 16 deleted, 8 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=1730
2011-05-04 18:29:08 +00:00
csilvers+iwyu 2d28a40a04 If --checkout command is "v4 edit" or "g4 edit" and --create_cl_if_possible
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
2011-05-04 18:20:05 +00:00
csilvers+iwyu 5fb7420847 Makes fix_includes.py (and therefore iwyu.py) print
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
2011-05-04 18:16:58 +00:00
csilvers+iwyu 0e8123bc34 Drop commenting on candidate lines for removal to reduce the noise of changes
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
2011-04-26 23:27:23 +00:00
csilvers+iwyu ea88085897 We weren't handling properly c-style comments that started at
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
2011-04-26 23:26:53 +00:00
csilvers+iwyu 4caf7dd9ea Rename --safe to the more-accurate --safe_headers.
R=jhi
DELTA=36  (1 added, 0 deleted, 35 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=1604
2011-04-26 23:24:47 +00:00