Commit Graph

98 Commits

Author SHA1 Message Date
Kim Gräsman a83809985d [fix_includes] Rephrase usage epilog 2022-12-16 22:16:39 +01:00
Kim Gräsman 523c16e8da [fix_includes] Use argparse instead of deprecated optparse
Roughly following
https://docs.python.org/3/library/argparse.html#upgrading-optparse-code.

The --help output comes out slightly different, but no other functional
change intended.
2022-12-16 22:16:39 +01:00
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
Daniel Hannon 67942da411 clarify purpose of --update_comments in help screen and manpages 2022-05-28 11:03:40 +02: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
Kim Gräsman d0ad085893 [fix_includes] Exit with zero on success 2022-02-26 12:21:26 +01: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
Tim Gates a95999b244 docs: fix simple typo, proceded -> preceded
There is a small typo in fix_includes.py.

Should read `preceded` rather than `proceded`.
2020-10-10 10:38:32 +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
Jakub Wilk 22f6d1d48e [fix_includes] Simplify regexp 2019-04-06 12:41:33 +02: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
Kim Grasman 6b236db52b [iwyu_tool] Fix formatting and add doc comment
No functional change.
2018-09-29 18:42:08 +02: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
Kim Grasman 854c91dbcc [fix_includes] Remove left-over comment 2018-08-19 10:55:07 +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
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 cdaa315aba Fix typo in fix_includes.py 2018-07-08 15:31:42 +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 bca15165b8 Flatten logic in FixManyFiles
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.
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 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 831b32bf12 Use OrderedDict to maintain IWYU output order.
Python 3 randomizes sort order of regular dicts, so output would get
jumbled for tests.
2015-05-11 20:38:37 +00:00
Kim Gräsman 062e01d0e5 Materialize a list for Python 3. 2015-05-11 20:38:19 +00:00
Kim Gräsman 687570f567 Introduce compat shim for Python 3 next(). 2015-05-11 20:38:01 +00:00
Kim Gräsman b2045c4f20 Remove None reorder spans before using them.
This makes the code Python 3-compatible as we're no longer comparing
reorder spans with None.
2015-05-11 20:37:43 +00:00
Kim Gräsman 3ff8c878db Remove uses of apply() to appease Python 3. 2015-05-11 20:37:25 +00:00
Kim Gräsman a43e9f63f0 Python 3-friendly range (i.e. don't use xrange) 2015-05-11 20:37:08 +00:00
Kim Gräsman ab485e3db3 Python 3-friendly dict items iteration. 2015-05-11 20:36:48 +00:00
Kim Gräsman 5daff2896f Python 3-friendly except syntax. 2015-05-11 20:36:13 +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
Kim Gräsman ab3f77b2c1 Fix issue 143: fix_includes c++ pattern 2014-06-04 04:17:08 +00:00
Kim Gräsman 35d075835e Allow forward slashes as path separators on Windows.
This makes fix_includes_test.py succeed on Windows too.
2014-06-03 18:12:04 +00:00
Kim Gräsman acf089d610 Fix issue #95: fix_includes.py now preserves line endings
If a file has mixed line endings or line endings can't be decided,
use Unix line endings.
Patch by André Stein.
2013-03-25 06:14:49 +00:00