70 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
kadir çetinkaya
2f5dc596b5
[IncludeCleaner] Also check for spellings of physical headers (#99843)
Some physical headers can have "conflicting" spellings, when same
filename exists under two different include search paths. e.g.
<stdlib.h>
can be provided by both standard library and underlying libc headers. In
such scenarios if the usage is from the latter include-search path,
users can't spell it directly.

This patch ensures we also consider spellings of such includes, in
addition to their physical files to prevent conflicting suggestions.
2024-07-22 14:58:07 +02: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
Nathan Ridge
5549b01736
[clangd] Make all calls to format::getStyle() go through getFormatStyleForFile() (#82948) 2024-03-05 23:11:29 -05: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
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
43c20367f4 [include-cleaner][clangd][clang-tidy] Ignore resource dir during include-cleaner analysis.
Differential Revision: https://reviews.llvm.org/D157610
2023-09-07 11:39:18 +00: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
Haojian Wu
dcb28244fa [clangd] Respect IWYU keep pragma for standard headers.
see the issue https://github.com/llvm/llvm-project/issues/64191

Differential Revision: https://reviews.llvm.org/D156650
2023-07-31 13:21:54 +02: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
Sam McCall
e289ee99ce [clangd] enable unused-include warnings for standard library headers
Other <angle-quoted> headers are still excluded.

Differential Revision: https://reviews.llvm.org/D155385
2023-07-19 23:43:47 +02:00
Haojian Wu
85c6d57eb3 [Format][Tooling] Fix HeaderIncludes::insert not respect the main-file header.
Differential Revision: https://reviews.llvm.org/D154963
2023-07-13 10:41:46 +02:00
Haojian Wu
6585dd3b83 [clangd] Replace the hacky include-cleaner macro-reference implementation.
Now MainFileMacros preserves enough information, we perform a just-in-time
convertion to interop with include-cleaner::Macro for include-cleaer features.

Differential Revision: https://reviews.llvm.org/D147034
2023-06-23 14:08:55 +02: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
Haojian Wu
d0e89116af [clangd] Fix fixAll not shown when there is only one unused-include and missing-include diagnostics.
Discovered during the review in https://reviews.llvm.org/D149437#inline-1444851.

Differential Revision: https://reviews.llvm.org/D149822
2023-05-15 14:02:20 +02:00
Haojian Wu
4b1cec0652 [clangd] Add batch fixes for include-cleaner diagnostics
For each unused-include/missing-include diagnostic, we provide fix-all
alternative to them.

This patch also adds LSP ChangeAnnotation support.

Differential Revision: https://reviews.llvm.org/D147684
2023-04-27 12:08:59 +02:00
Kadir Cetinkaya
08f0725a3a
[clangd] Deduplicate missing-include findings
Differential Revision: https://reviews.llvm.org/D149165
2023-04-26 10:33:55 +02: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
Haojian Wu
2524000187 [clangd] Fix a nullptr-dereference crash in computeIncludeCleanerFindings.
Be more robust, we shuold not crash  when we cannot find the corresponding token from the
tokenbuffer.

Differential Revision: https://reviews.llvm.org/D147686
2023-04-13 12:17:39 +02:00
Kadir Cetinkaya
f3a815aa77
[clangd] Map references from include'd files to directives
Differential Revision: https://reviews.llvm.org/D147139
2023-03-30 12:06:22 +02:00
Viktoriia Bakalova
481f888536 [clangd] Use expansion location for missing include diagnostics.
Differential Revision: https://reviews.llvm.org/D146727
2023-03-27 14:26:12 +00:00
Kadir Cetinkaya
43fcfdb1d6
[IncludeCleaner][clangd] Mark umbrella headers as users of private
Private headers inside umbrella files shouldn't be marked as unused.

Differential Revision: https://reviews.llvm.org/D146406
2023-03-23 13:08:07 +01:00
Haojian Wu
be39daea84 [clangd] Remove IWYU handling code that is used only for the old unused-include feature.
The old implementation has been removed, this patch removes the clangd's IWYU
code that is only used for the old implmentation (include-cleaner library has
better support):

- IWYU export pragma
- IWYU keep pragma

Differential Revision: https://reviews.llvm.org/D145916
2023-03-13 12:34:34 +01: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
df92d7911b Revert "Re-land [clangd] Add support for missing includes analysis."
This reverts commit 7bd56ddaeeb578331d3dfc0c4a2fe2727d0d1861.
2023-03-08 12:29:43 +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
7bd56ddaee Re-land [clangd] Add support for missing includes analysis.
This reverts commit 2eb5ac99a76dbbf8ac68c538211fabeaa5ac0bfd.

Differential Revision: https://reviews.llvm.org/D145576
2023-03-08 12:19:17 +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
Kadir Cetinkaya
7177a237b6
[clangd] Add config option for fast diagnostics mode
Also wire it up for use with patched preambles and introduce test cases
for behaviour we'd like to improve.

Differential Revision: https://reviews.llvm.org/D142890
2023-02-22 15:54:14 +01: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
Haojian Wu
ccb67491f0 [clangd] Simplify some tests in IncludeCleanerTests, NFC
They were pointed out in the review of https://reviews.llvm.org/D140875
2023-01-19 10:48:54 +01:00
Kadir Cetinkaya
3c2cb8e2f0
[clangd] Disable IncludeCleaner for ObjC 2022-08-22 11:28:06 +02:00
Aleksandr Platonov
42ee0d8c16 [clangd][unittests][IncludeCleaner] Don't call findReferencedFiles() if the result is not used
IncludeCleaner.RecursiveInclusion and IncludeCleaner.IWYUPragmaExport tests don't check referenced files list, so we don't need to call findReferencedFiles() there.

Reviewed By: kbobyrev

Differential Revision: https://reviews.llvm.org/D131706
2022-08-12 21:00:11 +03:00
Kirill Bobyrev
40f361ace3
[clangd] Include Cleaner: ignore headers with IWYU export pragmas
Disable the warnings with `IWYU pragma: export` or `begin_exports` +
`end_exports` until we have support for these pragmas. There are too many
false-positive warnings for the headers that have the correct pragmas for now
and it makes the user experience very unpleasant.

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D125468
2022-05-16 10:13:47 +02:00
Sam McCall
bf9921adb9 [clangd] Disable predefined macros in tests. NFC
These aren't needed. With them the generated predefines buffer is 13KB.
For every TestTU, we must:
 - generate the buffer (3 times: parsing preamble, scanning preamble, main file)
 - parse the buffer (again 3 times)
 - serialize all the macros it defines in the PCH
 - compress the buffer itself to write it into the PCH
 - decompress it from the PCH

Avoiding this reduces unit test time by ~25%.

Differential Revision: https://reviews.llvm.org/D125172
2022-05-09 14:44:51 +02:00
Haojian Wu
8052f4d22a [AST] Consider QualifiedTemplateName in TemplateName::getAsUsingDecl().
If the underlying template name of a qualified template name is a using
decl, TemplateName::getAsUsingDecl() will return it.

This will make the UsingTemplateName consumer life easier.

Differential Revision: https://reviews.llvm.org/D124437
2022-04-27 14:16:19 +02:00
Kirill Bobyrev
9f05b111ee
[clangd] Include Cleaner: suppress unused warnings for IWYU pragma: export
Add limited support for "IWYU pragma: export" - for now it just supresses the
warning similar to "IWYU pragma: keep".

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D124170
2022-04-21 17:00:06 +02:00
Kirill Bobyrev
e1c0d2fb82 [clangd] Correctly identify self-contained headers included rercursively
Right now when exiting the file Headers.cpp will identify the recursive
inclusion (with a new FileID) as non self-contained and will add it to the set
from which it will never be removed. As a result, we get incorrect results in
the IncludeStructure and Include Cleaner. This patch is a fix.

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D124166
2022-04-21 16:54:59 +02:00
Haojian Wu
95f0f69441 [clangd] Handle the new Using TemplateName.
Add supports in FindTarget and IncludeCleaner. This would
improve AST-based features on a tempalte which is found via a using
declaration. For example, go-to-def on `vect^or<int> v;` gives us the
location of `using std::vector`, which was not previously.

Base on https://reviews.llvm.org/D123127

Differential Revision: https://reviews.llvm.org/D123212
2022-04-20 15:42:24 +02: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
Sam McCall
e63d7bdc28 [clangd] Fix include-cleaner false-positive bug
For TemplateSpecializationType, we were checking the node's newness
twice, so it always failed the second test.

Fixes https://github.com/clangd/clangd/issues/1036
2022-02-26 14:11:48 +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