40 Commits

Author SHA1 Message Date
Jan Svoboda
da95d926f6
[clang][lex] Always pass suggested module to InclusionDirective() callback (#81061)
This patch provides more information to the
`PPCallbacks::InclusionDirective()` hook. We now always pass the
suggested module, regardless of whether it was actually imported or not.
The extra `bool ModuleImported` parameter then denotes whether the
header `#include` will be automatically translated into import the the
module.

The main change is in `clang/lib/Lex/PPDirectives.cpp`, where we take
care to not modify `SuggestedModule` after it's been populated by
`LookupHeaderIncludeOrImport()`. We now exclusively use the `SM`
(`ModuleToImport`) variable instead, which has been equivalent to
`SuggestedModule` until now. This allows us to use the original
non-modified `SuggestedModule` for the callback itself.

(This patch turns out to be necessary for
https://github.com/apple/llvm-project/pull/8011).
2024-02-08 10:19:18 -08:00
kadir çetinkaya
7837110ed8
[clangd] Track IWYU pragmas for non-preamble includes (#75612)
This makes PragmaIncldues copyable, and copies it from preamble when
building a
new AST.

Fixes https://github.com/clangd/clangd/issues/1843
Fixes https://github.com/clangd/clangd/issues/1571
2024-01-03 15:57:30 +01:00
Kazu Hirata
732bccb8c1 Use StringRef::{starts,ends}_with (NFC)
This patch replaces uses of StringRef::{starts,ends}with with
StringRef::{starts,ends}_with for consistency with
std::{string,string_view}::{starts,ends}_with in C++20.

I'm planning to deprecate and eventually remove
StringRef::{starts,ends}with.
2023-12-14 07:53:20 -08:00
kadir çetinkaya
fd2d5add43
[include-cleaner] Make sure exports of stdlib also works for physical files (#72246)
This was creating discrepancy in cases where we might have a physical
file entry (e.g. because we followed a source location from a stdlib
file) and tried to find its exporters.
2023-11-17 10:11:21 +01:00
Jan Svoboda
b0abc9dd44 [clang] NFCI: Use FileEntryRef in ASTReader::GetHeaderFileInfo()
This is the `ASTReader` counterpart to PR #67383.
2023-09-29 09:07:13 -07:00
kadir çetinkaya
d3cfc7126c
[include-cleaner] Always keep non-self-contained files (#65499) 2023-09-11 07:22:52 +02:00
Jan Svoboda
98e6deb62c [clang][tools] Use FileEntryRef in include_cleaner::Header 2023-09-09 09:54:13 -07:00
Viktoriia Bakalova
c3ad4b7636 [include-cleaner] Follow IWYU pragma: export links transitively.
Differential Revision: https://reviews.llvm.org/D157395
2023-08-09 12:10:13 +00:00
Kadir Cetinkaya
31873d3fca
[include-cleaner] Handle files with unnamed buffers
Some tools can register virtual buffers without identifiers into the
filemanager. Make sure we can handle pragmas in such cases.

Differential Revision: https://reviews.llvm.org/D157078
2023-08-04 11:13:03 +02:00
Kadir Cetinkaya
43974333dd
[include-cleaner] Unify always_keep with rest of the keep logic
Depends on D156122

Differential Revision: https://reviews.llvm.org/D156123
2023-08-02 12:47:53 +02:00
Kadir Cetinkaya
778a5e9bc6
[include-cleaner] Introduce support for always_keep pragma
Differential Revision: https://reviews.llvm.org/D156122
2023-08-02 12:47:53 +02:00
Sam McCall
16b5e1897b [include-cleaner] Loose matching for verbatim headers
A verbatim header usually corresponds to a symbol from a header with
a pragma "IWYU pragma: private, include <foo.h>".

Currently this is only satisfied if the main file contains exactly
  #include <foo.h>
In practice this is too strict, we also want to allow
  #include "path/to/foo.h"
so long as they resolve to the same file.

We cannot be 100% sure without doing IO, and we're not willing to do
that, but we can detect the common cases based on paths.

Differential Revision: https://reviews.llvm.org/D155819
2023-07-27 18:19:24 +02:00
Sam McCall
f6307b260b [include-cleaner] Switch Include from FileEntry* -> FileEntryRef
Unlike Header, we really do have a preferred spelling for an include: the one
that we used to open the file.

The fact that Header is still FileEntry* makes it difficult to accidentally
use path equality when we want inode equality.

Differential Revision: https://reviews.llvm.org/D155885
2023-07-26 13:41:55 +02:00
Haojian Wu
3b59842a72 [include-cleaner] Reorder SymbolReference fields to avoid padding space, NFC
This will bring down the size from 40 bytes to 32 bytes.

Differential Revision: https://reviews.llvm.org/D153018
2023-06-16 10:21:44 +02:00
Viktoriia Bakalova
c28506ba4b [clang-tidy] Implement an include-cleaner check.
Differential Revision: https://reviews.llvm.org/D148793
2023-06-02 15:21:20 +00:00
Haojian Wu
7de541235f [include-cleaner] NFC, use const HeaderSearch when possible. 2023-06-01 15:26:19 +02:00
Kazu Hirata
15aa965363 [clang-tools-extra] Use *{Set,Map}::contains (NFC) 2023-03-14 21:30:29 -07:00
Haojian Wu
f4a7448653 [include-cleaner] FindHeaders respects IWYU export pragma for standard headers.
Fixes https://github.com/llvm/llvm-project/issues/59927

Differential Revision: https://reviews.llvm.org/D141670
2023-01-16 10:09:18 +01:00
Sam McCall
d7bba07526 [include-cleaner] Improve header spelling in the presence of links
HeaderSearch uses FileEntry::getName() to determine the best spelling of a
header. FileEntry::getName() is now the name of the *last* retrieved ref.
This means that when FileManager::getFile() hits an existing inode through a new
path, it changes the spelling of that header.

In the absence of explicit logic to track the preferred name(s) of header files,
we should avoid gratuitously calling getFile() with paths different than how
the header was originally included, such as the result of realpath().

The originally-specified path should be fine here:
 - if the same filemanager is being used for record/analysis, we'll hit the
   filename cache
 - if a different filemanager is being used e.g. preamble scenario, we should
   get the same result unless either the working directory has changed (which it
   shouldn't, else many other things will fail) or the file has gone/changed
   inode (in which case the old method doesn't work either)

Needless to say this is fragile, but talking to @kadircet offline, it's good
enough for our purposes for now.

Differential Revision: https://reviews.llvm.org/D141478
2023-01-11 14:44:22 +01:00
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