3996 Commits

Author SHA1 Message Date
schittir
fdfcebb38d
[clang][SYCL] Add sycl_external attribute and restrict emitting device code (#140282)
This patch is part of the upstreaming effort for supporting SYCL
language front end.
It makes the following changes:
1. Adds sycl_external attribute for functions with external linkage,
which is intended for use to implement the SYCL_EXTERNAL macro as
specified by the SYCL 2020 specification
2. Adds checks to avoid emitting device code when sycl_external and
sycl_kernel_entry_point attributes are not enabled
3. Fixes test failures caused by the above changes

This patch is missing diagnostics for the following diagnostics listed
in the SYCL 2020 specification's section 5.10.1, which will be addressed
in a subsequent PR:
Functions that are declared using SYCL_EXTERNAL have the following
additional restrictions beyond those imposed on other device functions:
1. If the SYCL backend does not support the generic address space then
the function cannot use raw pointers as parameter or return types.
Explicit pointer classes must be used instead;
2. The function cannot call group::parallel_for_work_item;
3. The function cannot be called from a parallel_for_work_group scope.

In addition to that, the subsequent PR will also implement diagnostics
for inline functions including virtual functions defined as inline.

---------

Co-authored-by: Mariya Podchishchaeva <mariya.podchishchaeva@intel.com>
2025-08-20 12:37:37 -04:00
Younan Zhang
5f9630b388
[Clang] Reapply "Only remove lambda scope after computing evaluation context" (#154458)
The immediate evaluation context needs the lambda scope info to
propagate some flags, however that LSI was removed in
ActOnFinishFunctionBody which happened before rebuilding a lambda
expression.

The last attempt destroyed LSI at the end of the block scope, after
which we still need it in DiagnoseShadowingLambdaDecls.

This also converts the wrapper function to default arguments as a
drive-by fix, as well as does some cleanup.

Fixes https://github.com/llvm/llvm-project/issues/145776
2025-08-20 17:30:33 +08:00
Chuanqi Xu
2db239acd1 [C++20] [Modules] Improve the import-and-include pattern
The import and include problem is a long-standing issue with the use of
C++20 modules. This patch tried to improve this by skipping parsing
class and functions if their declaration is already defined in modules.

The scale of the patch itself is small as the patch reuses previous
optimization. Maybe we can skip parsing other declarations in the
future. But the patch itself should be good.
2025-08-20 11:47:00 +08:00
Oleksandr T.
29fa5b72e9
Revert "[Clang] improve -Wstring-concatenation to warn on every missing comma in initializer lists" (#154369)
Revert llvm/llvm-project#154018 changes due to excessive _false
positives_. The warning caused multiple benign reports in large
codebases (e.g. _Linux kernel_, _Fuchsia_, _tcpdump_). Since many of
these concatenations are intentional and follow project style rules, the
diagnostic introduced more false positives than value. This will be
revisited as a potential `clang-tidy` check instead.
2025-08-19 20:30:16 +03:00
Younan Zhang
0732693d81
Revert "[Clang] Only remove lambda scope after computing evaluation context" (#154382)
Revert due to breakage as reported in
https://github.com/llvm/llvm-project/pull/154106#discussion_r2285824084

Reverts llvm/llvm-project#154106
2025-08-20 00:56:56 +08:00
Younan Zhang
2b32ad1316
[Clang] Only remove lambda scope after computing evaluation context (#154106)
The immediate evaluation context needs the lambda scope info to
propagate some flags, however that LSI was removed in
ActOnFinishFunctionBody which happened before rebuilding a lambda
expression.

This also converts the wrapper function to default arguments as a
drive-by fix.

Fixes https://github.com/llvm/llvm-project/issues/145776
2025-08-19 21:59:23 +08:00
Oleksandr T.
c2eb895c20
[Clang] improve -Wstring-concatenation to warn on every missing comma in initializer lists (#154018)
Fixes #153745

---

This PR addresses a limitation in `-Wstring-concatenation`, where only
the first missing comma in an initializer list was diagnosed.
2025-08-19 00:10:53 +03:00
Shafik Yaghmour
f8740920ee
[Clang][Sema] Check the return value of DiagnoseClassNameShadow in ActOnEnumConstant (#143754)
Static analysis flagged that we were not checking the return value of
DiagnoseClassNameShadow when we did so everywhere else. Modifying this
case to match how other places uses it makes sense and does not change
behavior. Likely if this check fails later actions will fail as well but
it is more correct to exit early.
2025-08-16 14:08:39 -07:00
keinflue
af96ed6bf6
[clang] Inject IndirectFieldDecl even if name conflicts. (#153140)
This modifies InjectAnonymousStructOrUnionMembers to inject an
IndirectFieldDecl and mark it invalid even if its name conflicts with
another name in the scope.

This resolves a crash on a further diagnostic
diag::err_multiple_mem_union_initialization which via
findDefaultInitializer relies on these declarations being present.

Fixes #149985
2025-08-15 09:43:29 -07:00
Chuanqi Xu
ab5a5a90c0 [C++20] [Modules] Fix incorrect diagnostic for using befriend target
Close https://github.com/llvm/llvm-project/issues/138558

The compiler failed to understand the redeclaration-relationship when
performing checks when MergeFunctionDecl. This seemed to be a complex
circular problem (how can we know the redeclaration relationship before
performing merging?). But the fix seems to be easy and safe. It is fine
to only perform the check only if the using decl is a local decl.
2025-08-14 14:23:14 +08: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
Samarth Narang
ba10c1d46a
[clang][NFC] Code Cleanup of inferred noreturn attributes in explicit specializations. (#151815)
Addressing
https://github.com/llvm/llvm-project/pull/150003#discussion_r2249169463
2025-08-04 10:23:21 -04:00
Michael Buch
cd8f348c69
[clang][Attr] Remove 'literal label' form of AsmLabelAttr (#151858)
This was added purely for the needs of LLDB. However, starting with
https://github.com/llvm/llvm-project/pull/151355 and
https://github.com/llvm/llvm-project/pull/148877 we no longer create
literal AsmLabels in LLDB either. So this is unused and we can get rid
of it.

In the near future LLDB will want to add special support for mangling
`AsmLabel`s (specifically
https://github.com/llvm/llvm-project/pull/149827).
2025-08-04 14:34:45 +01:00
Kazu Hirata
32bc2512a3
[Sema] Use llvm::iterator_range::empty (NFC) (#151852) 2025-08-03 08:45:05 -07:00
Timm Baeder
fc712aa9a6
[clang][NFC] Remove leftover comment (#151822)
From d8ca85a184c0fb511fe6483c4c24e48b5b1eb07a
2025-08-02 16:36:45 +02:00
Timm Baeder
d8ca85a184
[clang][ExprConst] Remove Loc param (#151461)
The Loc param to these functions was weird and not always set in error
cases. It wasn't reliable to use.

This was almost entirely unused inside of clang and the one call site
that used the returned source location doesn't make a difference in
practice.
2025-08-01 14:09:06 +02:00
Tom Honermann
9de4e062d7
[SYCL] Restrict the sycl_kernel_entry_point attribute spelling to C++11 style. (#151405)
Previously, the `sycl_kernel_entry_point` attribute could be specified
using either the GNU or C++11 spelling styles. Future SYCL attributes
are expected to support only the C++11 spelling style, so support for
the GNU style is being removed.

In order to ensure consistent presentation of the attribute in
diagnostic messages, diagnostics specific to this attribute now require
the attribute to be provided as an argument. This delegates formatting
of the attribute name to the diagnostic engine.

As an additional nicety, "the" is added to some diagnostic messages so
that they read more like proper sentences.
2025-07-31 19:25:05 -04:00
jeremyd2019
a3228b6bf9
[Clang][Cygwin] Enable few conditions that are shared with MinGW (#149637)
The Cygwin target is generally very similar to the MinGW target. The
default auto-import behavior, the default calling convention, the
`.dll.a` import library extension, the `__GXX_TYPEINFO_EQUALITY_INLINE`
pre-define by `g++`, and the long double configuration.

Co-authored-by: Mateusz Mikuła <oss@mateuszmikula.dev>
2025-07-29 10:01:43 -07:00
Samarth Narang
22fef00522
[clang] Avoid inheriting [[noreturn]] in explicit function template specializations (#150003)
This patch fixes incorrect behavior in Clang where [[noreturn]] (either
spelled or inferred) was being inherited by explicit specializations of
function templates or member function templates, even when those
specializations returned normally.

Follow up on https://github.com/llvm/llvm-project/pull/145166
2025-07-24 03:04:05 +02:00
Devajith
38a977d00c
[clang-repl] Always clean up scope and context for TopLevelStmtDecl (#150215)
This fixes an issue introduced by
https://github.com/llvm/llvm-project/pull/84150, where failing to pop
compound scope, function scope info, and decl context after a failed
statement could lead to an inconsistent internal state.
2025-07-23 17:03:07 +02:00
Doug Gregor
8c26858f1a
[API Notes] Attach API notes to forward declarations of tags (#149951)
Forward declarations can still have useful API notes applied to them.
When the use of the tag is not a definition, apply the API notes
immediately.

Fixes rdar://156288588.
2025-07-22 17:49:13 -07:00
Kazu Hirata
b1a25ce73c
[Sema] Remove unnecessary casts (NFC) (#148762)
getAsmLabel() already returns Expr *.
2025-07-14 22:18:37 -07:00
Aaron Ballman
a2246eebca
[C23] Accept an _Atomic underlying type (#147802)
The underlying type of an enumeration is the non-atomic, unqualified
version of the specified type. Clang was rejecting such enumerations,
with a hard error, but now has the ability to downgrade the error into a
warning. Additionally, we diagnose (as a warning) dropping other
qualifiers. _Atomic is special given that an atomic type need not have
the same size as its non-atomic counterpart, and that the C++ version
of <stdatomic.h> defines _Atomic to std::atomic for easing cross-
language atomic use and std::atomic is an invalid enum base in C++.
(Note: we expose _Atomic in C++ even without including
<stdatomic,h>.)

Fixes #147736
2025-07-11 07:28:03 -04:00
Kazu Hirata
430c0376c8
[Sema] Remove an unnecessary cast (NFC) (#147154)
BitWidth is already of Expr *.
2025-07-05 10:44:52 -07:00
Ashwin Kishin Banwari
cc801b6570
[clang] [modules] Add err_main_in_named_module (#146635)
Revival of https://github.com/llvm/llvm-project/pull/146247 which got
reverted for broken test.

Now that https://github.com/llvm/llvm-project/pull/146461 is merged to
allow `extern "C++"` for main, we can merge this change.
2025-07-03 10:03:43 +08:00
Ashwin Banwari
2599a9aeb5
[clang] [modules] Implement P3618R0: Allow attaching main to the global module (#146461)
Remove the prior warning for attaching extern "C++" to main.
2025-07-02 09:52:10 +08:00
Chuanqi Xu
8a5b97a720 Revert "[clang] [modules] Add err_main_in_named_module (#146247)"
This reverts commit 473769ec9b2f860813229eb449fb4298dfc7ff94.

It breaks test in libc++

See https://github.com/llvm/llvm-project/pull/146247
2025-06-30 23:22:31 +08:00
Devon Loehr
5ab3114bd1
Expand annotation check for -Wunique-object-duplication on Windows. (#145944)
Since dllexport/dllimport annotations don't propagate the same way as
visibility, the unique object duplication warning needs to check both
the object in question and its containing class. Previously, we
restricted this check to static data members, but it applies to all
objects inside a class, including functions. Not checking functions
leads to false positives, so remove that restriction.
2025-06-30 10:51:04 -04:00
Ashwin Banwari
473769ec9b
[clang] [modules] Add err_main_in_named_module (#146247)
Close https://github.com/llvm/llvm-project/issues/146229

As the issue said, main shouldn't be in any modules.

new diagnostic output:
```
/my/code/directory/main.cpp:3:1: warning: 'main' should not be attached to a named module; consider adding C++ language linkage [-Wmain]
    3 | int main() {
      | ^
      | extern "C++" 
1 warning generated.
```
2025-06-30 17:09:03 +08:00
Kazu Hirata
c9cdc33dd6
[clang] Remove unused includes (NFC) (#146254)
These are identified by misc-include-cleaner.  I've filtered out those
that break builds.  Also, I'm staying away from llvm-config.h,
config.h, and Compiler.h, which likely cause platform- or
compiler-specific build failures.
2025-06-28 20:41:46 -07:00
Haojian Wu
0b6ddb02ef
[clang] NFC: Add alias for std::pair<FileID, unsigned> used in SourceLocation (#145711)
Introduce a type alias for the commonly used `std::pair<FileID,
unsigned>` to improve code readability, and make it easier for future
updates (64-bit source locations).
2025-06-26 14:12:51 +02:00
Steven Perron
01d648a429
[HLSL][SPIRV] Reapply "[HLSL][SPIRV] Add vk::constant_id attribute." (#144902)
- **Reapply "[HLSL][SPIRV] Add vk::constant_id attribute." (#144812)**
- **Fix memory leak.**
2025-06-19 11:52:55 -04:00
Steven Perron
5f69d680e2
Revert "[HLSL][SPIRV] Add vk::constant_id attribute." (#144812)
Reverts llvm/llvm-project#143544
2025-06-18 19:30:43 -04:00
Steven Perron
acde20b560
[HLSL][SPIRV] Add vk::constant_id attribute. (#143544)
The vk::constant_id attribute is used to indicate that a global const
variable
represents a specialization constant in SPIR-V. This PR adds this
attribute to clang.

The documentation for the attribute is
[here](https://github.com/microsoft/DirectXShaderCompiler/blob/main/docs/SPIR-V.rst#specialization-constants).

The strategy is to to modify the initializer to get the value of a
specialize constant for a builtin defined in the SPIR-V backend.

Implements https://github.com/llvm/wg-hlsl/pull/287

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

---------

Co-authored-by: Nathan Gauër <github@keenuts.net>
2025-06-18 06:39:52 -04:00
Eli Friedman
f2d2c99866
[clang] Remove separate evaluation step for static class member init. (#142713)
We already evaluate the initializers for all global variables, as
required by the standard. Leverage that evaluation instead of trying to
separately validate static class members.

This has a few benefits:

- Improved diagnostics; we now get notes explaining what failed to
evaluate.
- Improved correctness: is_constant_evaluated is handled correctly.

The behavior follows the proposed resolution for CWG1721.

Fixes #88462. Fixes #99680.
2025-06-17 16:43:55 -07:00
Finn Plummer
8cd05b88ec
[NFC][HLSL] Move Sema work from ParseMicrosoftRootSignatureAttributeArgs (#143184)
This separates semantic analysis from parsing by moving `RootSignatureDecl` creation, scope storage, and lookup logic into
`SemaHLSL`.

For more context see:
https://github.com/llvm/llvm-project/issues/142834.

- Define `ActOnStartRootSignatureDecl` and `ActOnFinishRootSignatureDecl` on `SemaHLSL`
- NFC so no test changes.

Resolves: https://github.com/llvm/llvm-project/issues/142834

---------

Co-authored-by: Aaron Ballman <aaron@aaronballman.com>
2025-06-17 11:27:35 -07:00
Devon Loehr
9670e09d0e
Enable unique-object-duplication warning for windows (#143537)
Followup to #125526. This expands the logic of the
unique-object-duplication warning so that it also works for windows
code.

For the most part, the logic is unchanged, merely substituting "has no
import/export annotation" in place of "has hidden visibility". However,
there are some small inconsistencies between the two; namely, visibility
is propagated through nested classes, while import/export annotations
aren't.

This PR:
1. Updates the logic for the warning to account for the differences
between posix and windows
2. Changes the warning message and documentation appropriately
3. Updates the tests to cover windows, and adds new test cases for the
places where behavior differs.

This PR was tested by building chromium (cross compiling linux->windows)
with the changes in place. After accounting for the differences in
semantics, no new warnings were discovered.
2025-06-13 10:29:42 -04:00
Aaron Ballman
9eef4d1c5f
Remove delayed typo expressions (#143423)
This removes the delayed typo correction functionality from Clang
(regular typo correction still remains) due to fragility of the
solution.

An RFC was posted here:
https://discourse.llvm.org/t/rfc-removing-support-for-delayed-typo-correction/86631
and while that RFC was asking for folks to consider stepping up to be
maintainers, and we did have a few new contributors show some interest,
experiments show that it's likely worth it to remove this functionality
entirely and focus efforts on improving regular typo correction.

This removal fixes ~20 open issues (quite possibly more), improves
compile time performance by roughly .3-.4%
(https://llvm-compile-time-tracker.com/?config=Overview&stat=instructions%3Au&remote=AaronBallman&sortBy=date),
and does not appear to regress diagnostic behavior in a way we wouldn't
find acceptable.

Fixes #142457
Fixes #139913
Fixes #138850
Fixes #137867
Fixes #137860
Fixes #107840
Fixes #93308
Fixes #69470
Fixes #59391
Fixes #58172
Fixes #46215
Fixes #45915
Fixes #45891
Fixes #44490
Fixes #36703
Fixes #32903
Fixes #23312
Fixes #69874
2025-06-13 06:45:40 -04:00
Corentin Jabot
5c76ae2894
[Clang] Support constexpr asm at global scope. (#143268)
I previously failed to realize this feature existed...

Fixes #137459
Fixes #143242
2025-06-08 09:16:57 +02:00
Nick Sarnie
3b9ebe9201
[clang] Simplify device kernel attributes (#137882)
We have multiple different attributes in clang representing device
kernels for specific targets/languages. Refactor them into one attribute
with different spellings to make it more easily scalable for new
languages/targets.

---------

Signed-off-by: Sarnie, Nick <nick.sarnie@intel.com>
2025-06-05 14:15:38 +00:00
Nathan Gauër
20d70196c9
[HLSL][SPIR-V] Implement vk::ext_builtin_input attribute (#138530)
This variable attribute is used in HLSL to add Vulkan specific builtins
in a shader.
The attribute is documented here:

17727e88fd/proposals/0011-inline-spirv.md

Those variable, even if marked as `static` are externally initialized by
the pipeline/driver/GPU. This is handled by moving them to a specific
address space `hlsl_input`, also added by this commit.

The design for input variables in Clang can be found here:
355771361e/proposals/0019-spirv-input-builtin.md


Co-authored-by: Justin Bogner <mail@justinbogner.com>
2025-06-04 13:22:37 +02:00
cor3ntin
5c063bebe4
[Clang] Fix a regression introduced by #138518 (#141342)
We did not handle the case where a variable could be initialized by a
CXXParenListInitExpr.

---------

Co-authored-by: Shafik Yaghmour <shafik.yaghmour@intel.com>
2025-05-28 07:00:22 +02:00
Kazu Hirata
12af64d0d7
[Sema] Remove unused includes (NFC) (#141419)
These are identified by misc-include-cleaner.  I've filtered out those
that break builds.  Also, I'm staying away from llvm-config.h,
config.h, and Compiler.h, which likely cause platform- or
compiler-specific build failures.
2025-05-25 10:55:32 -07:00
Jiefeng Wang
4f844e7881
[OpenCL] No need to check array of struct for kernel arguments (#138894)
Since arrays decay into pointers, no need to check them for arguments.
This commit reverts part of the changes from the commit "[OpenCL] Check
for invalid kernel arguments in array types"
3b238ed6626983beb238b95eada4172184fb2d29.
2025-05-23 10:38:35 +02:00
Aaron Ballman
7a3b5d789d
[C] Fix crash-on-invalid due to infinite recursion (#140925)
There are two related issues being fixed in this patch. Both issues
relate to use of an invalid structure which contains a member that we
error recover such that the field has the same type as the structure. In
both cases, we would hit an infinite loop while analyzing the fields
because the type of the field matches the type of the record.

Fixes #140887
2025-05-22 06:47:34 -04:00
cor3ntin
5c1db3823e
[Clang] Functions called in discarded statements should not be instantiated (#140576)
Functions referenced in discarded statements could be treated as
odr-used
because we did not properly set the correct evaluation context in some
places.

Fixes https://github.com/llvm/llvm-project/issues/140449
2025-05-20 07:22:02 +02:00
Aaron Ballman
7cf2860cbd
[C++] Fix a crash with __thread and dependent types (#140542)
We were checking whether the initializer is a valid constant expression
even if the variable was dependent. Now we delay that checking until
after the template has been instantiated.

Fixes #140509
2025-05-19 11:28:48 -04:00
Kazu Hirata
325281631a
[clang] Use *Map::try_emplace (NFC) (#140477)
We can simplify the code with *Map::try_emplace where we need
default-constructed values while avoding calling constructors when
keys are already present.
2025-05-19 06:19:53 -07:00
Grigory Pastukhov
48587f30d6
[clang] Add new warning: not eliding copy on return (missed NRVO) (#139973) 2025-05-16 15:47:53 -03:00
Oliver Hunt
8a05c20c96
Add an off-by-default warning to complain about MSVC bitfield padding (#117428)
This just adds a warning for bitfields placed next to other bitfields
where the underlying type has different storage. Under the MS struct
bitfield packing ABI such bitfields are not packed.
2025-05-13 14:00:20 -07:00