112 Commits

Author SHA1 Message Date
Timm Baeder
980d027a3f
[clang][bytecode] Allow This pointers in CPCE mode (#137761)
The outermost function doesn't have a This pointers, but inner calls
can. This still doesn't match the diagnostic output of the current
interpreter properly, but it's closer I think.
2025-04-29 10:14:15 +02:00
Timm Baeder
15579a8e72
[clang][bytecode] Check array sizes against step limit (#137679) 2025-04-29 05:47:22 +02:00
Timm Baeder
959905a5ad
[clang][bytecode] Don't create Function instances for builtins (#137618)
Now that we don't use them anymore in InterpBuiltin.cpp and we don't
create frames for them anymore anyway, just don't create Function
instances.
2025-04-28 14:08:42 +02:00
Timm Baeder
e045d55dd5
[clang][bytecode] Check for global decls in destructors (#137525)
Destructors can't be called on global variables.
2025-04-27 20:02:58 +02:00
Timm Baeder
b8f5e3c573
[clang][bytecode] Diagnose PointerToIntegral casts (#137444)
Always emit the diagnostic, but permit the cast.
2025-04-26 07:11:30 +02:00
Timm Baeder
a50269e999
[clang][bytecode] Diagnose dynamic_cast before C++20 (#137442)
Emit a CCE diagnostic.
2025-04-26 06:23:34 +02:00
Timm Baeder
ef87c3421c
[clang][bytecode] Check for dummy pointers when calling pseudo dtors (#137437) 2025-04-26 05:47:54 +02:00
Timm Baeder
58d60a4df9
[clang][bytecode] Diagnose destroying already dead locals (#137357) 2025-04-26 05:03:28 +02:00
Timm Baeder
d775b911c9
[clang][bytecode] Diagnose pseudo dtor calls before C++20 (#137303) 2025-04-25 13:03:58 +02:00
Timm Baeder
425b953968
[clang][bytecode] Emit diagnostics from GetGlobalUnchecked (#137203)
If the global is uninitialized.
2025-04-24 22:05:45 +02:00
Timm Baeder
9ae7aa79b1
[clang][bytecode] Diagnose comparing pointers to fields... (#137159)
... with different access specifiers.
2025-04-24 17:04:36 +02:00
Timm Baeder
55066b8361
[clang][bytecode] Compute pointer differences as 64bit integers (#137128)
And only convert to the target type after that.
2025-04-24 11:01:46 +02:00
Timm Baeder
1b6cbaa7b6
[clang][bytecode] Refine diagnostics for volatile reads (#136857)
Differentiate between a volarile read via a lvalue-to-rvalue cast of a
volatile qualified subexpression and a read from a pointer with a
volatile base object.
2025-04-23 18:52:35 +02:00
Timm Baeder
1a78ef9a9e
[clang][bytecode] Allow casts from void* only in std::allocator calls (#136714)
Otherwise, add the missing diagnostic.
2025-04-23 08:00:57 +02:00
Timm Baeder
f91df0d58d
[clang][bytecode] Diagnose failed MemberPtrPtr casts differently (#136407)
Return Invalid() here instead of just false to match the diagnostic
output of the current interpreter.
2025-04-19 11:34:25 +02:00
Timm Baeder
90ddb54440
[clang][bytecode] Enter a non-constant context when revisiting (#136104)
Otherwise, things like __builtin_is_constant_evaluated() return the
wrong value.
2025-04-17 12:50:28 +02:00
Timm Baeder
2d63faead4
[clang][bytecode][NFC] Remove PT_FnPtr (#135947)
We don't need this anymore since we don't return it from classify()
anymore.
2025-04-16 13:21:25 +02:00
Timm Baeder
559df834df
[clang][bytecode] Fix subtracting zero-sized pointers (#135929)
Add the appropriate diagnostic and fix the d-d case.
2025-04-16 10:48:42 +02:00
Timm Baeder
974bda8f61
[clang][bytecode] Reject constexpr-unknown pointers from Inc ops (#135548)
We used to accept c++ as a known value here, causing wrong codegen.
2025-04-13 18:57:55 +02:00
Timm Baeder
fafeaab6d9
[clang][bytecode] Misc TypeidPointer fixes (#135322)
Fix comparing type id pointers, add mor info when print()ing them, use
the most derived type in GetTypeidPtr() and the canonically unqualified
type when we know the type statically.
2025-04-11 10:35:28 +02:00
Timm Baeder
02f923f8e4
[clang][bytecode] Classify function pointers as PT_Ptr (#135026)
The Pointer class already has the capability to be a function pointer,
but we still classifed function pointers as PT_FnPtr/FunctionPointer.
This means when converting from a Pointer to a FunctionPointer, we lost
the information of what the original Pointer pointed to.
2025-04-10 06:40:54 +02:00
Timm Baeder
bdd087023f
[clang][bytecode] Fix various issues with multidimensional arrays (#134628)
This issue is very convoluted, but in essence, in the new version:

For a Pointer P that points to the root of a multidimensional, primitive
array:

`P.narrow()` does nothing.
`P.atIndex(0)` points `P[0]`
`P.atIndex(0).atIndex(0)` is the same as `P.atIndex(0)` (as before)
`P.atIndex(0).narrow().atIndex(0)` points to `P[0][0]`
`P.atIndex(0).narrow().narrow()` is the same as `P.atIndex(0).narrow()`.
2025-04-08 05:48:55 +02:00
Timm Baeder
7267dbfe10
[clang][bytecode] Fix comparing the addresses of union members (#133852)
Union members get the same address, so we can't just use
`Pointer::getByteOffset()`.
2025-04-01 09:00:46 +02:00
Timm Baeder
11dd7d98a6
[clang][bytecode] Reject constexpr-unknown values from comparisons (#133701) 2025-03-31 18:53:01 +02:00
Timm Baeder
db7475a770
[clang][bytecode] Ignore overflow in unary operators if requested (#132557)
Add PreInc and PreDec ops for this purpose and ignore the overflow if
UnaryOperator::canOverflow() returns false.
2025-03-22 18:03:50 +01:00
Ayokunle Amodu
02744c5010
[clang][diagnostics] Update note_constexpr_invalid_cast to use enum_select and adjust its uses (#130868)
Handles #123121

This patch updates `note_constexpr_invalid_cast` diagnostic to use
`enum_select` instead of `select,` improving readability and reducing
reliance on magic numbers in caller sites.
2025-03-19 12:36:08 -07:00
Timm Baeder
ca1bde0b91
[clang][bytecode] Check dtor instance pointers for active-ness (#128732)
And diagnose if we're trying to destroy an inactive member of a union.
2025-03-17 19:01:35 +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
Timm Baeder
f838a5e96c
[clang][bytecode] Fix diagnostic difference with opaque call cmps (#129702)
Try to dig out the call expression and diagnose this as an opaque call.
2025-03-04 15:04:57 +01:00
Timm Baeder
53d433e702
[clang][bytecode] Only emit literal_comparison for string literals (#129691)
This is what the current interpreter does as well.
2025-03-04 14:07:53 +01:00
Timm Baeder
dfa3af9255
[clang][bytecode] Expand subscript base if of pointer type (#128511)
This is similar to what we do in the AddOffset instruction when adding
an offset to a pointer.
2025-02-25 11:40:05 +01:00
Timm Baeder
1760289340
[clang][bytecode] Fix three-way unordered non-pointer comparisions (#127759)
This _can_ happen with non-pointers, but we shouldn't diagnose it in
that case.
2025-02-19 10:22:37 +01:00
Timm Baeder
5fbb6d919d
[clang][bytecode] Allow up/down casts of nullptr (#127615)
If the target type is a pointer type.
2025-02-18 14:43:35 +01:00
Timm Baeder
09d14149f6
[clang][bytecode] Fix return value of array CXXNewExprs (#127526)
Just like with the __builtin_operator_new version, we need to point to
the first array element, not the array element itself.
2025-02-18 02:41:25 +01:00
Timm Baeder
e8d848824b
[clang][bytecode] Fix dynamic array allocation return values (#127387)
We need to return a pointer to the first element, not the array itself.
2025-02-16 12:24:35 +01:00
Timm Baeder
199c791a1d
[clang][bytecode] Support partial initializers for CXXNewExprs (#126494)
For `new A[N]{1,2,3}`, we need to allocate N elements of type A, and
initialize the first three with the given InitListExpr elements.
However, if N is larger than 3, we need to initialize the remaining
elements with the InitListExpr array filler.

Similarly, for `new A[N];`, we need to initilize all fields with the
constructor of A. The initializer type is a CXXConstructExpr of
IncompleteArrayType in this case, which we can't generally handle.
2025-02-10 14:28:40 +01:00
Timm Baeder
ee25a85ccc
[clang][bytecode] Handle CXXPseudoDestructorExprs (#125835)
Make lifetime management more explicit. We're only using this for
CXXPseudoDestructorExprs for now but we need this to handle
std::construct_at/placement-new after destructor calls later anyway.
2025-02-05 12:40:30 +01:00
Timm Baeder
642e84f001
[clang][bytecode][NFC] Only get expr when checking for UB (#125397)
The Expr and its Type were unused otherwise.
2025-02-02 12:25:08 +01:00
Timm Baeder
06130ed341
Reapply "[clang][bytecode] Stack-allocate bottom function frame" (#12… (#125349)
…5325)

Move the BottomFrame to InterpState instead.
2025-02-01 18:12:35 +01:00
Jorge Gorbe Moya
386af4a5c6
Revert "[clang][bytecode] Stack-allocate bottom function frame" (#125325)
Reverts llvm/llvm-project#125253

It introduced an msan failure. Caught by a buildbot here:
https://lab.llvm.org/buildbot/#/builders/164/builds/6922/steps/17/logs/stdio
2025-01-31 20:09:14 -08:00
Timm Baeder
f3549814f8
[clang][bytecode] Stack-allocate bottom function frame (#125253)
Instead of heap-allocating it. This is similar to what the current
interpeter does. In C, we have no function calls, so the extra heap
allocation never makes sense.
2025-01-31 20:08:58 +01:00
Timm Baeder
e8674af6f4
[clang][bytecode] Diagnose IntegralToPointer casts to non-void (#123619)
But keep evaluating. This is what the current interpreter does as well.
2025-01-20 17:41:18 +01:00
Timm Baeder
b5c9cba3f3
[clang][bytecode] Don't memcpy() FixedPoint values (#123599)
llvm::FixedPoint is not trivially copyable.
2025-01-20 15:10:12 +01:00
Timm Baeder
046b064df0
[clang][bytecode][NFC] Use FixedPoint opaque int API (#123522)
Now that we have it, use it.
2025-01-20 08:44:59 +01:00
Timm Baeder
cfe26358e3
Reapply "[clang] Avoid re-evaluating field bitwidth" (#122289) 2025-01-11 07:12:37 +01:00
Timm Bäder
59bdea24b0 Revert "[clang] Avoid re-evaluating field bitwidth (#117732)"
This reverts commit 81fc3add1e627c23b7270fe2739cdacc09063e54.

This breaks some LLDB tests, e.g.
SymbolFile/DWARF/x86/no_unique_address-with-bitfields.cpp:

lldb: ../llvm-project/clang/lib/AST/Decl.cpp:4604: unsigned int clang::FieldDecl::getBitWidthValue() const: Assertion `isa<ConstantExpr>(getBitWidth())' failed.
2025-01-08 15:09:52 +01:00
Timm Baeder
81fc3add1e
[clang] Avoid re-evaluating field bitwidth (#117732)
Save the bitwidth value as a `ConstantExpr` with the value set. Remove
the `ASTContext` parameter from `getBitWidthValue()`, so the latter
simply returns the value from the `ConstantExpr` instead of
constant-evaluating the bitwidth expression every time it is called.
2025-01-08 14:45:19 +01:00
Timm Baeder
e86b68ff56
[clang][bytecode] Add support for typeid pointers (#121251)
Add it as another kind of pointer, saving both a `Type*` for the result
of the typeid() expression as well as one for the type of the typeid
expression.
2024-12-28 14:07:01 +01:00
Timm Baeder
56fd46edb3
[clang][bytecode] Remove a bitcast nullptr_t special case (#120188)
We still need to check the input pointer, so let this go through
BitCastPrim.
2024-12-17 13:23:14 +01:00