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.
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
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.
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.
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
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
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
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.
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.
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
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
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.
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.
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.
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.
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.
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.
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.
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.
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.
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
- 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.
- 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
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.
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.
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.
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.