1149 Commits

Author SHA1 Message Date
Shoaib Meenai
7945435f46
[clang] Add support for omitting only global destructors (#104899)
For mobile applications, it's common for global destructors to never be
called (because the applications have their own lifecycle independent of
the standard C runtime), but threads are created and destroyed as normal
and so thread-local destructors are still called. -fno-static-c++-destructors
omits unnecessary global destructors, which is useful for code size, but
it also omits thread-local destructors, which is unsuitable. Add a
ternary `-fc++-static-destructors={all,none,thread-local}` option
instead to allow omitting only global destructors.
2024-08-26 13:11:05 -07:00
Bill Wendling
94b8b11ac3
[Clang][NFC] Move FindCountedByField into FieldDecl (#104235)
FindCountedByField can be used in more places than CodeGen. Move it into
FieldDecl to avoid layering issues.
2024-08-15 05:27:19 +00:00
Chuanqi Xu
8410babc2b
[C++20] [Moduels] Correct the linkage of const variable in language linkage from module interfaces (#102574)
Close https://github.com/llvm/llvm-project/issues/99825

The root cause of the issue is that I didn't realize the things in
implicit global module (the language linkage in module interfaces)
should be considered in module purview.
2024-08-09 14:48:56 +08:00
Oleksandr T.
37ec6e5f12
[Clang] Strengthen checks for main to meet [basic.start.main]p3’s requirements (#101853)
Fixes #101512.
2024-08-08 01:15:35 +02:00
Chuanqi Xu
d384267ad0 [NFC] [Modules] Introduce 'DeclBase::isInNamedModule' interface
This patch introduces DeclBase::isInNamedModule API to ease the use
of modules slightly.
2024-07-12 13:35:56 +08:00
Chuanqi Xu
91d40ef6e3 Revert "[C++20] [Modules] [Itanium ABI] Generate the vtable in the module unit of dynamic classes (#75912)"
This reverts commit 18f3bcbb13ca83d33223b00761d8cddf463e9ffb, 15bb02650e26875c48889053d6a9697444583721 and
99873b35da7ecb905143c8a6b8deca4d4416f1a9.

See the post commit message in
https://github.com/llvm/llvm-project/pull/75912 to see the reasons.
2024-07-10 10:58:18 +08:00
Dmitriy Chestnykh
09a275e8a4
[clang] Use internal linkage for c23 constexpr vars. (#97846)
See C23 std 6.2.2p3.
Fixes #97830
2024-07-08 13:09:42 -04:00
Aaron Ballman
71ff749d6b Revert "[clang][AST] fix ast-print of extern <lang> with >=2 declarators"
This reverts commit 48f13d48a88c14acbaea7c3ee05018bb173fb360.

It broke some external bots:
https://green.lab.llvm.org/job/llvm.org/view/LLDB/job/as-lldb-cmake/6805/console
https://logs.chromium.org/logs/fuchsia/buildbucket/cr-buildbucket/8743609724828014497/+/u/clang/build/stdout
2024-07-01 14:19:37 -04:00
temyurchenko
48f13d48a8
[clang][AST] fix ast-print of extern <lang> with >=2 declarators
Fixes #93913
2024-07-01 09:25:27 -04:00
Chuanqi Xu
76864e6af1 [C++20] [Modules] Don't find module for linkage for decls in global
module

Possibly fix https://github.com/llvm/llvm-project/issues/96693

The direct reason is that we are calculating the linkage for the
declaration too early so that the linkage got calculated incorrectly.

And after I look into the problem, I found it is completely not
necessary to calculate the linkage there. It is for ModulesTS. So I
simply removes that legacy experimental code and fix the issue.
2024-06-28 16:12:50 +08:00
Akira Hatanaka
46c8f25b0a
Check whether EvaluatedStmt::Value is valid in VarDecl::hasInit (#94515)
VarDecl::isNull() doesn't tell whether the VarDecl has an initializer as
methods like ensureEvaluatedStmt can create an EvaluatedStmt even when
there isn't an initializer.

Revert e1c3e16d24b5cc097ff08e9283f53319acd3f245 as the change isn't
needed anymore with this change.

See the discussion in https://github.com/llvm/llvm-project/pull/93749.
2024-06-14 11:22:09 -07:00
Chuanqi Xu
99873b35da [NFC] [AST] Introduce Decl::isInAnotherModuleUnit and Decl::shouldEmitInExternalSource
Motivated by the review process in
https://github.com/llvm/llvm-project/pull/75912. This can also help to
simplify the code slightly.
2024-06-04 17:08:21 +08:00
Akira Hatanaka
e1c3e16d24
[clang] Fix a crash when a variable is captured by a block nested inside a lambda (#93749)
`Eval->Value.get` returns a null pointer when the variable doesn't have
an initializer. Use `cast_if_present` instead of `cast`.

This fixes https://github.com/llvm/llvm-project/issues/93625.

rdar://128482541
2024-05-30 16:52:37 -07:00
Vlad Serebrennikov
6677aef285
[clang][NFC] Remove const-qualification from FunctionTemplateSpecializationInfo::TemplateArguments (#92500)
This patch remove const-qualification from pointee type of
`FunctionTemplateSpecializationInfo::TemplateArguments`, because it's
(eventually) used to initialize `MultiLevelTemplateArgumentList`, which
can actually mutate the arguments via

f42f57b52d/clang/include/clang/Sema/Template.h (L197-L204)
Mutation seems to be required to correctly handle packs:

9144553207/clang/lib/Sema/SemaTemplateInstantiate.cpp (L1440-L1469)
2024-05-17 17:49:50 +04:00
Sander de Smalen
6a6fcbffbb [Clang][AArch64] NFC: Add IsArmStreamingFunction.
Simple refactoring to make a single interface that checks if a
FunctionDecl is a __arm[_locally]_streaming function.
2024-05-07 15:33:24 +00:00
Chuanqi Xu
d86cc73bbf [NFC] [Serialization] Avoid using DeclID directly as much as possible
This patch tries to remove all the direct use of DeclID except the real
low level reading and writing. All the use of DeclID is converted to
the use of LocalDeclID or GlobalDeclID. This is helpful to increase the
readability and type safety.
2024-04-25 14:59:09 +08:00
Chuanqi Xu
72b58146b1 Revert "[NFC] [Serialization] Avoid using DeclID directly as much as possible"
This reverts commit 42070a5c092ed420bf92ebf38229c594885e94c7.

I forgot to touch lldb.
2024-04-25 14:26:07 +08:00
Chuanqi Xu
42070a5c09 [NFC] [Serialization] Avoid using DeclID directly as much as possible
This patch tries to remove all the direct use of DeclID except the real
low level reading and writing. All the use of DeclID is converted to
the use of LocalDeclID or GlobalDeclID. This is helpful to increase the
readability and type safety.
2024-04-25 14:14:05 +08:00
Chuanqi Xu
c2a98fdeb3
[NFC] Move DeclID from serialization/ASTBitCodes.h to AST/DeclID.h (#89873)
Previously, the DeclID is defined in serialization/ASTBitCodes.h under
clang::serialization namespace. However, actually the DeclID is not
purely used in serialization part. The DeclID is already widely used in
AST and all around the clang project via classes like `LazyPtrDecl` or
calling `ExternalASTSource::getExernalDecl()`. All such uses are via the
raw underlying type of `DeclID` as `uint32_t`. This is not pretty good.

This patch moves the DeclID class family to a new header `AST/DeclID.h`
so that the whole project can use the wrapped class `DeclID`,
`GlobalDeclID` and `LocalDeclID` instead of the raw underlying type.
This can improve the readability and the type safety.
2024-04-25 13:53:22 +08:00
Chuanqi Xu
aac695da42 [NFC] [Serialization] Use semantical type 'DeclID' for 'CreateDeserialized'
Previously we use 'unsigned' as the type of ID in 'CreateDeserialized'.

And the type of `DeclID` in serialization is 'uint32_t', so there is
minor inconsistency.

Also more importantly, if we want to extend the type of DeclID from
uint32_t to uint64_t, we may be in trouble due to we forgot updating the
a lot of 'CreateDeserialized'.

So this patch tries to use semantical type 'DeclID' for
'*Decl::CreateDeserialized' to make sure it is tightly consistent.
2024-04-19 16:24:47 +08:00
Timm Baeder
3d56ea05b6
[clang][NFC] Fix FieldDecl::isUnnamedBitfield() capitalization (#89048)
We always capitalize bitfield as "BitField".
2024-04-18 07:39:29 +02:00
Chuanqi Xu
f2695a1c2f [C++20] [Modules] Avoid writing untouched DeclUpdates from GMF in
Reduced BMI

Mitigate https://github.com/llvm/llvm-project/issues/61447

The root cause of the above problem is that when we write a declaration,
we need to lookup all the redeclarations in the imported modules. Then
it will be pretty slow if there are too many redeclarations in different
modules. This patch doesn't solve the porblem.

What the patchs mitigated is, when we writing a named module, we shouldn't
write the declarations from GMF if it is unreferenced **in current
module unit**. The difference here is that, if the declaration is used
in the imported modules, we used to emit it as an update. But we
definitely want to avoid that after this patch.

For that reproducer in
https://github.com/llvm/llvm-project/issues/61447, it used to take 2.5s
to compile and now it only takes 0.49s to compile, which is a big win.
2024-04-18 11:00:28 +08:00
Chuanqi Xu
d26dd58ca5 [StmtProfile] Don't profile the body of lambda expressions
Close https://github.com/llvm/llvm-project/issues/87609

We tried to profile the body of the lambda expressions in
https://reviews.llvm.org/D153957. But as the original comments show,
it is indeed dangerous. After we tried to skip calculating the ODR
hash values recently, we have fall into this trap twice.

So in this patch, I choose to not profile the body of the lambda
expression. The signature of the lambda is still profiled.
2024-04-16 15:41:26 +08:00
Sirraide
ef164cee90
[Clang] [C++26] Implement P2573R2: = delete("should have a reason"); (#86526)
This implements support for the `= delete("message")` syntax that was
only just added to C++26
([P2573R2](https://isocpp.org/files/papers/P2573R2.html#proposal-scope)).
2024-04-14 12:30:01 +02:00
Bill Wendling
fca51911d4
[NFC][Clang] Improve const correctness for IdentifierInfo (#79365)
The IdentifierInfo isn't typically modified. Use 'const' wherever
possible.
2024-04-11 00:33:40 +00:00
Chris B
28ddbd4a86
[NFC] Refactor ConstantArrayType size storage (#85716)
In PR #79382, I need to add a new type that derives from
ConstantArrayType. This means that ConstantArrayType can no longer use
`llvm::TrailingObjects` to store the trailing optional Expr*.

This change refactors ConstantArrayType to store a 60-bit integer and
4-bits for the integer size in bytes. This replaces the APInt field
previously in the type but preserves enough information to recreate it
where needed.

To reduce the number of places where the APInt is re-constructed I've
also added some helper methods to the ConstantArrayType to allow some
common use cases that operate on either the stored small integer or the
APInt as appropriate.

Resolves #85124.
2024-03-26 14:15:56 -05:00
Sirraide
2cf2bc472d
[Clang] [CodeGen] Fix codegen bug in constant initialisation in C23 mode (#84981)
Consider the following code:
```c
bool const inf =  (1.0/0.0);
``` 

When trying to emit the initialiser of this variable in C23, we end up
hitting a code path in codegen in `VarDecl::evaluateValueImpl()` where
we check for `IsConstantInitialization && (Ctx.getLangOpts().CPlusPlus
|| Ctx.getLangOpts().C23)`, and if that is the case and we emitted any
notes, constant evaluation fails, and as a result, codegen issues this
error:
```
<source>:1:12: error: cannot compile this static initializer yet
    1 | bool const inf =  (1.0/0.0);
      |        
```

As a fix, only fail in C23 mode if we’re initialising a `constexpr`
variable.

This fixes #84784.
2024-03-13 14:59:55 +01:00
Chuanqi Xu
3f6bc1adf8 [C++20] [Moduls] Avoid computing odr hash for functions from comparing constraint expression
Previously we disabled to compute ODR hash for declarations from the
global module fragment. However, we missed the case that the functions
lives in the concept requiments (see the attached the test files for
example). And the mismatch causes the potential crashment.

Due to we will set the function body as lazy after we deserialize it and
we will only take its body when needed. However, we don't allow to take
the body during deserializing. So it is actually potentially problematic
if we set the body as lazy first and computing the hash value of the
function, which requires to deserialize its body. So we will meet a
crash here.

This patch tries to solve the issue by not taking the body of the
function from GMF. Note that we can't skip comparing the constraint
expression from the GMF directly since it is an key part of the
function selecting and it may be the reason why we can't return 0
directly for `FunctionDecl::getODRHash()` from the GMF.
2024-03-11 11:39:21 +08:00
Stefan Gränitz
4b70d17bcf
[clang-repl] Names declared in if conditions and for-init statements are local to the inner context (#84150)
Make TopLevelStmtDecl a DeclContext so that variables defined in statements
are attached to the TopLevelDeclContext. This fixes redefinition errors
from variables declared in if conditions and for-init statements. These
must be local to the inner context (C++ 3.3.2p4), but they had generated
definitions on global scope instead.

This PR makes the TopLevelStmtDecl looking more like a FunctionDecl and
that's fine because the FunctionDecl is very close in terms of semantics.

Additionally, ActOnForStmt() requires a CompoundScope when processing a
NullStmt body.

---------

Co-authored-by: Vassil Vassilev <v.g.vassilev@gmail.com>
2024-03-07 14:27:04 +01:00
Mariya Podchishchaeva
aced81c0a5
[C23] Implement N3018: The constexpr specifier for object definitions (#73099)
The implementation mostly reuses C++ code paths where possible,
including narrowing check in order to provide diagnostic messages in
case initializer for constexpr variable is not exactly representable in
target type.

The following won't work due to lack of support for other features:
- Diagnosing of underspecified declarations involving constexpr
- Constexpr attached to compound literals

Also due to lack of support for char8_t some of examples with utf-8
strings don't work properly.

Fixes https://github.com/llvm/llvm-project/issues/64742
2024-03-06 09:46:35 +01:00
Sirraide
e4882d83d7
[Clang] [Sema] Do not crash on dependent or invalid record decls when -fdump-record-layouts-complete is passed (#83688)
We no longer try to compute and dump the layout of types in cases where that isn’t possible.

This fixes #83684 and #83671.
2024-03-04 14:04:35 +01:00
Fangrui Song
1d0f86ba80
[Sema] Warn unused functions for FMV based on the target attribute (#81302)
The spurious -Wunused-function warning issue for `target_version` #80227
also applied to `__attribute__((target(...)))` based FMV. #81167 removed
warnings for all `target`-based FMV. This patch restores the warnings
for `__attribute__((target("default")))`.
2024-02-09 13:39:08 -08:00
Jon Roelofs
2095655f8e
[clang][sema] Fix -Wunused-function on target_version'd file-scope Fn's (#81167)
We should only warn if the default version is the one that is unused.

Fixes: https://github.com/llvm/llvm-project/issues/80227
2024-02-09 08:14:09 -08:00
Jon Roelofs
99d743320c
[clang][fmv] Drop .ifunc from target_version's entrypoint's mangling (#81194)
Fixes: https://github.com/llvm/llvm-project/issues/81043
2024-02-09 08:13:15 -08:00
Andrey Ali Khan Bolshakov
5518a9d767
[c++20] P1907R1: Support for generalized non-type template arguments of scalar type. (#78041)
Previously committed as 9e08e51a20d0d2b1c5724bb17e969d036fced4cd, and
reverted because a dependency commit was reverted, then committed again
as 4b574008aef5a7235c1f894ab065fe300d26e786 and reverted again because
"dependency commit" 5a391d38ac6c561ba908334d427f26124ed9132e was
reverted. But it doesn't seem that 5a391d38ac6c was a real dependency
for this.

This commit incorporates 4b574008aef5a7235c1f894ab065fe300d26e786 and
18e093faf726d15f210ab4917142beec51848258 by Richard Smith (@zygoloid),
with some minor fixes, most notably:

- `UncommonValue` renamed to `StructuralValue`

- `VK_PRValue` instead of `VK_RValue` as default kind in lvalue and
member pointer handling branch in
`BuildExpressionFromNonTypeTemplateArgumentValue`;

- handling of `StructuralValue` in `IsTypeDeclaredInsideVisitor`;

- filling in `SugaredConverted` along with `CanonicalConverted`
parameter in `Sema::CheckTemplateArgument`;

- minor cleanup in
`TemplateInstantiator::transformNonTypeTemplateParmRef`;

- `TemplateArgument` constructors refactored;

- `ODRHash` calculation for `UncommonValue`;

- USR generation for `UncommonValue`;

- more correct MS compatibility mangling algorithm (tested on MSVC ver.
19.35; toolset ver. 143);

- IR emitting fixed on using a subobject as a template argument when the
corresponding template parameter is used in an lvalue context;

- `noundef` attribute and opaque pointers in `template-arguments` test;

- analysis for C++17 mode is turned off for templates in
`warn-bool-conversion` test; in C++17 and C++20 mode, array reference
used as a template argument of pointer type produces template argument
of UncommonValue type, and
`BuildExpressionFromNonTypeTemplateArgumentValue` makes
`OpaqueValueExpr` for it, and `DiagnoseAlwaysNonNullPointer` cannot see
through it; despite of "These cases should not warn" comment, I'm not
sure about correct behavior; I'd expect a suggestion to replace `if` by
`if constexpr`;

- `temp.arg.nontype/p1.cpp` and `dr18xx.cpp` tests fixed.
2024-01-21 21:28:57 +01:00
cor3ntin
e90e43fb9c
[Clang][NFC] Rename CXXMethodDecl::isPure -> is VirtualPure (#78463)
To avoid any possible confusion with the notion of pure function and the
gnu::pure attribute.
2024-01-18 15:30:58 +01:00
Craig Topper
142f270c27 Recommit "[AST] Use APIntStorage to fix memory leak in EnumConstantDecl. (#78311)"
With lldb build fix.

Original message:

EnumConstantDecl is allocated by the ASTContext allocator so the
destructor is never called.

This patch takes a similar approach to IntegerLiteral by using
APIntStorage to allocate large APSInts using the ASTContext allocator as
well.

The downside is that an additional heap allocation and copy of the data
needs to be made when calling getInitValue if the APSInt is large.

Fixes #78160.
2024-01-16 13:52:17 -08:00
Craig Topper
f3d534c425 Revert "[AST] Use APIntStorage to fix memory leak in EnumConstantDecl. (#78311)"
This reverts commit 4737959d91fab7673b1bb642f88658bb2a24d723.

Missed an lldb update.
2024-01-16 12:39:47 -08:00
Craig Topper
4737959d91
[AST] Use APIntStorage to fix memory leak in EnumConstantDecl. (#78311)
EnumConstantDecl is allocated by the ASTContext allocator so the
destructor is never called.

This patch takes a similar approach to IntegerLiteral by using
APIntStorage to allocate large APSInts using the ASTContext allocator as
well.

The downside is that an additional heap allocation and copy of the data
needs to be made when calling getInitValue if the APSInt is large.

Fixes #78160.
2024-01-16 12:10:38 -08:00
Nikita Popov
c6bb89f308
[clang] Fix assertion failure when initializing union with FAM (#77298)
When initializing a union that constrain a struct with a flexible array
member, and the initializer list is empty, we currently trigger an
assertion failure. This happens because getFlexibleArrayInitChars()
assumes that the initializer list is non-empty.

Fixes https://github.com/llvm/llvm-project/issues/77085.
2024-01-09 09:18:07 +01:00
Timm Bäder
d5c98e7837 [clang][AST][NFC] const-qualify some local references 2023-12-21 06:32:32 +01:00
Timm Bäder
c52a46a53b [clang][AST][NFC] Remove a local variable
The reference could be const, but this is even better.
2023-12-19 10:54:58 +01:00
Timm Bäder
17fa04e32f [clang][AST][NFC] Make declarationReplaces()'s first parameter const
And const qualify some local variables
2023-12-19 10:19:52 +01:00
Timm Bäder
b0ac829d71 [clang][AST][NFC] const-qualify a few locals in isPlaceholderVar 2023-12-19 09:46:06 +01:00
Krystian Stasiowski
29bd78b2f6
[Clang][Sema] Diagnose friend function specialization definitions (#72863)
Per [[class.friend]p6](http://eel.is/c++draft/class.friend#6) a friend
function shall not be defined if its name isn't unqualified. A
_template-id_ is not a name, meaning that a friend function
specialization does not meet this criteria and cannot be defined.

GCC, MSVC, and EDG all consider friend function specialization
definitions to be invalid de facto explicit specializations and diagnose
them as such.

Instantiating a dependent friend function specialization definition
[currently sets off an assert](https://godbolt.org/z/Krqdq95hY) in
`FunctionDecl::setFunctionTemplateSpecialization`. This happens because
we do not set the `TemplateSpecializationKind` of the `FunctionDecl`
created by template argument deduction to `TSK_ExplicitSpecialization`
for friend functions
[here](https://github.com/llvm/llvm-project/blob/main/clang/lib/Sema/SemaTemplate.cpp#L9600).
I changed the assert condition because I believe this is the correct
behavior.
2023-12-11 06:35:57 -08:00
Chuanqi Xu
0f7aaeb324 [C++20] [Modules] Allow export from language linkage
Close https://github.com/llvm/llvm-project/issues/71347

Previously I misread the concept of module purview. I thought if a
declaration attached to a unnamed module, it can't be part of the module
purview. But after the issue report, I recognized that module purview is
more of a concept about locations instead of semantics.

Concretely, the things in the language linkage after module declarations
can be exported.

This patch refactors `Module::isModulePurview()` and introduces some
possible code cleanups.
2023-11-09 17:44:41 +08: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
Simon Pilgrim
539e076e1c Fix MSVC "not all control paths return a value" warnings. NFC. 2023-11-03 09:50:09 +00:00
Vlad Serebrennikov
8775947633
[clang][NFC] Refactor clang::Linkage (#71049)
This patch introduces a new enumerator `Invalid = 0`, shifting other enumerators by +1. Contrary to how it might sound, this actually affirms status quo of how this enum is stored in `clang::Decl`:
```
  /// If 0, we have not computed the linkage of this declaration.
  /// Otherwise, it is the linkage + 1.
  mutable unsigned CacheValidAndLinkage : 3;
```
This patch makes debuggers to not be mistaken about enumerator stored in this bit-field. It also converts `clang::Linkage` to a scoped enum.
2023-11-02 20:57:29 +04:00
Vlad Serebrennikov
a8ead56068
[clang][NFC] Rename ArgPassingKind to RecordArgPassingKind (#70955)
During the recent refactoring (b120fe8d3288c4dca1b5427ca34839ce8833f71c) this enum was moved out of `RecordDecl`. During post-commit review it was found out that its association with `RecordDecl` should be expressed in the name.
2023-11-01 20:38:28 +04:00