21 Commits

Author SHA1 Message Date
Viktoriia Bakalova
de81dc8fdf [include-cleaner] Filter template instantiations from AST roots.
Fix: https://github.com/llvm/llvm-project/issues/59825
Differential Revision: https://reviews.llvm.org/D141271
2023-01-09 12:54:20 +00:00
Benjamin Kramer
854c10f8d1 [Clang] Prepare for llvm::Optional becoming std::optional.
The needed tweaks are mostly trivial, the one nasty bit is Clang's usage
of OptionalStorage. To keep this working old Optional stays around as
clang::CustomizableOptional, with the default Storage removed.
Optional<File/DirectoryEntryRef> is replaced with a typedef.

I tested this with GCC 7.5, the oldest supported GCC I had around.

Differential Revision: https://reviews.llvm.org/D140332
2022-12-20 00:41:40 +01:00
Krzysztof Parzyszek
205c0589f9 Revert "[clang] Convert OptionalFileEntryRefDegradesToFileEntryPtr to std::optional"
This reverts commit 8f0df9f3bbc6d7f3d5cbfd955c5ee4404c53a75d.

The Optional*RefDegradesTo*EntryPtr types want to keep the same size as
the underlying type, which std::optional doesn't guarantee. For use with
llvm::Optional, they define their own storage class, and there is no way
to do that in std::optional.

On top of that, that commit broke builds with older GCCs, where
std::optional was not trivially copyable (static_assert in the clang
sources was failing).
2022-12-18 11:23:54 -08:00
Krzysztof Parzyszek
8f0df9f3bb [clang] Convert OptionalFileEntryRefDegradesToFileEntryPtr to std::optional 2022-12-17 15:24:14 -08:00
Sam McCall
d3714c2b27 [include-cleaner] Move RecordedPP::RecordedIncludes -> Includes in Types.h. NFC
Requiring everything that wants to match Includes to depend on Record is weird.
This isn't lightweight enough that it feels perfect in Types, could be its own
header instead. But pragmatically it doesn't add bad deps, and is widely used.

Differential Revision: https://reviews.llvm.org/D139014
2022-11-30 18:14:55 +01:00
Sam McCall
9961fa1653 [include-cleaner] Record whether includes are spelled with <angle> quotes
This is needed to accurately remove headers with tooling::IncludeHeaders in the
rare cases where <foo> and "foo" resolve to something different.

This is also nice to have in HTML report and command-line -print=changes output.

Differential Revision: https://reviews.llvm.org/D139018
2022-11-30 18:13:10 +01:00
Viktoriia Bakalova
7452e053e5 [include-cleaner] Implement IWYU begin_keep/end_keep pragma support.
Implement support for begin_keep/end_keep pragmas.

Differential Revision: https://reviews.llvm.org/D138797
2022-11-29 14:51:20 +00:00
Haojian Wu
454a34d2c0 [include-cleaner] Fix -Woverloaded-virtual warning, NFC. 2022-11-29 15:09:56 +01:00
Sam McCall
99b5ec1fd1 [include-cleaner] Merge 2 parseIWYUPragma impls in libToolingInclusions
Based on include-cleaner's version, but:

- remove assert that can fail for input `/\<newline>* */`
- assert was also checking the wrong condition: that the prefix *differed* from
  either `//` or from `/*`. Avoid use of strncmp where we can.
- add a comment that the brittleness of the text matching is intentional

Differential Revision: https://reviews.llvm.org/D138780
2022-11-28 13:20:09 +01:00
Viktoriia Bakalova
10d183b889 [include-cleaner] Capture private headers in PragmaIncludes.
Save file IDs of IWYU private headers and report them as private.

Reviewed By: hokein

Differential Revision: https://reviews.llvm.org/D138678
2022-11-28 10:02:02 +01:00
Sam McCall
3e658abd41 [include-cleaner] Show details for #include directives (used/unused)
Differential Revision: https://reviews.llvm.org/D138649
2022-11-24 15:36:44 +01:00
Viktoriia Bakalova
6ebd0aa420 [include-cleaner] Record macro references in #ifdef clause.
Records macro references in #ifdef clauses as ambiguous.

Reviewed By: hokein

Differential Revision: https://reviews.llvm.org/D138559
2022-11-24 14:48:25 +01:00
Haojian Wu
0cf8885144 [include-cleaner] Add self-contained file support for PragmaIncludes.
And use it findHeaders. findHeaders now finds all header candidates
given a symbol location (these headers will be attached with proper
signals, in a followup patch).

Differential Revision: https://reviews.llvm.org/D137698
2022-11-18 13:52:30 +01:00
Haojian Wu
15b2702e83 [include-cleaner] NFC, correct a comment in
PragmaIncludes::RecordPragma.
2022-11-11 14:40:08 +01:00
Sam McCall
61c2276cb2 [include-cleaner] verbatimSpelling->verbatim, clean up some silly init-lists. NFC 2022-11-11 12:10:12 +01:00
Haojian Wu
7795548048 [include-cleaner] Initial version for the "Location=>Header" step
This patch implements the initial version of "Location => Header" step:

- define the interface;
- integrate into the existing workflow, and use the PragmaIncludes;

Differential Revision: https://reviews.llvm.org/D137320
2022-11-11 10:34:22 +01:00
Haojian Wu
f3e8a117d2 [include-cleaner] Add export IWYU pragma support.
- add support to PragmaIncludes to handle IWYU export/begin_exports/end_exports
  pragma;
- implement an API to retrieve the direct exporter headers;

Differential Revision: https://reviews.llvm.org/D137319
2022-11-10 15:39:30 +01:00
Kadir Cetinkaya
d19ba74dee
[Includecleaner] Introduce RefType to ast walking
RefTypes are distinct categories for each reference to a symbol. They
are signals indicating strength of a reference, that'll enable different
decision making based on the finding being provided.

There are 3 kinds of ref types:
- Explicit, the reference is spelled in the code.
- Implicit, the reference is not directly visible in the code.
- Ambigious, the reference exists but can't be proven as used (e.g.
  overloads brought in by a using decl but not used by the code).

Differential Revision: https://reviews.llvm.org/D135859
2022-11-08 16:29:31 +01:00
Sam McCall
8249dc2104 [include-cleaner] Record main-file macro occurences and includes
The occurrences are roots for finding used headers, like walkAST.
Includes are the targets we're matching used headers against.

Differential Revision: https://reviews.llvm.org/D136723
2022-11-08 15:20:40 +01:00
Haojian Wu
9ab0827f70 [include-cleaner] Add a data-structure to capture IWYU pragmas.
PragmaIncludes captures the pragma-based header-mapping information, it is used
in the "Location => Header" step to determine the final spelling header for a
symbol (rather than the header directive).

The structure is by design to be used inside the include-cleaner library
and clangd.

Differential Revision: https://reviews.llvm.org/D136071
2022-10-31 15:21:24 +01:00
Sam McCall
6fa0e026c8 [include-cleaner] Add include-cleaner tool, with initial HTML report
The immediate goal is to start producing an HTML report to debug and explain
include-cleaner recommendations.
For now, this includes only the lowest-level piece: a list of the references
found in the source code.

How this fits into future ideas:
 - under refs we can also show the headers providing the symbol, which includes
   match those headers etc
 - we can also annotate the #include lines with which symbols they cover, and
   add whichever includes we're suggesting too
 - the include-cleaner tool will likely have modes where it emits diagnostics
   and/or applies edits, so the HTML report is behind a flag

Differential Revision: https://reviews.llvm.org/D135956
2022-10-18 18:09:41 +02:00