79437 Commits

Author SHA1 Message Date
Oleksandr T.
bfdeb58730
[Clang] use constant evaluation context for constexpr if conditions (#123667)
Fixes #123524

---

This PR addresses the issue of immediate function expressions not
properly evaluated in `constexpr` if conditions. Adding the
`ConstantEvaluated` context for expressions in `constexpr` if statements
ensures that these expressions are treated as manifestly
constant-evaluated and parsed correctly.
2025-03-10 01:53:20 +02:00
Ryosuke Niwa
5c3b05996f
[alpha.webkit.UnretainedLambdaCapturesChecker] Add a WebKit checker for lambda capturing NS or CF types. (#128651)
Add a new WebKit checker for checking that lambda captures of CF types
use RetainPtr either when ARC is disabled or enabled, and those of NS
types use RetainPtr when ARC is disabled.
2025-03-09 14:59:46 -07:00
Chris B
e85e29c299
[HLSL] select scalar overloads for vector conditions (#129396)
This PR adds scalar/vector overloads for vector conditions to the
`select` builtin, and updates the sema checking and codegen to allow
scalars to extend to vectors.

Fixes #126570
2025-03-09 16:01:12 -05:00
Chris B
a7d5b3f711
[HLSL] Disallow virtual inheritance and functions (#127346)
This PR disallows virtual inheritance and virtual functions in HLSL.
2025-03-09 12:18:44 -05:00
Timm Baeder
0f732481ac
[clang][bytecode] Fix getting pointer element type in __builtin_memcmp (#130485)
When such a pointer is heap allocated, the type we get is a pointer
type. Take the pointee type in that case.
2025-03-09 12:57:42 +01:00
Timm Baeder
227f9544a4
[clang][bytecode][NFC] Bail out on non constant evaluated builtins (#130431)
If the ASTContext says so, don't bother trying to constant evaluate the
given builtin.
2025-03-09 06:35:19 +01:00
Liberty
c4ed0ad1f5
[Clang] Fix typo 'dereferencable' to 'dereferenceable' (#116761)
This patch corrects the typo 'dereferencable' to 'dereferenceable' in
CGCall.cpp.
The typo is located within a comment inside the `void
CodeGenModule::ConstructAttributeList` function.
2025-03-08 19:35:20 +00:00
Timm Baeder
aff6ab9d90
[clang][bytecode] Surround bcp condition with Start/EndSpeculation (#130427)
This is similar to what the current interpreter is doing - the
FoldConstant RAII object surrounds the entire HandleConditionalOperator
call, which means the condition and both TrueExpr or FalseExpr.
2025-03-08 19:37:20 +01:00
Timm Baeder
e4fe22a8bd
[clang][bytecode][NFC] Check conditional op condition for ConstantExprs (#130425)
Same thing we now do in if statements. Check the condition of a
conditional operator for a statically known true/false value.
2025-03-08 18:29:19 +01:00
Timm Baeder
46d218d1af
[clang][bytecode] Implement __builtin_{memchr,strchr,char_memchr} (#130420)
llvm has recently started to use `__builitn_memchr` at compile time, so
implement this. Still needs some work but the basics are done.
2025-03-08 16:52:06 +01:00
Timm Baeder
3b8f9a228c
[clang][bytecode] Loosen assertion This() for array elements (#130399)
getRecord() returns null on array elements, even for composite arrays.
The assertion here was overly restrictive and having an array element as
instance pointer should be fine otherwise.
2025-03-08 13:13:52 +01:00
Timm Baeder
d08cf7900d
[clang][bytecode] Implement __builtin_constant_p (#130143)
Use the regular code paths for interpreting.

Add new instructions: `StartSpeculation` will reset the diagnostics
pointers to `nullptr`, which will keep us from reporting any diagnostics
during speculation. `EndSpeculation` will undo this.

The rest depends on what `Emitter` we use.

For `EvalEmitter`, we have no bytecode, so we implement `speculate()` by
simply visiting the first argument of `__builtin_constant_p`. If the
evaluation fails, we push a `0` on the stack, otherwise a `1`.

For `ByteCodeEmitter`, add another instrucion called `BCP`, that
interprets all the instructions following it until the next
`EndSpeculation` instruction. If any of those instructions fails, we
jump to the `EndLabel`, which brings us right before the
`EndSpeculation`. We then push the result on the stack.
2025-03-08 06:06:14 +01:00
Chris B
0ea52234fc
[DXC] Add -metal flag to DXC driver (#130173)
This adds a flag to the DXC driver to enable calling the metal shader
converter if it is available to convert the final shader output for
metal.
2025-03-07 17:28:41 -06:00
Ziqing Luo
b2563028cf
[-Wunsafe-buffer-usage] Add alloc_size knowledge to the 2-param span constructor warning (#114894)
We can take advantage of the attribute `alloc_size`.  For example, 
```
void * malloc(size_t size) __attribute__((alloc_size(1)));

std::span<char>{(char *)malloc(x), x}; // this is safe
```

rdar://136634730
2025-03-07 15:05:20 -08:00
Ryosuke Niwa
c419acdf82
[alpha.webkit.UncountedCallArgsChecker] Recognize CXXUnresolvedConstructExpr as a safe origin. (#130258)
Handle CXXUnresolvedConstructExpr in tryToFindPtrOrigin so that
constructing Ref, RefPtr, CheckedRef, CheckedPtr, ... constructed in
such a way that its type is unresolved at AST level will be still
treated as a safe pointer origin.

Also fix a bug in isPtrOfType that it was not recognizing
DeducedTemplateSpecializationType.
2025-03-07 14:40:33 -08:00
Aaron Ballman
b19ed9c043
[C2y] Implement WG14 N3409 (#130299)
This paper removes UB around use of void expressions. Previously, code
like this had undefined behavior:
```
  void foo(void) {
    (void)(void)1;
    extern void x;
    x;
  }
```
and this is now well-defined in C2y. Functionally, this now means that
it is valid to use `void` as a `_Generic` association.
2025-03-07 14:46:29 -05:00
Andy Kaylor
8eb9b947af
[CIR] Emit init of local variables (#130164)
Local variable initialization was previously being ignored. This change
adds support for initialization of scalar variables with constant values
and introduces the constant emitter framework.
2025-03-07 10:23:06 -08:00
Joseph Huber
3ed4daf9a7 [Clang] Add missing printer for vector type attribute 2025-03-07 10:39:09 -06:00
Timm Baeder
d6a4828c8a
[clang][bytecode] Special-case ConstantExpr in if conditions (#130294)
This happens a lot with `if constexpr` with a condition based on a
template param. In those cases, the condition is a ConstantExpr with a
value already set, so we can use that and ignore the other branch.
2025-03-07 17:35:06 +01:00
Joseph Huber
0a41fb71f1
[Clang] Treat ext_vector_type as a regular type attribute (#130177)
Summary:
This attribute is mostly borrowed from OpenCL, but is useful in general
for accessing the LLVM vector types. Previously the only way to use it
was through typedefs. This patch changes that to allow use as a regular
type attribute, similar to address spaces.
2025-03-07 10:09:06 -06:00
erichkeane
67960e5c08 [OpenACC] Ensure decl OpenACC constructs don't crash
I initially implemented codegen to be a 'no-op' for these declarations,
  which I thought was properly implemented.  However, when they are a
  top-level decl, we have a separate switch.  This patch makes sure they
  are properly emitted at top-level as a no-op, and adds a test for both
  top-level and not top-level.
2025-03-07 08:05:19 -08:00
erichkeane
1c1140c4cf [OpenACC] Enable 'device_type' for 'routine'
There is a slightly different list for routine on which clauses are
permitted after it (like the rest of the constructs), but this
implements and tests them to make sure we get them right.
2025-03-07 06:49:29 -08:00
Aaron Ballman
9fc3310798
[C2y] Implement WG14 N3411 (#130180)
This paper (https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3411.pdf)
allows a source file to end without a newline. Clang has supported this
as a conforming extension for a long time, so this suppresses the
diagnotic in C2y mode but continues to diagnose as an extension in
earlier language modes. It also continues to diagnose if the user passes
-Wnewline-eof explicitly.
2025-03-07 08:34:22 -05:00
Jan Voung
81168e2dc1
[clang][dataflow] Add test for crash repro and clean up const accessor handling (#129930)
Add test for https://github.com/llvm/llvm-project/issues/125589

The crash is actually incidentally fixed by
https://github.com/llvm/llvm-project/pull/128437 since it added a branch
for the reference case and would no longer fall through when the return
type is a reference to a pointer.

Clean up a bit as well:
- make the fallback for early returns more consistent (check if
  returning optional and call transfer function for that case)
- check RecordLoc == nullptr in one place
- clean up extra spaces in test
- clean up parameterization in test of `std::` vs `$ns::$`
2025-03-07 08:16:46 -05:00
Michael Jabbour
180f8032f0
[clang] Fix ASTWriter crash after merging named enums (#114240)
Clang already removes parsed enumerators when merging typedefs to
anonymous enums. This is why the following example decl used to be
handled correctly while merging, and ASTWriter behaves as expected:

```c
typedef enum { Val } AnonEnum;
```
However, the mentioned mechanism didn't handle named enums. This leads
to stale declarations in `IdResolver`, causing an assertion violation in
ASTWriter ``Assertion `DeclIDs.contains(D) && "Declaration not
emitted!"' failed`` when a module is being serialized with the following
example merged enums:

```c
typedef enum Enum1 { Val_A } Enum1;
enum Enum2 { Val_B };
```

The PR applies the same mechanism in the named enums case.

Additionally, I dropped the call to
`getLexicalDeclContext()->removeDecl` as it was causing a wrong
odr-violation diagnostic with anonymous enums.

Might be easier to to review commit by commit. Any feedback is
appreciated.

### Context

This fixes frontend crashes that were encountered when certain
Objective-C modules are included on Xcode 16. For example, by running
`CC=/path/to/clang-19 xcodebuild clean build` on a project that contains
the following Objective-C file:

```c
#include <os/atomic.h>

int main() {
  return memory_order_relaxed;
}
```
This crashes the parser in release, when ASTReader tries to load the
enumerator declaration.
2025-03-07 11:02:19 +01:00
zhaohui
3a67c7c6f7
[Clang] Check for uninitialized use in lambda within CXXOperatorCallExpr (#129198)
Track whether a LambdaExpr is an immediate operand of a
CXXOperatorCallExpr using a new flag, isInCXXOperatorCall. This enables
special handling of capture initializations to detect uninitialized
variable uses, such as in `S s = [&]() { return s; }();`.

Fix #128058
2025-03-07 16:01:23 +08:00
Yanzuo Liu
d933882ed3
[Clang] Add test for CWG2285 "Issues with structured bindings" (#126421)
The resolution of [CWG2285](https://wg21.link/cwg2285) adds the point of
declaration of a structured binding, and was implemented in
bdb84f374c
.

Drive-by changes: modify comment and diagnostic messages mentioned in
CWG2285.
2025-03-07 07:16:51 +01:00
Owen Pan
3664b4e2d5
[clang-format] Remove special handling of C++ access specifiers in C (#129983)
This effectively reverts d1aed486efc6d35a81ca4acbabb4203c4b91cda9
because of
#129426.
2025-03-06 21:08:25 -08:00
Yaxun (Sam) Liu
2674034422
Reland "[HIP] Use original file path for CUID" (#111885)
This patch fixes the buildbots failure of lit tests on MacOS. Since
clang driver options depend on toolchain, we cannot hardcode CUID hash.
On MacOS there is an extra -mlinker-version= option.
2025-03-06 22:46:41 -05:00
Kito Cheng
55f86cf023
[RISCV][clang] Fix wrong VLS CC detection (#130107)
RISCVABIInfo::detectVLSCCEligibleStruct should early exit if VLS calling
convention is not used, however the sentinel value was not set to
correctly, it should be zero instead of one.
2025-03-07 11:15:20 +08:00
Joseph Huber
d5cef39d74
[Clang] Make '-Wglobal-constructors` work on the GNU attributes (#129917)
Summary:
The `-Wglobal-constructors` option is useful for restricting the usage
of global constructors / destructors. However, it currently ignores the
attributes that introduce global constructors, meaning that the module
can still have ctors if `-Werror` is set. If this is intentional by the
user, I believe it would be more correct to push the diagnostic.
2025-03-06 20:43:42 -06:00
Joseph Huber
ca39a029b6
[Clang] Fix 'gpuintrin.h' match when included with no arch set (#129927)
Summary:
These require `+ptx` features to be set even though they're guarded by
the `__nvvm_reflect`. Rather than figure out how to hack around that
with the `target` attribute I'm just going to disable it for 'generic'
builds and use the slow version for now.
2025-03-06 19:37:39 -06:00
Matt Arsenault
3304a430f2
clang/HIP: Do not call ocml in scalbln implementations (#129639)
I do not understand why this was calling the float version with
an implicit cast from the long. Just clamp to the bounds of int,
and use the generic ldexp (this is also how musl does it, except
scalbnf is the base implementation there).

Somehow INT_MIN was also not defined, so deal with that.
2025-03-07 07:55:26 +07:00
Tom Honermann
813325e5d4
[Clang][NFC] Change uses of getAs() to castAs() where the target type is assured. (#130188)
Static analysis identified two uses of `getAs()` for which the result
pointer was unconditionally dereferenced. Source code inspection
confirmed that the target type is assured by prior checks. This change
replaces these uses of `getAs()` with `castAs()`.

The first case, in `clang/lib/CodeGen/Targets/AArch64.cpp`, performs a
cast to `BuiltinType` following a check for `isBuiltinType()`.

The second case, in `clang/lib/Sema/SemaTemplateVariadic.cpp`, performs
a cast to `PackExpansionType` on the result of a call to `getAsType()`
on an object of type `TemplateArgument` following confirmation that
`isPackExpansion()` returned true and that `getKind()` returned
`TemplateArgument::Type`. Inspection of `isPackExpansion()` revealed
that it only returns true when the template argument kind is
`TemplateArgument::Type` if `isa<PackExpansionType>(getAsType())` is
true.
2025-03-06 18:12:20 -05:00
Oleksandr T.
93f0f3d33b
[Clang] add -Wshift-bool warning to handle shifting of bool (#127336)
Fixes #28334

--- 

This PR introduces the `-Wshift-bool` warning to detect and warn against
shifting `bool` values using the `>>` operator. Shifting a `bool`
implicitly converts it to an `int`, which can lead to unintended
behavior.
2025-03-07 01:05:56 +02:00
Sam Elliott
3492245ac0
[RISCV] QCI Interrupt Support (#129957)
This change adds support for `qci-nest` and `qci-nonest` interrupt
attribute values. Both of these are machine-mode interrupts, which use
instructions in Xqciint to push and pop A- and T-registers (and a few
others) from the stack.

In particular:
- `qci-nonest` uses `qc.c.mienter` to save registers at the start of the
function, and uses `qc.c.mileaveret` to restore those registers and
return from the interrupt.
- `qci-nest` uses `qc.c.mienter.nest` to save registers at the start of
the function, and uses `qc.c.mileaveret` to restore those registers and
return from the interrupt.
- `qc.c.mienter` and `qc.c.mienter.nest` both push registers ra, s0
(fp), t0-t6, and a0-a10 onto the stack (as well as some CSRs for the
interrupt context). The difference between these is that
`qc.c.mienter.nest` re-enables M-mode interrupts.
- `qc.c.mileaveret` will restore the registers that were saved by
`qc.c.mienter(.nest)`, and return from the interrupt.

These work for both standard M-mode interrupts and the non-maskable
interrupt CSRs added by Xqciint.

The `qc.c.mienter`, `qc.c.mienter.nest` and `qc.c.mileaveret`
instructions are compatible with push and pop instructions, in as much
as they (mostly) only spill the A- and T-registers, so we can use the
`Zcmp` or `Xqccmp` instructions to spill the S-registers. This
combination (`qci-(no)nest` and `Xqccmp`/`Zcmp`) is not implemented in
this change.

The `qc.c.mienter(.nest)` instructions have a specific register storage
order so they preserve the frame pointer convention linked list past the
current interrupt handler and into the interrupted code and frames if
frame pointers are enabled.

Co-authored-by: Pankaj Gode <quic_pgode@quicinc.com>
2025-03-06 13:31:08 -08:00
erichkeane
7d8da04c26 [OpenACC] Implement 'nohost' construct AST/Sema
'nohost' is only valid on routine, and states that the compiler
shouldn't compile this routine for the host. It has no arguments, so no
checking is required besides putting it in the AST.
2025-03-06 12:50:49 -08:00
erichkeane
1b75b9e665 [OpenACC] Handle sema for gang, worker, vector, seq clauses on routine
These 4 clauses are mutually exclusive, AND require at least one of
them. Additionally, gang has some additional restrictions in that only
the 'dim' specifier is permitted. This patch implements all of this, and
ends up refactoring the handling of each of these clauses for
readabililty.
2025-03-06 11:53:46 -08:00
Michael Liao
d01a06b844 [clang][test] Fix shared build after Module Triple change. NFC 2025-03-06 13:20:06 -05:00
Morris Hafner
710de09f17
[CIR] Upstream global variable linkage types (#129072)
This change implements variable linkage types in ClangIR except for
common linkage which requires Comdat support.

---------

Co-authored-by: Morris Hafner <mhafner@nvidia.com>
Co-authored-by: Henrich Lauko <xlauko@mail.muni.cz>
2025-03-06 10:17:58 -08:00
Matheus Izvekov
a24523ac8d
[clang] Implement instantiation context note for checking template parameters (#126088)
Instead of manually adding a note pointing to the relevant template
parameter to every relevant error, which is very easy to miss, this
patch adds a new instantiation context note, so that this can work using
RAII magic.

This fixes a bunch of places where these notes were missing, and is more
future-proof.

Some diagnostics are reworked to make better use of this note:
- Errors about missing template arguments now refer to the parameter
which is missing an argument.
- Template Template parameter mismatches now refer to template
parameters as parameters instead of arguments.

It's likely this will add the note to some diagnostics where the
parameter is not super relevant, but this can be reworked with time and
the decrease in maintenance burden makes up for it.

This bypasses the templight dumper for the new context entry, as the
tests are very hard to update.

This depends on #125453, which is needed to avoid losing the context
note for errors occuring during template argument deduction.
2025-03-06 14:58:42 -03:00
Amr Hesham
9ecb0f58eb
[Clang][diagnostics] Improve the diagnostics for chained comparisons (#129285)
Improve the diagnostics for chained comparisons to report actual
expressions and operators

Fixes #129069
2025-03-06 18:55:18 +01:00
apple-fcloutier
c628e8e9ea
[clang] Fix FP -Wformat in functions with 2+ attribute((format)) (#129954)
When defining functions with two or more format attributes, if the
format strings don't have the same format family, there is a false
positive warning that the incorrect kind of format string is being
passed at forwarded format string call sites.

This happens because we check that the format string family of each
format attribute is compatible before we check that we're using the
associated format parameter. The fix is to move the check down one
scope, after we've established that we are checking the right parameter.

Tests are updated to include a true negative and a true positive of this
situation.
2025-03-06 09:12:22 -08:00
erichkeane
93b022944c [OpenACC] fix 'loop' restriction of auto/seq/independent
We previously allowed duplicates of auto/seq/independent on a 'loop'
construct. This is disallowed by the restriction (which says exactly one
    of...), so this patch ensures they are disallowed.
2025-03-06 08:55:32 -08:00
Matt Arsenault
bfea84946d
clang: Hack around opencl enqueue_block using wrong ABI for aggregrate (#130011)
EmitAggExprToLValue started wrapping the temporary alloca in an
addrspacecast
at some point. We take the direct type from this as the pointer argument
for the
runtime function type, but this isn't correct. Technically, we should be
querying
the target's ABI for what IR to produce for this sequence. The
assumption seems to
always have been that this will be indirectly passed with byval (or
byref).

I started working on a patch to go through the ABI handling, but it
seems to
require more time and/or clang expertise than I have at the moment.
2025-03-06 23:13:28 +07:00
erichkeane
5bb112feae [NFC][OpenACC] Fix unused variable warning from df1e102e
Looks like I did a dyn_cast when all I needed was an isa! This patch
fixes that up.
2025-03-06 07:08:00 -08:00
erichkeane
df1e102e2a [OpenACC] implement AST/Sema for 'routine' construct with argument
The 'routine' construct has two forms, one which takes the name of a
function that it applies to, and another where it implicitly figures it
out based on the next declaration. This patch implements the former with
the required restrictions on the name and the function-static-variables
as specified.

What has not been implemented is any clauses for this, any of the A.3.4
warnings, or the other form.
2025-03-06 06:42:17 -08:00
Matt Arsenault
4703f8b661
clang/HIP: Use generic builtins for f32 exp and log (#129638)
Stop using ocml declarations which are just a shim around the
intrinsics.
2025-03-06 21:40:48 +07:00
Oleksandr T.
09e0100017
[Clang] use parameter location for abbreviated function templates (#129139)
Fixes #46386

--- 

When an abbreviated function template appears in an `extern "C"` block
and all template parameters are invented,
`TemplateParams->getTemplateLoc()` becomes invalid, leading to an
incorrect error location. These changes ensure that the error points to
the parameter's location when the template location is invalid.
2025-03-06 15:12:53 +02:00
Zahira Ammarguellat
7358973df1
[CLANG-CL] Remove the 'static' specifier for _FUNCTION_ in MSVC mode. (#128184)
The macro `FUNCTION` is returning the `static` specifier for static
templated functions. It's not the case for MSVC.
See https://godbolt.org/z/KnhWhqs47
Clang-cl is returning:
`__FUNCTION__ static inner::C<class A>::f`
`__FUNCTION__ static inner::C<class A>::f`
for the reproducer.
2025-03-06 07:51:45 -05:00