Commit Graph

179 Commits

Author SHA1 Message Date
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
J.Ru 0d0832fee7 Treat explicit template instantiations as full uses
Teach IsFowardDecl() to not confuse an explicit template instantiation
with a forward declaration.

Also make sure to report explicit instantiations (declaration or
definition) as full uses during visitation.

Fixes #558
2018-12-07 09:12:01 +01:00
J.Ru 1c79773839 Refactor CanIgnoreType for clarity and reuse 2018-11-13 07:31:39 +01:00
J.Ru b4eeaed91f Make 'comment' a default argument in Report* functions
Instead of having two separate versions of each.
2018-11-13 07:31:39 +01:00
Kim Grasman 569a7ee144 Handle fixed-point casts
These were added in Clang r344530 and r345063.

I'm not entirely sure what spellings would trigger these, but they
should be similar to the floating-point casts, so add handle them
similarly.
2018-10-26 23:47:48 +02:00
Kim Grasman 61d691a3c1 Handle OpenCL opaque type casts
Clang r345038 unified casts from zero to OpenCL event_t and queue_t to a
single cast-kind: CK_ZeroToOCLOpaqueType.

Update the cast handling to cover for this.
2018-10-26 23:36:40 +02:00
J.Ru 2a2d15cf8a Replace remaining dyn_cast_or_null with DynCastFrom 2018-08-11 16:39:58 +02:00
J.Ru 29291f385b Don't assume an implicit decl is always a function
GetNodesBelow was incorrectly assuming that an implicit decl can only be
a FunctionDecl, and so it simply converted every decl without checking
the result.

As a counter-example of this we have BuiltinTemplateDecl (among others)

Fixes #150
2018-08-11 16:39:58 +02:00
J.Ru e31cd2adda Handle builtins properly during template traversal
Don't assume that the decl for a template specialization must always be
a record. If, for example, such template is built in, the decl will derive
from TemplateDecl instead.

Also, explictly look for BuiltinTemplateDecl during post processing to
correctly identify them as builtins. If this is not done, a following
check would ignore them anyway but in that case they will be reported as
"backwards includes".

Function builtins handled now using getBuiltinID/isBuiltinFunc from clang
to identify all the predefined symbols that have no include and should be
ignored. Before, only the symbols prefixed with __builtin_ were identified.

Fixes #150
2018-08-11 16:39:58 +02:00
J.Ru af84cfcbeb Ignore the class qualifier in out of line methods
Instead of aborting the AST traversal completely whenever a class
qualifier is found, skip the traversal of the class template and its
methods in such contexts only.

The reasoning here is that a class template will be inspected during the
regular instantiation anyway, so there is no need to duplicate efforts.

Apart from performance implications, scanning the template here makes
IWYU report all template parameters as if specified explicitly by the
user (effectively ignoring default template params). For example,
std::allocator would get reported as a full-use during a call to
push_back() because it is defined outside of the std::vector class.

Closes #533
2018-04-23 21:58:59 +02:00
Kim Grasman 2a47425859 Recognize C++11 range-for statements
The range-init expression requires special treatment: it's a reference
type, but the language generates code that needs to see its complete
type.

Fix #463.
2018-03-09 22:01:11 +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 d3e8a0d860 Make the instantiated type part of the caller stack
Right before starting with the template traversal, to differentiate
between the template proper and the calling code, the AST stack is
partitioned by pointing the data member *caller_ast_node_* to the root
node where the template code starts.

However, ScanInstantiatedType started in the context of the parent
expression and relied on TraverseType to push the current template
instantiation type to the AST stack before continuing.

This was wrong, since it caused the instantiation site (user code) to
be attributed as part of the template being instantiated (library code).
Specifically for default arguments, this AST stack was queried by
*GetLocOfTemplateThatProvides* to decide if the user, or the template,
should be responsible for the types of the arguments, ultimately causing
a type that the user defined not to be reported as a full use.

Closes #500
2017-12-22 19:22:38 +01:00
J.Ru cd157da809 Skip non-ctors during instantiation of implicit methods
InstantiateImplicitMethods() wrongly assumed a ctor decl must be
present just by checking that it is not a template decl. This is
no longer true in C++11 where a using decl is also a valid ctor type.

Fixes #402
2017-10-04 20:05:19 +02:00
Volodymyr Sapsai 8a9df4055c Fix requiring allocator for getting container iterator with libc++. (#448)
libc++ code is

    template <class _Tp, class _Allocator>
    inline _LIBCPP_INLINE_VISIBILITY
    typename vector<_Tp, _Allocator>::iterator
    vector<_Tp, _Allocator>::begin() _NOEXCEPT
    {
        return __make_iter(this->__begin_);
    }

and we reported use of allocator when visiting `vector<_Tp, _Allocator>`
because here `_Allocator` is not a default template argument. There was
no such problem in libstdc++ because there `begin` is defined inside a class.

Also I wasn't able to reproduce the issue with non-STL types because for
not precomputed types we replayed template arguments usage. And when we
populate the cache we see which template arguments are default so there
are no problems during replay.

Update test to remove method calls using object template arguments. As result,
on macOS it fixed errors

    tests/cxx/badinc.cc:1534: Unexpected diagnostic:
    I2_Enum is defined in "tests/cxx/badinc-i2.h", which isn't directly #included.

    tests/cxx/badinc.cc:1580: Unexpected diagnostic:
    I2_Enum is defined in "tests/cxx/badinc-i2.h", which isn't directly #included.

but introduced another error

    tests/cxx/badinc.cc:1527: Unmatched regex:
    I2_Enum is...*badinc-i2.h

I think it's a reasonable trade-off.
2017-07-03 14:36:21 -07:00
Kristoffer Henriksson a2d8e28250 Allow IWYU pragma: keep on forward declarations as an escape hatch for cases where IWYU incorrectly concludes that a forward declaration is not necessary and recommends its removal. 2017-06-20 21:55:20 -07:00
Kim Grasman 13cd94804b Remove second-guessing for DeclRefExprs in CallExprs
In TraverseDeclRefExpr we would exit early if we were under a
CallExpr, with the motivation that CallExpr had already handled the
relevant cases.

However, VisitCallExpr only looks specifically for reference arguments
passed to vararg functions, and ignores everything else.

Let TraverseDeclRefExpr work on all expressions, so that creating
function pointers to templates works in all contexts, including
CallExprs.

Fixes issue #425.
2017-05-17 19:49:03 +02:00
Kim Grasman 34baf6e65c Equate alias declarations and typedef in templates
Typedefs are exempted from IWYU checks in templates, so treat C++11
alias declarations the same.

(side note: this behavior is broken, but at least now it's consistent)

Fixes issue #412.
2017-05-17 19:48:32 +02:00
Kim Grasman 953970c487 Handle locations of nested macros better
We've seen that IWYU completely misses the mark when finding the use location
for htons from inet.h.

This patch should handle nested macros better. Test case based on htons added to
macro_location.cc

Fix #334 and #358.
2017-04-18 22:17:27 +02:00
Kim Grasman 7bb1d89b3a Handle CK_ZeroToOCLQueue cast kind
Added in Clang in r290431, and handled here to silence warning.
2017-01-01 16:13:33 +01:00
EugeneZelenko eb7f5d1ef0 Fix Clang-tidy warnings
- readability-redundant-member-init warnings
- modernize nested templates; '> >'
- modernize-use-default warnings
2016-11-16 21:04:24 +01:00
Volodymyr Sapsai cf4d496319 Update to reflect changes in Clang.
In r283766 started using unique_ptr instead of raw pointers in the
late-parsed templates map.

In r283815 changed FileEntry::Name from raw pointer to StringRef.
2016-10-10 22:52:04 -07:00
Kim Grasman 3e98eed9c7 Improve macro use-attribution heuristics
Bugs #334, #335 and #338 were all caused by the new macro location
rule that considers a forward-decl in the macro definition file a
hint that uses of the declared type inside a macro are attributed
to the macro's expansion location.

Several different kinds of declarations were mis-characterized as
forward decls.

This patch improves IsForwardDecl to better detect this, and
simplifies the heuristic rule so that any forward-declaration
outside of a macro in the same file as the macro definition will
count as a use-attribution hint.
2016-10-07 21:19:25 +02:00
Kim Grasman bb61a50408 Terminology: as-typed -> as-written
Clang uses mostly "as-written" in its APIs. Follow that convention
more consistently.
2016-09-04 17:06:41 +02:00
Kim Grasman 25e3f93276 Use emplace_back
Thanks to clang-tidy/modernize-use-emplace and Eugene Zelenko.
2016-08-15 22:35:35 +02:00
Kim Grasman 7e7428a576 Remove unused using decls
Found by clang-tidy, misc-unused-using-decls.

Original patch by Eugene Zelenko, thanks!
2016-08-15 22:32:56 +02:00
Kim Grasman 9c432a9763 Recognize nested classes in friend declarations
Fragments such as this:

    friend class Container<T>::Iterator;

would not be recognized as needing a forward-declaration, so IWYU
would suggest removing forward-decls of Iterator.

This patch changes this and expands the test suite for nested classes
to cover containing class templates and friend declarations.

Fixes issue #331.
2016-08-15 21:10:02 +02:00
Kim Grasman 2141bde3be Fix #332: Classify pointer arithmetic as a full use
For pointer arithmetic, the compiler needs to know the size of the
pointed-to types.

Detect all valid forms of pointer arithmetic and report both the
right-hand and left-hand expression types.
2016-08-01 21:44:20 -07:00
Kim Grasman 4a57380c21 Update to reflect changes in Clang
Clang r277024 introduced a new OpenCL cast kind, CK_IntToOCLSampler,
which is a built-in type for OpenCL. Ignore it.
2016-07-29 22:06:08 +02: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
EugeneZelenko 137907952c Fix some Clang-tidy warnings
NULL/0 -> nullptr
C standard library include -> C++ counterparts
Use override keyword
Range for loops in selected places
Occasional use of auto
Remove unused usings
Remove unnecessary uses of c_str()
Closing comments for anonymous namespaces
2016-06-18 09:59:40 +02:00
Kim Grasman b70df35a23 Fix naming in TraverseCXXConstructExpr 2016-03-27 21:06:38 +02:00
Kim Grasman 3527466f10 Fix naming in TraverseCXXRecordDecl 2016-03-27 21:06:29 +02:00
Kim Grasman e2244f9c58 Fix naming in VisitCXXNewExpr 2016-03-27 21:06:13 +02:00
Kim Grasman 178b04f014 Fix #127: Improve macro location logic
- Rename GetUseLocationForMacroExpansion -> GetCanonicalUseLocation
- Let macro authors forward-declare symbols to push responsibility
  to expansion
- Symbols passed as arguments to macros are now attributed to expansion

Second commit attempt, with explicit use of spelling-location for
uses attributed to macro definition.
2016-03-15 22:21:39 +01:00
Kim Grasman 380c96abb1 Revert "Fix #127: Improve macro location logic"
This reverts commit 32d04eecd5.

Unexpectedly broke the badinc test on Ubuntu/GCC.
2016-03-11 08:06:56 +01:00
Kim Grasman 32d04eecd5 Fix #127: Improve macro location logic
- Rename GetUseLocationForMacroExpansion -> GetCanonicalUseLocation
- Let macro authors forward-declare symbols to push responsbility to expansion
- Symbols passed as arguments to macros are now attributed to expansion
2016-03-10 19:18:59 +01:00
Kim Grasman d5cf673c5a Better location reporting for unary operators
The location for the use of 'x' in 'sizeof(*(x))' will now point to 'x'
instead of '*'. This helps simplify future changes for reporting of macro
locations.

No functional change.
2016-03-10 19:03:56 +01:00
Kim Grasman 0d1dcc6ff9 Update to reflect changes in Clang
r257559 added a new cast kind CK_BooleanToSignedIntegral, and we saw a
-Wswitch warning in IWYU's handling. Add and ignore the new cast kind
to the switch.
2016-01-16 14:23:27 +01:00
Kim Grasman 9929f88f94 Fix typo in comment 2015-12-31 09:52:48 +01: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
Volodymyr Sapsai 6f2780e938 Part of fix for the issue #166. Follow typedef chains in templates. 2015-10-01 22:52:43 -07:00
Kim Gräsman 2bcfc440d9 Remove some more unnecessary uses of FullSourceLoc. 2015-03-02 19:54:28 +00:00
Kim Gräsman 66700be77a Update to reflect changes in Clang.
Clang r230123 removed DeclContext::lookup_const_result.
2015-02-21 16:53:46 +00:00
Kim Gräsman 4ffc1ac5d9 Fix issue 171: Support catch statements.
Catching a type requires that it's complete. This was found by one of my
co-workers when we ran IWYU over our ~1MLOC code base.
2015-02-04 20:54:21 +00:00
Kim Gräsman 0b15a83af9 Fix issue 157: Return different exit codes. 2014-11-30 09:26:43 +00:00
Kim Gräsman a886a399a6 Fix issue 159: Don't instantiate deleted methods.
Now check that implicit methods are not deleted before attempting to
instantiate them. Add a single test file to capture the constructs we used
to crash on.

Original patch by SmSpillaz.
2014-10-24 16:53:25 +00:00
Kim Gräsman 461e0eed28 Remove misguided template keyword to fix GCC build. 2014-09-16 19:03:33 +00:00
Kim Gräsman c5772d7cd0 Update to reflect changes in Clang
Preprocessor::addPPCallbacks now takes std::unique_ptr instead of raw pointer.
2014-09-11 19:30:17 +00:00