105 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
Tongsheng Wu
8435de0916
[clangd] Print include spelling in IncludeCleaner error message (#136237) 2025-04-21 02:09:51 -04: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
Utkarsh Saxena
5f1adf0433
[clangd] Fix crash with null check for Token at Loc (#94528)
Fixes https://github.com/llvm/llvm-project/issues/94599
2024-06-07 11:08:25 +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
3093d731df
[clangd] Avoid libFormat's objective-c guessing heuristic where possible (#84133)
This avoids a known libFormat bug where the heuristic can OOM on certain
large files (particularly single-header libraries such as miniaudio.h).

The OOM will still happen on affected files if you actually try to
format them (this is harder to avoid since the underlyting issue affects
the actual formatting logic, not just the language-guessing heuristic),
but at least it's avoided during non-modifying operations like hover,
and modifying operations that do local formatting like code completion.

Fixes https://github.com/clangd/clangd/issues/719
Fixes https://github.com/clangd/clangd/issues/1384
Fixes https://github.com/llvm/llvm-project/issues/70945
2024-03-11 04:16:45 -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
2336f792bc
[clangd] Dont require confirmation for include-cleaner batch-fixes (#76826)
False negative/positive rate has decreased to the degree that these
extra confirmations no longer provide any value, but only create
friction in the happy case.

When we have false analysis, people usually need to apply the fixes and
run the builds to discover the failure. At that point they can add
relevant IWYU pragmas to guide analysis, and will be more likely to
create bug reports due to extra annoyance :)
2024-01-04 12:57:42 +01: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
d5953e3e30 [clangd] 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-13 23:26:09 -08:00
Jan Svoboda
cb92511c4d
[clang] NFC: Remove OptionalDirectoryEntryRefDegradesToDirectoryEntryPtr (#74900) 2023-12-08 18:22:53 -08: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
Jan Svoboda
c03d18441c [clang] NFCI: Use FileEntryRef in PreamblePatch 2023-09-20 16:10:18 -07: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
90ecadde62
[include-cleaner] Filter references to identity macros
Despite being true positives, these results just confuse users. So
filter them out.

Differential Revision: https://reviews.llvm.org/D157905
2023-08-18 14:40: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
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
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
209694ded0 [clangd] Make the order of missing-include edits deterministic
Fixes https://github.com/llvm/llvm-project/issues/63995
2023-07-21 14:16:16 +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
Viktoriia Bakalova
1f7c7d4bdd [clangd] Update symbol collector to use include-cleaner.
Differential Revision: https://reviews.llvm.org/D152900
2023-07-19 13:47:02 +00: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
Haojian Wu
507d766d76 [include-cleaner] Add an IgnoreHeaders flag to the command-line tool.
Reviewed By: kadircet

Differential Revision: https://reviews.llvm.org/D153340
2023-07-06 11:18:44 +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
Haojian Wu
b472176829 [clangd] Remove unused includes in IncludeCleaner.cpp, NFC 2023-06-16 15:15:07 +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
Haojian Wu
eed4a4d02e [clang-tidy] Correct the include-cleaner-check diagnostic message for missing-includes.
We should print the symbol name rather than the header name in the
message.

Differential Revision: https://reviews.llvm.org/D153013
2023-06-15 15:26:48 +02: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
Haojian Wu
be8da1f6e6 [clangd] Use FileManager for getCanonicalPath, NFC
get rid of the SourceManager dependency -- getCanonicalPath doesn't use
other SourceManager fields.
2023-06-02 12:55:21 +02:00
Sam McCall
8f8479789a [clangd] downgrade missing-includes diagnostic to Information level
In practice, a Warning on every occurrence is very unpopular, even on a codebase
with clear rules about direct inclusion & moderately good compliance.

This change has various practical effects (in vscode for concreteness):
  - makes the diagnostic decoration less striking (blue vs yellow)
  - makes these diagnostics visually distinct from others when reading
  - causes these diagnostics to sort last in the "problems" view
  - allows these diagnostics to be easily filtered from the "problems" view

Differential Revision: https://reviews.llvm.org/D149912
2023-05-16 15:03:50 +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
Kadir Cetinkaya
5e74a3dc2d
[clangd][IncludeCleaner] Use a proper comparator for deduplicating findings
Previous one didn't have strict weak ordering guarantees.
2023-05-01 12:19:33 +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
Utkarsh Saxena
ed365f464a [clangd] Use FileEntryRef for canonicalizing filepaths.
Using FileEntry for retrieving filenames give the name used for the last access. FileEntryRef gives the first access name.

Last access name is suspected to change with unrelated changes in clang or the underlying filesystem. First access name gives more stability to the name and makes it easier to track.

Differential Revision: https://reviews.llvm.org/D148213
2023-04-13 16:49:30 +02: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
144562e678
[clangd] Treat preamble patch as main file for include-cleaner analysis
Since we redefine all macros in preamble-patch, and it's parsed after
consuming the preamble macros, we can get false missing-include diagnostics
while a fresh preamble is being rebuilt.

This patch makes sure preamble-patch is treated same as main file for
include-cleaner purposes.

Differential Revision: https://reviews.llvm.org/D148143
2023-04-12 21:03:21 +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
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
481f888536 [clangd] Use expansion location for missing include diagnostics.
Differential Revision: https://reviews.llvm.org/D146727
2023-03-27 14:26:12 +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
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