88 Commits

Author SHA1 Message Date
Timm Baeder
c51be1be3a
[clang][bytecode] Fix checking for integer overflow (#137962)
We need to evaluate both the True/False expressions of a conditional
operator as well as the LHS/RHS of a binary operator in more cases.
2025-05-01 07:35:33 +02:00
Timm Baeder
f8724bd873
[clang][bytecode] Check live-ness when calling dtors (#137645)
We can't call a destructor on a dead pointer.
2025-04-28 17:47:30 +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
e086d7b146
[clang][bytecode] Don't create function frames for builtin calls (#137607)
They don't have local variables etc. so don't create frames for them.
2025-04-28 13:11:15 +02:00
Timm Baeder
60b3a5b7e7
[clang][bytecode] Fix two small builtin_constant_p cases (#137587)
Only accept string literals if we're pointing to the first index and do
accept complex literals.
2025-04-28 09:21:49 +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
ef87c3421c
[clang][bytecode] Check for dummy pointers when calling pseudo dtors (#137437) 2025-04-26 05:47:54 +02:00
Timm Baeder
211b51e471
[clang][bytecode] Propagate IsVolatile bit to subobjects (#137293)
For
```c++
  struct S {
    constexpr S(int=0) : i(1) {}
    int i;
  };
  constexpr volatile S vs;
```

reading from `vs.i` is not allowed, even though `i` is not volatile
qualified. Propagate the IsVolatile bit down the hierarchy, so we know
reading from `vs.i` is a volatile read.
2025-04-25 11:23:34 +02:00
Timm Baeder
5eca2ddeba
[clang][bytecode] Don't diagnose const extern reads in CPCE mode (#137285)
They might become constexpr later.
2025-04-25 08:54:34 +02:00
Timm Baeder
3b58a60086
[clang][bytecode] Allow forming pointers to fields of extern globals (#137211)
This should be fine as long as we're not reading from it.

Note that this regresses
CXX/special/class.init/class.inhctor.init/p1.cpp, which used to work
fine with the bytecode interpreter.

That's because this code now fails:

```c++
  struct Param;
  struct A {
    constexpr A(Param);
    int a;
  };

  struct B : A { B(); using A::A; int b = 2; };
  struct Wrap1 : B { constexpr Wrap1(); };
  struct Wrap2 : Wrap1 {};
  extern const Wrap2 b;

  struct Param {
    constexpr Param(int c) : n(4 * b.a + b.b + c) {}
    int n;
  };
```
and reports that the Param() constructor is never a valid constant
expression. But that's true and the current interpeter should report
that as well. It also fails when calling at compile time.
2025-04-25 07:43:33 +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
c5d59723cb
[clang][bytecode] Reject constexpr-unknown values in CheckStore (#136279) 2025-04-18 12:48:16 +02:00
Timm Baeder
05eafd9f2b
[clang][bytecode] Explicitly mark constexpr-unknown variables as such (#135806)
Instead of trying to figure out what's constexpr-unknown later on.
2025-04-16 09:00:52 +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
Oliver Hunt
1cd59264aa
[RFC] Initial implementation of P2719 (#113510)
This is a basic implementation of P2719: "Type-aware allocation and
deallocation functions" described at http://wg21.link/P2719

The proposal includes some more details but the basic change in
functionality is the addition of support for an additional implicit
parameter in operators `new` and `delete` to act as a type tag. Tag is
of type `std::type_identity<T>` where T is the concrete type being
allocated. So for example, a custom type specific allocator for `int`
say can be provided by the declaration of

  void *operator new(std::type_identity<int>, size_t, std::align_val_t);
  void  operator delete(std::type_identity<int>, void*, size_t, std::align_val_t);

However this becomes more powerful by specifying templated declarations,
for example

template <typename T> void *operator new(std::type_identity<T>, size_t, std::align_val_t);
template <typename T> void operator delete(std::type_identity<T>, void*, size_t, std::align_val_t););

Where the operators being resolved will be the concrete type being
operated over (NB. A completely unconstrained global definition as above
is not recommended as it triggers many problems similar to a general
override of the global operators).

These type aware operators can be declared as either free functions or
in class, and can be specified with or without the other implicit
parameters, with overload resolution performed according to the existing
standard parameter prioritisation, only with type parameterised
operators having higher precedence than non-type aware operators. The
only exception is destroying_delete which for reasons discussed in the
paper we do not support type-aware variants by default.
2025-04-10 17:13:10 -07: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
11dd7d98a6
[clang][bytecode] Reject constexpr-unknown values from comparisons (#133701) 2025-03-31 18:53:01 +02:00
Timm Baeder
cb7b10c66e
[clang][bytecode] Fail on mutable reads from revisited variables (#133064)
When revisiting a variable, we do that by simply calling visitDecl() for
it, which means it will end up with the same EvalID as the rest of the
evaluation - but this way we end up allowing reads from mutable
variables. Disallow that.
2025-03-26 12:29:31 +01:00
Timm Baeder
849e5ea94f
[clang][bytecode] Add Descriptor::getDataType() (#132681)
This returns the type of data in the Block, which might be different
than the type of the expression or declaration we created the block for.
This lets us remove some special cases from CheckNewDeleteForms() and
CheckNewTypeMismatch().
2025-03-24 09:47:52 +01:00
Timm Baeder
49f06075a6
[clang][bytecode] Fix union copy/move operator active check (#132238)
Don't call CheckActive for copy/move operators. They will activate the
union member.
2025-03-20 19:08:55 +01:00
Timm Baeder
2f808dd070
[clang][bytecode] Compile most recent function decl (#131730)
We used to always do this because all calls went through the code path
that calls getMostRecentDecl(). Do it now, too.
2025-03-18 07:29:38 +01: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
83356f3b62
[clang][bytecode] Compile functions lazily (#131596)
Create the Function* handles for all functions we see, but delay the
actual compilation until we really call the function. This speeds up
compile times with the new interpreter a bit.
2025-03-17 15:58:35 +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
bdbc434498
[clang][bytecode] Ignore function calls with depth > 0... (#129887)
... when checking for a potential constant expression. This is also what
the current interpreter does.
2025-03-05 16:21:03 +01:00
Timm Baeder
aeca2aa193
[clang][bytecode] Fix CallPtr return type check (#129722)
CallExpr::getType() isn't enough here in some cases, we need to use
CallExpr::getCallReturnType().
2025-03-04 17:14:13 +01:00
Timm Baeder
1d8eb436ca
[clang][bytecode] Diagnose member calls on inactive union fields (#129709)
Unless the function is a constructor, which is allowed to do this since
it will activate the member.
2025-03-04 16:14:47 +01:00
Timm Baeder
82d111e820
[clang][bytecode][NFC] Minor cleanups (#129553)
Pull local variables in to the closest scope, remove some unnecessary
calls to getLocation() and remove an outdated comment.
2025-03-04 06:17:07 +01:00
Timm Baeder
96336acb48
[clang][bytecode] Tighten double-destroy check (#129528)
The instance pointer of the current function being the same as the one
we're destroying is only relevant if said function is also a destructor.
2025-03-03 16:26:56 +01:00
Timm Baeder
dff2ca424c
[clang][bytecode] Add special case for anonymous unions (#128681)
This fixes the expected output to match the one of the current
interpreter.
2025-02-25 12:46:06 +01:00
Timm Baeder
8102fec00b
[clang][bytecode] Reject calls to pure virtual functions (#128412) 2025-02-23 11:44:37 +01:00
Timm Baeder
c38befd94f
[clang][bytecode] Fix delete[] dtor order (#128411)
As always, call array dtors in reverse order.
2025-02-23 11:32:35 +01:00
Timm Baeder
6db96c9ecc
[clang][bytecode] Always reject ctors of invalid parent decls (#128295)
The copy constructor of an invalid declaration might still be perfectly
valid, but we still need to reject it.
2025-02-22 22:04:44 +01:00
Timm Baeder
2c00b3b859
[clang][bytecode] Silently reject ctors of invalid decls (#128290)
The follow-up diagnostic would otherwise be:

array.cpp:111:33: note: undefined constructor '(unnamed struct at
array.cpp:111:11)' cannot be used in a constant expression
array.cpp:111:11: note: declared here
  111 | constexpr struct { Unknown U; } InvalidCtor;
      |           ^

... and complaining about the undefined constructor of a class that is
invalid anyway doesn't make much sense.
2025-02-22 08:01:56 +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
9387fd9631
[clang][bytecode] Fix diagnosing replaceable global allocator functions (#126717)
Don't return true here in InvalidNewDeleteExpr just because we are in
C++26 mode. This invalid there as well.

Testcase reduced from
libcxx/test/std/utilities/smartptr/unique.ptr/unique.ptr.create/make_unique_for_overwrite.pass.cpp
2025-02-11 16:51:21 +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
c475356603
[clang][bytecode][NFC] Only call getSource() when necessary (#125419) 2025-02-03 08:51:30 +01:00
Timm Baeder
51c7338cc6
[clang][bytecode] Fix dummy handling for p2280r4 (#124396)
This makes some other problems show up like the fact that we didn't
suppress diagnostics during __builtin_constant_p evaluation.
2025-01-29 09:32:35 +01:00
Timm Baeder
e6030d3895
[clang][bytecode] Use std::allocator calls for Descriptor source (#123900)
... for the dynamic blocks created for operator new calls. This way we
get the type of memory allocated right. As a side-effect, the
diagnostics now point to the std::allocator calls, which is an
improvement.
2025-01-24 11:50:56 +01:00
Timm Baeder
d70f54f248
[clang][bytecode] Fix reporting failed local constexpr initializers (#123588)
We need to emit the 'initializer of X is not a constant expression' note
for local constexpr variables as well.
2025-01-20 13:25:50 +01:00
Timm Baeder
1be64c27f1
[clang][bytecode] Fix diagnostic mismatch with current interpreter (#123571)
Don't report dead pointers if we've checking for a potential constant
expression.
2025-01-20 10:48:38 +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
ce1587346b
[clang][bytecode] Allow checking builtin functions... (#119328)
... in checkingPotentialConstantExpression mode. This is what the
current interpreter does, yet it doesn't do so for
`__builtin_operator_new`.
2024-12-10 07:42:13 +01:00
Timm Baeder
82ed9c0319
[clang][bytecode][NFC] Remove APValue Result argument where unnecessary (#118199)
This is unneeded in almost all circumstances. We only return an APValue
back to clang when the evaluation is finished, and that is always done
by an EvalEmitter - which has its own implementation of the Ret
instructions.
2024-12-01 17:36:19 +01:00
Timm Baeder
a9731dff0a
[clang][bytecode][NFC] Avoid a getSource() call (#117311)
This is only needed when we actually emit a diagnostic, so move the
getSource() after the early return.
2024-11-22 14:00:10 +01:00
smanna12
95f4aa44ae
[clang][bytecode] Add assert to ensure correct state restoration in CallBI function (#115496)
This commit adds an assert statement to the CallBI function to ensure
that the interpreter state (S.Current) is correctly reset to the
previous frame (FrameBefore) after InterpretBuiltin returns true. This
helps catch any potential issues during development and debugging.
2024-11-21 08:45:34 -06:00
Simon Pilgrim
d800ea7cb1
Adjust MSVC disabled optimization pragmas to be _MSC_VER only (#116704)
Alter the #ifdef values from #110986 and #115292 to use _MSC_VER instead of _WIN32 to stop the pragmas being used on gcc/mingw builds

Noticed by @mstorsjo
2024-11-21 13:33:13 +00:00