22399 Commits

Author SHA1 Message Date
Kazu Hirata
65eb5e8b87
[Sema] Use llvm::is_contained (NFC) (#139833) 2025-05-13 23:52:51 -07:00
cor3ntin
6c1bb48cc4
[Clang] Remove workaround for libstdc++4.7 (#139693)
We document libstdc++4.8 as the minimum supported version, and we
carried a hack for `include/tr1/hashtable.h` fixed in 4.7.

Cleanup some libstdc++ compatibility comments.
2025-05-14 08:41:10 +02:00
Hood Chatham
e29b70ee11
[WebAssembly][Clang] Add __builtin_wasm_ref_is_null_extern (#139580)
I also fixed __builtin_wasm_ref_null_extern() to generate a diagnostic
when it gets an argument. It seems like `SemaRef.checkArgCount()` has a
bug that makes it unable to check for 0 args.
2025-05-13 21:36:51 -07:00
Younan Zhang
866f1cd6a9
[Clang] Stop changing DC when instantiating dependent friend specializations (#139436)
Since 346077aa, we began using the primary template's lexical
DeclContext for template arguments in order to properly instantiate a
friend definition.

There is a missed peculiar case, as in a friend template is specialized
within a dependent context. In this scenario, the primary template is
not a definition, whereas the specialization is. So the primary
template's DeclContext doesn't provide anything meaningful
for instantiation.

Fixes https://github.com/llvm/llvm-project/issues/139052
2025-05-14 11:45:33 +08: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
Hood Chatham
3b3adefd58
[Clang] Fix Sema::checkArgCount for 0-arg functions (#139638)
When calling a function that expects zero arguments with one argument,
`Call->getArg(1)` will trap when trying to format the diagnostic.

This also seems to improve the rendering of the diagnostic some of the
time. Before:
```
$ ./bin/clang -c a.c
a.c:2:30: error: too many arguments to function call, expected 2, have 4
    2 |   __builtin_annotation(1, 2, 3, 4);
      |                           ~  ^
```
After:
```
$ ./bin/clang -c a.c
a.c:2:30: error: too many arguments to function call, expected 2, have 4
    2 |   __builtin_annotation(1, 2, 3, 4);
      |                              ^~~~
```

Split from #139580.

---------

Co-authored-by: Mariya Podchishchaeva <mariya.podchishchaeva@intel.com>
2025-05-13 13:01:28 -07:00
Aaron Ballman
131c8f84bb
[OpenMP] Fix crash with invalid size expression (#139745)
We weren't correctly handling size expressions with errors before trying
to get the type of the size expression.

No release note needed because support for 'stripe' was added to the
current release.

Fixes #139433
2025-05-13 12:55:53 -04: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
Max Graey
8aaac80ddd
[NFC] Use more isa and isa_and_nonnull instead dyn_cast for predicates (#137393)
Also fix some typos in comments

---------

Co-authored-by: Mehdi Amini <joker.eph@gmail.com>
2025-05-13 22:34:42 +08:00
Amr Hesham
a4186bd04b
[clang][OpenMP] Add error for large expr in collapse clause (#138592)
Report error when OpenMP collapse clause has an expression that can't be
represented in 64-bit

Issue #138445
2025-05-12 21:34:35 +02:00
Kazu Hirata
e74877bafe [Sema] Fix a warning
This patch fixes:

  clang/lib/Sema/SemaHLSL.cpp:973:15: error: variable 'SignatureDecl'
  set but not used [-Werror,-Wunused-but-set-variable]
2025-05-12 10:16:00 -07:00
Finn Plummer
dd3d7cfe2e
[HLSL][RootSignature] Define and integrate rootsig clang attr and decl (#137690)
- Defines a new declaration node `HLSLRootSignature` in `DeclNodes.td`
that will consist of a `TrailingObjects` of the in-memory construction
of the root signature, namely an array of `hlsl::rootsig::RootElement`s

- Defines a new clang attr `RootSignature` which simply holds an
identifier to a corresponding root signature declaration as above

- Integrate the `HLSLRootSignatureParser` to construct the decl node in
`ParseMicrosoftAttributes` and then attach the parsed attr with an
identifier to the entry point function declaration.

- Defines the various required declaration methods

- Add testing that the declaration and reference attr are created
correctly, and some syntactical error tests.

It was previously proposed that we could have the root elements
reference be stored directly as an additional member of the attribute
and to not have a separate root signature decl. In contrast, by defining
them separately as this change proposes, we will allow a unique root
signature to have its own declaration in the AST tree. This allows us to
only construct a single root signature for all duplicate root signature
attributes. Having it located directly as a declaration might also prove
advantageous when we consider root signature libraries.

Resolves https://github.com/llvm/llvm-project/issues/119011
2025-05-12 09:59:46 -07:00
Aaron Ballman
d5974524de
[OpenMP] Fix crash with invalid argument to simd collapse (#139313)
Same as with other recent crash fixes, this is checking whether the
argument expression contains errors or not.

Fixes #138493
2025-05-12 08:04:22 -04:00
Younan Zhang
802d8d9077
[Clang] Don't ditch typo-corrected lookup result (#139374)
For a member function call like 'foo.bar<int>()', there are two
typo-correction points after parsing the dot. The first occurs in
ParseOptionalCXXScopeSpecifier, which tries to annotate the template
name following any scope specifiers.

If the template name bar is not found within 'foo', the parser was
previously instructed to drop any function templates found outside of
the scope. This was intended to prevent ambiguity in expressions like
'foo->bar < 7', as explained in commit 50a3cddd. However, it's
unnecessary to discard typo-corrected results that were strictly
resolved within the scope 'foo'.

We won't perform a second typo-correction in ParseUnqualifiedId after
the name being annotated.

Fixes https://github.com/llvm/llvm-project/issues/139226
2025-05-10 20:44:45 +08:00
halbi2
0b9c63dfe9
[clang] Warn about deprecated volatile-qualified return types (#137899)
The old codepath in GetFullTypeForDeclarator was under "if (not a class type)"
so that it failed to warn for class types. Move the diagnostic outside
of the "if" so that it warns in the proper situations.

Fixes #133380

Co-authored-by: cor3ntin <corentinjabot@gmail.com>
2025-05-10 08:03:20 +02:00
Yutong Zhu
2da57f8105
[Clang] Improve -Wtautological-overlap-compare diagnostics flag (#133653)
This PR attempts to improve the diagnostics flag
`-Wtautological-overlap-compare` (#13473). I have added code to warn
about float-point literals and character literals. I have also changed
the warning message for the non-overlapping case to provide a more
correct hint to the user.

Fixes #13473.
2025-05-10 01:11:28 +02:00
Shafik Yaghmour
b1c7801069
[Clang][NFC] Adding note on details that are not immediately obvious (#138349)
This code was flagged by static analysis. It was a false positive but
the reason why this code is valid is subtle and folks refactoring this
code in the future could easily miss it.
2025-05-09 14:23:45 -07:00
Oliver Hunt
65a6cbde5b
[clang] Add support for __ptrauth being applied to integer types (#137580)
Allows the __ptrauth qualifier to be applied to pointer sized integer types,
updates Sema to ensure trivially copyable, etc correctly handle address
discriminated integers, and updates codegen to perform authentication
around arithmetic on the types.
2025-05-09 13:12:09 -07:00
cor3ntin
52b18b4e82
[Clang] Reland: Diagnose invalid function types in dependent contexts (#139246)
When forming an invalid function type, we were not diagnosing it if the
call was dependent.

However, we later rely on the function type to be sensible during
argument deduction.

We now diagnose anything that is not a potential function type,
to avoid constructing bogus call expressions.

Fixes https://github.com/llvm/llvm-project/issues/138657
Fixes https://github.com/llvm/llvm-project/issues/115725
Fixes https://github.com/llvm/llvm-project/issues/68852
Fixes #139163
2025-05-09 19:59:20 +02:00
PiJoules
a6385a87a2
[Sema] Refactor IsFunctionConversion (#139172)
A bunch of uses of IsFunctionConversion don't use the third argument and
just make a dummy QualType to pass. This splits IsFunctionConversion
into 2 functions, one that just takes 2 arguments and does the check,
and one that does the actual conversion using the 3rd argument. Both
functions can be const and replace current uses appropriately.
2025-05-09 10:19:52 -07:00
Aaron Ballman
ab6c4f5085
[OpenMP] Fix a crash on invalid with unroll partial (#139280)
You cannot get the integer constant expression's value if the expression
contains errors.

Fixes https://github.com/llvm/llvm-project/issues/139267
2025-05-09 12:32:44 -04:00
Aaron Ballman
b249b49c13
[OpenMP] Fix crash when diagnosing dist_schedule (#139277)
We were failing to pass a required argument when emitting the
diagnostic, so the source range was being used in place of an index.
This caused a failed assertion due to the incorrect index.

Fixes #139266
2025-05-09 11:12:19 -04:00
Krzysztof Parzyszek
6094080d27
[clang][OpenMP] Pass OpenMP version to getOpenMPDirectiveName (#139115)
The OpenMP version is stored in language options in ASTContext. If the
context is not available, use the fallback version.

RFC:
https://discourse.llvm.org/t/rfc-alternative-spellings-of-openmp-directives/85507
2025-05-09 07:41:54 -05:00
Aaron Ballman
187a83f86c
[OpenMP] No long crash on an invalid sizes argument (#139118)
We were trying to get type information out of an expression node which
contained errors. That causes the type of the expression to be
dependent, which the code was not expecting. Now we handle error
conditions with an early return.

Fixes #139073
2025-05-09 06:52:06 -04:00
Matheus Izvekov
abd5ee9355
Revert "[Clang] Diagnose invalid function types in dependent contexts (#138731)" (#139176)
This reverts commit cf9b4d1e7961214deabd99a9fc3b1d4c9e78a71f.

Causes breakages as reported here:
https://github.com/llvm/llvm-project/pull/138731#issuecomment-2864298000
2025-05-08 20:56:48 -03:00
Oleksandr T.
61b435ec4d
[Clang] show attribute namespace in diagnostics (#138519)
This patch enhances Clang's diagnosis of an unknown attribute by
printing the attribute's namespace in the diagnostic text. e.g.,

```cpp
[[foo::nodiscard]] int f(); // warning: unknown attribute 'foo::nodiscard' ignored
```
2025-05-09 00:49:01 +03:00
cor3ntin
09c80e2944
Reland [Clang] Deprecate __is_trivially_relocatable (#139061)
The C++26 standard relocatable type traits has slightly different
semantics, so we introduced a new
``__builtin_is_cpp_trivially_relocatable``
when implementing trivial relocation in #127636.

However, having multiple relocatable traits would be confusing
in the long run, so we deprecate the old trait.

As discussed in #127636

`__builtin_is_cpp_trivially_relocatable` should be used instead.
2025-05-08 19:25:50 +02:00
Helena Kotas
3bc3b1c6c0
[HLSL][NFC] Rename isImplicit() to hasRegisterStot() on HLSLResourceBindingAttr (#138964)
Renaming because the name `isImplicit` is ambiguous. It can mean
implicit attribute or implicit binding.
2025-05-08 10:03:21 -07:00
dyung
ab2e7aa517
Revert "[Clang] Deprecate __is_trivially_relocatable" (#139027)
Reverts llvm/llvm-project#138835

This is causing a test failure on a bot:
https://lab.llvm.org/buildbot/#/builders/144/builds/24541
2025-05-08 01:38:04 -04:00
Prabhu Rajasekaran
20d6375796
[clang] Handle CC attrs for UEFI (#138935)
UEFI's default ABI is MS ABI. Handle the calling convention attributes
accordingly.
2025-05-07 21:42:01 -07:00
Yaxun (Sam) Liu
c16297cd3f
[CUDA][HIP] Fix host/device attribute of builtin (#138162)
When a builtin function is passed a pointer with a different
address space, clang creates an overloaded
builtin function but does not copy the host/device attribute. This
causes
error when the builtin is called by device functions
since CUDA/HIP relies on the host/device attribute to treat
a builtin function as callable on both host and device
sides.

Fixed by copying the host/device attribute of the original
builtin function to the created overloaded builtin function.
2025-05-07 22:03:33 -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
fahadnayyar
62a2f0fdc7
[APINotes] Add support for SWIFT_RETURED_AS_UNRETAINED_BY_DEFAULT (#138699)
This patch adds support in APINotes for annotating C++ user-defined
types with: `swift_attr("returned_as_unretained_by_default")`
This attribute allows to specify a default ownership convention for
return values of `SWIFT_SHARED_REFERENCE` c++ types. Specifically, it
marks all unannotated return values of this type as `unretained` (`+0`)
by default, unless explicitly overridden at the API level using
`swift_attr("returns_retained")` or `swift_attr("returns_unretained")`.

The corresponding Swift compiler support for this annotation enables
developers to suppress warnings about unannotated return ownership in
large codebases while maintaining safe and predictable ownership
semantics. By enabling this in APINotes, library authors can define this
behavior externally without needing to modify C++ source headers
directly.

### Example usage in APINotes:
```
- Name: RefCountedTypeWithDefaultConvention
  SwiftImportAs: reference
  SwiftDefaultOwnership: unretained

```
rdar://150764491
2025-05-07 13:42:39 -07:00
cor3ntin
43c514bd42
[Clang] Deprecate __is_trivially_relocatable (#138835)
The C++26 standard relocatable type traits has slightly different
semantics, so we introduced a new
``__builtin_is_cpp_trivially_relocatable`` when implementing trivial
relocation in #127636.

However, having multiple relocatable traits would be confusing in the
long run, so we deprecate the old trait.

As discussed in #127636

`__builtin_is_cpp_trivially_relocatable` should be used instead.

---------

Co-authored-by: Aaron Ballman <aaron@aaronballman.com>
2025-05-07 22:23:41 +02:00
Aaron Ballman
99f2bc2f5d Add braces to silence -Wdangling-else; NFC 2025-05-07 10:39:36 -04:00
Aaron Ballman
b59ab701e9
[C] Handle comma operator for implicit int->enum conversions (#138752)
In C++, the type of an enumerator is the type of the enumeration,
whereas in C, the type of the enumerator is 'int'. The type of a comma
operator is the type of the right-hand operand, which means you can get
an implicit conversion with this code in C but not in C++:
```
  enum E { Zero };
  enum E foo() {
    return ((void)0, Zero);
  }
```
We were previously incorrectly diagnosing this code as being
incompatible with C++ because the type of the paren expression would be
'int' there, whereas in C++ the type is 'E'.

So now we handle the comma operator with special logic when analyzing
implicit conversions in C. When analyzing the left-hand operand of a
comma operator, we do not need to check for that operand causing an
implicit conversion for the entire comma expression. So we only check
for that case with the right-hand operand.

This addresses a concern brought up post-commit:
https://github.com/llvm/llvm-project/pull/137658#issuecomment-2854525259
2025-05-07 10:05:00 -04:00
Yanzuo Liu
91f1830cb6
[Clang][Sema] Handle invalid variable template specialization whose type depends on itself (#134522) 2025-05-07 08:35:11 +08:00
Michael Spencer
32fb8c5f5f
[clang][modules] Lazily load by name lookups in module maps (#132853)
Instead of eagerly populating the `clang::ModuleMap` when looking up a
module by name, this patch changes `HeaderSearch` to only load the
modules that are actually used.

This introduces `ModuleMap::findOrLoadModule` which will load modules
from parsed but not loaded module maps. This cannot be used anywhere
that the module loading code calls into as it can create infinite
recursion.

This currently just reparses module maps when looking up a module by
header. This is fine as redeclarations are allowed from the same file,
but future patches will also make looking up a module by header lazy.

This patch changes the shadow.m test to use explicitly built modules and
`#import`. This test and the shadow feature are very brittle and do not
work in general. The test relied on pcm files being left behind by prior
failing clang invocations that were then reused by the last invocation.
If you clean the cache then the last invocation will always fail. This
is because the input module map and the `-fmodule-map-file=` module map
are parsed in the same module scope, and `-fmodule-map-file=` is
forwarded to implicit module builds. That means you are guaranteed to
hit a module redeclaration error if the TU actually imports the module
it is trying to shadow.

This patch changes when we load A2's module map to after the `A` module
has been loaded, which sets the `IsFromModuleFile` bit on `A`. This
means that A2's `A` is skipped entirely instead of creating a shadow
module, and we get textual inclusion. It is possible to construct a case
where this would happen before this patch too.

An upcoming patch in this series will rework shadowing to work in the
general case, but that's only possible once header -> module lookup is
lazy too.
2025-05-06 16:40:01 -07:00
cor3ntin
cf9b4d1e79
[Clang] Diagnose invalid function types in dependent contexts (#138731)
When forming an invalid function type, we were not diagnosing it if the
call was dependent.

However, we later rely on the function type to be sensible during
argument deduction.

We now diagnose anything that is not a potential function type, to avoid
constructing bogus call expressions.

Fixes #138657
Fixes #115725
Fixes #68852
2025-05-06 22:51:04 +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
Oliver Hunt
f1985d583d
[clang] Ensure type aware allocators handle transparent decl contexts (#138616)
We were testing the immediate DeclContext for found new and delete
operators, which is incorrect if the declarations are contained by a
transparent decl as can be induced with extern or export statements.
2025-05-05 22:39:17 -07: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
erichkeane
91867337ad [OpenACC] Fix infinite loop bug with the device_type checking
I noticed while writing a test that we ended up infinite looping thanks
to an early exit not correctly setting the next step of the loop.
2025-05-05 10:35:49 -07:00
cor3ntin
abd1057865
[Clang] Minimal support for availability attributes on partial specializations (#138426)
There are some limitations.

Because we only know which partial specialization to refer to when
instantiating, and because we can't instantiate the class before we
require a complete type, we can only use the partial specialization once
we have a complete class.

Similarly, because we don't know if a class is ever going to be
complete, we always warn on availability of the primary. Therefore, we
only warn for the partial specialization if we did not warn on the
primary.

I considered alternatives to address that second limitation:
 - Delay warnings to the end of the TU
 - Tracking where each availability attribute originally comes from.

However, both of these have drawbacks, and the use case is probably less
motivated than wanting to deprecate the use of a specific
specialization.

Fixes #44496
2025-05-05 19:31:36 +02:00
Aaron Ballman
e7e2042343
Fix crash with invalid VLA in a type trait (#138543)
Transforming an expression to a potentially evaluated expression can
fail. If it does so, no longer attempt to make the type trait
expression, instead return an error expression. This ensures we don't
try to compute the dependence for an invalid type.

Fixes #138444
2025-05-05 13:14:31 -04:00
cor3ntin
13926e1490
[Clang] Preserve CXXParenListInitExpr in TreeTransform. (#138518)
We were converting a CXXParenListInitExpr to a ParenListExpr in
TreeTransform.

However, ParenListExpr is typeless, so Clang could not rebuild the
correct initialization sequence in some contexts.

Fixes #72880
2025-05-05 18:46:40 +02: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
Hans Wennborg
a94e560958
Only emit -Wmicrosoft-goto in C++ mode (#138507)
Follow-up to #138009 which added diagnostics for "jump past
initialization" in C mode, in which case they're not an MS extension.
2025-05-05 13:40:31 +02:00
Aaron Ballman
5f24ae9251
[C] Update -Wimplicit-void-ptr-cast for null pointer constants (#138271)
Null pointer constants require a bit of extra effort to handle in C.

Fixes #138145
2025-05-05 07:06:20 -04:00
Kazu Hirata
1812a43a22
[clang] Remove unused local variables (NFC) (#138468) 2025-05-04 14:14:23 -07:00