315 Commits

Author SHA1 Message Date
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
James Y Knight
4205da0f13
NFC: Clean up of IntrusiveRefCntPtr construction from raw pointers. (#151782)
This commit handles the following types:
- clang::ExternalASTSource
- clang::TargetInfo
- clang::ASTContext
- clang::SourceManager
- clang::FileManager

Part of cleanup #151026
2025-08-01 22:23:30 -04:00
ganenkokb-yandex
a57aaedc35
Ast importer visitors (#138838)
I've rebased commit from
[Evianaive](https://github.com/Evianaive/llvm-project/commits?author=Evianaive)
and compiled it.
I hope it will speed up fix for #129393.

---------

Co-authored-by: Evianaive <153540933@qq.com>
2025-07-09 21:12:31 +03:00
Marco Elver
c7ccfc6dfc
Thread Safety Analysis: Support reentrant capabilities (#137133)
Introduce the `reentrant_capability` attribute, which may be specified
alongside the `capability(..)` attribute to denote that the defined
capability type is reentrant. Marking a capability as reentrant means
that acquiring the same capability multiple times is safe, and does not
produce warnings on attempted re-acquisition.

The most significant changes required are plumbing to propagate if the
attribute is present to a CapabilityExpr, and introducing
ReentrancyDepth to the LockableFactEntry class.
2025-05-26 17:03:55 +02:00
Timm Baeder
9ca8248a91
[clang] Save ShuffleVectorExpr args as ConstantExpr (#139709)
The passed indices have to be constant integers anyway, which we verify
before creating the ShuffleVectorExpr. Use the value we create there and
save the indices using a ConstantExpr instead. This way, we don't have
to evaluate the args every time we call getShuffleMaskIdx().
2025-05-14 16:13:01 +02:00
Balázs Kéri
4b30b3f901
[clang][ASTImporter] Fix AST import if anonymous namespaces are merged (#128735)
Fix of a faulty case that is shown in the second of the added
tests (an anonymous namespace is imported that resides in a `extern "C"`
block).
2025-05-06 10:17:26 +02:00
Aaron Puchert
9c73eba8aa
Merge similar Clang Thread Safety attributes (#135561)
Some of the old lock-based and new capability-based spellings behave
basically in the same way, so merging them simplifies the code
significantly.

There are two minor functional changes: we only warn (instead of an
error) when the try_acquire_capability attribute is used on something
else than a function. The alternative would have been to produce an
error for the old spelling, but we seem to only warn for all function
attributes, so this is arguably more consistent.

The second change is that we also check the first argument (which is the
value returned for a successful try-acquire) for `this`. But from what I
can tell, this code is defunct anyway at the moment (see #31414).
2025-04-15 23:21:34 +02:00
Matheus Izvekov
cfee056b4e
[clang] NFC: introduce UnsignedOrNone as a replacement for std::optional<unsigned> (#134142)
This introduces a new class 'UnsignedOrNone', which models a lite
version of `std::optional<unsigned>`, but has the same size as
'unsigned'.

This replaces most uses of `std::optional<unsigned>`, and similar
schemes utilizing 'int' and '-1' as sentinel.

Besides the smaller size advantage, this is simpler to serialize, as its
internal representation is a single unsigned int as well.
2025-04-03 14:27:18 -03:00
Haojian Wu
da2b415b19 [clang] Add tracking source deduction guide for the explicitly-written
deduction guide.

We miss this case in the original f94c481543bdd3b11a668ad78d46593cf974788f commit.
2025-02-02 23:06:25 +01:00
antangelo
f94c481543
[clang] Track source deduction guide for alias template deduction guides (#123875)
For deduction guides generated from alias template CTAD, store the
deduction guide they were originated from. The source kind is also
maintained for future expansion in CTAD from inherited constructors.

This tracking is required to determine whether an alias template already
has a deduction guide corresponding to some deduction guide on the
original template, in order to support deduction guides for the alias
from deduction guides declared after the initial usage.
2025-01-27 18:59:12 -05:00
Michael Buch
5c5bbffe75
[clang][ASTImporter] Import source location of explicit object parameter instead of copying it (#124305)
We used to copy the `SourceLocation` instead of importing it, which
isn't correct since the `SourceManager`'s of the source and target
ASTContext might differ.

Also adds test that confirms that we import the explicit object
parameter location for `ParmVarDecl`s. This is how Clang determines
whether a parameter `isExplicitObjectParamater`. The LLDB expression
evaluator relies on this for calling "explicit object member functions".
2025-01-27 14:57:09 +00:00
Balázs Kéri
7e01a322f8
[clang][ASTImporter] Fix unused variable warning (NFC) (#122686) 2025-01-13 11:35:38 +01:00
Balázs Kéri
b270525f73
[clang][ASTImporter] Not using primary context in lookup table (#118466)
`ASTImporterLookupTable` did use the `getPrimaryContext` function to get
the declaration context of the inserted items. This is problematic
because the primary context can change during import of AST items, most
likely if a definition of a previously not defined class is imported.
(For any record the primary context is the definition if there is one.)
The use of primary context is really not important, only for namespaces
because these can be re-opened and lookup in one namespace block is not
enough. This special search is now moved into ASTImporter instead of
relying on the lookup table.
2025-01-13 09:46:45 +01:00
Kazu Hirata
a3e62d849f [AST] Fix a warning
This patch fixes:

  clang/unittests/AST/ASTImporterTest.cpp:3397:9: error: unused
  variable 'ToTU' [-Werror,-Wunused-variable]
2025-01-11 00:11:42 -08:00
Timm Baeder
cfe26358e3
Reapply "[clang] Avoid re-evaluating field bitwidth" (#122289) 2025-01-11 07:12:37 +01:00
Timm Bäder
59bdea24b0 Revert "[clang] Avoid re-evaluating field bitwidth (#117732)"
This reverts commit 81fc3add1e627c23b7270fe2739cdacc09063e54.

This breaks some LLDB tests, e.g.
SymbolFile/DWARF/x86/no_unique_address-with-bitfields.cpp:

lldb: ../llvm-project/clang/lib/AST/Decl.cpp:4604: unsigned int clang::FieldDecl::getBitWidthValue() const: Assertion `isa<ConstantExpr>(getBitWidth())' failed.
2025-01-08 15:09:52 +01:00
Timm Baeder
81fc3add1e
[clang] Avoid re-evaluating field bitwidth (#117732)
Save the bitwidth value as a `ConstantExpr` with the value set. Remove
the `ASTContext` parameter from `getBitWidthValue()`, so the latter
simply returns the value from the `ConstantExpr` instead of
constant-evaluating the bitwidth expression every time it is called.
2025-01-08 14:45:19 +01:00
kefan cao
d85b22ed5d
[Clang][ASTMatcher] Add dependentTemplateSpecializationType matcher (#121435)
Fixes https://github.com/llvm/llvm-project/issues/121307
2025-01-03 16:32:02 -05:00
Amr Hesham
6230f1ba94
[Clang][ASTMatcher] Add dependentNameType AST matcher (#121263)
Fixes: https://github.com/llvm/llvm-project/issues/121240
2024-12-29 19:27:10 -05:00
Amr Hesham
48bf0a9457
[Clang][ASTMatcher] Add dependentScopeDeclRefExpr matcher (#120996)
Fixes https://github.com/llvm/llvm-project/issues/120937
2024-12-27 19:10:24 -05:00
Kazu Hirata
c35108e244
[AST] Migrate away from PointerUnion::get (NFC) (#119949)
Note that PointerUnion::get has been soft deprecated in
PointerUnion.h:

  // FIXME: Replace the uses of is(), get() and dyn_cast() with
  //        isa<T>, cast<T> and the llvm::dyn_cast<T>
2024-12-14 09:17:56 -08:00
Balázs Kéri
7be501c1d3
[clang][ASTImporter] Allow import of similar friend template with different depth (#115734)
This fix applies to a case that occurs when the AST contains a friend
template that is contained within another template and this (outer)
template has specialization. (See the added test code in the commit.)
2024-12-02 10:08:14 +01:00
Balázs Kéri
55cbbce095
[clang][ASTImporter] Fix of unchecked Error object (#112688)
After commits 9c72a30 and 30a9cac error handling in function
'importTemplateParameterDefaultArgument' was not correct,
probably related to (not) using std::move. A crash with unchecked
Error result could happen when the import error path was taken.

Here a test is added that reproduces this case and the problem is
fixed.
2024-10-18 12:53:47 +02:00
Balázs Kéri
9c72a308d8
[clang][ASTImporter] New fix for default template parameter values. (#101836)
Commit e4440b8 added a change that introduced new crash in an
incorrectly handled case. This is fixed here. Default argument
definition or inheritance is preserved in the "To" AST compared to
the "From". If the default argument is defined already in the "To"
AST it can be duplicated at the import.
2024-09-06 09:08:26 +02:00
Ding Fei
a58f20b5ca
[clang][ASTImporter] support import return with UnaryTransformType (#101517)
This fixes infinite recursion crash on return with UnaryTransformType,
whose underlying type is a SubstTemplateTypeParmType which is
associated with current imported function.
2024-08-07 11:36:08 +08:00
Ding Fei
5518b46c12
[clang][ASTImporter] Remove trailing return testing on lambda proto (#101031)
Lambdas without trailing return could also have return type defined
inside its body.

This fixes crashes (infinite recursion) on lambda expr without
parameters (no parentheses).
2024-08-07 11:35:22 +08:00
Balázs Kéri
518d8632ce
[clang][ASTImporter] Fix import of template parameter default values. (#100100)
Default values of template parameters (non-type, type, template) were
not correctly handled in the "inherited" case. This occurs if the first
declaration contains the default value but a next one not. The default
value is "inherited" from the first.

In ASTImporter it was only possible to set the inherited status after
the template object was created with the template parameters that were
imported without handling the inherited case. The import function of the
template parameter contains not enough information (previous
declaration) to set the inherited-from status. After the template was
created, default value of the parameters that should be inherited is
reset to inherited mode.
2024-07-30 10:25:45 +02:00
Michael Buch
31769e4d08
Revert "Reland [clang][ASTImport] Add support for import of empty records" (#100903)
This reverts commit 88e5206f2c96a34e23a4d63f0a38afb2db044f0a. The
original change went in a while ago (last year) in
https://reviews.llvm.org/D145057. The specific reason I'm proposing a
revert is that this is now causing exactly the issue that @balazske
predicted in https://reviews.llvm.org/D145057#4164717:
> Problematic case is if the attribute has pointer to a Decl or Type
that is imported here in a state when the field is already created but
not initialized. Another problem is that attributes are added a second
time in Import(Decl *)

This now came up in the testing of LLDB support for
https://github.com/llvm/llvm-project/issues/93069. There,
`__compressed_pair`s are now replaced with fields that have an
`alignof(...)` and `[[no_unique_address]]` attribute. In the specific
failing case, we're importing following `std::list` method:
```
size_type& __sz() _NOEXCEPT { return __size_; }
```
During this process, we create a new `__size_` `FieldDecl` (but don't
initialize it yet). Then we go down the `ImportAttrs` codepath added in
D145057. This imports the `alignof` expression which then references the
uninitialized `__size_` and we trigger an assertion.

Important to note, this codepath was added specifically to support
`[[no_unique_address]]` in LLDB, and was supposed to land with
https://reviews.llvm.org/D143347. But the LLDB side of that never
landed, and the way we plan to support `[[no_unique_address]]` doesn't
require things like the `markEmpty` method added here. So really, this
is a dead codepath, which as pointed out in the original review isn't
fully sound.
2024-07-29 08:47:57 +01:00
Ding Fei
978c40b448
[clang][ASTImporter][NFC] add unittests for unnamed EnumDecl (#100545)
These tests are for multiple anonymous EnumDecls structural eq test &
importing.

We found the anonymous enums importing issue a few days ago and tried to
fix it
but 0a6233a68c7b575d05bca0f0c708b7e97cc710d1 already did this. I think
these tests are still useful for regressions.
2024-07-26 09:34:56 +08:00
Balázs Kéri
0a6233a68c
[clang][ASTImporter] Fix import of anonymous enums if multiple are present (#99281)
After changes in PR #87144 and #93923 regressions appeared in some
cases. The problem was that if multiple anonymous enums are present in a
class and are imported as new the import of the second enum can fail
because it is detected as different from the first and causes ODR error.

Now in case of enums without name an existing similar enum is searched,
if not found the enum is imported. ODR error is not detected. This may
be incorrect if non-matching structures are imported, but this is the
less important case (import of matching classes is more important to
work).
2024-07-23 08:43:05 +02:00
Dan McArdle
d698760687
[clang][ThreadSafety] Revert stricter typing on trylock attributes (#97293)
This PR reverts #95290 and the one-liner followup PR #96494.

I received some substantial feedback on #95290, which I plan to address
in a future PR.

I've also received feedback that because the change emits errors where
they were not emitted before, we should at least have a flag to disable
the stricter warnings.
2024-07-01 09:47:09 -04:00
Dan McArdle
c1bde0a2cb
[clang][ThreadSafety] Check trylock function success and return types (#95290)
With this change, Clang will generate errors when trylock functions have
improper return types. Today, it silently fails to apply the trylock
attribute to these functions which may incorrectly lead users to believe
they have correctly acquired locks before accessing guarded data.

As a side effect of explicitly checking the success argument type, I
seem to have fixed a false negative in the analysis that could occur
when a trylock's success argument is an enumerator. I've added a
regression test to warn-thread-safety-analysis.cpp named
`TrylockSuccessEnumFalseNegative`.

This change also improves the documentation with descriptions of of the
subtle gotchas that arise from the analysis interpreting the success arg
as a boolean.

Issue #92408
2024-06-24 09:29:13 -04:00
Qizhi Hu
3b020d51f1
[clang][ASTImport] fix issue on anonymous enum import (#93923)
Don't skip searching in `ToContext` during importing `EnumDecl`. And
`IsStructuralMatch` in `StructralEquivalence` can make sure to determine
whether the found result is match or not.

---------

Co-authored-by: huqizhi <836744285@qq.com>
2024-06-04 20:06:15 +08:00
Matheus Izvekov
2bde13cda1
[clang] NFCI: use TemplateArgumentLoc for NTTP DefaultArgument (#92852)
This is an enabler for https://github.com/llvm/llvm-project/pull/92855

This allows an NTTP default argument to be set as an arbitrary
TemplateArgument, not just an expression.
This allows template parameter packs to have default arguments in the
AST, even though the language proper doesn't support the syntax for it.

This allows NTTP default arguments to be other kinds of arguments, like
packs, integral constants, and such.
2024-05-22 12:18:44 -03:00
Matheus Izvekov
e42b799bb2
[clang] NFCI: use TemplateArgumentLoc for type-param DefaultArgument (#92854)
This is an enabler for a future patch.

This allows an type-parameter default argument to be set as an arbitrary
TemplateArgument, not just a type.
This allows template parameter packs to have default arguments in the
AST, even though the language proper doesn't support the syntax for it.

This will be used in a later patch which synthesizes template parameter
lists with arbitrary default arguments taken from template
specializations.

There are a few places we used SubsType, because we only had a type, now
we use SubstTemplateArgument.
SubstTemplateArgument was missing arguments for setting Instantiation
location and entity names.
Adding those is needed so we don't regress in diagnostics.
2024-05-21 20:27:50 -03:00
Ding Fei
0ccdd4c28f
[ASTImporter] Fix infinite recurse on return type declared inside body (#68775)
Lambda without trailing auto could have return type declared inside the
body too.

Fixes #68775
2024-04-20 10:11:02 +08:00
Qizhi Hu
ab80d00438
[clang][ASTImporter] fix variable inline of CXX17 (#87314)
Fix crash in the testcase from
https://github.com/llvm/llvm-project/issues/75114#issuecomment-1872595956
Forget to set inline of variable declaration would make
`isThisDeclarationADefinition` get incorrect result and didn't get
imported variable. This will lead to a new `VarTemplateDecl` being
created and call `setDescribedVarTemplate` again which produces the
crash.

Co-authored-by: huqizhi <836744285@qq.com>
2024-04-05 08:41:06 +08:00
Balázs Kéri
9f80ecb308
[clang][ASTImporter] Improve import of variable template specializations. (#78284)
Code of `VisitVarTemplateSpecializationDecl` was rewritten based on code
of `VisitVarDecl`. Additional changes (in structural equivalence) were
made to make tests pass.
2024-01-29 10:04:17 +01:00
Julian Schmidt
cbaadb1f0f
[clang][ASTMatcher] Add matchers for CXXFoldExpr (#71245)
Adds support for the following matchers related to `CXXFoldExpr`:
`cxxFoldExpr`, `callee`,
  `hasInit`, `hasPattern`, `isRightFold`, `isLeftFold`,
  `isUnaryFold`, `isBinaryFold`, `hasOperator`, `hasLHS`, `hasRHS`.
2024-01-16 17:13:38 +01:00
Balázs Kéri
5e355942cd
[clang][ASTImporter] Fix import of variable template redeclarations. (#72841)
In some cases variable templates (specially if static member of record)
were not correctly imported and an assertion "Missing call to
MapImported?" could happen.
2024-01-15 12:38:02 +01:00
Balázs Kéri
4b0314d14f
[clang][ASTImporter] Improve import of friend class templates. (#74627)
A friend template that is in a dependent context is not linked into
declaration chains (for example with the definition of the befriended
template). This condition was not correctly handled by `ASTImporter`.
2024-01-11 11:18:11 +01:00
Qizhi Hu
4de971c230
[clang][ASTImporter] import InstantiatedFromMember of ClassTemplateSpecializationDecl (#76493)
import of `ClassTemplateSpecializationDecl` didn't set
`InstantiatedFromMember` and this makes ast-dump crash. import and set
`InstantiatedFromMember`. fix
[issue](https://github.com/llvm/llvm-project/issues/76469)

Co-authored-by: huqizhi <836744285@qq.com>
2024-01-04 21:21:53 +08:00
Qizhi Hu
9b99a307b2
[clang][ASTImporter] skip TemplateTypeParmDecl in VisitTypeAliasTemplateDecl (#74919)
Skip checking `TemplateTypeParmDecl ` in `VisitTypeAliasTemplateDecl`.
[Fix this crash](https://github.com/llvm/llvm-project/issues/74765)

Co-authored-by: huqizhi <836744285@qq.com>
2023-12-25 10:13:35 +08:00
Qizhi Hu
625197d39c
[clang][ASTImporter] Support Importer of BuiltinBitCastExpr (#74813)
Since import `ExplicitCastExpr` lacks of processing
`BuiltinBitCastExprClass` type, it would reach to the 'unreachable' code
and produce the crash. This patch aims to fix the
[crash](https://github.com/llvm/llvm-project/issues/74774) and try to
handle `BuiltinBitCastExpr`.

Co-authored-by: huqizhi <836744285@qq.com>
2023-12-22 18:06:59 +08:00
Balázs Kéri
0d903b689a
[clang][ASTImporter] Import AlignValueAttr correctly. (#75308)
Expression of attribute `align_value` was not imported. Import of the
attribute is corrected, a test for it is added, other related tests with
FIXME are updated.
Fixes #75054.
2023-12-22 10:07:38 +01:00
Qizhi Hu
72e8ab7d44
[clang][ASTImporter] add processing of SubstNonTypeTemplateParmExpr in isAncestorDeclContextOf (#74991)
Lack of processing of `SubstNonTypeTemplateParmExpr` in
`isAncestorDeclContextOf` would make `hasAutoReturnTypeDeclaredInside`
returns false and lead to infinite recursion. This patch adds the
processor and try to fix [this
issue](https://github.com/llvm/llvm-project/issues/74839)

Co-authored-by: huqizhi <836744285@qq.com>
2023-12-21 15:05:33 +08:00
Qizhi Hu
3287ae8f65
[clang][ASTImporter] IdentifierInfo of Attribute should be set using 'ToASTContext' (#73290)
Co-authored-by: huqizhi <836744285@qq.com>
2023-11-29 09:33:37 +08:00
Balázs Kéri
3d8a9105d7
[clang][ASTImporter] Fix import of SubstTemplateTypeParmType in return type of function. (#69724)
Import of a function with `auto` return type that is expanded to a
`SubstTemplateTypeParmType` could fail if the function itself is the
template specialization where the parameter was replaced.
2023-11-24 14:35:10 +01:00
Vlad Serebrennikov
edd690b02e
[clang][NFC] Refactor TagTypeKind (#71160)
This patch converts TagTypeKind into scoped enum. Among other benefits,
this allows us to forward-declare it where necessary.
2023-11-03 21:45:39 +04:00
Exile
39dfaf0d0d
[clang][ASTImporter] Fix crash when template class static member imported to other translation unit. (#68774)
Fixes: #68769

Co-authored-by: miaozhiyuan <miaozhiyuan@feysh.com>
2023-11-01 18:29:37 +08:00