40 Commits

Author SHA1 Message Date
Harald-R
7df458b473
Follow style configuration in clangd when inserting missing includes (#140594)
The missing include diagnostic has the capability to introduce the
necessary headers into the source file. However, it does not currently
follow the inclusion style found in the `.clangd` file. For example, if
the file explicitly mentions that headers should be include with angled
brackets, they could be included with quotes instead. More details in
https://github.com/llvm/llvm-project/issues/138740. This PR fixes this
gap, so that the style configuration is followed.
2025-06-05 11:36:01 +02:00
kleines Filmröllchen
1f90797f6a
[clangd] Allow specifying what headers are always included via "" or <> (#67749)
Projects can now add config fragments like this to their .clangd:

```yaml
Style:
  QuotedHeaders: "src/.*"
  AngledHeaders: ["path/sdk/.*", "third-party/.*"]
```

to force headers inserted via the --header-insertion=iwyu mode matching
at least one of the regexes to have <> (AngledHeaders) or ""
(QuotedHeaders) around them, respectively. For other headers (and in
conflicting cases where both styles have a matching regex), the current
system header detection remains.

Fixes https://github.com/clangd/clangd/issues/1247
2024-12-27 14:14:29 -05:00
Vadim D
8c5a7a1fc4
[clangd] Add config option to allow detection of unused angled includes (#87208)
This PR adds a new `AnalyzeAngledIncludes` option to `Includes` section
of clangd config. This option enables unused include checks for all includes
that use the `<>` syntax, not just standard library includes.
2024-05-29 13:29:57 -04:00
Kadir Cetinkaya
550e09db1a
[clangd][NFC] Delete dead code 2024-04-04 10:01:13 +02:00
Nathan Ridge
5549b01736
[clangd] Make all calls to format::getStyle() go through getFormatStyleForFile() (#82948) 2024-03-05 23:11:29 -05:00
Sam McCall
d97a3419c0 [clangd] Loose include-cleaner matching for verbatim headers
This updates clangd to take advantage of the APIs added in D155819.
The main difficulties here are around path normalization.

For layering and performance reasons Includes compares paths lexically, and so
we should have consistent paths that can be compared across addSearchPath() and
add(): symlinks resolved or not, relative or absolute.
This patch spells out that requirement, for most tools consistent use of
FileManager/HeaderSearch is enough.

For clangd this does not work: IncludeStructure doesn't hold FileEntrys due to
the preamble/main-file split. It records paths, but canonicalizes them first.
We choose to use this canonical form as our common representation, so we have
to canonicalize the directory entries too. This is done in preamble-build and
recorded in IncludeStructure, as canonicalization is quite expensive.

Differential Revision: https://reviews.llvm.org/D155878
2023-07-27 19:20:53 +02:00
Viktoriia Bakalova
96e50797d6 [clangd] Fix the range for include reference to itself.
Differential Revision: https://reviews.llvm.org/D155215
2023-07-14 12:51:14 +00:00
Viktoriia Bakalova
6a6c7ed5cd [clangd] Use include_cleaner spelling strategies in clangd.
Differential Revision: https://reviews.llvm.org/D152913
2023-06-14 14:06:35 +00:00
Kadir Cetinkaya
031ffc3e06
[clangd] Decouple IncludeCleaner implementation from Config
This should help managing tests as we change defaults in configs.

Differential Revision: https://reviews.llvm.org/D152685
2023-06-12 11:38:58 +02:00
Viktoriia Bakalova
cd5fcea6d4 [clangd] Revert to older include spelling approach. 2023-06-05 15:18:28 +00:00
Viktoriia Bakalova
90c5fe9822 [include-cleaner] Allow multiple strategies for spelling includes.
Summary:

Reviewers:

Subscribers:

Differential Revision: https://reviews.llvm.org/D150185
2023-06-05 09:47:12 +00:00
Viktoriia Bakalova
9e9b1effac [clangd] Implement cross reference request for #include lines.
Differential Revision: https://reviews.llvm.org/D147044
2023-04-20 07:11:48 +00:00
Viktoriia Bakalova
655baae2af [clangd] Show used symbols on #include line hover.
Differential Revision: https://reviews.llvm.org/D146244
2023-03-28 14:05:34 +00:00
Viktoriia Bakalova
2bececb8be [clangd] Add provider info on symbol hover.
Differential Revision: https://reviews.llvm.org/D144976
2023-03-23 16:21:51 +00:00
Haojian Wu
7ab16be4c0 [clangd] UnusedIncludes: Strict config now uses the include-cleaner-library implementation.
And remove the classical clangd-own unused-include implementation.

Differential Revision: https://reviews.llvm.org/D145773
2023-03-10 11:56:11 +01:00
Viktoriia Bakalova
2e82eb1f74 Re-land [clangd] Add support for missing includes analysis.
This reverts commit fd8c9ef20a9519dccd5b8178b29ed4574285d36f.

Differential Revision: https://reviews.llvm.org/D145577
2023-03-08 13:29:52 +00:00
Viktoriia Bakalova
fd8c9ef20a Revert "Re-land [clangd] Add support for missing includes analysis."
This reverts commit 85a5c17b66768353b7fff717904e42805bb6a547.
2023-03-08 12:34:15 +00:00
Viktoriia Bakalova
46447e0ba2 Revert "Revert "Re-land [clangd] Add support for missing includes analysis.""
This reverts commit 9814b4d07f614e83e7a244f74fc562f2b5cc9b15.
2023-03-08 12:27:17 +00:00
Viktoriia Bakalova
9814b4d07f Revert "Re-land [clangd] Add support for missing includes analysis."
This reverts commit 85a5c17b66768353b7fff717904e42805bb6a547.
2023-03-08 12:24:51 +00:00
Viktoriia Bakalova
85a5c17b66 Re-land [clangd] Add support for missing includes analysis.
This reverts commit 2eb5ac99a76dbbf8ac68c538211fabeaa5ac0bfd.
2023-03-08 11:02:12 +00:00
Nico Weber
2eb5ac99a7 Revert "[clangd] Add support for missing includes analysis."
This reverts commit 38b9fb5a129db3e086610d53b534833273c5b4d0.
Breaks tests on Windows, see comments on https://reviews.llvm.org/D143496
2023-03-07 22:14:11 -05:00
Viktoriia Bakalova
38b9fb5a12 [clangd] Add support for missing includes analysis.
Differential Revision: https://reviews.llvm.org/D143496
2023-03-07 16:07:19 +00:00
Haojian Wu
939dce12f9 [clangd] Implement unused include warnings with include-cleaner library.
A prototype of using include-cleaner library in clangd:

- (re)implement clangd's "unused include" warnings with the library
- the new implementation is hidden under a flag `Config::UnusedIncludesPolicy::Experiment`

Differential Revision: https://reviews.llvm.org/D140875
2023-01-19 14:31:40 +01:00
Kazu Hirata
f71ffd3b73 [clang-tools-extra] Use std::optional instead of llvm::Optional (NFC)
This patch replaces (llvm::|)Optional< with std::optional<.  I'll post
a separate patch to clean up the "using" declarations, #include
"llvm/ADT/Optional.h", etc.

This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2023-01-07 20:19:42 -08:00
Kazu Hirata
71f557355d [clang-tools-extra] Add #include <optional> (NFC)
This patch adds #include <optional> to those files containing
llvm::Optional<...> or Optional<...>.

I'll post a separate patch to actually replace llvm::Optional with
std::optional.

This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2023-01-07 20:02:20 -08:00
Kirill Bobyrev
012e90bb24
Reland "[clangd] IncludeCleaner: Add support for IWYU pragma private"
This lands 4cb38bfe76b7ef157485338623c931d04d17b958 again.
2022-04-05 16:57:39 +02:00
Kirill Bobyrev
f43c4c5be2 Revert "[clangd] IncludeCleaner: Add support for IWYU pragma private"
This reverts commit 4cb38bfe76b7ef157485338623c931d04d17b958.

Awkwardly enough, this builds Windows buildbots:

http://45.33.8.238/win/55402/step_9.txt

It is yet unclear why this is happening but I will need more time to
diagnose the issue.
2022-03-31 17:59:52 +02:00
Kirill Bobyrev
4cb38bfe76
[clangd] IncludeCleaner: Add support for IWYU pragma private
Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D120306
2022-03-31 12:49:52 +02:00
Kirill Bobyrev
d5106c8f97
[clangd] NFC: Cleanup IncludeCleaner API
Make a further improvement to decrease verbosity of the API: ASTContext
provides SourceManager access.

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D119842
2022-03-08 13:43:25 +01:00
Kirill Bobyrev
46a6f5ae14 [clangd] NFC: Move stdlib headers handling to Clang
This will allow moving the IncludeCleaner library essentials to Clang
and decoupling them from the majority of clangd.

The patch itself just moves the code, it doesn't change existing
functionality.

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D119130
2022-02-09 11:05:39 +01:00
Kirill Bobyrev
089d9c50b2
[clangd] IncludeCleaner: Decrease API dependency on clangd
Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D118882
2022-02-03 14:40:53 +01:00
Sam McCall
478863ef58 [clangd] Basic IncludeCleaner support for c/c++ standard library
There are some limitations here, so this is behind a flag for now (in addition
to the config setting for the overall feature).

- symbols without exactly one associated header aren't handled right
- no macro support
- referencing std::size_t usually doesn't leave any trace in the AST that the
  alias in std was used, so we associate with stddef.h instead of cstddef.
  (An AST issue not specific to stdlib, but much worse there)

Differential Revision: https://reviews.llvm.org/D114077
2022-01-03 18:19:56 +01:00
Christian Kühnel
5bd643d31d [clangd] cleanup of header guard names
Renaming header guards to match the LLVM convention.
This patch was created by automatically applying the fixes from
clang-tidy.

I've removed the [NFC]  tag from the title, as we're adding header guards in some files and thus might trigger behavior changes.

Differential Revision: https://reviews.llvm.org/D113896
2021-12-02 15:58:35 +00:00
Kirill Bobyrev
34cc210aa8
[clangd] IncludeCleaner: Attribute symbols from non self-contained headers to their parents
When a symbol comes from the non self-contained header, we recursively uplift
the file we consider used to the first includer that has a header guard. We
need to do this while we still have FileIDs because every time a non
self-contained header is included, it gets a new FileID but is later
deduplicated by HeaderID and it's not possible to understand where it was
included from.

Based on D114370.

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D114623
2021-11-26 16:20:48 +01:00
Kirill Bobyrev
f47564ea87
[clangd] IncludeCleaner: Skip non self-contained headers
Headers without include guards might have side effects or can be the files we
don't want to consider (e.g. tablegen ".inc" files). Skip them when translating
headers to the HeaderIDs that we will consider as unused.

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D112695
2021-10-29 17:57:31 +02:00
Sam McCall
73453e7ade [clangd] Avoid expensive checks of buffer names in IncludeCleaner
This changes the handling of special buffers (<command-line> etc) that
SourceManager treats as files but FileManager does not.

We now include them in findReferencedFiles() and drop them as part of
translateToHeaderIDs(). This pairs more naturally with the data representations
we're using, and so avoids a bunch of converting between representations for
filtering.

Differential Revision: https://reviews.llvm.org/D112652
2021-10-28 08:00:57 +02:00
Kirill Bobyrev
e3c6090e59 [clangd] IncludeCleaner: Support macros
Collect the macro definition locations for all the macros used in the main
file.

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D112447
2021-10-27 10:30:04 +02:00
Kirill Bobyrev
6d314ee570
[clangd] Add a way to enable IncludeCleaner through config
This is useful for dogfooding the feature and spotting bugs.

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D111870
2021-10-26 12:53:05 +02:00
Kirill Bobyrev
ebfcd06d42 [clangd] IncludeCleaner: Mark used headers
Follow-up on D105426.

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D108194
2021-10-05 18:08:24 +02:00
Kirill Bobyrev
d1ec581ebf
[clangd] IncludeCleaner as a library: Find all references to symbols in the file
This is the first patch in an ongoing attempt of Include Cleaner: unused/missing
headere diagnostics, an IWYU-like functionality implementation for clangd. The
work is split into (mostly) distinct and parallelizable pieces:

- Finding all referenced locations (this patch).
- Finding all referenced locations of macros.
- Building IncludeGraph and marking headers as unused, used and directly used.
- Making use of the introduced library and add an option to use in clangd.

---

* Adding support for standard library headers (possibly through mapping
  genfiles).

Based on https://reviews.llvm.org/D100540.

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D105426
2021-08-18 10:08:35 +02:00