26 Commits

Author SHA1 Message Date
Nathan Ridge
093af77ece
[clangd] Avoid calling resolveTypeOfCallExpr() twice in HeuristicResolver::resolveExprToType() (#164353)
Fixes the performance regression reported at
https://github.com/llvm/llvm-project/pull/156282#issuecomment-3421570600
2025-10-22 04:34:21 +00:00
Nathan Ridge
43ec53b849
[clang][HeuristicResolver] Default argument heuristic for template template parameters (#156404)
Fixes https://github.com/clangd/clangd/issues/2478
2025-09-17 20:51:52 -04:00
Nathan Ridge
e07af8cbbf
[clang][HeuristicResolver] Use HeuristicResolver to implement getApproximateType() in SemaCodeComplete (#156282)
This patch ports over the remaining functionality present in
SemaCodeComplete's getApproximateType() but not in HeuristicResolver,
into HeuristicResolver, and uses HeuristicResolver to reimplement
getApproximateType().

This has the effect of enhancing both code completion (which now
benefit from the full range of HeuristicResolver's capabilities), and
existing consumers of HeuristicResolver like clangd's go-to-definition
whose behaviour now has parity with code completion.

Fixes https://github.com/clangd/clangd/issues/2432
2025-09-16 15:18:50 -04:00
Matheus Izvekov
ba9d1c41c4
[clang] AST: remove DependentTemplateSpecializationType (#158109)
A DependentTemplateSpecializationType (DTST) is basically just a
TemplateSpecializationType (TST) with a hardcoded DependentTemplateName
(DTN) as its TemplateName.

This removes the DTST and replaces all uses of it with a TST, removing a
lot of duplication in the implementation.

Technically the hardcoded DTN is an optimization for a most common case,
but the TST implementation is in better shape overall and with other
optimizations, so this patch ends up being an overall performance
positive:
<img width="1465" height="38" alt="image"
src="https://github.com/user-attachments/assets/084b0694-2839-427a-b664-eff400f780b5"
/>

A DTST also didn't allow a template name representing a DTN that was
substituted, such as from an alias template, while the TST does allow it
by the simple fact it can hold an arbitrary TemplateName, so this patch
also increases the amount of sugar retained, while still being faster
overall.

Example (from included test case):
```C++
template<template<class> class TT> using T1 = TT<int>;
template<class T> using T2 = T1<T::template X>;
```

Here we can now represent in the AST that `TT` was substituted for the
dependent template name `T::template X`.
2025-09-12 13:55:38 -03:00
Kazu Hirata
0d7f66d49c
[Sema] Use llvm::is_contained instead of llvm::all_of (NFC) (#158213)
The code in question uses llvm::all_of and llvm::identity to see if
every pointer is nonnull:

  Ptr1 && Ptr2 && Ptr3 && ...

This patch simplifies the expression by checking for the absence of
nullptr with !llvm::is_contained.
2025-09-12 07:51:56 -07:00
Matheus Izvekov
249167a898
[clang] NFC: reintroduce clang/include/clang/AST/Type.h (#155050)
This reintroduces `Type.h`, having earlier been renamed to `TypeBase.h`,
as a redirection to `TypeBase.h`, and redirects most users to include
the former instead.

This is a preparatory patch for being able to provide inline definitions
for `Type` methods which would otherwise cause a circular dependency
with `Decl{,CXX}.h`.

Doing these operations into their own NFC patch helps the git rename
detection logic work, preserving the history.

This patch makes clang just a little slower to build (~0.17%), just
because it makes more code indirectly include `DeclCXX.h`.
2025-08-27 13:11:34 -03:00
Matheus Izvekov
bcd1530836
[clang] NFC: rename clang/include/clang/AST/Type.h to TypeBase.h (#155049)
This is a preparatory patch, to be able to provide inline definitions
for `Type` functions which depend on `Decl{,CXX}.h`. As the latter also
depends on `Type.h`, this would not be possible without some
reorganizing.

Splitting this rename into its own patch allows git to track this as a
rename, and preserve all git history, and not force any code
reformatting.

A later NFC patch will reintroduce `Type.h` as redirection to
`TypeBase.h`, rewriting most places back to directly including `Type.h`
instead of `TypeBase.h`, leaving only a handful of places where this is
necessary.

Then yet a later patch will exploit this by making more stuff inline.
2025-08-27 13:09:48 -03:00
Mythreya Kuricheti
f44eaf47dc
[clang][HeuristicResolver] Resolve explicit object parameter to enclosing record type (#155143)
Heuristically resolve the type of a `this auto` parameter to the record type
in the declaration. 

```cpp
struct Foo {
  int member {};
  auto&& getter1(this auto&& self) { // assume `self` is is `Foo`
    return self.member;
};
```

Fixes https://github.com/clangd/clangd/issues/2323
2025-08-26 23:54:50 -04:00
Kazu Hirata
136b541304
[clang] Replace SmallSet with SmallPtrSet (NFC) (#154262)
This patch replaces SmallSet<T *, N> with SmallPtrSet<T *, N>.  Note
that SmallSet.h "redirects" SmallSet to SmallPtrSet for pointer
element types:

  template <typename PointeeType, unsigned N>
class SmallSet<PointeeType*, N> : public SmallPtrSet<PointeeType*, N>
{};

We only have 30 instances that rely on this "redirection", with about
half of them under clang/.  Since the redirection doesn't improve
readability, this patch replaces SmallSet with SmallPtrSet for pointer
element types.

I'm planning to remove the redirection eventually.
2025-08-19 07:11:39 -07:00
Matheus Izvekov
91cdd35008
[clang] Improve nested name specifier AST representation (#147835)
This is a major change on how we represent nested name qualifications in
the AST.

* The nested name specifier itself and how it's stored is changed. The
prefixes for types are handled within the type hierarchy, which makes
canonicalization for them super cheap, no memory allocation required.
Also translating a type into nested name specifier form becomes a no-op.
An identifier is stored as a DependentNameType. The nested name
specifier gains a lightweight handle class, to be used instead of
passing around pointers, which is similar to what is implemented for
TemplateName. There is still one free bit available, and this handle can
be used within a PointerUnion and PointerIntPair, which should keep
bit-packing aficionados happy.
* The ElaboratedType node is removed, all type nodes in which it could
previously apply to can now store the elaborated keyword and name
qualifier, tail allocating when present.
* TagTypes can now point to the exact declaration found when producing
these, as opposed to the previous situation of there only existing one
TagType per entity. This increases the amount of type sugar retained,
and can have several applications, for example in tracking module
ownership, and other tools which care about source file origins, such as
IWYU. These TagTypes are lazily allocated, in order to limit the
increase in AST size.

This patch offers a great performance benefit.

It greatly improves compilation time for
[stdexec](https://github.com/NVIDIA/stdexec). For one datapoint, for
`test_on2.cpp` in that project, which is the slowest compiling test,
this patch improves `-c` compilation time by about 7.2%, with the
`-fsyntax-only` improvement being at ~12%.

This has great results on compile-time-tracker as well:

![image](https://github.com/user-attachments/assets/700dce98-2cab-4aa8-97d1-b038c0bee831)

This patch also further enables other optimziations in the future, and
will reduce the performance impact of template specialization resugaring
when that lands.

It has some other miscelaneous drive-by fixes.

About the review: Yes the patch is huge, sorry about that. Part of the
reason is that I started by the nested name specifier part, before the
ElaboratedType part, but that had a huge performance downside, as
ElaboratedType is a big performance hog. I didn't have the steam to go
back and change the patch after the fact.

There is also a lot of internal API changes, and it made sense to remove
ElaboratedType in one go, versus removing it from one type at a time, as
that would present much more churn to the users. Also, the nested name
specifier having a different API avoids missing changes related to how
prefixes work now, which could make existing code compile but not work.

How to review: The important changes are all in
`clang/include/clang/AST` and `clang/lib/AST`, with also important
changes in `clang/lib/Sema/TreeTransform.h`.

The rest and bulk of the changes are mostly consequences of the changes
in API.

PS: TagType::getDecl is renamed to `getOriginalDecl` in this patch, just
for easier to rebasing. I plan to rename it back after this lands.

Fixes #136624
Fixes https://github.com/llvm/llvm-project/issues/43179
Fixes https://github.com/llvm/llvm-project/issues/68670
Fixes https://github.com/llvm/llvm-project/issues/92757
2025-08-09 05:06:53 -03:00
Nathan Ridge
e64289baa0
[clang][Sema] Unify getPrototypeLoc helpers in SemaCodeComplete and clangd (#143345)
HeuristicResolver houses the unified implementation.

Fixes https://github.com/llvm/llvm-project/issues/143240
2025-07-05 10:59:16 -04:00
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
Nathan Ridge
715ad67c80
[clang][CodeComplete] Use HeuristicResolver in getAsRecordDecl() (#130473)
Fixes https://github.com/llvm/llvm-project/issues/130468
2025-04-11 02:02:30 -04:00
Matheus Izvekov
dc17429ae6
[clang] improved preservation of template keyword (#133610) 2025-04-01 17:15:18 -03:00
Nathan Ridge
44573bc152
[clang][HeuristicResolver] Default argument heuristic for template parameters (#132465)
Fixes https://github.com/clangd/clangd/discussions/1056
2025-03-21 19:15:18 -04:00
Nathan Ridge
7a4cb9bac5
[clang-tidy][NFC] Expose HeuristicResolver::lookupDependentName() and use it in StandaloneEmptyCheck (#128391)
The use replaces CXXRecordDecl::lookupDependentName() which
HeuristicResolver aims to supersede.
2025-02-24 11:31:46 -05:00
Nathan Ridge
c7eb5204a6
[clang][HeuristicResolver] Track the expression whose type is being simplified after each step in simplifyType() (#126689)
Fixes https://github.com/llvm/llvm-project/issues/126536
2025-02-11 17:25:15 -05:00
Nathan Ridge
d78b5ceb16
[clang][HeuristicResolver] Additional hardening against an infinite loop in simplifyType() (#126690) 2025-02-11 09:17:41 +01:00
Nathan Ridge
2941fa3941
[clang][HeuristicResolver] Only perform qualifier check for instance methods (#125166)
Fixes https://github.com/llvm/llvm-project/issues/125164
2025-01-31 00:25:53 -05:00
Nathan Ridge
7fd84264ed
[clang][Sema] Handle pointer and reference type more robustly in HeuristicResolver::resolveMemberExpr() (#124451)
Fixes https://github.com/llvm/llvm-project/issues/124450
2025-01-30 01:54:30 -05:00
Nathan Ridge
a4d17c44f1
[clang][Sema] Generalize HeuristicResolver::resolveTypeToRecordDecl() to resolveTypeToTagDecl() (#124699) 2025-01-28 13:37:30 -05:00
Nathan Ridge
194c74e016
[clang][Sema] Null-check type in resolveMemberExpr() before checking for auto type (#124628)
Fixes https://github.com/clangd/clangd/issues/2301
2025-01-27 19:51:30 -05:00
Nathan Ridge
a01e1d4e04
[clang][Sema] Handle dependent qualifier in HeuristicResolver::resolveDeclRefExpr() (#124515) 2025-01-27 03:31:56 -05:00
Nathan Ridge
df9b31f1e0
[clang][Sema] Handle undeduced auto types in HeuristicResolver (#124236)
Fixes https://github.com/clangd/clangd/issues/897
2025-01-24 16:11:18 -05:00
Nathan Ridge
4740e09703
[clang][Sema] Respect qualification of methods in heuristic results (#123551)
Fixes https://github.com/llvm/llvm-project/issues/123549
2025-01-21 03:54:42 -05: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