Commit Graph

41 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 3ad4d9b058 [iwyu_tool] Exit with max exit code of IWYU invocations 2022-02-26 12:21:26 +01:00
Sven Panne 0997656913 Always use one-line messages with --output-format=clang
Apart from being easier to read by humans, this improves the cooperation
with other tools (e.g. Jenkins' warnings-ng plugin, various Emacs modes)
quite a bit. As an example, here the previous output:

    Foo.h:1:1: error: add the following line
    class Bar;
    Foo.h:18:1: error: remove the following line
    #include "Bar.h"
    Foo.cc:1:1: error: add the following line
    #include "Baz.h"          // for Huey, Dewey, Louie (ptr only)
    Foo.cc:19:1: error: remove the following line
    #include "Blah.h"

With this patch:

    Foo.h:1:1: error: add 'class Bar;'
    Foo.h:18:1: error: superfluous '#include "Bar.h"'
    Foo.cc:1:1: error: add '#include "Baz.h"' (for Huey, Dewey, Louie (ptr only))
    Foo.cc:19:1: error: superfluous '#include "Blah.h"'
2021-09-26 16:40:14 +02:00
Florian Schmaus 8c8a6537d3 [iwyu_tool] Allow float as -l argument
Most tools, e.g. GNU Make or ninja, allow the -l argument to be
provided as floating point value. Hence it is sensible that we allow
this too.
2021-04-13 20:26:57 +02:00
Florian Schmaus 6e6b06ed13 [iwyu_tool] Add support for load average limiting via --load
If "--load N" is given, potentially limit the capacity of new jobs.
2021-04-05 14:54:31 +02:00
Omer Anson a7499e4a2b [iwyu_tool] Notify success/failure via exit code
Modify the exit code of iwyu_tool.py so that it returns 0 only
when include-what-you-use does make any recommendations.

Related: #440
2020-12-07 07:41:37 +01:00
Florian Schmaus a8cd91a62b [iwyu_tool] remove compiler wrappers from the command list
If a compiler wrapper like ccache is used in the compile command
database, then iwyu_tool will only replace the wrapper with
IWYU_EXECUTABLE, while keeping the compiler executable in the
compile_args list. This leads to "error: no such file or directory
'c++'" warnings.

Fixes #789.
2020-05-08 19:56:33 +02:00
Kim Grasman 3e17d6156c [iwyu_tool] Fail fast if include-what-you-use is not found 2019-12-21 15:55:47 +01:00
Kim Grasman 0f49a6028e [iwyu_tool] Print all diagnostic ouptut to stderr 2019-12-21 15:55:47 +01:00
Kim Grasman b280c3fd6f [python] Clean up license headers
Apply all changes suggested by iwyu-check-license-header.py to get
strictly conforming license headers.
2019-12-04 21:42:46 +01:00
Miklos Vajna 38dba88261 [iwyu_tool] Use directory from compilation database if availabl
If the entry looks like this:

    {
        "arguments": [
            ....
        ],
        "directory": "/path/to/test",
        "file": "Test.cpp"
    },

And we invoked iwyu_tool.py in /path/to, then the constructed abs path was
/path/to/Test.cpp, not /path/to/test/Test.cpp, fix this.
2019-10-06 10:46:19 +02:00
Kim Grasman 3776ea410e [iwyu_tool] Add test for compilation db path canonicalization
This drove out a refactor to read the file separately from
canonicalization for easier testing.
2019-09-30 21:58:01 +02:00
Asier Lacasta cd358ea9cf [iwyu_tool] Use extra args verbatim
This is a breaking change.

Originally, the extra arguments after '--' were only intended for IWYU
args, and so '-Xiwyu' was automatically prepended to each of them to
make life easier on users.

But this made it impossible to add additional Clang args, which is also
a useful feature.

The extra arguments are now forwarded as-written to main and users need
to manually add '-Xiwyu' before every IWYU argument.
2019-01-04 23:12:09 +01:00
Kim Grasman 88dea9a97c [iwyu_tool] Make bootstrap argument parsing more testable
Much easier to test if it doesn't directly access sys.exit
or sys.argv.

No functional change.
2019-01-04 23:12:09 +01:00
Kim Grasman 0e03ac1969 [iwyu_tool] Rename iwyu_args -> extra_args
No functional change.
2019-01-04 23:12:09 +01:00
Kim Grasman 6f8c0020f0 [iwyu_tool] Fold consecutive whitespace in command strings
Fix bug where every additional whitespace character would turn into an
empty argument in argument list.
2018-12-22 19:31:55 +01:00
Kim Grasman 1d4f2673ce [iwyu_tool] Fix command splitting for Windows
shlex.split does not do the right thing on Windows, not even with the
posix=False flag set.

Work around this with a hand-built win_split function, based on the
more or less official specification:
https://docs.microsoft.com/en-us/previous-versions/17w5ykft(v=vs.140)

Add tests.

No behavior change for non-Windows systems.

Fixes #583.
2018-12-11 22:10:57 +01:00
Kim Grasman 8e2b4455f1 [iwyu_tool] Improve MSVC-style driver detection
Match cl.exe without case on Windows to allow for e.g. CL.EXE.

Also match 'clang-cl', which could presumably be used as a cross-tool on
non-Windows systems.

Add tests.
2018-12-11 21:56:39 +01:00
Kim Grasman a7b16c0a88 [iwyu_tool] Case-insensitive path match on Windows
User-provided paths are now matched case-insensitively.

Also fix a bug where /a/b/partial would match partial filenames. The new
implementation is stricter and only matches proper directory prefixes or
the full path name.

Closes bug #582.
2018-12-09 21:02:16 +01:00
Kim Grasman b7438423be [iwyu_tool] Style cleanup
- Two blank lines between module-level entities.
- Imports after doc comment
- Naming in execute
- Collapse IWYUToolTestBase into IWYUToolTests

No functional change.
2018-12-09 19:19:15 +01:00
Tom Rix 114934378e When include-what-you-use if found, return the real path
If iwyu_tool.py is call by a relative path, include-what-you-use will
also be called with a static, relative path. Which breaks once the
cwd changes.  So force the tool path to be absolute.
2018-11-21 22:12:03 +01:00
Martin Villagra a766b58d60 look for iwyu binary from environment variable in iwyu_tool.py 2018-11-06 07:09:41 +01:00
Philip Pfaffe dbab920114 [iwyu_tool] Drop multiprocessing
Python's multiprocessing module is convenient for task parallelization,
but it is rather weak when it comes to error reporting and handling
KeyboardInterrupt. It was basically impossible to interrupt iwyu_tool
with Ctrl+C.

Also, it seems silly to launch a process just to launch a process --
iwyu_tool.py basically just manages include-what-you-use child
processes.

Remove the use of multiprocessing and handle process scheduling
ourselves.
2018-09-29 15:35:53 +02:00
Philip Pfaffe 2e1e0e16db [iwyu_tool] Refactor for testability and add unit test
- Add execute function to run all invocations (asynchronously)
- Move process invocation to Invocation.run
- Make Invocation.from_compile_command a class method to make it easier
  to stub Invocation.run when under test.

With this in place, add basic test cases for compile command parsing and
execution.
2018-09-08 11:46:53 +02:00
Kim Grasman 3e110237e7 [iwyu_tool] Introduce class for IWYU invocations
The new Invocation class knows how to parse a compilation database entry
and expose an IWYU command and a cwd.

This simplifies the main flow a little; all compilation database entries
are transformed to Invocations up-front, and then the pool can just work
away at process invocations.

No functional change.
2018-08-18 11:29:55 +02:00
Kim Grasman 5facf069ba [iwyu_tool] Improve compilation database filtering
- Add new function slice_compilation_db that returns a reduced
  compilation database tailored for the provided source files.
- Warn if path in selection does not exist on disk
- Warn if path in selection does not exist in compilation database

This also fixes a bug where iwyu_tool.py would say a file did not exist
in compilation database, when in fact it didn't exist on disk.
2018-08-18 11:29:55 +02:00
Kim Grasman 43d4e46d65 [iwyu_tool] Extract compilation database parsing to a function
No functional change.
2018-08-18 11:29:55 +02:00
Kim Grasman bc013c23e3 [iwyu_tool] Make executable search more explicit
This obviates the need for a custom environment, and gets rid of any
unnecessary PATH search when launching IWYU.
2018-08-18 11:29:55 +02:00
Kim Grasman 4c9767b29f [iwyu_tool] Improve include-what-you-use process launch
- Don't use shell=True, instead use different executable names for
  Windows/Unix
- Use shlex.split to parse 'command' entry of compilation
  database. Should handle arguments with spaces correctly.
- Maintain the compile command as a list throughout, instead of dumbing
  down to a space-separated string
- Move -Xiwyu-prefixing to command-line parsing
- In verbose mode, print include-what-you-use command-line with a '# '
  prefix instead of trailing colon.

No functional change intended.
2018-08-18 11:29:55 +02:00
Kim Grasman f8842ba8ce [iwyu_tool] Make formatters pure functions
Rather than printing, they now transform IWYU output to a new string.

Also remove iwyu_outputter and replace with an identity lambda.
2018-08-18 11:29:55 +02:00
Kim Grasman 61f8b907a4 [iwyu_tool] Cosmetic style cleanup
- Order imports by length
- Rephrase a doc comment
- Fix up blank lines
- Make all comments complete sentences

No functional change.
2018-08-18 11:29:55 +02:00
Paul Seyfert 615507a3cd pick include-what-you-use from same path as iwyu_tool.py
Avoid that iwyu_tool.py relies on finding include-what-you-use from the
PATH, i.e. even if iwyu_tool.py is not in the path and gets executed by
absolute path, it should still succeed in finding the executable binary
include-what-you-use.
2018-05-29 21:12:27 +02:00
J.Ru 079bf8ebf3 Add directory support to iwyu_tool.py
For each directory that is given, iwtyu_tool will search for all
source files contained in it (or in its subdirectories) and
process them all.
2018-05-25 20:32:27 +02:00
Kim Gräsman fbcc7e31f2 Support 'arguments' key in compilation databases
Move things around a little to make this easier to implement -- run_iwyu
now translates from a compilation database entry to a command-line.

This should fix issue #456.
2017-09-18 20:53:47 +02:00
Paul Seyfert bcc3f0a58e Add -j switch to iwyu_tool.py for parallel execution
Use Python's multiprocessing to run source files in parallel. Default number of
jobs is 1, so no functional change by default.

Minor bug (missing comma in argument parser) fixed and squashed by Kim
Grasman. This is the equivalent of PR #444.
2017-08-22 21:16:31 +02:00
Sven Panne d53f86d8f5 Add a new option for a more tool-friendly output.
The standard output format of iwyu is not very friendly for tools like
Jenkins' warnings-plugin (https://github.com/jenkinsci/warnings-plugin) or
editors.  This change adds a new -o/--output-format command line option with
'iwyu' being the traditional format and 'clang' the new one for mangling the
output into something clang-like, enabling nice integration into
emacs/Jenkins/etc. out of the box.

Somehow this should probably be the default and be handled on the native
side (not as a post-processing step), but the current change is probably the
least controversial and easiest one for now.
2016-11-28 20:48:43 +01:00
Sven Panne 2b0a939829 Use a variable name which makes pylint happy 2016-11-10 21:12:41 +01:00
Sven Panne 920dc142a1 Handle symbolic links in source paths correctly
When use realpath on source files given on the command line, so we should
better use realpath for the paths in the compilation DB, too.  Otherwise we
don't find source paths which contain a symbolic link and incorrectly emit a
warning instead.
2016-11-10 21:11:45 +01:00
Kim Grasman 0341c6c095 Use realpath instead of abspath
This allows the tool to accept source paths with symbolic links.

Adapted from a patch by Eugene Zelenko, thanks!
2016-07-10 22:23:52 +02:00
Kim Gräsman 0ad943e592 Add svn:executable on iwyu_tool.py. 2015-03-02 20:21:54 +00:00
Kim Gräsman 1ae54565cf Add Python wrapper to use compilation databases.
This should fix/work around issues #87 and #164.

Ideally IWYU should support compilation databases natively, but the Clang
Tooling infrastructure doesn't fit cleanly into IWYU's model, so we'll
start with this as a workable compromise.

Based on initial patches by Ryan Pavlik and showard314, thanks!
2015-03-02 20:17:29 +00:00