Commit Graph

48 Commits

Author SHA1 Message Date
Kim Gräsman 890a3150d7 Format all return statements on their own line
This is the unadorned result of:

    git grep -En "\w.*return .*;" -- ':!tests/*' | grep-format

Having return statements on their own line makes it easier to set
breakpoints in most debuggers.

No functional change.
2023-01-07 12:27:16 +01:00
Alexey Storozhev 7115b0e30a Get a FileEntry for <new> imports
This change is driven by the idea of switching to FileEntry/FileID-based
way of calculating IWYU violations.

Currently IWYU calculates the difference between presented and desired
imports by comparing the list of strings (OneIncludeOrForwardDeclareLine::line),
but it would probably be easier and more reliable to operate with
FileEntries and FileIDs to determine the unused imports and their
locations.

In order to switch from a string-based solution all the
"stringly"-represented imports should be replaced with a corresponding
FileEntry or a FileID.

This change uses the preprocessor to lookup the FileEntry for `<new>`
in the corner case that reports the "placement new" symbol usage.
2021-05-22 14:19:14 +02:00
Alexey Storozhev a2a92b6c6d Delete unused `using` declarations 2021-02-08 07:08:24 +01:00
John Bytheway b4838853e5 Remove trailing whitespace from source files 2020-05-24 14:14:59 +02:00
Kim Grasman f4ef6528d9 Expose use flags directly from OneUse
This makes it clearer which flags are used where.
2019-12-26 16:41:12 +01:00
Kim Grasman 7c2ec8f05b Rename port.h -> iwyu_port.h 2019-12-26 16:12:02 +01:00
J.Ru 27da44e009 Introduce an extra use flag UF_ExplicitInstantiation
To signal that the use refers to an explicit instantiation, for which the
"canonical" decl is not suitable.

Also, since it is meant to be used in a specific context, the ReportDeclUse
prototype has been adapted to take optional extra flags as an additional
input.
2018-12-07 09:12:01 +01:00
Kim Grasman 2406addcc7 Count free function definitions as uses
Now that we can provide context for uses with use flags, we can tweak
IWYU behavior when a function is being defined.

This allows us to treat a function definition as a use of all
previously-seen declarations.

Fixes #179, #491 and #441.
2018-03-09 20:37:52 +01:00
Kim Grasman aa2bc1f9e0 Replace in_cxx_method_body with an extensible use-flag
This makes it possible to pass more context information about a use
from the detection phase to later IWYU analysis phases.
2018-03-09 20:37:52 +01:00
J.Ru 576e80f7f3 Always report the expanded location for a decl
In cases where a declaration is generated using a macro, iwyu was
incorrectly suggesting to include the file that defined such macro
instead of the actual location where it was ultimately expanded.

When reporting the declaration for a use, make sure to always refer
to the expansion/instantiation of such declaration. Whether or not
it was part of a macro expansion should be irrelevant for the use.

Fixes #282
2017-10-04 20:16:37 +02:00
Kim Grasman dbceeb3c14 Fix inconsistent parameter names
Found by the clang-tidy check
readability-inconsistent-declaration-parameter-name

No functional change.

Based on patch by Eugene Zelenko, thanks!
2016-08-15 22:24:39 +02:00
Kim Grasman 94949d09a0 Add comments for closing namespace brace
Suggested by clang-tidy, original patch by Eugene Zelenko.

No functional change.
2016-08-15 22:07:27 +02:00
Kim Grasman aa2dd1bb8f Whitespace changes from clang-(tidy|format)
Presumably this makes formatting more consistent.
No functional change.

This is based on an original patch by Eugene Zelenko, thanks!
2016-08-15 22:04:47 +02:00
Volodymyr Sapsai 04f1c92537 Handle internal headers guarded by macro and x-macros (fix issue #109).
I've made a few trade-offs in implementation which I'd like to explain more.

First, code doesn't distinguish between guarded internal headers and
headers with x-macros. They are handled the same way. But in tests both
patterns are tested. It is done not to cover all code paths but to test
include-what-you-use from user's perspective.

Also I check if file defining macro is immediate includer. I decided not
to check if it includes file using the macro transitively until we have
such real-life use cases. Current implementation is strict in order to avoid
unexpected results.

For some cases I am reusing mechanism that keeps files included with the
"IWYU pragma: keep" comment. The downside is that it keeps all lines
including this file which might be not entirely correct for x-macros.
Though x-macros are close to pure textual includes and we cannot reason
about textual includes.

I also didn't include a few test cases because I think they don't
represent real-life use cases. These test cases are:
* when associated header is included by non-associated header;
* when file defining macro and file using macro are both included by the
  third file.
2016-08-14 17:28:46 -07:00
Kim Grasman 5b61a55190 Fix #310: Replace Each with C++11 range for loops
The Each construct was nice, but it's outlived its usefulness, the range for
loops are both easier to read and write.

For iteration over maps, we consistently use 'auto' to avoid repeating the map
value type.
2016-07-11 07:19:51 +02:00
Kim Grasman e58967a2ec Make header guards consistent
- Remove DEVTOOLS_MAINTENANCE_ from header guards, that was a
  now-unnecessary Googleism

- Fix header guard to match filename in all production code

- Fix header guard to match path in all tests
2016-05-25 22:17:31 +02:00
Volodymyr Sapsai 51fd96f226 Revert "Handle internal headers guarded by macro and x-macros (fix issue #109)."
This reverts commit 5fe99b054b.

It hits assertion
iwyu_include_picker.cc:986: Assertion failed: filepath_visibility_map_[quoted_filepath_pattern] == vis Same file seen with two different visibilities
2016-05-04 21:52:14 -07:00
Volodymyr Sapsai 5fe99b054b Handle internal headers guarded by macro and x-macros (fix issue #109).
I've made a few trade-offs in implementation which I'd like to explain more.

First, code doesn't distinguish between guarded internal headers and
headers with x-macros. They are handled the same way. But in tests both
patterns are tested. It is done not to cover all code paths but to test
include-what-you-use from user's perspective.

Also I check if file defining macro is immediate includer. I decided not
to check if it includes file using the macro transitively until we have
such real-life use cases. Current implementation is strict in order to avoid
unexpected results.

Note, that file using macro should be included with #include, not with
defines LLONG_MIN, LLONG_MAX and #include_next system <limits.h> which
on Mac OS X uses [2] LLONG_MIN and LLONG_MAX in C++11 mode.

For some cases I am reusing mechanism that keeps files included with the
"IWYU pragma: keep" comment. The downside is that it keeps all lines
including this file which might be not entirely correct for x-macros.
Though x-macros are close to pure textual includes and we cannot reason
about textual includes.

I also didn't include a few test cases because I think they don't
represent real-life use cases. These test cases are:
* when associated header is included by non-associated header;
* when file defining macro and file using macro are both included by the
  third file.

[1] http://clang.llvm.org/doxygen/limits_8h_source.html
[2] http://opensource.apple.com/source/Libc/Libc-1082.20.4/include/limits.
2016-05-03 22:52:29 -07:00
Chris Glover 4c2e039e9d Add support for using declarations such that "using std::swap;" does not cause all std::swap headers to be included.
This was caused by the fact that IWYU, when encountering a UsingDecl, adds everything that the UsingDecl could refer to as a forward declarable. Because IWYU automatically upgrades function decls to a full use (ie: not forward declarable), unnecessary includes are added based on unused functions.

This change alters the logic as follows;

- Remove the code that adds the UsingShadowDecls of an encountered UsingDecl.
- Record the fact that we've encountered a UsingDecl and mark it as un-used.
- Add support to the Record* functions such that when encountering a UsingShadowDecl, we can know we're accessing a Decl through a UsingDecl.
- Mark referenced UsingDecl as 'used' when accessed through a UsingShadowDecl.
- At the end of processing, if a UsingDecl has not been referenced, arbitrarily add one of the symbols it is referencing so that we don't remove all headers it refers to and break compilation.

The change also adds several unit test that catch variations of this problem an demonstrate that they're fixed.
2015-10-25 18:21:39 -04:00
Kim Gräsman 0b15a83af9 Fix issue 157: Return different exit codes. 2014-11-30 09:26:43 +00:00
Kim Gräsman 289e966f96 Fix issue 140: pragma keep doesn't keep duplicates
The removal of duplicates is now countered by recording the included FileEntry
of all include lines marked "IWYU pragma: keep", and then marking all includes
of said FileEntry as desired.

We think that if the user goes to the trouble of marking an include line with
the keep pragma, they can probably explicitly remove additional includes of
that header manually if they're not wanted.
2014-10-05 19:38:22 +00:00
Kim Gräsman 24d23e0ef8 Make #include line duplicate removal explicit.
Refactor CalculateDesiredIncludesAndForwardDeclares so it
doesn't implicitly remove duplicate #include lines or forward
declares.

Rather, it keeps track of all of them, and then explicitly
cleans out duplicates.

This is a preparatory step for fixing issue 140.
2014-08-25 19:37:29 +00:00
Kim Gräsman 452df335a0 Fix issue 126: Precompiled-headers support with --prefix_header_includes
IWYU now supports a new --pch_in_code switch which indicates the first
include directive in the translation unit is a precompiled header.
Precompiled headers are treated as prefix headers, even though they
were not specified on the command-line, but they can never be removed.
Finally, they sort first in include lists generated by IWYU, because
most compilers require the PCH to be included before anything else.
2014-04-27 20:35:44 +00:00
Volodymyr Sapsai 63ed99e20a Handle macros and placement operator new in prefix headers (issue #125).
Keep FileEntry for the files defining macros, find FileEntry by quoted include
for placement operator new.

Known problems: added 2 tests fail on Windows due to issue #129 "Unused
templates are ignored on Windows".
2014-04-24 16:08:25 +00:00
Kim Gräsman b3c6e2f7e8 Fix issue 115: rename "internal" to "associated" headers.
Both terms were used for the same concept (a header with the same base name as its associated source file, e.g. foo.h/foo.cpp) inconsistently within the code base.
No functional change.
2013-12-22 22:21:06 +00:00
Volodymyr Sapsai d30ef5a05f Add --prefix_header_includes option (fixes issue #112).
--prefix_header_includes controls presence of includes and forward declarations
involving files included via command-line option -include. Issue is reported by
Max Dyckhoff.
2013-12-15 12:33:18 +00:00
Volodymyr Sapsai f17313577c Make "associated desired includes" mechanism more obvious.
Extract a method to show that the same cycle is used for the same purpose. No 
functional change intended.
2013-11-07 14:09:22 +00:00
csilvers+iwyu c23f0b9eef Some refactoring to straighten out dependencies.
This was motivated by an attempt to understand how iwyu_ast_util.cc
depended on iwyu_output, which turned out to be the use of VERRS.
I split the VERRS definition out into a separate iwyu_verrs module.
In the fallout from this I discovered some circular dependencies
that this CL attempts to disentangle.

R=csilvers
DELTA=603  (349 added, 222 deleted, 32 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=3892
2011-12-01 02:30:18 +00:00
csilvers+iwyu ac177329d5 Fix a bug that would come up when using -I -- when looking for
associated header files, we would use the filename as it
exists on the filesystem, not the one that is already used in
the code (due to -I).  As a result, we could suggest a user
add an #include of an associated .h file, even if they were
already #including it (but typed differently due to -I).

Patch is from vasp...@gmail.com, at
http://code.google.com/p/include-what-you-use/issues/detail?id=5#c25

R=dsturtevant,chandlerc
DELTA=77  (71 added, 0 deleted, 6 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=3430
2011-10-07 21:51:59 +00:00
csilvers+iwyu 4ca582117b iwyu would get confused when seeing the same #include twice
(because, say, a .h was included twice and didn't have a
header guard, so all its content was repeated).  Fix this by
doing a uniqueness check when adding #includes.

We could speed this up by storing a map, but typically a file
doesn't have so many includes that a quadratic-time algorithm
over them is going to matter.

R=dsturtevant
DELTA=77  (75 added, 0 deleted, 2 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=3315
2011-09-23 17:19:07 +00:00
csilvers+iwyu 60272bf071 Don't ask me why, but the automated system I use for updating patches wants to undo Paul Holden's latest patches and then re-apply them. I'm sure there's a way to skip doing that, but I don't know what it is, so I'm going to let it do its thing. This is step 1 (the revert).
Revision created by MOE tool push_codebase.
MOE_MIGRATION=2805
2011-08-01 20:57:19 +00:00
csilvers+iwyu 256da9418d Include-what-you-use fixit -- run iwyu on itself to fix up includes (part 2).
R=csilvers
DELTA=51  (3 added, 47 deleted, 1 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=1736
2011-05-04 18:32:59 +00:00
csilvers+iwyu 5ca17d139e Include-what-you-use fixit -- fix #includes on iwyu itself.
R=csilvers
DELTA=123  (22 added, 5 deleted, 96 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=1732
2011-05-04 18:30:53 +00:00
csilvers+iwyu 3d55404056 Include-what-you-use fixes by running it on itself.
R=dsturtevant,csilvers
	DELTA=215  (135 added, 45 deleted, 35 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=1731
2011-05-04 18:29:59 +00:00
csilvers+iwyu 3f0a6a763e Add the ability to add arbitrary comments to the warning
messages that iwyu produces (and that are shown with
--mode=why for iwyu.py).  Use this ability to comment two
non-obvious warnings we give: when requiring a full use
because of autocasting (one-arg, not-explicit constructor),
and when requiring a full use for the return type of a
function declaration.  Update appropriate existing tests to
check for the comment when appropriate.

R=wan,dsturtevant
DELTA=173  (82 added, 25 deleted, 66 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=1726
2011-05-04 18:21:22 +00:00
csilvers+iwyu 54eef2afa3 Reorganize the commandline flags by storing them in a globally
accessible 'commandlineflags' struct.  This replaces the
rather ad-hoc collection of variables and functions, spread
across 3 different files, that we have now.  The motivation is
adding a new commandline flag that I want to be visible in yet
a fourth file; I figured that was a good time to consolidate.
Now iwyu_globals holds everything.

R=wan,dsturtevant
DELTA=306  (161 added, 112 deleted, 33 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=1591
2011-04-26 23:12:44 +00:00
csilvers+iwyu 4813f2716e Handle dependencies on 'using' declarations properly. If we
use a type via a using declaration, we need to make sure to
use that using declaration as well.  It may be in a different
file from the type!

There may be corner cases we don't handle right yet, but we do
the right thing at least most of the time.  One case we don't
handle is when there's a using declaration but we ignore it
('using foo::a; foo::a = 1').  We'd do better if clang would
report whether a given decl-use was via a using declaration or
not, but right now it doesn't.  cf:
http://lists.cs.uiuc.edu/pipermail/cfe-dev/2011-April/014564.html
(though that applies only to using vars, not functions).

In order to implement this reasonably, I had to (finally!)
unify the type and decl reporting in iwyu.cc, rather than
iwyu_output.cc.  This allows me to remove a bit of duplicated
code, and also makes the logic easier to follow.

R=dsturtevant
DELTA=358  (286 added, 71 deleted, 1 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=1576
2011-04-26 23:05:45 +00:00
csilvers+iwyu 056e1acbb0 My check for whether a class was nested or not was entirely
broken -- it left out the common case that the class is being
used inside the outer class without a qualifier.  A new test
shows where we would do the wrong thing in that case.

Fixing this brought up more problems, that could all be traced
to the fact we 'cheat' and create fake uses of
forward-declares when we want to be sure not to delete them.
I get rid of the cheating and just say, when we see a
forward-decalare, whether we want to definitely-keep it or
not.  That yields much nicer code (I think).

I also don't know what I was thinking when I said "if there
are multiple fwd-decls in a class, just keep one arbitrarily."
Obviously we should keep the first one; the arbitrary one may
come too late.  These changes brought that bug to light too
(an existing test started failing), but now it's squashed.

Finally, I resolved a TODO, getting rid of
IsDeclNodeInsideFriend.  I had known this was possible for a
while, I just had never gotten around to actually doing it.
Doesn't affect any tests.

R=dsturtevant
DELTA=141  (75 added, 26 deleted, 40 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=1453
2011-04-14 04:48:03 +00:00
csilvers+iwyu ed381497a3 Add a test to protect against syntax errors in main(). Also
test some main()-invariant logic while I'm at it.

DELTA=19  (16 added, 0 deleted, 3 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=1409
2011-04-13 03:11:35 +00:00
csilvers+iwyu 37c67ac363 I was not correctly getting the location for member-exprs. I
thought I was saying the location is where the . (or ->) is,
which is what I want, but clang doesn't actually expose that.
So I have go through some hoops to try to figure it out.

We were actually seeing a problem with this when running:

blaze build --host_cpu=k8 --compile_only -k --crosstool_top=//third_party/llvm/crosstool --plugin=//devtools/maintenance/include_what_you_use:run_iwyu //gws/plugins/local/src:enhanced_listing_ad

It has 'msg_->MSG_foo' in it, where MSG_foo is a macro.  We
were attributing this use to the file defining MSG_foo, rather
than to us.  With this change, we properly attribute it to us.

R=dsturtevant
DELTA=150  (125 added, 6 deleted, 19 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=1347
2011-04-12 05:00:17 +00:00
csilvers+iwyu de5266f972 no_include pragma.
Usage: // IWYU pragma: no_include "foo/bar/baz.h"
This inhibits IWYU from suggesting that baz.h be included.

R=csilvers
DELTA=109  (98 added, 1 deleted, 10 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=1339
2011-04-12 04:56:17 +00:00
csilvers+iwyu aad4c62b13 Fix up the handling of fwd-decls in some situations. These
situations are ones where iwyu requires a full definition but
the language allows a forward-declare.  This causes trouble in
cases like:
class Foo;
typedef Foo Bar;
class Foo { ... };

In this case, we require the full definition of Foo for the
typedef, which leads us to believe the forward-declaration is
extraneous, even though it's actually necessary.

I fix this by detecting when iwyu wants a definition that
occurs after the use, and making iwyu want the
forward-declaration then as well.

While testing this, I found some bugs in the existing code
that tries to handle forward-declarations inside classes -- it
wasn't handling uses in initializers (which should be treated
like method bodies) or the implicit destructor code.  I added
logic (and tests) around those.

I also discovered a bug where GetDefinitionAsWritten() was
sometimes converting templated class to the untemplated
CXXRecordDecl version.  GetDefinitionAsWritten() should
preserve the templated-ness of is argument.  It does so better
now.

R=dsturtevant
DELTA=159  (135 added, 5 deleted, 19 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=739
2011-03-04 00:30:28 +00:00
csilvers+iwyu 97d04c4a90 Replace assert() by CHECK_, which is always executed, even in
opt mode.

R=wan
DELTA=131  (16 added, 15 deleted, 100 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=738
2011-03-04 00:29:56 +00:00
csilvers+iwyu fe45cabe52 Attempting to get nested class forward declarations right.
R=csilvers,wan
DELTA=145  (96 added, 2 deleted, 47 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=731
2011-03-04 00:26:01 +00:00
csilvers+iwyu 817eacf5c3 drheld pointed out that iwyu was violating its most basic
'include what you use' mantra when it came to forward
declares: before this CL, it said, "if someone you are
#including from forward-declares a class for you, you don't
have to forward-declare that class yourself."  But that means
you can't refactor .h files to remove unnecessary
forward-declares.  If you need to forward-declare something,
you should forward-declare it yourself, not get that fwd-decl
from an #include.

(Note the story is different if you're getting the
*definition* of a class from an #include, even if you only
need a forward-declaration for that class.  In that case,
using the #include is fine.  The only thing this CL addresses
is when you try to use a .h file to forward-declare something
for you.)

R=dsturtevant
DELTA=206  (137 added, 41 deleted, 28 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=727
2011-03-04 00:23:37 +00:00
csilvers 69d76e1239 * Rewrite iwyu_include_picker; now more pragma-friendly (csilvers)
* Add support for FakeNamedDecls for tests (dsturtevant)
	* Write our own symbol sanitizer rather than getQualified... (csilvers)
	* Change run_iwyu_tests to better fit python test framework (csilvers)
	* Revamp IWYU pragmas, now support keep + export + private (dsturtevant)
	* Fix a bug when nested classes are defined out of line (csilvers)
	* Add the ability to print an arbitrary ASTNode (csilvers)

All code by csilvers was reviewed by wan and dsturtevant.  Code by
dsturtevant was reviewed by csilvers.
2011-02-19 02:32:52 +00:00
csilvers 000a1fff6d Fixes to get code to compile under MSVC 10. Submitted by pichet2...:
http://code.google.com/p/include-what-you-use/issues/detail?id=7
with small changes by csilvers to encapsulate all arch-specific
information in one file, port.h.

Reviewed by csilvers
2011-02-08 06:12:32 +00:00
csilvers dee92b5e0a Initial release! Grains of salt not included. 2011-02-04 22:28:15 +00:00