1720 Commits

Author SHA1 Message Date
Nathan Ridge
fa0498fdae
[clang][HeuristicResolver] Apply default argument heuristic in resolveDeclRefExpr as well (#132576)
This is a follow-up to https://github.com/llvm/llvm-project/pull/131074.

After moving the default argument heuristic to `simplifyType` in that
patch, the heuristic no longer applied to the 
`DependentScopeDeclRefExpr` case, because that wasn't using
`simplifyType`.

This patch fixes that, with an added testcase.
2025-04-11 02:35:04 -04:00
Jan Svoboda
7a370748c0
[clang][lex] Store non-owning options ref in HeaderSearch (#132780)
This makes it so that `CompilerInvocation` can be the only entity that
manages ownership of `HeaderSearchOptions`, making it possible to
implement copy-on-write semantics.
2025-03-25 12:14:06 -07:00
Alex Hoppen
3bcbb47258
[clangd] Use SymbolName to represent Objective-C selectors (#82061)
This is a cleaner design than using identifier and an optional `Selector`. It also allows rename of Objective-C method names if no declaration is at hand and thus no `Selector` instance can be formed. For example, when finding the ranges to rename based on an index that’s not clangd’s built-in index.
2025-03-25 14:34:00 -04:00
Chuanqi Xu
e5f100676e
[clangd] [C++20] [Modules] Add modules suffix for 'Header' Source Switch (#131591)
Support the trivial "header"/source switch for module interfaces.

I initially thought the naming are bad and we should rename it. But
later I feel it is better to split patches as much as possible.

From the codes it looks like there are problems. e.g., `isHeaderFile`.
But let's try to fix them in different patches.
2025-03-25 09:43:13 +08:00
Kazu Hirata
4a7643400c
[clang-tools-extra] Use *Set::insert_range (NFC) (#132589)
DenseSet, SmallPtrSet, SmallSet, SetVector, and StringSet recently
gained C++23-style insert_range.  This patch replaces:

  Dest.insert(Src.begin(), Src.end());

with:

  Dest.insert_range(Src);
2025-03-23 00:23:19 -07:00
Matheus Izvekov
14f7bd63b9
Reland: [clang] preserve class type sugar when taking pointer to member (#132401)
Original PR: #130537
Originally reverted due to revert of dependent commit. Relanding with no
changes.

This changes the MemberPointerType representation to use a
NestedNameSpecifier instead of a Type to represent the base class.

Since the qualifiers are always parsed as nested names, there was an
impedance mismatch when converting these back and forth into types, and
this led to issues in preserving sugar.

The nested names are indeed a better match for these, as the differences
which a QualType can represent cannot be expressed syntatically, and
they represent the use case more exactly, being either dependent or
referring to a CXXRecord, unqualified.

This patch also makes the MemberPointerType able to represent sugar for
a {up/downcast}cast conversion of the base class, although for now the
underlying type is canonical, as preserving the sugar up to that point
requires further work.

As usual, includes a few drive-by fixes in order to make use of the
improvements.
2025-03-21 13:20:52 -03:00
Matheus Izvekov
5151e6d7fe
Revert "Reland: [clang] preserve class type sugar when taking pointer to member" (#132280)
Reverts llvm/llvm-project#132234

Needs to be reverted due to dependency.

This blocks reverting another PR, see here:
https://github.com/llvm/llvm-project/pull/131965#issuecomment-2741619498
2025-03-20 17:52:48 -03:00
Matheus Izvekov
578f38cd08
Reland: [clang] preserve class type sugar when taking pointer to member (#132234)
Original PR: #130537
Reland after updating lldb too.

This changes the MemberPointerType representation to use a
NestedNameSpecifier instead of a Type to represent the base class.

Since the qualifiers are always parsed as nested names, there was an
impedance mismatch when converting these back and forth into types, and
this led to issues in preserving sugar.

The nested names are indeed a better match for these, as the differences
which a QualType can represent cannot be expressed syntatically, and
they represent the use case more exactly, being either dependent or
referring to a CXXRecord, unqualified.

This patch also makes the MemberPointerType able to represent sugar for
a {up/downcast}cast conversion of the base class, although for now the
underlying type is canonical, as preserving the sugar up to that point
requires further work.

As usual, includes a few drive-by fixes in order to make use of the
improvements.
2025-03-20 15:33:54 -03:00
Matheus Izvekov
6cd62ad08c
Revert "[clang] improve class type sugar preservation in pointers to members" (#132215)
Reverts llvm/llvm-project#130537

This missed updating lldb, which we didn't notice due to lack of
pre-commit CI.
2025-03-20 11:09:25 -03:00
Matheus Izvekov
9b1f905b48
[clang] improve class type sugar preservation in pointers to members (#130537)
This changes the MemberPointerType representation to use a
NestedNameSpecifier instead of a Type to represent the class.

Since the qualifiers are always parsed as nested names, there was an
impedance mismatch when converting these back and forth into types, and
this led to issues in preserving sugar.

The nested names are indeed a better match for these, as the differences
which a QualType can represent cannot be expressed syntactically, and it
also represents the use case more exactly, being either dependent or
referring to a CXXRecord, unqualified.

This patch also makes the MemberPointerType able to represent sugar for
a {up/downcast}cast conversion of the base class, although for now the
underlying type is canonical, as preserving the sugar up to that point
requires further work.

As usual, includes a few drive-by fixes in order to make use of the
improvements, and removing some duplications, for example
CheckBaseClassAccess is deduplicated from across SemaAccess and
SemaCast.
2025-03-20 10:30:24 -03:00
Mythreya
9cdbc47144
[clangd] Add HeaderInsertion config option (#128503)
This is the config file equivalent of the `--header-insertion` command line option

Fixes https://github.com/clangd/clangd/issues/2032
2025-03-20 02:22:29 -04:00
Nathan Ridge
8266cd9f84
[clangd] Disable cppcoreguidelines-macro-to-enum clang-tidy checker (#129478)
Clangd does not support its checker because the checker relies on having
seen preprocessor conditionals that occur in the preamble, and clangd
does not currently replay those.

This checker was already disabled under its main name,
modernize-macro-to-enum (https://github.com/clangd/clangd/issues/1464).
This commit disables it under the alternative name
cppcoreguidelines-macro-to-enum as well.

Fixes https://github.com/llvm/llvm-project/issues/127965
2025-03-04 03:46:54 -05:00
Ujan RoyBandyopadhyay
440ea3ecdc
[clangd] Reduce superfluous rename conflicts (#121515)
This commit adds a namespace check to the code for detecting name
collisions, allowing `bar` to be renamed to `foo` in the following
snippet:

```c
typedef struct foo {} Foo;
Foo bar;
```

Previously, such a rename would fail because a declaration for `foo`
already exists in the same scope.
2025-02-27 13:06:03 -06:00
Chuanqi Xu
ae839b0250
[clangd] [C++20] [Modules] Add scanning cache (#125988)
Previously, everytime we want to get a source file declaring a specific
module, we need to scan the whole projects again and again. The
performance is super bad. This patch tries to improve this by
introducing a simple cache.
2025-02-26 16:03:04 +08:00
Sirraide
b0210fee94
[Clang] [NFC] Fix more -Wreturn-type warnings in tests everywhere (#123470)
With the goal of eventually being able to make `-Wreturn-type` default to an 
error in all language modes, this is a follow-up to #123464 and updates even
more tests, mainly clang-tidy and clangd tests.
2025-02-20 19:49:37 +01:00
David Goldman
1841bcd5d1
[clangd] Update XRefs to support overridden ObjC methods (#127109)
- Support finding implementors of a protocol and discovering subclasses for ObjC interfaces via the implementations call
- Support jumping to the overridden method when you trigger goto definition on an override
- Properly find references to overridden methods
2025-02-19 13:21:41 -05:00
Nikolas Klauser
0865ecc515
[clang] Extend diagnose_if to accept more detailed warning information, take 2 (#119712)
This is take two of #70976. This iteration of the patch makes sure that
custom
diagnostics without any warning group don't get promoted by `-Werror` or
`-Wfatal-errors`.

This implements parts of the extension proposed in
https://discourse.llvm.org/t/exposing-the-diagnostic-engine-to-c/73092/7.

Specifically, this makes it possible to specify a diagnostic group in an
optional third argument.
2025-01-28 08:41:31 +01:00
Petr Polezhaev
2b0e2255d6
[clangd] Support .clangd command line modifications for C++ modules (#122606)
Tunnels `Manger` object into the `ScanningAllProjectModules` so it can
be used to perform necessary command-line modifications (which also adds
`--resources` path previously added there explicitly). This allows using
the experimental C++ modules support with gcc.

This was discussed in the issue with @ChuanqiXu9 and @kadircet

Closes #112635
2025-01-22 17:27:28 +08:00
Nathan Ridge
ae932becb2
[clang][Sema] Upstream HeuristicResolver from clangd (#121314)
Fixes https://github.com/llvm/llvm-project/issues/121310
2025-01-17 17:01:00 -05:00
Bevin Hansson
030d48b7db
[clangd] Augment code completion results with documentation from the index. (#120099)
When looking up code completions from Sema, there is no associated
documentation. This is due to crash issues with stale preambles.
However, this also means that code completion results from other
than the main file do not have documentation in certain cases,
which is a bad user experience.

This patch performs a lookup into the index using the code
completion result declarations to find documentation, and
attaches it to the results.

Fixes clangd/clangd#2252
Fixes clangd/clangd#564
2025-01-15 01:14:20 -05:00
Benjamin Kramer
a3b4d91474 Fix -Wdangling-assignment-gsl in ClangdLSPServerTests
I think this is a false positive for a non-capturing lambda, but I can't
find anything in the standard that guarantees that these have eternal
lifetime.
2025-01-08 16:16:46 +01:00
Nathan Ridge
30ba8be22e
[clangd] Add a unit test suite for HeuristicResolver (#121313)
Fixes https://github.com/clangd/clangd/issues/2154
2025-01-07 20:52:06 -05:00
Nathan Ridge
bc87a537d9
[clangd] Remove clangd's HasValue GMock matcher (#121309)
An equivalent matcher under the name Optional has since been added
upstream to GMock.

Fixes https://github.com/llvm/llvm-project/issues/121308
2025-01-02 01:42:21 -05: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
cor3ntin
db93ef14ae
[Clang] Implement CWG2813: Class member access with prvalues (#120223)
This is a rebase of #95112 with my own feedback apply as @MitalAshok has
been inactive for a while.
It's fairly important this makes clang 20 as it is a blocker for #107451

--- 

[CWG2813](https://cplusplus.github.io/CWG/issues/2813.html)

prvalue.member_fn(expression-list) now will not materialize a temporary
for prvalue if member_fn is an explicit object member function, and
prvalue will bind directly to the object parameter.

The E1 in E1.static_member is now a discarded-value expression, so if E1
was a call to a [[nodiscard]] function, there will now be a warning.
This also affects C++98 with [[gnu::warn_unused_result]] functions.

This should not affect C where TemporaryMaterializationConversion is a
no-op.

Closes #100314
Fixes #100341

---------

Co-authored-by: Mital Ashok <mital@mitalashok.co.uk>
2024-12-18 10:44:42 +01:00
Joseph Huber
e1271dd5a7
[clangd] Index reserved symbols from *intrin.h system headers (#119735)
Summary:
`clangd` intentionally suppresses indexing symbols from system headers
as these are likely implementation details the user does not want.
Howver, there are plenty of system headers that provide extensions that
we want to index, such as vector intrinsic headers. This patch adds an
extra check for these commonly-named '*intrin.h' headers. This is not
fully inclusive for all symbols the user might want, but it's a good
start.

Fixes: https://github.com/llvm/llvm-project/issues/118684

---------

Co-authored-by: Nathan Ridge <zeratul976@hotmail.com>
2024-12-15 08:04:47 -06:00
Christian Kandeler
8d714db7f9
[clangd] Consider expression statements in ExtractVariable tweak (#112525)
For instance:
  int func();
  int main()
  {
    func(); // => auto placeholder = func();
  }
2024-12-11 10:58:22 +01:00
Nathan Ridge
73adf26d50
[clangd] Handle DeducedTemplateSpecializationType in HeuristicResolver (#119107)
Fixes https://github.com/clangd/clangd/issues/2227
2024-12-09 12:59:01 -05:00
Nathan Ridge
70c1764d7a
[clang] [Sema] Preserve nested name specifier prefix in MemberPointerType (#118236)
Fixes https://github.com/llvm/llvm-project/issues/118198
Fixes https://github.com/clangd/clangd/issues/2235
2024-12-07 20:17:07 -05:00
Nathan Ridge
9ccde12f5e
[clang] Compute accurate begin location for CallExpr with explicit object parameter (#117841)
The explicit object parameter is written before the callee expression,
so the begin location should come from the explicit object parameter.

Fixes https://github.com/llvm/llvm-project/issues/116335
2024-12-05 19:52:23 -05:00
Nathan Ridge
61fe67a401
[clangd] support outgoing calls in call hierarchy (#117673)
This reverts commit ce0f11325e0c62c5b81391589e9b93b412a85bc1.
2024-12-05 10:10:42 +01:00
Augie Fackler
ce0f11325e Revert "[clangd] Re-land "support outgoing calls in call hierarchy" (#117673)"
This reverts commit 7be3326200ef382705d8e6b2d7dc5378af96b34a.

Per https://protobuf.dev/programming-guides/dos-donts/#add-required
this will re-land tomorrow without the required fields.
2024-12-04 15:58:56 -05:00
Nathan Ridge
7be3326200
[clangd] Re-land "support outgoing calls in call hierarchy" (#117673)
Co-authored-by: Quentin Chateau <quentin.chateau@gmail.com>
2024-12-04 01:07:57 -05:00
Christian Kandeler
9acd8e3810
[clangd] Drop requirement for named template parameters (#117565)
... in DefineOutline tweak for function templates. As opposed to class
templates, the name is not required for writing an out-of-line
definition.
2024-11-26 10:11:07 +01:00
Nathan Ridge
d77cab823f
Revert "[clangd] Support outgoing calls in call hierarchy (#77556)" (#117668)
This reverts commit ca184cfc088a843e545e5f04b48813e6f9bfba77.
2024-11-26 00:08:33 -05:00
Nathan Ridge
ca184cfc08
[clangd] Support outgoing calls in call hierarchy (#77556)
Co-authored-by: Quentin Chateau <quentin.chateau@gmail.com>
2024-11-25 23:58:33 -05:00
Nathan Ridge
5518bb215b
[clangd] Check getFunctionTypeLoc() for validity in InlayHintVisitor (#117296)
Fixes https://github.com/clangd/clangd/issues/2223
2024-11-22 03:11:07 -05:00
Christian Kandeler
5310855316
[clangd] Fix erroneous qualification of template type parameters (#116821)
...in DefineOutline tweak.
E.g. moving the following definition:
  `template<typename T> struct S { T f^oo() const { return T(); } };`
would result in:
 `template<typename T> S<T>::T S::foo() const { return T(); }`
instead of:
  `template<typename T> T S::foo() const { return T(); }`
2024-11-20 10:15:29 +01:00
Christian Kandeler
8a6a76b1e1
[clangd] Let DefineOutline tweak handle member function templates (#112345) 2024-11-19 12:47:15 +01:00
Nathan Ridge
545917cb4b
[clangd] Harden incomingCalls() against possible misinterpretation of a range as pertaining to the wrong file (#111616)
`CallHierarchyIncomingCall::fromRanges` are interpreted as ranges in the
same file as the `CallHierarchyItem` representing the caller
(`CallHierarchyIncomingCall::from`).

In C/C++, it's possible for the calls to be in a different file than the caller,
as illustrated in the added test case.

With this patch, such calls are dropped, rather than their ranges being
incorrectly interpreted as pertaining to the wrong file.
2024-11-19 01:32:04 -05:00
Michael Park
4fb1f2e58a
[clangd] Fix the modification detection logic in ClangdLSPServer::applyConfiguration. (#115438)
Prior to this, the "old != new" check would always evaluate to true because it was comparing a pre-mangling new command to a post-mangling old command.
2024-11-15 02:39:09 -05:00
Julian Schmidt
9ae21b073a
[clangd] fix extract-to-function for overloaded operators (#81640)
When selecting code that contains the use of overloaded operators,
the SelectionTree will attribute the operator to the operator
declaration, not to the `CXXOperatorCallExpr`. To allow
extract-to-function to work with these operators, make unselected
`CXXOperatorCallExpr`s valid root statements, just like `DeclStmt`s.

Partially fixes clangd/clangd#1254

---------

Co-authored-by: Nathan Ridge <zeratul976@hotmail.com>
2024-11-15 01:05:20 -05:00
Chuanqi Xu
e385e0d3e7
[clangd] [Modules] Support Reusable Modules Builder (#106683)
This is the following patch of
https://github.com/llvm/llvm-project/pull/66462 to optimize its
performance.

# Motivation

To avoid data races, we choose "per file owns its dependent modules"
model. That said, every TU will own all the required module files so
that we don't need to worry about thread safety. And it looks like we
succeeded that we focus on the interfaces and structure of modules
support in clangd. But after all, this model is not good for
performance. Image we have 10000 TUs import std, we will have 10000
std.pcm in the memory. That is terrible both in time and space.

Given the current modules support in clangd works pretty well (almost
every issue report I received is more or less a clang's issue), I'd like
to improve the performance.

# High Level Changes

After this patch, the built module files will be owned by the module
builder and each TU will only have a reference to the built module
files.

The module builder have a map from module names to built module files.
When a new TU ask for a module file, the module builder will check if
the module file lives in the map and if the module file are up to date.
If yes, the module file will be returned. If no, the module file entry
would be erased in the module builder. We use `shared_ptr<>` to track
module file here so that the other TU owning the out dated module file
won't be affected. The out dated module file will be removed
automatically if other TU gets update or closed.

(I know the out dated module file may not exist due to the `CanReuse`
mechanism. But the design here is natural and can be seen as a redundant
design to make it more robust.)

When we a build a module, we will use the mutex and the condition
variable in the working thread to build it exclusively. All other
threads that also want the module file would have to wait for that
working thread. It might not sounds great but I think if we want to make
it asynchronous, we have to refactor TUScheduler as far as I know.

# Code Structure Changes

Thanks for the previous hard working reviewing, the interfaces almost
don't change in this patch. Almost all the work are isolated in
ModulesBuilder.cpp. A outliner is that we convert `ModulesBuilder` to an
abstract class since the implementation class needs to own the module
files.

And the core function to review is
`ReusableModulesBuilder::getOrBuildModuleFile`. It implements the core
logic to fetch the module file from the cache or build it if the module
file is not in the cache or out of date. And other important entities
are `BuildingModuleMutexes`, `BuildingModuleCVs`, `BuildingModules` and
`ModulesBuildingMutex`. These are mutexes and condition variables to
make sure the thread safety.

# User experience

I've implemented this in our downstream and ask our users to use it. I
also sent it https://github.com/ChuanqiXu9/clangd-for-modules here as
pre-version. The feedbacks are pretty good. And I didn't receive any bug
reports (about the reusable modules builder) yet.

# Other potential improvement

The are other two potential improvements can be done:
1. Scanning cache and a mechanism to get the required module information
more quickly. (Like the module maps in
https://github.com/ChuanqiXu9/clangd-for-modules)
2. Persist the module files. So that after we close the vscode and
reopen it, we can reuse the built module files since the last
invocation.
2024-11-12 17:45:05 +08:00
timon-ul
e102338b6e
Support call hierarchy for fields and non-local variables (#113900)
Fixes https://github.com/clangd/clangd/issues/1308
2024-11-03 01:38:25 -05:00
Tor Shepherd
2eb1699184
[clangd] Add inlay hints for default function arguments (#95712)
The new inlay hints have the `DefaultArguments` kind and can be enabled in config similar to other inlay kint kinds.
2024-10-19 18:19:01 -04:00
Christian Kandeler
0cfa6e2092
[clangd] Let DefineOutline tweak handle member functions (#95235)
... of class templates.
2024-10-14 11:00:02 +02:00
Nathan Ridge
18ca7ad339
[clangd] Add ArgumentLists config option under Completion (#111322)
The new config option is a more flexible version of
--function-arg-placeholders, allowing users more detailed control of
what is inserted in argument list position when clangd completes the
name of a function in a function call context.

Fixes https://github.com/llvm/llvm-project/issues/63565

Co-authored-by: MK-Alias <ImNotReadingThis@maininator.com>
2024-10-06 20:32:54 -04:00
Nathan Ridge
acf92a47c0
[clang-tidy] Avoid capturing a local variable in a static lambda in UseRangesCheck (#111282)
Fixes https://github.com/llvm/llvm-project/issues/109367
2024-10-06 18:13:36 -04:00
Tor Shepherd
81fcdc6359
[clangd] Add CodeAction to swap operands to binary operators (#78999)
This MR resolves https://github.com/llvm/llvm-project/issues/78998
2024-10-03 18:34:02 +02:00
kadir çetinkaya
3d9cf8b397
[clangd] Improve filtering logic for undesired proto symbols (#110091)
This used to filter any names with `_` in them, apart from
enum-constants. Resulting in discrepancies in behavior when we had
fields that have `_` in the name, or for accessors like `set_`, `has_`.

The logic seems to be trying to filter mangled names for nested entries,
so adjusted logic to only do so for top-level decls, while still
preserving some public top-level helpers.

Heuristics are still leaning towards false-negatives, e.g. if a
top-level entity has `_` in its name (`message Foo_Bar {}`), it'll be
filtered, or an enum that prefixes its type name to constants
(`enum Foo { Foo_OK }`).
2024-09-30 14:40:54 +02:00