390 Commits

Author SHA1 Message Date
Timm Baeder
e0acf6592b
[clang][bytecode] Call CheckFinalLoad in all language modes (#154496)
Fixes #153997
2025-08-21 08:24:09 +02:00
Timm Baeder
e16ced3ef4
[clang][bytecode] Diagnose one-past-end reads from global arrays (#154484)
Fixes #154312
2025-08-20 10:34:44 +02:00
Timm Baeder
32ba045bea
[clang][bytecode] Fix comparing pointers to union members (#154342)
If one of them is a one-past-end pointer.
2025-08-20 07:09:20 +02:00
Timm Baeder
fb8ee3adb6
[clang][bytecode] Move pointers from extern globals to new decls (#154273) 2025-08-19 10:54:33 +02:00
Timm Baeder
da05208bfb
[clang][bytecode] Create temporary before discarding CXXConstructExpr (#154280)
Fixes #154110
2025-08-19 10:27:26 +02:00
Timm Baeder
ddcd3fdcfd
[clang][bytecode][NFC] use both-note in literals test (#154277)
And harmonize the RUN lines.
2025-08-19 10:02:23 +02:00
Timm Baeder
25c137e43b
[clang][bytecode] Save a per-block dynamic allocation ID (#154094)
This fixes an old todo item about wrong allocation counting and some
diagnostic differences.
2025-08-19 06:52:21 +02:00
Timm Baeder
6ce13ae1c2
[clang][bytecode] Always track item types in InterpStack (#151088)
This has been a long-standing problem, but we didn't use to call the
destructors of items on the stack unless we explicitly `pop()` or
`discard()` them.

When interpretation was interrupted midway-through (because something
failed), we left `Pointer`s on the stack. Since all `Block`s track what
`Pointer`s point to them (via a doubly-linked list in the `Pointer`),
that meant we potentially leave deallocated pointers in that list. We
used to work around this by removing the `Pointer` from the list before
deallocating the block.

However, we now want to track pointers to global blocks as well, which
poses a problem since the blocks are never deallocated and thus those
pointers are always left dangling.

I've tried a few different approaches to fixing this but in the end I
just gave up on the idea of never knowing what items are in the stack.
We already have an `ItemTypes` vector that we use for debugging
assertions. This patch simply enables this vector unconditionally and
uses it in the abort case to properly `discard()` all elements from the
stack. That's a little sad IMO but I don't know of another way of
solving this problem.

As expected, this is a slight hit to compile times:
https://llvm-compile-time-tracker.com/compare.php?from=574d0a92060bf4808776b7a0239ffe91a092b15d&to=0317105f559093cfb909bfb01857a6b837991940&stat=instructions:u
2025-08-18 17:15:31 +02:00
Timm Baeder
8f0da9b8bd
[clang][bytecode] Disable EndLifetime op for array elements (#154119)
This breaks a ton of libc++ tests otherwise, since calling
std::destroy_at will currently end the lifetime of the entire array not
just the given element.

See https://github.com/llvm/llvm-project/issues/147528
2025-08-18 16:32:50 +02:00
Timm Baeder
0d05c42b6a
[clang][bytecode] Improve __builtin_{,dynamic_}object_size implementation (#153601) 2025-08-18 11:12:33 +02:00
Timm Baeder
e44784fb44
[clang][bytecode] Fix pseudo dtor calls on non-pointers (#153970)
The isGLValue() check made us ignore expressions we shouldn't ignore.
2025-08-17 08:47:57 +02:00
Timm Baeder
373206d5e0
[clang][bytecode] Prefer ParmVarDecls as function parameters (#153952)
We might create a local temporary variable for a ParmVarDecl, in which
case a DeclRefExpr for that ParmVarDecl should _still_ result in us
choosing the parameter, not that local.
2025-08-16 17:22:14 +02:00
Mikołaj Piróg
768eae72cd
[Clang] Honor -flax-vector-conversions=none on some tests (#153433)
As in title. This is done as a step towards enabling the
`-flax-vector-conversions=none` globally as a default
2025-08-14 17:02:52 +02:00
Timm Baeder
56131e3959
[clang][bytecode] Diagnose incomplete types more consistently (#153368)
To match the diagnostics of the current interpreter.
2025-08-13 10:40:21 +02:00
Timm Baeder
2c40fd6dd1
[clang][bytecode] Use Param decl as variable source if we can (#152909)
This results in diagnostics that are closer to what the current
interpreter produces.
2025-08-10 16:58:42 +02:00
Timm Baeder
870aa979c4
[clang][bytecode] Use visitExpr() in interpretCall (#152857)
This is the correct function to use and it will create a variable scope.

Fixes #152822
2025-08-09 18:30:26 +02:00
Timm Baeder
1b1f352cb9
[clang][bytecode] Handle reads on zero-size arrays (#152706) 2025-08-08 16:03:02 +02:00
Timm Baeder
193995d5a2
[clang][bytecode] Handle more invalid member pointer casts (#152546) 2025-08-07 19:24:01 +02:00
Timm Baeder
8704ca0fb8
[clang][ExprConst] Consider integer pointers of value 0 nullptr (#150164)
When casting a 0 to a pointer type, the IsNullPtr flag was always set to
false, leading to weird results like a pointer with value 0 that isn't a
null pointer.

This caused

```c++
struct B { const int *p;};
template<B> void f() {}
template void f<B{nullptr}>();
template void f<B{fold(reinterpret_cast<int*>(0))}>();
```

to be valid code, since nullptr and (int*)0 aren't equal. This seems
weird and GCC doesn't behave like this.
2025-08-06 16:49:00 +02:00
Timm Baeder
90d1d23507
[clang][bytecode] Overrride locs for certain CXXConstructExprs (#152185)
Do it only if we will end up skipping the initializer anyway because
it's a trivial copy or move constructor.
2025-08-06 09:07:40 +02:00
Timm Baeder
dc7c3c2b2b
[clang][bytecode] Disable location tracking for implicit field inits (#150190) 2025-08-05 13:14:01 +02:00
Timm Baeder
b557cd3e8f
[clang][bytecode][NFC] Add a c++11 test case (#152104)
This test case breaks when ignoring trivial CXXConstructExprs of array
types, so make sure we don't do that.
2025-08-05 11:28:05 +02:00
Timm Baeder
8f7dfc689c
[clang][bytecode] Call CheckLocalLoad in GetLocal (#152090)
I forgot to call this here as well. It was only used in the EvalEmitter
implementation of the function. Also fix a problem where we didn't
diagnose out-of-lifetime reads here.
2025-08-05 08:46:22 +02:00
Timm Baeder
df3f629735
[clang][bytecode][NFC] Only collect non-null args if we have to (#152074)
Only do this if the function really has a NonNullArg.
2025-08-05 07:54:25 +02:00
Timm Baeder
d02714c85a
[clang][bytecode] Return success for pointers to locals (#151980)
They aren't valid, but we leave them successful here and
CheckLValueConstantExpression will diagnose them later.
2025-08-05 05:18:47 +02:00
Timm Baeder
76e38ca981
[clang][bytecode] Try to load primitive values directly (#151833)
Instead of doing a GetPtrLocal + Load or GetPtrGlobal + Load pair, try
to load the value directly.
2025-08-04 12:28:33 +02:00
Timm Baeder
a1dfcc6e81
[clang][bytecode] Fix D3DCOLORtoUBYTE4 hlsl test (#151819)
HLSL is using CK_FloatingToIntegral casts to cast to vectors, which we
don't support (neither does the current interpreter). Also fix a crash
when trying to promote the HLSL bool, which can't be promoted it seems.
2025-08-02 16:18:38 +02:00
Timm Baeder
f4aaf6fe5c
[clang][bytecode] Fix a crash in codegen (#151515)
getRecord() can return nullptr if any one of the fields does, in this
case because the array is too large for us to allocate.
2025-07-31 15:50:02 +02:00
Timm Baeder
1b9ee0bb74
[clang][bytecode] Fix diagnostics for int128 enums (#151340)
CheckEnumValue was not handling PT_IntAP/PT_IntAPS.
2025-07-31 05:59:17 +02:00
Eli Friedman
0bbe1b30fa
[clang] Forbid reinterpret_cast of function pointers in constexpr. (#150557)
This has been explicitly forbidden since C++11, but somehow the edge
case of converting a function pointer to void* using a cast like
`(void*)f` wasn't handled.

Fixes #150340 .
2025-07-30 18:15:17 -07:00
Timm Baeder
23eef9a7c4
[clang][bytecode] Activate primitive fields before initializing them (#149963)
The initializer itself might need the field to be active.
2025-07-23 06:09:27 +02:00
Timm Baeder
01b47eb86c
[clang][bytecode] Only implicitly start lifetime of trivially-default-constructible union members (#149835)
See
faee39baa8
2025-07-23 05:28:37 +02:00
Timm Baeder
a5d9ba6625
[clang][bytecode] Error on bitcasts to bool that aren't 0 or 1 (#149996)
This is also what the current interpreter does.
2025-07-22 14:23:01 +02:00
Timm Baeder
fed6db45df
[clang][bytecode] Diagnose dereferencing a null pointer (#149330) 2025-07-19 21:39:06 +02:00
Corentin Jabot
c43f828d59
[Clang] Be less strict about diagnosing null pointer dereference. (#149648)
In #143667, we made constant evaluation fail on `*null_ptr`, as this is
UB. However, `&(*(foo*)0)` seems to be a common pattern, which made
#143667 too disruptive.

So instead of failing the evaluation, we note the UB, which let clang
recovers when checking for constant initialization.

Fixes #149500
2025-07-19 19:30:16 +02:00
Timm Baeder
ef49ed4829
[clang][bytecode] Use bytecode interpreter in isPotentialConstantExprU… (#149462)
…nevaluated

Fake a function call to the given function and evaluate the given
expression as if it was part of that function call.

Fixes #149383
2025-07-19 07:18:51 +02:00
Timm Baeder
b7660a5415
[clang][bytecode] Fix const-in-mutable fields (#149286)
For mutable and const fields, we have two bits in InlineDescriptor,
which both get inherited down the hierarchy. When a field is both const
and mutable, we CAN read from it if it is a mutable-in-const field, but
we _can't_ read from it if it is a const-in-mutable field. We need
another bit to distinguish the two cases.
2025-07-18 11:20:48 +02:00
Timm Baeder
3bb4355bb8
[clang][bytecode] Report mutable reads when copying unions (#149320) 2025-07-18 11:10:57 +02:00
Corentin Jabot
9e5470e7d6
[Clang] Diagnose forming references to nullptr (#143667)
Per [decl.ref],

> Because a null pointer value or a pointer past the end of an object
does not point to an object, a reference in a well-defined program
cannot refer to such things.

Note this does not fixes the new bytecode interpreter.

Fixes #48665
2025-07-16 14:25:24 +02:00
Timm Baeder
3b8a18c27a
[clang][bytecode] Fix contains check using llvm::find (#149050)
We need to compare to the end() interator.
2025-07-16 13:26:10 +02:00
Timm Baeder
17d3029331
[clang][bytecode] Make union activation more granular (#148835)
Only activate things if the syntactical structure suggests so. This adds
a bunch of new opcodes to control whether to activate in stores, etc.

Fixes #134789
2025-07-16 09:03:33 +02:00
Timm Baeder
36cbd43ae8
[clang][bytecode] Check new/delete mismatch earlier (#147732)
This fixes a mismatch in diagnostic output with the current intepreter.
2025-07-10 07:33:33 +02:00
Timm Baeder
376b3f7049
[clang][bytecode] Devirtualize calls during con- and destruction (#147685)
When compiliung compiling a ctor or dtor, we need to devirtualize the
virtual function calls so we always call the implementation of the
current class.
2025-07-09 14:37:10 +02:00
Timm Baeder
5cefb9a367
[clang][bytecode] Fix __builtin_is_within_lifetime in initializers (#147480) 2025-07-08 17:17:15 +02:00
Oleksandr T.
2e8e254d18
[Clang] include attribute scope in diagnostics (#144619)
This patch updates diagnostics to print fully qualified attribute names,
including scope when present.
2025-07-08 11:36:52 +03:00
Timm Baeder
36dd61f12a
[clang][bytecode] Fix activating nested unions (#147338)
When activating the new pointer, we need to de-activate pointers of all
parent unions, not just the topmost one.
2025-07-08 07:45:17 +02:00
Timm Baeder
1828381ed2
[clang][bytecode] Fix APValue generation for RValueReferenceType (#147207)
We need to ignore the lvalue path, just like we do for lvalue reference
types.
2025-07-06 21:12:18 +02:00
Timm Baeder
9c7320e5d3
[clang][bytecode] Fix visiting for-range loop variable (#147188)
Make sure we're properly registering DecompositionDecls.
2025-07-06 18:59:24 +02:00
Timm Baeder
ec9eefcef5
[clang][bytecode] Fix a crash in overflow builtins (#147189)
Only initialize pointers that can be initialized.
2025-07-06 16:42:06 +02:00
Timm Baeder
db4e927f9f
[clang][bytecode] Misc union fixes (#146824)
First, don't forget to also activate fields which are bitfields on
assignment.

Second, when deactivating fields, recurse into records.
2025-07-06 14:55:29 +02:00