Commit Graph

1277 Commits

Author SHA1 Message Date
Sameer Rahmani 785ae7e7b1
Fix the incorrect include path of Triple.h 2023-02-08 15:30:48 +00:00
Petr Bred 1a2cab9b71 [clang compat] Remove unused llvm::Optional
Clang 125f4457a54a550846732763ee36b1447ec8d66e changed.
The LLVM project is migrating from llvm::Optional to std::optional.

We would previously see llvm::Optional via an indirect include:
    iwyu_preprocessor.h
      iwyu_output.h
        clang/AST/Decl.h
          clang/AST/APValue.h
            clang/Basic/LLVM.h

Since we don't use it, remove the using-declaration.

Signed-off-by: Petr Bred <bredpetr@gmail.com>
2023-02-02 19:27:36 +01:00
Bolshakov f9f4fd2eef Test reporting types hidden in tpl arguments
The full type use reporting in the three covered cases is ensured
with excessive `CanIgnoreType` calls which check not only the type
to be reported but also the type before dereferencing (which occurs
to be in the resugaring map). This approach should be refactored
in subsequent commits.
2023-02-01 21:26:28 +01:00
Bolshakov eb1697e002 Fix comment 2023-02-01 21:26:28 +01:00
Kim Gräsman 81ee985377 Ignore uses of declarations from inside of functions
Using type deduction, a function can define a local type and return it:

    auto func() {
      struct X {};
      return X();
    }

Before this change, IWYU would register X as a use for callers of func.

In certain situations (union inside lambda inside macro) this would lead
to a forward-declare use trying to format a forward-declaration of the
type local to the function in PrintForwardDeclare. This led to a crash
since the lambda was unnamed, but there's really no point trying to
forward-declare a type that is private to a function.

Generalize this so we ignore all uses of symbols declared inside a
function. In order to get in contact with that symbol, we must already
have access to the function, so the containing header must already be
included.

Fixes issue: 795
2023-01-22 20:35:42 +01:00
Kim Gräsman 80410de523 Renumber the full-use trimming steps
... to make room for a new one at the very top, now denoted 'TBD'.

We only do this separately to keep the diff less noisy.
2023-01-22 20:35:42 +01:00
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 f7c5a795ab Desugar UsingType among others
`clang::UsingType` denotes a sugar type which is introduced by a using-
declaration (e.g., `using ns::SomeType;`). It has not such a special
meaning in IWYU as `TypedefType` or `TemplateSpecializationType`.

No functional change is expected.
2023-01-22 20:22:59 +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
Bolshakov 8a6009f587 Test using-declaration referring to typedef
Test case added in order not to break it at subsequent tweaking.
2023-01-22 20:08:02 +01:00
Bolshakov a5bcb853b9 Format test case properly 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 c3f92ad08f Remove outdated TODO
<iterator> is now required, not due to a bug, but because it provides
std::inserter.

Remove TODO and update why-comment.
2023-01-07 21:26:41 +01:00
Kim Gräsman d888b82d4f [clang compat] Use new header for frontend diagnostics
In December 2018, the header clang/Frontend/FrontendDiagnostic.h moved
to clang/Basic/DiagnosticFrontend.h. The old header was left around for
compatibility.

Use the new one instead.

Sorry I'm late.
2023-01-07 21:26:41 +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 849c2a2f55 Format all lines with trailing whitespace
This is the result of:

    git grep -n "\s$" | grep-format

No functional change.
2023-01-07 12:27:16 +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 d83677176c Add '// IWYU:' to known clang-format comment pragmas
This makes sure formatting doesn't line-wrap diagnostic checks in tests.
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 44fa8c7bd0 [ci] Upgrade to actions/checkout@v3
The v2 action depends on deprecated Node.js 12, as indicated by warning
from GitHub Actions:

    Node.js 12 actions are deprecated. For more information see: [1].
    Please update the following actions to use Node.js 16:
      actions/checkout@v2

(See [2] for an example.)

Upgrade to actions/checkout@v3 to silence the warning.

Unfortunately this new version has a very confusing policy for default
checkout ref: "[...] the last merge commit of the pull request merge
branch". There does not appear to be a convenient way to fall back to
the v2 behavior.

Add some conditional logic to resolve the HEAD of the right branch
whether it's a schedule, push or pull_request event.

[1] https://github.blog/changelog/2022-09-22-github-actions-all-actions-will-begin-running-on-node16-instead-of-node12/.
[2] https://github.com/include-what-you-use/include-what-you-use/actions/runs/3718310359
2022-12-20 20:59:51 +01:00
Kim Gräsman 5a990cdae7 [ci] Uninstall and install packages in batch
This cuts the time for the 'Install prerequisites' step almost in half,
since it doesn't need to re-scan the apt database for every package.
2022-12-20 20:07:24 +01:00
Kim Gräsman d6ed4a5309 [ci] Remove --force-overwrite workaround
Reverts 6c3b7bcfb8.

This has now been fixed upstream.
2022-12-20 20:07:24 +01:00
Kim Gräsman ba654f182f [cmake] Print full source path of resource headers
This makes it easier to see where the resource headers come from.
2022-12-20 19:54:01 +01:00
Kim Gräsman 5220063727 [cmake] Print LLVM source dir when configuring
This makes it easier to spot which LLVM install was picked up.
2022-12-20 19:54:01 +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
Kim Gräsman 3511ccdedd Silently break cycles in mappings
A long-standing problem with IWYU has been the preprocessor
metaprogramming techniques used in Boost.

IWYU is fine with cycles in the include graph, but did not accept cycles
in the corresponding mapping graph (a potential rationale might have
been to let people know if they explicitly added contradictory mappings,
but we really don't know what drove this decision).

Looking at the algorithm expanding the transitive closure of the mapping
graph, it expands all nodes in depth-first order. Cycle detection is
necessary to protect the algorithm from infinite recursion in the
presence of a cycle. But there should be no difference in the transitive
closure between a graph with cycles and one without. DFS will still
visit all nodes, it will just avoid visiting the same subtree twice.

Once the expansion is done, mapping lookup is an inherently
non-recursive operation (made cheaper by performing the recursive
expansion up-front). See the GetCandidateHeadersForFilepath family of
functions and their respective callers.

Two conclusions:

* Cycles are already detected by the transitive closure algorithm
* No code _after_ the transitive closure is affected by any cycles

Therefore, update the transitive closure expansion to ignore cycles
instead of failing on them. Add diagnostic logging at level 8 so we can
look into its decision-making if necessary.

Fixes issue #424
2022-12-16 22:23:36 +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 06f7c21cbc Test template instantiation reporting 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
Bolshakov ac93b84f01 Change member variable name in test
Names starting with an underscore aren't allowed in a global namespace,
according to the standard.
2022-12-16 22:22:31 +01:00
Bolshakov 7b9c04c0d3 Apply formatting 2022-12-16 22:22:31 +01:00
Bolshakov 42fece0244 Remove superfluous semicolons after method bodies 2022-12-16 22:22:31 +01:00
Kim Gräsman a83809985d [fix_includes] Rephrase usage epilog 2022-12-16 22:16:39 +01:00
Kim Gräsman 523c16e8da [fix_includes] Use argparse instead of deprecated optparse
Roughly following
https://docs.python.org/3/library/argparse.html#upgrading-optparse-code.

The --help output comes out slightly different, but no other functional
change intended.
2022-12-16 22:16:39 +01:00
Kim Gräsman 6c3b7bcfb8 [ci] Add --force-overwrite hack to work around packaging bug
Installation of libclang-16-dev would fail with:

    Unpacking libclang-16-dev (1:16~++20221216031545+6c5f3f62bdb2-1~exp1~20221216151636.624) ...
    dpkg: error processing archive /var/cache/apt/archives/libclang-16-dev_1%3a16~++20221216031545+6c5f3f62bdb2-1~exp1~20221216151636.624_amd64.deb (--unpack):
      trying to overwrite '/usr/lib/x86_64-linux-gnu/libclang-16.so.1', which is also in package libclang1-16 1:16~++20221216031545+6c5f3f62bdb2-1~exp1~20221216151636.624
    ...
    Unpacking libclang-dev (1:16.0-57~exp1~20221014130100.5) ...
    Errors were encountered while processing:
      /var/cache/apt/archives/libclang-16-dev_1%3a16~++20221216031545+6c5f3f62bdb2-1~exp1~20221216151636.624_amd64.deb
    ...
    E: Sub-process /usr/bin/dpkg returned an error code (1)
    Error: Process completed with exit code 100.

libclang-16-dev somehow depends on libclang1-16, and both are trying to
install the same libclang1.so.

Use --force-override switch to comfort dpkg, even though it's not
recommended in practice. See e.g. https://askubuntu.com/a/491086/852068.

Let's remove this as soon as possible.
2022-12-16 22:05:33 +01:00
Kim Gräsman c8ad78d584 [ci] Clean up more base clang packages before installing
The libclang-dev packages are actually called libclang-NN-dev, not
libclang-dev-NN, so make the glob a little more permissive to remove
more preinstalled packages.
2022-12-16 22:05:33 +01:00
Kim Gräsman d537ae32d2 [ci] Remove packaging workaround for missing libclang-16.so.1
This reverts commit 39e45aa453.
2022-12-16 22:05:33 +01:00
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 616927dec4 Avoid dynamic mapping to self for reverse macro dependency
Commit 04f1c92537 added a mechanism that
dynamically adds a mapping from the includee to the includer in this
case:

    // a.h
    #if CONTROL_FLAG
    void foo();
    #else
    void bar();
    #endif

    // b.h
    #define CONTROL_FLAG
    #include "a.h"

However, there was no check that includee and includer were different,
so regular #include guards would cause self-mappings, which would
eventually lead to cycles in mappings and assertion failures, e.g.:

    Cycle in include-mapping:
      <z.hpp> ->
      <z.hpp>
    ,,,/iwyu_include_picker.cc:845: Assertion failed: Cycle in include-mapping
    Aborted

Avoid this by checking if the file defining the macro is the same as the
file using it; in that case we don't need a mapping.
2022-12-04 16:18:13 +01:00
Kim Gräsman 6d5414fcf3 Add consistent logging for dynamic mappings
* Do all this logging on level 8, whether logging is on for file or not
* Always say "Add dynamic mappings..."
* Don't log quoted from->to, that's already logged in AddMapping
* State the reason dynamic mapping is added
* Include file paths where available

This should make it easier to troubleshoot strange effects from dynamic
mappings triggered by IWYU itself.
2022-12-04 16:18:13 +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