Commit Graph

283 Commits

Author SHA1 Message Date
Kim Gräsman bd305afe7d Support 'IWYU pragma: export' for forward declarations
Covers both begin_export/end_export blocks and single-line export
pragmas.

Like with 'IWYU pragma: keep' marks the forward decl as automatically
desired to avoid removing manually exported but unused decls.

Add a simple testcase and update documentation.
2023-01-22 20:32:32 +01:00
Kim Gräsman 2dc0645882 Simplify pragma keep handling for forward declarations
Rename ForwardDeclareInKeepRange to ForwardDeclareIsMarkedKeep, take a
NamedDecl and move all location and text wrangling into the function.

This is a layering violation in a sense, because the preprocessor
doesn't know anything about declarations. But the API is much cleaner
that way from the callers' perspective -- they typically just have a
decl and shouldn't be trusted to use the right location info.

No functional change.
2023-01-22 20:32:32 +01:00
Bolshakov 971a300bb9 Report only explicit typedefs
Previously, ReportDeclUse was called for TypedefDecl when TypedefType
is passed to ReportTypeUse. This change short-circuits that path. But
an underlying type should still be reported if the typedef doesn't
"provide" it. Hence, corresponding logic is moved into ReportTypeUse.
So ReportDeclUse reports typedef declarations and ReportTypeUse reports
typedef underlying types now.
2023-01-22 20:08:02 +01:00
Kim Gräsman afebd59684 Increase log level of covariant trace message
No need to have this at level 3, bump it to 6.
2023-01-07 22:39:42 +01:00
Kim Gräsman d7e4a20e8f Clean out IWYU pragma comments in IWYU itself
None of these pragmas are necessary at this point.
2023-01-07 21:26:41 +01:00
Kim Gräsman 35effcf918 Add GetKindName AST util
The core AST nodes (Decl, Stmt, Type, TypeLoc) all have a kind denoting
their specific derived type, and a corresponding function to get a
printable string for the kind.

Implement a polymorphic GetKindName that calls the right function
depending on type, and also adds a suffix to mirror the clang class
name.

Make all uses of the get...Name functions call these wrappers instead.

No visible change intended.
2023-01-07 18:12:43 +01:00
Andrey Ali Khan Bolshakov d3e23c367b
Fwd decls for explicit types only
Report fwd-decls only for explicitly written types

There is no point in reporting forward-declaration of a type when just
its pointer is used and the type is not explicitly written
in the source. Explicitly written pointer types are handled, e.g.,
in VisitTemplateSpecializationType and VisitTagType.

Add test cases for redundant fwd-decl absence
2023-01-07 18:06:15 +01:00
Kim Gräsman b65c4b63a7 Format all lines with leading =
This is the unadorned output of:

    git grep -En "^\s*=" -- ':!tests/*' | grep-format

There are some examples with comments between the variable name and the
'=' that clang-format left unchanged.

No functional change.
2023-01-07 12:27:16 +01:00
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
Kim Gräsman fe86edad2e Improve GetCanonicalUseLocation logging 2023-01-07 11:52:16 +01:00
Kim Gräsman 8a735a52e1 Format GetCanonicalUseLocation
This function had some bad indentation that disagrees with clang-format.

To prepare for some changes and make their diff more focused, reformat
up-front.
2023-01-07 11:52:16 +01:00
Kim Gräsman 7864ab35f4 Remove full stop from log messages
This has snuck in in a few places, but is not useful or conventional.
2023-01-07 11:34:11 +01:00
Kim Gräsman 711a4bd008 Rephrase VisitTemplateName comment
Rephrase a little and fix typo.
2023-01-06 17:09:21 +01:00
Kim Gräsman cced95dba5 Do not crash if IsInHeader is called for builtin
In very particular circumstances (see test case), VisitFunctionDecl
would see a function definition that was an implicit constructor of a
builtin (va_list_tag).

Rather than crashing for implicit code, just say it's not in a header.

This feels a little questionable, because depending on how IsInHeader is
used, it might be misleading that it returns false for builtins/implicit
code. But I think it makes sense for now.

Fixes issue #1162.
2022-12-30 12:45:24 +01:00
Kim Gräsman e28a2853e7 [clang compat] Adopt OptionalFileEntryRef
Clang 854c10f8d185286d941307e1033eb492e085c203 changed
PPCallbacks::InclusionDirective and Preprocessor::LookupFile to use
clang::OptionalFileEntryRef instead of llvm::Optional<FileEntryRef>.

Update our overrides and calls to match.
2022-12-20 19:26:56 +01:00
Bolshakov 278ba9de27 Remove redundant code
It became superfluous after generalization of reporting types needed
for template instantiation.
2022-12-16 22:22:31 +01:00
Bolshakov c3bcb661f6 Handle sugared template specs in template args
Non-sugared template specialization type template arguments are already
analyzed due to 'TraverseType' call inside
'InstantiatedTemplateVisitor::TraverseSubstTemplateTypeParmTypeHelper'
and subsequent 'TraverseTemplateSpecializationTypeHelper' call.
Components of sugared template specialization types are added into
resugar_map, otherwise their template arguments would not be reported.
Test case has been extended.
2022-12-16 22:22:31 +01:00
Bolshakov 526827957b Fix template instantiation reporting
Full template specialization type use requires full information about
its template-argument-dependent fields and nested typedefs. But earlier,
it wasn't reported in many cases when template specialization type isn't
written explicitly in non-fwd-decl context.
2022-12-16 22:22:31 +01:00
Kim Gräsman 68646a5efd Work around dependent template alias crash
Only attempt to report decl uses of template names that have an
underlying template decl. This ignores uses of dependent templates and
potentially other such variations.

Even if that causes us to miss reporting for some valid constructs, it
allows further analysis of files that would otherwise crash IWYU.

Fixes issue #1140
2022-12-04 16:09:05 +01:00
Kim Gräsman 44444960dc Remove unnecessary namespace qualifiers
Add using-declaration for clang::TemplateDecl, and remove explicit
namespace qualifiers for use of RecordDecl and CXXRecordDecl, which are
already pulled in with using-declarations.

No functional change intended.
2022-12-04 16:09:05 +01:00
Daniel Hannon cf1624a4e2 Add begin_keep and end_keep pragmas, test and docs
this was a proposed issue in #1095 where one could
make a block of keeps as opposed to marking every
keep with a pragma: keep instruction.

added test to verify and updated documentation
where appropriate.
2022-11-02 19:33:10 +01:00
Kim Gräsman 109fdb4cdd Revert "See through C++20 consteval for conversion functions"
A proper fix is now available in LLVM as of
https://github.com/llvm/llvm-project/commit/403d7d8d7093d6637a006f8b3f7538

This reverts commit 7d7fd80da7.
2022-11-01 19:18:17 +01:00
jspam b74819dc5f
Use more exact location for caught exceptions
This change provides better location info when using macros such as:

    BOOST_CHECK_THROW(..., Exc);

The use of type Exc would be attributed to the file defining the macro
before this patch, but is now correctly attributed to the expansion
location.

Co-authored-by: Kim Gräsman <kim.grasman@gmail.com>
2022-10-09 16:35:19 +02:00
Bolshakov 862812049a Avoid 'autocast' reporting for function definition
A function may just transmit passed-by-reference parameter somewhere.
Requirement to explicitly write forward declaration in the same file
(.cpp-file) to avoid '#include' suggestion is impractical when that type
is already fwd-declared in the corresponding header.
'Autocast' may still make sense for header-defined functions, due to
unlimited number of possible callers, so analysis of those fuctions
is kept.

Both function declaration site handling and call site handling
are changed.
2022-10-08 16:17:50 +02:00
Bolshakov 8751dac97b Avoid redundant enum type reporting
There is no need in reporting enum opaque declaration or full use if
enumeration type name or enumerator name isn't explicitly written,
because it should already be present elsewhere in the translation unit,
probably through included headers.
2022-09-04 13:20:54 +02:00
Kim Gräsman 4ad1d428ff Refactor CanForwardDeclareType for better sugar tolerance
These changes have no effect on the current test suite, but there are
obvious weaknesses when sugared types are taken into account.

The way CanForwardDeclareType asks if the parent is-a particular AST
node type is generally unsound in the presence of sugar, such as
ElaboratedType, which may show up above any Type in the tree.

An example:

  class C {};
  typedef C FooBar;

produces:

  `-TypedefDecl 0x559e505c6e30
    `-ElaboratedType 0x559e505c6df0 'C' sugar
      `-RecordType 0x559e505c6cc0 'C'
        `-CXXRecord 0x559e505c6c30 'C'

If we were to call CanForwardDeclare for the type C, it would not
classify as part of a TypedefDecl, because its parent is-a
ElaboratedType.

Therefore, delay any parent type checking until we've rewinded the
current AST node using MostElaboratedAncestor (there's a case to be made
that we should add a more general MostSugaredAncestor to walk _up_ the
tree until a desugared node is reached, much the same way as Desugar
will walk _down_ the tree. But that's good idea for a separate project.)

This was inspired by Clang 15f3cd6bfc670ba6106184a903eb04be059e5977,
which wraps the majority of Type nodes in an additional ElaboratedType
node.
2022-08-31 22:30:12 +02:00
Kim Gräsman ce63c68ed2 [clang compat] Back up through sugar before parent type check
This is kind-of the inverse of desugaring -- walk up the ancestor chain until
we're at the most elaborated (should really be "sugared") ancestor. Then we can
make assumptions about parent types again.

Clang 15f3cd6bfc670ba6106184a903eb04be059e5977 made this necessary, because many
more type nodes are now wrapped in ElaboratedType, which breaks naive parent
type checking.
2022-08-31 22:24:54 +02:00
Kim Gräsman ed7860fa6f [clang compat] Desugar types before author-intent analysis
We already did desugaring (or, historically, RemoveElaboration) before
insertion or lookup in responsibility maps -- e.g.
GetCallerResponsibleTypesForAutocast and
GetCallerResponsibleTypesForFnReturn -- in most cases.

Clang after 15f3cd6bfc670ba6106184a903eb04be059e5977 wraps many more
type nodes in ElaboratedType, so we need to be even more diligent about
this.
2022-08-31 22:24:54 +02:00
Kim Gräsman 7fd6c9f798 Replace all uses of RemoveSubstTemplateTypeParm with Desugar
No visible functional change, but we now desugar more aggressively.

This patch is the result of:

  git grep -l "RemoveSubstTemplateTypeParm" | xargs sed -i -e 's/RemoveSubstTemplateTypeParam/Desugar/'
2022-08-31 22:16:16 +02:00
Kim Gräsman 77e9128cba Replace all uses of RemoveElaboration with Desugar
Desugar performs the same desugaring as RemoveElaboration, and more.

This patch is a straight:

  git grep -l "RemoveElaboration" | xargs sed -i -e 's/RemoveElaboration/Desugar/'

followed by a test cleanup in badinc.cc, where Desugar sees through more sugar
than RemoveElaboration, and actually produces better results.
2022-08-31 22:16:16 +02:00
Kim Gräsman f0eb46972a Check for SubstTemplateTypeParmType before desugaring
RemovePointersAndReferencesAsWritten should be allowed to return a
desugared type, so typecheck for SubstTemplateTypeParmType on the type
before pointers/refs (and any sugar) have been removed.
2022-08-31 22:16:16 +02:00
Kim Gräsman 204ebafb22 [clang compat] Use getAs<> instead of dynamic cast for Types
Downcasting an AST Type* to a more specific type is increasingly unsafe in the
AST. In general any Type* picked out of the AST might contain so-called 'sugar',
i.e. visible or invisible structure that does not change the identity of the
underlying type, but adds information.

After Clang 15f3cd6bfc670ba6106184a903eb04be059e5977, almost every Type is
wrapped in an ElaboratedType with kind ETK_None (i.e. "no elaboration").

ElaboratedType counts as sugar, and thus needs to be stripped off before we do
dynamic type checking of Type nodes.

The recommended way to look through sugar downwards in the tree is
'type->getAs<MostDerivedType>()', which will recursively remove sugar until a
node of MostDerivedType is reached. If there is no MostDerivedType node it
returns nullptr, so it's safe to consider getAs a desugaring dynamic downcast.

There are still some places where we use DynCastFrom or isa<> on Types, I have
only modified the ones that had any effect on broken testcases. Planning to look
into the rest separately once the test suite is working.

This fixes 4 broken testcases, and is an improvement towards fixing #1092.
2022-08-28 16:55:43 +02:00
Kim Gräsman 78a4fe951a [clang compat] Use elaborated type specifier in CanForwardDeclareType
Use the new IsElaboratedTypeSpecifier instead of node type-checking.

Just because an AST node is an ElaboratedType does not mean it's an
actual elaborated type specifier (which is what we care about here).

This fixes 10 broken testcases after Clang 15f3cd6bfc670ba6106184a903eb
wrapped every Type inside an ElaboratedType in the AST, but it is
strictly a bugfix in general: all elaborated type nodes are not type
specifiers.

Part of fix for #1092.
2022-08-28 16:16:04 +02:00
Kim Gräsman 0be9166fe6 Rename IsElaborationNode to IsElaboratedTypeSpecifier
'Elaborated type specifier' is the formal term for 'struct X', 'class Y' or
'union Z'. Use it to avoid potential confusion around what 'elaboration node'
means.

Clean up a comment that claimed 'MostElaboratedAncestor' used
'IsElaborationNode', where it actually doesn't.

No functional change.
2022-08-28 16:16:04 +02:00
Kim Gräsman bfe1909a9c Remove fruitless Base::Visit calls
The IwyuBaseAstVisitor has a base class, BaseAstVisitor, but it only has a
small number of Visit functions implemented, for logging.

So there is no need to call Base::VisitX in general, only for the few with
an implementation that actually does something, and they are preserved
here.
2022-07-24 22:15:58 +02:00
Kim Gräsman d2d4d15d8d Allow forward-declaration of elaborated types in type decls
This covers the common C pattern of declaring a typedef for a struct before the
struct itself, e.g.

   typedef struct foo foo_t;

   struct foo {
     int value;
     foo_t *next;
   };

Fixes issue #1065.
2022-07-24 22:02:19 +02:00
Bolshakov 0f7a7aba8c Report aliased template parameter
Template-nested typedef can't be responsible for any parameter
of the template, because an exact type is known only on template
specialization.
2022-07-23 13:18:27 +02:00
Bolshakov 57b812bf35 Report C++20 concept declaration use 2022-07-23 10:31:48 +02:00
Bolshakov b46a96e5a6 Fix autocast to reference
CastExpr for constructor conversion is absent in the AST in such a case
2022-07-16 18:30:23 +02:00
Bolshakov 12d2c18fa6 Report enum type name instead of constant name
The reason is that
1) enumerators led to much noise in explanatory comments
(the idea is similar to cedf9d6984323a0a68847d), and
2) without this change, EnumClass::Item is reported separately
as fwd-decl use of EnumClass along with the full use
of EnumClass::Item.

Unnamed enums are an exception.
2022-06-13 07:47:21 +02:00
Bolshakov fb41044b9f Suggest enumeration opaque declarations
Opaque (i.e., in fact, forward) declarations are allowed for scoped
enumerations and unscoped ones with underlying type explicitly
specified.
2022-06-13 07:47:21 +02:00
Bolshakov 799a8ef1b5 Replace RecordDecl by TagDecl
This is preparation for adding suggestions for enumeration opaque
(forward) declarations. Enums should be treated almost similar
to classes and structs, so clang::RecordDecl should be replaced
by clang::TagDecl, which is a superclass for RecordDecl and EnumDecl,
in many places.
2022-06-13 07:47:21 +02:00
Bolshakov db69a0c4d3 Perform full analysis of typedef components
We used to ignore all analysis of AST nodes inside a `typedef`, but that
caused us to miss author-intent analysis of nested typedefs.

Remove the special casing for member-of-typedef now that reporting as a
whole is less granular (we now report only the parent type instead of
all nested components).

Add test cases to demonstrate that nested typedefs observe
the author-intent rules.
2022-05-28 22:54:19 +02:00
Kim Gräsman 1375e56503 Consider all null decls ignorable
Non-template code only allows the null decl pointer to be ignored.

For instantiated templates, CanIgnoreDecl is quite sophisticated and uses a
set to ignore subtrees we've visited before, but it doesn't explicitly ignore
the null decl.

There are cases in templated code where the decl can be null as well -- most
prominently for builtin types.

With this test input:

  $ cat t.cc
  #include <algorithm>
  #include <cassert>
  #include <vector>

  void f() {
    std::vector<int> v;
    assert(std::none_of(v.cbegin(), v.cend(),
                        [](auto value) { return value == 100; }));
  }

  $ include-what-you-use -stdlib=libc++ t.cc

(note the explicit use of libc++)

std::vector<int>::const_iterator collapses to a 'const int *', which has no
declaration, and so yields a null decl. Prior to this fix, that would lead to a
cast assertion or segfault attempting to cast the resulting null pointer to a
FunctionDecl in GetCanonicalUseLocation.

Add an assertion for non-null to GetCanonicalUseLocation to clarify that
null filtering must happen before canonicalizing the use-location.

I have not found a way to capture this in a reduced testcase.

Fixes #989.
2022-05-27 18:11:55 +02:00
Kim Gräsman 9dee4b779b Print space between pointer and decl
This avoids confusing log output like "0x00405028void x();".
2022-05-19 21:41:34 +02:00
Kim Gräsman 14551a7126 Allow printing of null AST entities
Before, this would segfault attempting to print e.g. null decls.

These printers are predominantly used for logging, and it seems like a shame for
debug logging to crash on a null pointer if the code around it does not.

Remove now-unnecessary null-checks in logging.
2022-05-19 21:41:34 +02:00
Kim Gräsman cd6ee274dd Replace Print... with stream chaining
Replace both PrintStmt and PrintASTNode with their Printable counterparts.

No functional change intended.
2022-05-19 21:41:34 +02:00
Kim Gräsman 9d94cea91c Only compute use-flags once for fwd decl uses
Rather than recomputing use-flags for decl and using-decl (if one exists),
compute it once into a temporary.

Remove gratuitous const to fit other use-flags expression on one line.
2022-04-23 11:44:32 +02:00
Kim Gräsman 3233575084 Pull using-decl directly from shadow
No use abstracting GetUsingDeclarationOf when we already have the using-decl.

Collapse DynCastFrom into auto + llvm::dyn_cast.
2022-04-23 11:44:32 +02:00
Bolshakov ae98e1ebd7 Maintenance: redundant code removed
self-written using-decl lookup seems to became redundant after introducing
UsingType handling (ce4ccc665)
2022-04-21 21:46:25 +02:00