3964 Commits

Author SHA1 Message Date
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
Aaron Ballman
e3f87e1591
[C] Fix a false-positive with tentative defn compat (#139738)
C++ has a carve-out that makes a declaration with 'extern' explicitly
specified and no initializer be a declaration rather than a definition.
We now account for that to silence a diagnostic with:
```
  extern const int i;
  const int i = 12;
```
which is valid C++.

Addresses an issue that was brought up via post-commit review.
2025-05-13 10:59:55 -04:00
Sirraide
45b5cc08e5
[Clang] Fix the warning group of several compatibilty diagnostics (#138872)
There are a few diagnostics that are incorrectly grouped under
`-Wc++20-compat` instead of `-Wpre-c++20-compat`.

I grepped for any remaining `-Wc++xy-compat` diagnostics, but they all
seem to actually be about compatibility with C++XY.

Fixes #138775.
2025-05-08 01:41:57 +02:00
cor3ntin
300d4026f7
[Clang] Implement the core language parts of P2786 - Trivial relocation (#127636)
This adds

- The parsing of `trivially_relocatable_if_eligible`,
`replaceable_if_eligible` keywords
- `__builtin_trivially_relocate`, implemented in terms of memmove. In
the future this should
- Add the appropriate start/end lifetime markers that llvm does not have
(`start_lifetime_as`)
     - Add support for ptrauth when that's upstreamed

- the `__builtin_is_cpp_trivially_relocatable` and
`__builtin_is_replaceable` traits


Fixes #127609
2025-05-06 14:13:32 +02:00
Kees Cook
04364fb888
[randstruct] Also randomize composite function pointer structs (#138385)
Check for struct members that are structs filled only with function
pointers by recursively examining it. Since the lamba
IsFunctionPointerOrForwardDecl cannot call itself directly, move it into
a helper function, EntirelyFunctionPointers, so it can be called from
the lambda.

Add test for composite function pointer structs getting automatically
randomized.

Add more tests for validating automatic randomization vs explicitly
annotated with "randomize_layout", and excluded with
"no_randomize_layout".

Reorder the "should we randomize?" "if" statement to check for
enablement before checking for Record details.

Fixes #138355
2025-05-05 14:30:41 -07:00
Aaron Ballman
15f7e02940
[C23] Disable diagnostic on struct defn in prototype (#138516)
Thanks to changes to type compatibility rules via WG14 N3007, these
functions can now be called with a compatible type even within the same
TU, which makes the -Wvisibility diagnostic too chatty to have on by
default.

So in C23 mode, -Wvisibility will only diagnose an incomplete tag type
declared in a function prototype. If the tag is defined in the
prototype, the diagnostic is silenced.
2025-05-05 10:40:05 -04:00
cor3ntin
1101b76732
[Clang] Implement CWG3005 Function parameters should never be name-independent (#138245)
We already attempted to implement this (it was the intent of the paper),
in that parameters were never considered name-independent. However, we
failed to check that any previously found parameter declaration was also
name-independent.

Note that, as worded, the current resolution is insufficient (I wrote to
CWG with better wording), but there is some consensus on the design
outcome.

Fixes #136373
2025-05-02 20:56:28 +02:00
Aaron Ballman
a635bbf141
[C] Update the -Wdefault-const-init-unsafe wording (#138266)
This drops the "and is incompatible with C++" phrasing from the
diagnostic unless -Wc++-compat is explicitly passed. This makes the
diagnostic less confusing when it is on by default rather than enabled
because of C++ compatibility concerns
2025-05-02 12:23:40 -04:00
Aaron Ballman
a4ceac7e3e
[C23] Implement WG14 N3037 (#132939)
This changes the type compatibility rules so that it is permitted to
redefine tag types within the same TU so long as they are equivalent
definitions.

It is intentionally not being exposed as an extension in older C
language modes. GCC does not do so and the feature doesn't seem
compelling enough to warrant it.
2025-05-02 09:19:21 -04:00
Aaron Ballman
543f112e14
[C] Add -Wjump-bypasses-init (#138009)
We already diagnose when a jump bypasses initialization in C++ because
such code is ill-formed there. However, we were not using this to
diagnose those same jumps in C.

-Wjump-bypasses-init is grouped under -Wc++-compat and diagnoses this
situation as a compatibility issue with C++. This diagnostic is off by
default.

The diagnostic could perhaps be enabled by default for C, but due to the
design of jump diagnostic handling, it not a trivial task. For now,
we'll add the diagnostic as off-by-default so we get incremental
improvement, but a follow-up could try to refactor jump diagnostics so
we can enable this by default in C and have it as a C++ compatibility
diagnostic as well.
2025-05-02 09:06:31 -04:00
Aaron Ballman
85c810060e
[C] Diagnose use of C++ keywords in C (#137234)
This adds a new diagnostic group, -Wc++-keyword, which is off by default
and grouped under -Wc++-compat. The diagnostic catches use of C++
keywords in C code.

This change additionally fixes an issue with -Wreserved-identifier not
diagnosing use of reserved identifiers in function parameter lists in a
function declaration which is not a definition.

Fixes https://github.com/llvm/llvm-project/issues/21898
2025-05-02 07:20:02 -04:00
Vlad Serebrennikov
cf2f13a867 [clang][NFC] Convert Sema::CCEKind to scoped enum 2025-05-02 14:01:40 +03:00
Vlad Serebrennikov
6e63b68389 [clang][NFC] Convert Sema::OverloadKind to scoped enum 2025-05-02 13:44:32 +03:00
Vlad Serebrennikov
4ba27780d3 [clang][NFC] Convert Sema::CorrectTypoKind to scoped enum 2025-05-02 13:20:41 +03:00
Vlad Serebrennikov
140c2b6d08 [clang][NFC] Convert Sema::AllowFoldKind to scoped enum 2025-05-02 10:48:08 +03:00
Vlad Serebrennikov
7e71db8463 [clang][NFC] Convert Sema::AvailabilityMergeKind to scoped enum 2025-05-02 07:56:57 +03:00
Vlad Serebrennikov
ed673aac7a [clang][NFC] Convert Sema::OffsetOfKind to scoped enum 2025-05-02 07:49:47 +03:00
Vlad Serebrennikov
8089c3d1ed [clang][NFC] Convert Sema::NonTagKind to scoped enum 2025-05-02 07:45:22 +03:00
Vlad Serebrennikov
09fb9adbd2 [clang][NFC] Convert Sema::NonTrivialCUnionContext to scoped enum 2025-05-02 07:39:54 +03:00
Vlad Serebrennikov
001cc34275
[clang] Add scoped enum support to StreamingDiagnostic (#138089)
This patch adds templated `operator<<` for diagnostics that pass scoped
enums, saving people from `llvm::to_underlying()` clutter on the side of
emitting the diagnostic. This eliminates 80 out of 220 usages of
`llvm::to_underlying()` in Clang.

I also backported `std::is_scoped_enum_v` from C++23.
2025-05-01 17:03:47 +04:00
Aaron Ballman
bc9aa0f4c4
[C] Add -Wtentative-definition-compat (#137967)
This adds a new diagnostic to warn about redeclaration of a tentative
definition in C. This is incompatible with C++, so the new diagnostic
group is under -Wc++-compat.
2025-05-01 07:31:48 -04:00
Aaron Ballman
00f9ef282c
[C] Modify -Wdefault-const-init (#137961)
Post-commit review feedback on
https://github.com/llvm/llvm-project/pull/137166 raised a concern from
the Linux kernel about wanting to silence the new diagnostic when the
uninitialized object is a const member of a structure. These members can
be initialized later if the containing object is non-const, such as
through a call to memset, for example.

This splits the diagnostic groups into:
```
-Wc++-compat
  -Wdefault-const-init
    -Wdefault-const-init-field
    -Wdefault-const-init-var
    -Wdefault-const-init-unsafe
      -Wdefault-const-init-field-unsafe
      -Wdefault-const-init-var-unsafe
```

---------

Co-authored-by: Mariya Podchishchaeva <mariya.podchishchaeva@intel.com>
2025-05-01 07:08:06 -04:00
Vlad Serebrennikov
5f3e5df481 [clang][NFC] Convert Sema::BuiltinCountedByRefKind to scoped enum 2025-05-01 08:01:03 +03:00
Helena Kotas
f1750300aa
[HLSL] Resource initialization by constructors (#135120)
- Adds resource constructor that takes explicit binding for all resource
classes.
- Updates implementation of default resource constructor to initialize
resource handle to `poison`.
- Removes initialization of resource classes from Codegen.
- Initialization of `cbuffer` still needs to happen in `CGHLSLRuntime`
because it does not have a corresponding resource class type.
- Adds `ImplicitCastExpr` for builtin function calls. Sema adds these
automatically when a method of a template class is instantiated, but
some resource classes like `ByteAddressBuffer` are not templates so they
need to have this added explicitly.

Design proposal: llvm/wg-hlsl#197

Closes #134154
2025-04-29 18:42:22 -07:00
Aaron Ballman
2f976956e5
[C] Diagnose declarations hidden in C++ (#137368)
This introduces a new diagnostic, -Wc++-hidden-decl, which is grouped
under -Wc++-compat, that diagnoses declarations which are valid in C but
invalid in C++ due to the type being at the wrong scope. e.g.,
```
  struct S {
    struct T {
      int x;
    } t;
  };

  struct T t; // Valid C, invalid C++
```
This is implementing the other half of #21898
2025-04-29 07:58:00 -04:00
Justin Bogner
f9855917f6
[HLSL] Treat classes and structs as packed by default (#137391)
Fixes #121010.
2025-04-28 13:44:46 -07:00
Vlad Serebrennikov
ee29afe1e5 [clang][NFC] Convert LookupResultKind to scoped enum 2025-04-28 08:42:02 +03:00
Vlad Serebrennikov
f3a61f61ee [clang][NFC] Convert LookupResult::AmbiguityKind to scoped enum 2025-04-28 08:22:57 +03:00
Aaron Ballman
576161cb60
[C] Warn on uninitialized const objects (#137166)
Unlike C++, C allows the definition of an uninitialized `const` object.
If the object has static or thread storage duration, it is still
zero-initialized, otherwise, the object is left uninitialized. In either
case, the code is not compatible with C++.

This adds a new diagnostic group, `-Wdefault-const-init-unsafe`, which
is on by default and diagnoses any definition of a `const` object which
remains uninitialized.

It also adds another new diagnostic group, `-Wdefault-const-init` (which
also enabled the `unsafe` variant) that diagnoses any definition of a
`const` object (including ones which are zero-initialized). This
diagnostic is off by default.

Finally, it adds `-Wdefault-const-init` to `-Wc++-compat`. GCC diagnoses
these situations under this flag.

Fixes #19297
2025-04-25 08:21:41 -04:00
Oliver Hunt
5b16941f57
[clang] Ensure correct copying of records with authenticated fields (#136783)
When records contain fields with pointer authentication, even simple
copies can require
additional work be performed. This patch contains the core functionality
required to
handle user defined structs, as well as the implicitly constructed
structs for blocks, etc.

Co-authored-by: Ahmed Bougacha
Co-authored-by: Akira Hatanaka
Co-authored-by: John Mccall
2025-04-24 16:22:50 -07:00
Aaron Ballman
71ce9e26ae
Control analysis-based diagnostics with #pragma (#136323)
Previously, analysis-based diagnostics (like -Wconsumed) had to be
enabled at file scope in order to be run at the end of each function
body. This meant that they did not respect #pragma clang diagnostic
enabling or disabling the diagnostic.

Now, these pragmas can control the diagnostic emission.

Fixes #42199
2025-04-23 06:55:10 -04:00
Matheus Izvekov
fe94f11407
[clang] Fix elaborated keyword canonicalization (#135916) 2025-04-16 16:27:24 -03:00
Sirraide
616613c80b
[Clang] [Sema] Fix a crash when a friend function is redefined as deleted (#135679)
NB: This only fixes the crash introduced in Clang 19; we still accept
this code even though we shouldn’t:
```c++
struct S {
    friend int f() { return 3; }
    friend int f() = delete;
};
```
I tried figuring out a way to diagnose this redeclaration, but it seems
tricky because I kept running into issues around defaulted comparison
operators. From my testing, however, this fix here would still be
required even once we do start diagnosing this.

Fixes #135506.
2025-04-16 14:08:59 +02:00