22399 Commits

Author SHA1 Message Date
Hans Wennborg
f4e14bf213
[Sema] Warn about omitting deprecated enumerator in switch (#138562)
This undoes part of 3e4e3b17c14c15c23c0ed18ca9165b42b1b13ae3 which added
the "Omitting a deprecated constant is ok; it should never materialize."
logic.

That seems wrong: deprecated means the enumerator is likely to be
removed in future versions, not that it cannot materialize.

Also move warnings about the use of deprecated enumerators in switch cases
behind a separate flag, -Wdeprecated-switch-case, for users who wish to
handle such enums explicitly and suppress the warning.
2025-05-23 09:40:49 +02:00
cor3ntin
73fda83275
[Clang] Do not put the definition of concept nodes in the Sema library (#141104)
It is a layering violation
2025-05-22 21:57:45 +02:00
Timm Baeder
827027b3a3
[clang] Move Diags.isIgnored() check below faster checks (#141084)
Because this check is relatively slow and the others aren't as much.


http://llvm-compile-time-tracker.com/compare.php?from=4a158f675be7fd1b3763bf39980d801db89744f8&to=886a8fab041ff7574d54cccddbc1a9b968c1bb58&stat=instructions:u
2025-05-22 19:44:17 +02:00
Oleksandr T.
50127ac054
[Clang] add typo correction for unknown attribute names (#140629)
This patch enhances Clang's diagnosis for unknown attributes by
providing typo correction suggestions for known attributes.

```cpp
[[gmu::deprected]] // expected-warning {{unknown attribute 'gmu::deprected' ignored; did you mean 'gnu::deprecated'?}}
int f1(void) {
  return 0;
}

[[deprected]] // expected-warning {{unknown attribute 'deprected' ignored; did you mean 'deprecated'?}}
int f2(void) {
  return 0;
}
```
2025-05-22 17:03:32 +03: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
Younan Zhang
bf6d24a34d
[Clang] Do not defer variable template instantiation for undeduced types (#141009)
The previous approach broke the instantiation convention for templated
substitutions, as we were attempting to instantiate the initializer
even when it was still dependent.

We deferred variable template instantiation until the end of the TU.
However, type deduction requires the initializer immediately,
similar to how constant evaluation does.

Fixes https://github.com/llvm/llvm-project/issues/140773
Fixes #135032
Fixes #134526

Reapplies https://github.com/llvm/llvm-project/pull/138122
2025-05-22 14:05:52 +08:00
Kazu Hirata
6d2a7b4546
[clang] Use llvm::find_if (NFC) (#140983) 2025-05-21 20:31:52 -07:00
Paul Kirth
f0ddadf12c
Revert "[Clang] Fix missed initializer instantiation bug for variable templates" (#140930)
Reverts llvm/llvm-project#138122

The patch causes a regression and prevents compiling valid C++ code. 
The code was accepted by earlier versions of clang and GCC. 
See https://github.com/llvm/llvm-project/issues/140773 for details.
2025-05-22 11:01:06 +08:00
Sarah Spall
5ba57a81f2
[HLSL] Update Sema Checking Diagnostics for builtins (#138429)
Update how Sema Checking is done for HLSL builtins to allow for better
error messages, mainly using 'err_builtin_invalid_arg_type'.
Try to follow the formula outlined in issue #134721 
Closes #134721
2025-05-21 13:03:07 -07:00
erichkeane
f65b35d89f [OpenACC] Stop trying to analyze invalid Var-Decls.
The code to analyze VarDecls for the purpose of ensuring a magic-static
isn't present in a 'routine' was getting confused/crashed because we
create something that looks like a magic-static during error-recovery,
but it is still an invalid decl.

This patch causes us to just 'give up' in the case where the vardecl is
already invalid.

Fixes: #140920
2025-05-21 10:31:21 -07:00
Daan De Meyer
32946ddd2e
[clang-include-cleaner] Make cleanup attr report expr location (#140233)
Instead of reporting the location of the attribute, let's report the
location of the function reference that's passed to the cleanup
attribute as the first argument. This is required as the attribute might
be coming from a macro which means clang-include-cleaner skips the use
as it gets attributed to the header file declaringt the macro and not to
the main file.

To make this work, we have to add a fake argument to the CleanupAttr
constructor so we can pass in the original Expr alongside the function
declaration.

Fixes #140212
2025-05-21 16:40:30 +02:00
A. Jiang
f37c24194e
[Clang] Set the final date for workaround for libstdc++'s format_kind (#140831)
We can use 20250520 as the final date, see the following commits.
- GCC releases/gcc-15 branch:
  - https://gcc.gnu.org/g:fedf81ef7b98e5c9ac899b8641bb670746c51205
  - https://gcc.gnu.org/g:53680c1aa92d9f78e8255fbf696c0ed36f160650
- GCC master branch:
  - https://gcc.gnu.org/g:9361966d80f625c5accc25cbb439f0278dd8b278
  - https://gcc.gnu.org/g:c65725eccbabf3b9b5965f27fff2d3b9f6c75930

Follows-up #139560.
2025-05-21 21:46:27 +08:00
Aaron Ballman
7fe387ac35
[C] Don't diagnose null pointer macros in -Wimplicit-void-ptr-cast (#140724)
This silences the diagnostic when the right-hand side is a null pointer
constant that comes from a macro expansion, such as NULL. However, we do
not limit to just NULL because other custom macros may expand to an
implicit void * cast in C while expanding to something else in C++.
2025-05-21 06:36:21 -04:00
cor3ntin
f4cebe5d73
[Clang] Fix a regression introduced by #140576 (#140859)
Lambda bodies should not be treated as subexpressions of the enclosing
scope.
2025-05-21 10:18:14 +02:00
Younan Zhang
701fe51fd6
[Clang] Fix an inadvertent overwrite of sub-initializers (#140714)
When using InitChecker with VerifyOnly, we create a new designated
initializer to handle anonymous fields. However in the last call to
CheckDesignatedInitializer, the subinitializer isn't properly preserved
but it gets overwritten by the cloned one. Which causes the initializer
to reference the dependent field, breaking assumptions when we
initialize the instantiated specialization.

Fixes https://github.com/llvm/llvm-project/issues/67173
2025-05-21 10:02:34 +08:00
Aaron Puchert
317c932622
Suppress errors from well-formed-testing type traits in SFINAE contexts (#135390)
There are several type traits that produce a boolean value or type based
on the well-formedness of some expression (more precisely, the immediate
context, i.e. for example excluding nested template instantiation):
* `__is_constructible` and variants,
* `__is_convertible` and variants,
* `__is_assignable` and variants,
* `__reference_{binds_to,{constructs,converts}_from}_temporary`,
* `__is_trivially_equality_comparable`,
* `__builtin_common_type`.

(It should be noted that the standard doesn't always base this on the
immediate context being well-formed: for `std::common_type` it's based
on whether some expression "denotes a valid type." But I assume that's
an editorial issue and means the same thing.)

Errors in the immediate context are suppressed, instead the type traits
return another value or produce a different type if the expression is
not well-formed. This is achieved using an `SFINAETrap` with
`AccessCheckingSFINAE` set to true. If the type trait is used outside of
an SFINAE context, errors are discarded because in that case the
`SFINAETrap` sets `InNonInstantiationSFINAEContext`, which makes
`isSFINAEContext` return an `optional(nullptr)`, which causes the errors
to be discarded in `EmitDiagnostic`. However, in an SFINAE context this
doesn't happen, and errors are added to `SuppressedDiagnostics` in the
`TemplateDeductionInfo` returned by `isSFINAEContext`. Once we're done
with deducing template arguments and have decided which template is
going to be instantiated, the errors corresponding to the chosen
template are then emitted. At this point we get errors from those type
traits that we wouldn't have seen if used with the same arguments
outside of an SFINAE context. That doesn't seem right.

So what we want to do is always set `InNonInstantiationSFINAEContext`
when evaluating these well-formed-testing type traits, regardless of
whether we're in an SFINAE context or not. This should only affect the
immediate context, as nested contexts add a new `CodeSynthesisContext`
that resets `InNonInstantiationSFINAEContext` for the time it's active.

Going through uses of `SFINAETrap` with `AccessCheckingSFINAE` = `true`,
it occurred to me that all of them want this behavior and we can just
use this parameter to decide whether to use a non-instantiation context.
The uses are precisely the type traits mentioned above plus the
`TentativeAnalysisScope`, where I think it is also fine. (Though I think
we don't do tentative analysis in SFINAE contexts anyway.)

Because the parameter no longer just sets `AccessCheckingSFINAE` in Sema
but also `InNonInstantiationSFINAEContext`, I think it should be renamed
(along with uses, which also point the reviewer to the affected places).
Since we're testing for validity of some expression, `ForValidityCheck`
seems to be a good name.

The added tests should more or less correspond to the users of
`SFINAETrap` with `AccessCheckingSFINAE` = `true`. I added a test for
errors outside of the immediate context for only one type trait, because
it requires some setup and is relatively noisy.

We put the `ForValidityCheck` condition first because it's constant in
all uses and this would then allow the compiler to prune the call to
`isSFINAEContext` when true.

Fixes #132044.
2025-05-20 23:02:51 +02:00
Aaron Ballman
c555c8d554
[C] Do not diagnose flexible array members with -Wdefault-const-init-field-unsafe (#140578)
This addresses post-commit review feedback from someone who discovered
that we diagnosed code like the following:
```
  struct S {
    int len;
    const char fam[];
  } s;
```
despite it being invalid to initialize the flexible array member.

Note, this applies to flexible array members and zero-sized arrays at
the end of a structure (an old-style flexible array member), but it does
not apply to one-sized arrays at the end of a structure because those do
occupy storage that can be initialized.
2025-05-20 14:40:12 -04:00
Aaron Ballman
6fb23afb8d
[C] Do not diagnose unions with -Wdefault-const-init (#140725)
A default-initialized union with a const member is generally reasonable
in C and isn't necessarily incompatible with C++, so we now silence the
diagnostic in that case. However, we do still diagnose a const-
qualified, default-initialized union as that is incompatible with C++.
2025-05-20 13:04:24 -04:00
erichkeane
e8dff7bea4 [OpenACC] Fix location of array-section diagnostic.
In a sub-subscript of an array-section, it is actually an array section.
So make sure we get the location correct when there isn't a 'colon' to
look at.
2025-05-20 09:04:32 -07:00
erichkeane
2f66e5fcba [OpenACC] Fixed error recovery during jump diagnostics for OpenACC
We didn't consider/test a case where the structured block/loop would be
empty, which happens when the body is ONLY a break.

Fixes: #140712
2025-05-20 06:31:13 -07:00
Dmitry Sidorov
d56d537f9b
[Clang] Allow vector and matrix type attributes for sub-byte _BitInt (#140253)
It is useful for several cases, particularly for 4-bit integers.
2025-05-20 15:06:36 +02:00
cor3ntin
9c33faf862
[Clang] Use correct evaluation contexts when instantiating a var without initializer (#140699)
The evaluation context was improperly set up, such that we were trying
to set up cleanups for a global variable at the point of use, which led
to incorrect diagnostics about the variable not being capturable.

Fixes #140632
Fixes #140622
2025-05-20 14:04:34 +02: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
Jim Lin
d561d595c4
[RISCV] Implement intrinsics for XAndesVPackFPH (#140007)
This patch implements clang intrinsic support for XAndesVPackFPH.

The document for the intrinsics can be found at:

https://github.com/andestech/andes-vector-intrinsic-doc/blob/ast-v5_4_0-release-v5/auto-generated/andes-v5/intrinsic_funcs.adoc#andes-vector-packed-fp16-extensionxandesvpackfph
and with policy variants

https://github.com/andestech/andes-vector-intrinsic-doc/blob/ast-v5_4_0-release-v5/auto-generated/andes-v5/policy_funcs/intrinsic_funcs.adoc#andes-vector-packed-fp16-extensionxandesvpackfph

Co-authored-by: Tony Chuan-Yue Yuan <yuan593@andestech.com>
2025-05-20 13:16:51 +08:00
erichkeane
e3950a049a [OpenACC] Fix 'vector' checking when inside combined construct
For some reason when implementing 'vector' I didn't include switch
entries for the combined constructs.  I audited the rest of the uses of
this pattern, and got it right everywhere else, so I'm not sure why I
missed it here.

Fixes: #140339
2025-05-19 09:57:22 -07: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
Krzysztof Drewniak
4bdd116b80
[AMDGPU] Add a new amdgcn.load.to.lds intrinsic (#137425)
This PR adds a amdgns_load_to_lds intrinsic that abstracts over loads to
LDS from global (address space 1) pointers and buffer fat pointers
(address space 7), since they use the same API and "gather from a
pointer to LDS" is something of an abstract operation.

This commit adds the intrinsic and its lowerings for addrspaces 1 and 7,
and updates the MLIR wrappers to use it (loosening up the restrictions
on loads to LDS along the way to match the ground truth from target
features).

It also plumbs the intrinsic through to clang.
2025-05-19 07:15:04 -07: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
Kazu Hirata
0fc9cd13a6
[clang] Use llvm::unique (NFC) (#140459) 2025-05-18 09:47:06 -07:00
Kazu Hirata
e003326cd8
[Sema] Use llvm::is_contained (NFC) (#140455) 2025-05-18 09:45:48 -07:00
Kazu Hirata
4388f38fbd
[clang] Use llvm::max_element (NFC) (#140435) 2025-05-18 07:33:35 -07:00
dty2
0b553e055a
[Clang] Fix missed initializer instantiation bug for variable templates (#138122)
Fixes https://github.com/llvm/llvm-project/issues/135032

Due to nested templates, when instantiating the outer layer (the
template class), the inner layer (the template variable) uses delayed
instantiation.
This causes the declaration (VarDecl) of the template variable to retain
the type from the original template declaration (i.e., auto), and it
loses the initializer.
Later, when instantiating the template variable, its
VarTemplateSpecializationDecl type depends on the VarDecl type.
Thus, the VarTemplateSpecializationDecl also has no initializer, and its
type remains auto.
Ultimately, when building the reference expression in
Sema::BuildDeclarationNameExpr, the expression's type is auto and stays
as auto until code generation, triggering llvm_unreachable in
CodeGenTypes::ConvertType.

Since I noticed that the deduction of auto type is caused by the
initializer
I plan to do special processing for template variables of type auto,
that is, to prevent their delayed instantiation
so that their initializers will not be lost when the outer template
class is instantiated
2025-05-18 18:42:07 +08:00
Kazu Hirata
6290cc33e9
[clang] Use llvm::stable_sort (NFC) (#140413) 2025-05-17 17:02:45 -07:00
Kazu Hirata
f9f69dac2a
[clang] Remove redundant control flow statements (NFC) (#140359) 2025-05-17 12:59:47 -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
Helena Kotas
f95f3030e5
[HLSL] Implicit resource binding for cbuffers (#139022)
Constant buffers defined with the `cbuffer` keyword do not have a
constructor. Instead, the call to initialize the resource handle based
on its binding is generated in codegen. This change adds initialization
of `cbuffer` handles that have implicit binding.

Closes  #139617
2025-05-15 19:49:29 -07:00
Finn Plummer
a4eb0db062
[HLSL][RootSignature] Add metadata generation for descriptor tables (#139633)
- prereq: Modify `RootSignatureAttr` to hold a reference to the owned
declaration
- Define and implement `MetadataBuilder` in `HLSLRootSignature`
- Integrate and invoke the builder in `CGHLSLRuntime.cpp` to generate
the Root Signature for any associated entry functions
- Add tests to demonstrate functionality in `RootSignature.hlsl`

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

Note: this is essentially just
https://github.com/llvm/llvm-project/pull/125131 rebased onto the new
approach of constructing a root signature decl, instead of holding the
elements in `AdditionalMembers`.
2025-05-15 14:54:00 -07:00
Aaron Ballman
0eb4bd27d1
[C] Silence unreachable -Watomic-access diagnostics (#140064)
Accessing the member of a structure or union which is _Atomic-qualified
is undefined behavior in C. We currently diagnose that with a warning
that defaults to an error. In turn, this means we reject a valid program
if the access it not reachable because of the error. e.g.,

  if (0)
    SomeAtomicStruct.Member = 12; // Was diagnosed

This silences the diagnostic if the member access is not reachable.
2025-05-15 12:24:12 -04:00
Vincent
d58d5d6e9d
[clang] Compound Literal Statement Constant Expression Assertion Fix (#139479)
Compound literals initializer-list should be a constant expression if it
is defined outside the body of a function.

Emit error instead of falling through tripping assertion error.

Fixes #139160
2025-05-15 12:23:45 -04:00
cor3ntin
381a649fb9
[Clang] Add warnings when mixing different charN_t types (#138708)
charN_t represent code units of different UTF encodings. Therefore the
values of 2 different charN_t objects do not represent the same
characters.

In order to avoid comparing apples and oranges, we add new warnings to
warn on:
  - Implicit conversions
  - Comparisons
  - Other cases involving arithmetic conversions

We only produce the warning if we cannot establish the comparison would
be safe through constant evaluation.

The new `-Wimplicit-unicode-conversion` warning is enabled by default.

Note that this PR intentionally doesn;t touches char/wchar_t, but it
would be worth considering also warning on extending the new warnings to
these types (in a follow up)

Additionally most arithmetic operations on charN_t don't really make
sense (ie what does it mean to addition code units), so we could add
warnings for that.

Fixes #138526
2025-05-15 18:16:05 +02:00
cor3ntin
a2f156b84a
[Clang] Fix deduction of explicit object member functions (#140030)
When taking the address of an overload set containing an explicit object
member, we should not take the
explicit object parameter into account.
2025-05-15 15:29:56 +02:00
Lukacma
6fc0312919
[Clang][AArch64] Add fp8 variants for untyped NEON intrinsics (#128019)
This patch adds fp8 variants to existing intrinsics, whose operation
doesn't depend on arguments being a specific type.

It also changes mfloat8 type representation in memory from `i8` to
`<1xi8>`
2025-05-15 14:01:41 +01:00
Helena Kotas
520773b47e
[HLSL] Add resource constructor with implicit binding for global resources (#138976)
Adds constructor for resources with implicit binding and applies it to
all resources without binding at the global scope.
Adds Clang builtin function
`__builtin_hlsl_resource_handlefromimplicitbinding` that gets translated
to `llvm.dx|spv.resource.handlefromimplicitbinding` intrinsic calls.
Specific bindings are assigned in DXILResourceImplicitBinding pass.

Design proposals:

https://github.com/llvm/wg-hlsl/blob/main/proposals/0024-implicit-resource-binding.md

https://github.com/llvm/wg-hlsl/blob/main/proposals/0025-resource-constructors.md

One change from the proposals is that the `orderId` parameter is added
onto the constructor. Originally it was supposed to be generated in
codegen when the `llvm.dx|spv.resource.handlefromimplicitbinding` call
is emitted, but that is not possible because the call is inside a
constructor, and the constructor body is generated once per resource
type and not resource instance. So the only way to inject instance-based
data like `orderId` into the
`llvm.dx|spv.resource.handlefromimplicitbinding` call is that it must
come in via the constructor argument.

Closes #136784
2025-05-14 18:41:17 -07:00
Tomohiro Kashiwada
cb53ece2ca
[Cygwin] Template instantiations should be exported by default (#139799)
Behaves as same as both of Clang and GCC targetting MinGW. Required for
compatibility for Cygwin-GCC.

Divided from https://github.com/llvm/llvm-project/pull/138773
2025-05-14 22:22:35 +03:00
Tomohiro Kashiwada
a3d52ea99e
[Cygwin] RTTI and VTable should be dllexport-ed (#139798)
Behaves as same as both of Clang and GCC targetting MinGW. Required for
compatibility for Cygwin-GCC.

Divided from https://github.com/llvm/llvm-project/pull/138773
2025-05-14 22:22:18 +03:00
Shafik Yaghmour
7e690db515
[Clang][NFC] Introduce no local variable to avoid use after move (#139784)
Static analysis flagged the use of Left.size() because we just moved out
of Left and that would be undefined behavior. Fix is to take the size
and store it in a local variable instead.
2025-05-14 07:31:14 -07: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
erichkeane
65a9a3d5b1 [OpenACC] Fix asserts when checking clauses after invalid directive
When implementing the parsing for OpenACC I ensured that we could always
continue to do diagnostics/etc.  For the most part, I was consistent
with that assumption throughout clause Sema, but in 3 cases I left in
some unreachables for cases where this would happen. I've now properly
handled all 3 in a reasonable way.

Fixes: #139894
2025-05-14 07:03:16 -07:00
Aaron Ballman
06bf100386
[C++20] Fix a crash with spaceship and vector types (#139767)
Vector types cannot be directly compared, you get an error when you try
to do so. This patch causes the explicitly defaulted spaceship operator
to be implicitly deleted.

Fixes #137452
2025-05-14 06:58:15 -04:00
Yanzuo Liu
86ba681e28
[Clang][Sema] Disable checking invalid template id in initializer of primary variable template std::format_kind with libstdc++ (#139560)
#134522 triggers compilation error with libstdc++, in which primary
variable template `std::format_kind` is defined like

```cpp
template <typename R>
constexpr auto format_kind =
__primary_template_not_defined(
  format_kind<R>
);
```

See #139067 or <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120190>.

This PR disables checking template id in initializer of primary variable
template `std::format_kind` in libstdc++ (by checking `__GLIBCXX__`).

Fixes #139067
2025-05-14 10:25:01 +02:00