961 Commits

Author SHA1 Message Date
Timm Baeder
36cb9894b3
[clang][bytecode] Only reject function types in Pointer::toRValue() (#180722)
No test because I'm not sure how to reproduce this, but this patch fixes
`CodeGen/ptrauth-qualifier-function.c`.

For function pointer types and function reference types, we use
`Pointer`s these days, so we _can_ return them.
2026-02-10 13:11:45 +01:00
Timm Baeder
af74bc96c2
[clang][bytecode] Improve rejecting UnaryExprOrTypeTraitExprs (#180710)
Some of them work just fine, even if the expression contains errors.
2026-02-10 11:44:26 +01:00
Timm Baeder
3230de5e65
[clang][bytecode] Fix assertion failure when returning function type (#180681)
... as an rvalue. Which can't work, so reject.
2026-02-10 10:54:45 +01:00
Timm Baeder
7f1907cea0
[clang][bytecode] Allow bool sizes in array new expressions (#180696)
Looks like this is a thing, so allow them.
2026-02-10 09:27:17 +01:00
Timm Baeder
8ab034fa15
[clang][bytecode] Handle invalid UnaryExprOrTypeTraitExprs (#180692) 2026-02-10 09:24:11 +01:00
Timm Baeder
a1da10689c
[clang][bytecode] Fix non-initializing __builtin_shufflevector (#180691)
Create a local temporary we can use as destination.
2026-02-10 09:15:32 +01:00
Timm Baeder
0f8d8dc5b9
[clang][bytecode] Reject composite copies on primitive pointers (#180683)
This should fail.
2026-02-10 08:45:11 +01:00
Timm Baeder
1cf62af0ee
[clang][bytecode] Don't call InterpFrame::getThis() on the bottom frame (#180682)
This happens when we're in checkingPotentialConstantExpression() and we
try to evaluate a delete expression.
2026-02-10 08:43:25 +01:00
Timm Baeder
461e433718
[clang][bytecode] Don't use trunc() to increase APInt bitWidth (#180536)
`FieldDecl::getBitWidthValue()` can return a value higher than the type
size of the bit field. We need to account for that.
2026-02-10 07:53:23 +01:00
Timm Baeder
6dc9a484fd
[clang][bytecode] Handle missing target label in break statement (#180532)
Happens in error cases.
2026-02-10 06:55:10 +01:00
Timm Baeder
a1acc4ab9f
[clang][bytecode] Fix discarded addrof operators... (#180534)
... of member pointers.
2026-02-10 06:00:58 +01:00
Timm Baeder
fdd9555fc0
[clang][bytecode] Fix discarded Mulc/DivC opcodes (#180537)
We need to pop the pointer in that case.
2026-02-10 05:46:16 +01:00
Timm Baeder
1c4a98f768
[clang][bytecode] Fix discarded builtin_complex (#180539)
This is the only builtin that returns a non-primitive type I think.
2026-02-10 05:21:00 +01:00
Timm Baeder
3cd0bb7f67
[clang][bytecode] Check evaluate{String,Strlen} for pointer type (#180524)
We can only use block pointers here.
2026-02-09 16:02:53 +01:00
Timm Baeder
4344268ad6
[clang] Return std::optional from all Expr::tryEvaluate* API (#179230)
tryEvaluateString was returning an std::optional, but the other try* API
was not. Update tryEvaluateObjectSize and tryEvaluateStrLen to return an
std::optional<uint64_t>.
2026-02-09 13:37:14 +01:00
Timm Baeder
f71e321966
[clang][bytecode] Implement constexpr step limit (#176150)
This only calls `noteStep()` on jump opcodes, so this works for loops.
It does not prevent "hangs" when a function is just _very_ long (could
be interesting how this interfaces with expand statements?).

Fixes https://github.com/llvm/llvm-project/issues/165951
2026-02-09 12:22:48 +01:00
Timm Baeder
43358cb3d6
[clang][bytecode] Check pointer lifetime in CheckDestructor (#179957)
So we diagnose double-destroy scenarios.
2026-02-06 07:06:18 +01:00
Yanzuo Liu
ddda8d7d25
[clang][bytecode] Fix reading union template parameter object (#179899)
Before this patch, reading union template parameter object will trigger
diagnostics saying it's not initialized. This patch fixes this issue.

Reading union template parameter with no active fields, class type
fields, or bit-fields is handled as a drive-by.

AI usage: The implementation was generated by AI and modified by me
afterwards.

Assisted-by: GPT-5.2

---------

Co-authored-by: Timm Baeder <tbaeder@redhat.com>
2026-02-05 16:37:58 +01:00
Timm Baeder
4366324c89
[clang][bytecode] Improve __builtin_object_size handling (#179271)
This fixes a few more tests from `pass-object-size.c`, but we still
can't enable the entire file.
2026-02-05 08:32:50 +01:00
Timm Baeder
9d944c0ed2
[clang][bytecode] Visit 2nd atomic_is_lock_free arg as lvalue (#179673) 2026-02-05 07:12:55 +01:00
Serosh
7c64723f34
[clang][bytecode] Fix stack corruption in pointer arithmetic discard (#176555)
The bytecode compiler was ignoring the DiscardResult flag in 
VisitPointerArithBinOp
, causing pointer addition and subtraction results to persist on the
stack when they should have been popped (e.g., in comma expressions).
This led to stack corruption and assertion failures in subsequent
operations that encountered an unexpected pointer on the stack.

This patch refactors the unified addition/subtraction logic to ensure
the result is properly popped when DiscardResult is true.

Fixes #176549
2026-02-04 20:18:55 -08:00
Timm Baeder
5da7e9a2ad
[clang][bytecode] Return Invalid() from atomic_is_lock_free calls (#179676)
If they are invalid. This is what the current interpreter does.
2026-02-04 16:11:13 +01:00
woruyu
f541056513
[Clang] Fixes builtin_bswapg builtin for bool type (#179177)
This PR resolves #178317, the related PR: #169285
2026-02-04 13:49:45 +00:00
Timm Baeder
d26b035d69
[clang][bytecode] Remove an incorrect assertion (#179644)
There are situations where DiscardResult is set, but we still wish to
emit a float value, e.g. when we will discard the result of an operation
involving such a float value.
2026-02-04 14:30:10 +01:00
Timm Baeder
1da316985c
[clang][bytecode] Reject void ArraySubscriptExprs (#179619)
This happens in C when we subscript an expression of type void*, but
there's nothing for us to do here. Just reject it early.

Fixes https://github.com/llvm/llvm-project/issues/177758
2026-02-04 13:24:26 +00:00
Timm Baeder
890cdbe653
[clang][bytecode] Handle a null record better (#179645)
This would otherwise later assert in vsitZeroRecordInitializer().
2026-02-04 14:17:11 +01:00
Timm Baeder
cd31effb0a
[clang][bytecode] Reject invalid CXXNewExprs (#179629)
If they contain errors, we can't rely on any of their API returning sane
values.
2026-02-04 13:55:50 +01:00
Timm Baeder
a8af0901dd
[clang][bytecode] Don't call getOffset on non-block pointers (#179628)
Fixes https://github.com/llvm/llvm-project/issues/177587
2026-02-04 12:39:21 +01:00
Timm Baeder
87e38d3b97
[clang][bytecode][NFC] Add Pointer::canDeref (#179618) 2026-02-04 09:18:23 +01:00
puneeth_aditya_5656
85c5029917
[clang][bytecode] Fix crash when dereferencing cast to larger type (#179030)
## Summary
When dereferencing a pointer that was `reinterpret_cast` to a larger
type (e.g. `*(int**)""`), the bytecode interpreter would crash with an
assertion failure because it tried to read more bytes than the
allocation contained.

## Changes
- Add a size check in `Pointer::toRValue()` before calling `deref<T>()`
to ensure the allocation is large enough
- If the allocation is too small, return `std::nullopt` to gracefully
fail the constant evaluation instead of crashing
- Add regression test

Fixes #179015
2026-02-04 07:48:18 +01:00
puneeth_aditya_5656
5b80848f06
[clang][bytecode] Fix crash on __builtin_infer_alloc_token with struct argument (#178936)
## Summary
- Fix crash when passing non primitive types (structs) to
`__builtin_infer_alloc_token`
- The bytecode interpreter's discard loop dereferenced an empty
`OptPrimType` for non primitive arguments

## Test plan
- Added regression test in `clang/test/SemaCXX/alloc-token.cpp`
- Existing tests continue to pass

Fixes #178892
2026-02-02 17:15:46 +01:00
Timm Baeder
d368773249
[clang][bytecode] Use in Expr::tryEvaluateObjectSize() (#179197)
This is like https://github.com/llvm/llvm-project/pull/179033, which
broke a few builders for reasons I still don't really understand. I ran
the other clang tests and this version fixes a few of the introduced
regressions.

This still regresses `CodeGen/pass-object-size.c`, but that's a
pre-existing issue.

Patch is of coursed based on #179033 by @mariusdr.
2026-02-02 14:03:13 +01:00
Timm Baeder
136bbde262
[clang][ExprConst] Move shared EvalInfo state into interp::State (#177738)
Instead of having `InterpState` call into its parent `EvalInfo`, just
save the state in `interp::State`, where both subclasses can access it.
2026-02-02 09:00:10 +01:00
marius doerner
8e7a2d8b42
Revert "[clang][bytecode] Use in Expr::tryEvaluateObjectSize (#1790… (#179099)
…33)"

This reverts commit 756c321c33af2be0bd40707948aae3c06163a0a6.

Test failure in clang/test/AST/ByteCode/builtins.c in CI build

CC @tbaederr
2026-02-01 12:34:09 +01:00
marius doerner
756c321c33
[clang][bytecode] Use in Expr::tryEvaluateObjectSize (#179033)
Fixes #138474

Use new bytecode intepreter in `Expr::tryEvaluateObjectSize`. Reuses the
already existing implementation for `__builtin_object_size` in of the
intepreter.

---------

Co-authored-by: Timm Baeder <tbaeder@redhat.com>
2026-02-01 10:08:42 +01:00
Timm Baeder
68850427ba
[clang][bytecode] Reject CK_BitCast nodes with errors early (#179087)
Fixes https://github.com/llvm/llvm-project/issues/179020
2026-02-01 10:08:06 +01:00
puneeth_aditya_5656
5c213a9000
[clang][bytecode] Fix crash on __builtin_align_up with one-past-end pointers (#178652)
## Summary
Fix assertion failure when evaluating
`__builtin_align_up`/`__builtin_align_down`/`__builtin_is_aligned` with
one-past-end pointers like `&array[size]`.

## Root Cause
`getIndex()` calls `getOffset()` which asserts when `Offset ==
PastEndMark`. This happens for one-past-end element pointers.

## Fix
Check `isElementPastEnd()` before calling `getIndex()`. For past-end
pointers, use `getNumElems()` instead which gives the correct index
value.

## Test
Added test cases in `builtin-align-cxx.cpp` for one-past-end pointer
alignment.

Fixes #178647
2026-01-31 16:34:53 +01:00
Timm Baeder
afb2e4f2e2
[clang][bytecode] Clean up interp::Function parameter handling (#178621)
Replace the multiple data structures with a vector + a map holding all
`ParamDescriptor`s. Update docs.
2026-01-30 10:07:23 +01:00
Timm Baeder
deafb6b105
[clang][bytecode][NFC] Use Block::deref() in EvalEmitter (#178630)
Instead of doing the casting around `Block::data()` ourselves.
2026-01-29 13:26:40 +01:00
Timm Baeder
0d562c9668
[clang][bytecode] Only check static lambda captures if we have to (#178452)
Call `getCaptureFields()` only if the function is static, because we
only care about the captures in that case.
2026-01-29 06:16:25 +01:00
byteforge
e72c4fc734
[clang][constexpr] Move inf/nan/denormal handling into FP binop callbacks (#178421)
Update the callback signature for `EvaluateFpBinOpExpr` and
`interp__builtin_elementwise_fp_binop` to return
`std::optional<APFloat>`, allowing individual callbacks to decide
whether to handle special floating-point cases (inf/nan/denormal).

Previously, the helper functions had hardcoded validation that forced
all callbacks to reject these cases. This blocked intrinsics needing
custom validation (e.g., rounding mode checks). Now each callback
controls its own validation and returns `std::nullopt` when the fold is
invalid.

Fixes #178416
2026-01-28 17:07:14 +00:00
Timm Baeder
7c3a2113fe
[clang][ExprConst] Fix rendering of explicit this parameters (#177551)
in compile-time backtraces.

The two test cases used to be rendered as `foo(s, 0)` and `foo2(s)`.
2026-01-28 12:59:24 +01:00
Timm Baeder
14cd8f0fba
[clang][bytecode][NFC] Clean up InterpState includes (#178130) 2026-01-27 10:17:04 +01:00
Timm Baeder
d9332389b3
[clang][bytecode][NFC] Make InterpState::Allocator private (#178129) 2026-01-27 09:41:21 +01:00
Serosh
60250aaa25
[clang][bytecode] Fix crash in void functions returning non-void expr… (#176550)
The bytecode compiler was incorrectly emitting an 
RVOPtr
opcode for void functions if the return expression had a non-void type
(e.g. from a conditional operator). This triggered an assertion in the
interpreter because void functions lack RVO metadata.

This patch updates 
visitReturnStmt
 to check the function’s return type and use 
discard()
 for the expression in void contexts, preventing erroneous RVO pathing.

Fixes #176536
2026-01-27 07:18:22 +01:00
Ayush Kumar Gaur
a150e8081a
[clang][bytecode] Avoid crash in constexpr wcslen on invalid argument… (#177891)
### What the problem ?

Fix a clang bytecode constant interpreter crash when evaluating
wcslen("x") in a constant-expression path.

Previously we asserted on wchar element size mismatch and crashed in
assertion builds.

### Why it happened

The expression is already ill-typed (char[2] → const wchar_t*), clang
correctly emits a diagnostic, but later ICE checking tries to fold the
expression and reaches the builtin handler. That path must not crash.

### whats the Fix

Replace the assert(ElemSize == wchar_t size) with a graceful failure
(return false / emit appropriate diagnostic) when element sizes don’t
match, so evaluation stops safely.

### how to Test it

Add a regression test that triggers ICE checking with
-fexperimental-new-constant-interpreter and ensures clang does not crash
(verifies it reports the type error / static_assert failure instead).

Fixes #177844.

<img width="1366" height="768" alt="Screenshot_2026-01-25_22_07_50"
src="https://github.com/user-attachments/assets/488dd0b0-3c17-4f45-b2d0-9339b8fd3409"
/>

where wclen_crash.c is 

`
#include <wchar.h>

static_assert(wcslen("x") == 'x');
int main() {}
`

---------

Co-authored-by: Timm Baeder <tbaeder@redhat.com>
2026-01-26 17:01:02 +01:00
Timm Baeder
cf19b6cdf0
[clang][bytecode] Relax assertions in Pointer::{begin,end}Lifetime (#177906)
We could get here for array roots, for which the assertion doesn't
necessarily hold.
2026-01-26 14:12:10 +01:00
flovent
a9b7b4d957
[clang][bytecode] Fix crash caused by overflow of Casting float number to integer (#177815)
Before this PR evaluation process will stop immediately regradless of
whether it's set to handle overflow,

this will prevent us getting value from stack, which leads to crash(with
or without assertion).

Closes  #177751.
2026-01-26 07:54:22 +01:00
FYLGQ
e68eadf849
[clang][bytecode] Fix crash on discarded complex comparison (#177731)
Fixes llvm#176902: [clang][bytecode] crashes on ill-formed
_Static_assert comparing complex value

This patch resolves a crash in Clang's constant evaluation when handling
complex number comparisons in discarded expressions, such as those
involving short-circuiting logical operators. The crash occurred due to
unnecessary evaluation of the comparison in the experimental constant
interpreter.

The issue was originally observed and minimized in the following
example:

```cpp
#define EVAL(a, b) _Static_assert(a == b, "")

void foo() {
  EVAL(; + 0, 1i);
}
```

This patch ensures that such comparisons are handled correctly without
triggering assertions when the result is discarded.

Tests

A regression test has been added to verify that complex comparisons in
discarded expressions no longer cause crashes during constant
evaluation.

Local verification:


llvm-project-main/clang/lib/AST/ByteCode/constant-eval-complex-discard.c
passes.

Full ninja check-clang may fail locally due to a known GCC ICE when
building Clang unittests; this is unrelated to the change itself.
2026-01-26 07:52:17 +01:00
Timm Baeder
de8126d62a
[clang][bytecode] Fix mulc/divc op for IntegralAP types (#177565)
We need to allocate those.

Fixes https://github.com/llvm/llvm-project/issues/176740
2026-01-23 13:04:39 +01:00