130 Commits

Author SHA1 Message Date
Timm Baeder
e2f8ec7255
[clang][Interp] Fix variables referring to their own address (#70587)
This was a combination of issues:
  1) We can subtract pointers that don't point into arrays.
  2) In C, everything is possible.
2023-11-15 08:03:06 +01:00
Timm Baeder
216dfd5ff0
[clang][Interp] Fix stack peek offset for This ptr (#70663)
`Function::getArgSize()` include both the instance and the RVO pointer,
so we need to subtract here.
2023-11-14 16:22:22 +01:00
Timm Baeder
ba11d314a6
[clang][Interp] Only diagnose null field access in constant contexts (#69223)
Looks like this should work as long as we don't dereference the value.
2023-10-26 16:15:29 +02:00
Timm Baeder
7f677fe310
[clang][Interp] Add explicit dummy descriptors (#68888)
Instead of (ab)using incomplete array types for this, add a 'Dummy' bit
to Descriptor. We need to be able to differentiate between the two when
adding an offset.
2023-10-26 15:15:25 +02:00
Timm Baeder
658874e084
[clang][Interp] Handle unknown-size arrays better (#68868)
We unfortunately actually need to do some checks for array-to-pointer
decays it seems.
2023-10-26 13:53:54 +02:00
Timm Baeder
c8b267e98f
[clang][Interp] Handle variadic functions (#67814)
Similarly to the code we already had for builtin functions, we need to
check the call expression for the arguments passed.
2023-10-24 12:33:29 +02:00
Timm Bäder
93373c3da9 [clang][Interp][NFC] Use a const reference in IncDecHelper 2023-10-19 14:21:52 +02:00
Timm Baeder
59d2dc239b
[clang][Interp] IntegralAP zero-initializers (#68081) 2023-10-19 08:20:53 +02:00
Timm Baeder
7bc793a692
[clang][Interp] Check pointer inc/dec ops for null (#69168) 2023-10-17 06:53:33 +02:00
Timm Baeder
3efa4794ec
[clang][Interp] Support AddOffset with 128bit offsets (#68679)
We do a similar thing a few lines above for `Index`:

```c++
  // Get a version of the index comparable to the type.
  T Index = T::from(Ptr.getIndex(), Offset.bitWidth());
```
2023-10-13 14:47:46 +02:00
Timm Baeder
26d9f851cf
[clang][Interp] Add basic support for _BitInt (#68069)
Make sure we pass the expected bitwidth around when casting to
IntAP/IntAPS.

This makes it easier to test the `IntegralAP` code for different bit
widths than 128.
2023-10-11 08:53:21 +02:00
Timm Bäder
6172960cea [clang][Interp] Basic support for bit fields
Differential Revision: https://reviews.llvm.org/D155548
2023-10-10 13:38:26 +02:00
Timm Baeder
17414eae24
[clang][Interp] Fix returning nullptr from functions (#67229)
isLive() is false for null pointers, so we need to special-case this
here.
2023-10-10 11:19:14 +02:00
Timm Baeder
f58d54ab96
[clang][Interp] Diagnose uninitialized bases (#67131) 2023-10-03 06:46:31 +02:00
Timm Bäder
18461dc454 [clang][Interp] Add IntegralAP for arbitrary-precision integers (#65844) 2023-10-01 07:11:07 +02:00
Timm Bäder
4bae636abf Revert "[clang][Interp] Add IntegralAP for arbitrary-precision integers (#65844)"
This reverts commit 16b9e6fbac4c0bd94c66e7670a41b5c266cf7bff.

This breaks buildbots.
2023-09-30 20:12:14 +02:00
Timm Baeder
16b9e6fbac
[clang][Interp] Add IntegralAP for arbitrary-precision integers (#65844)
This adds `IntegralAP` backing the two new primtypes `IntAP` (unsigned
arbitrary-precision int) and `IntAPS` (same but signed).

We use this for `int128` support (which isn't available on all host
systems we support AFAIK) and I think we can also use this for `_BitInt`
later.
2023-09-30 20:08:22 +02:00
Timm Baeder
512739ebbb
[clang][Interp] Three-way comparisons (#65901) 2023-09-29 09:41:58 +02:00
Timm Bäder
23c39f9a9e [clang][Interp] Diagnose unknown parameter values
Differential Revision: https://reviews.llvm.org/D156509
2023-09-15 13:10:19 +02:00
Timm Bäder
177f9ab2fb [clang][Interp] Check pointer inc/dec ops for initialization
Differential Revision: https://reviews.llvm.org/D158702
2023-09-15 13:04:04 +02:00
Timm Bäder
4b5fe9c42d [clang][Interp] Check floating results for NaNs
Differential Revision: https://reviews.llvm.org/D156506
2023-09-11 12:21:36 +02:00
Timm Bäder
87461d6696 [clang][Interp] Implement __builtin_offsetof
Differential Revision: https://reviews.llvm.org/D156400
2023-09-11 12:03:47 +02:00
Timm Bäder
5704dde307 [clang][Interp] Reject calling virtual constexpr functions in pre-c++20
Differential Revision: https://reviews.llvm.org/D157619
2023-09-05 12:21:26 +02:00
Timm Bäder
a13f036949 [clang][Interp] Check one-past-the-end pointers in GetPtrField
Rename CheckBaseDerived to something more general and call it in
GetPtrField() as well, so we don't crash later in Pointer::toAPValue().

Differential Revision: https://reviews.llvm.org/D149149
2023-09-05 11:00:40 +02:00
Timm Bäder
12a789710e [clang][Interp] BaseToDerived casts
We can implement these similarly to DerivedToBase casts. We just have to
walk the class hierarchy, sum the base offsets and subtract it from the
current base offset of the pointer.

Differential Revision: https://reviews.llvm.org/D149133
2023-09-05 10:53:54 +02:00
Timm Bäder
c6ce32d9c3 [clang][Interp][NFC] Fix a comment 2023-09-05 10:28:47 +02:00
Sindhu Chittireddy
bbcc7c5614 [NFC] Initialize member pointer and avoid potential null dereference
Differential Revision: https://reviews.llvm.org/D158775
2023-08-25 11:35:44 -07:00
Timm Bäder
8a58f0d370 [clang][Interp] Handle global composite temporaries
We only did this for primitive temporaries.

Unfortunately, the existing Pointer::toAPValue() won't do here, since
we're expected to set an rvalue on the LifetimeExtendedTemporaryDecl.

Differential Revision: https://reviews.llvm.org/D144457
2023-08-20 11:15:17 +02:00
Takuya Shimizu
985a72b6b3 [clang][Diagnostics] Provide source range to integer-overflow warnings
BEFORE:

```
overflow.cpp:1:21: warning: overflow in expression; result is -2147483648 with type 'int' [-Winteger-overflow]
    1 | int x = __INT_MAX__ + 1 + 3;
      |                     ^
overflow.cpp:2:9: warning: overflow in expression; result is -2147483648 with type 'int' [-Winteger-overflow]
    2 | int a = -(1 << 31) + 1;
      |         ^
```
AFTER:

```
overflow.cpp:1:21: warning: overflow in expression; result is -2147483648 with type 'int' [-Winteger-overflow]
    1 | int x = __INT_MAX__ + 1 + 3;
      |         ~~~~~~~~~~~~^~~
overflow.cpp:2:9: warning: overflow in expression; result is -2147483648 with type 'int' [-Winteger-overflow]
    2 | int a = -(1 << 31) + 1;
      |         ^~~~~~~~~~
```

Reviewed By: tbaeder
Differential Revision: https://reviews.llvm.org/D157383
2023-08-19 22:05:12 +09:00
Timm Bäder
cf10061da7 [clang][Interp] Fully serialize Floating values to bytes
The Floating class wraps a APFloat, which might heap allocate memory to
represent large floating values. When writing those to bytecode, we
would free() the heap allocation after writing, when destroying the
actual APFloat we wrote.

Fix this by seralizing a Floating as Semantics + APInt.

This will be neccessary in more cases later, when we support
arbitrary-precision integers or _BitInt.

Differential Revision: https://reviews.llvm.org/D155165
2023-08-17 12:41:39 +02:00
Timm Bäder
8a25145058 [clang][Interp] Pass CallExpr to builtin functions
For some builtins, we need to do quite a bit of type checking ourselves,
so pass the call expression along. This way we can inspect arguments,
expected return value, etc.

Differential Revision: https://reviews.llvm.org/D155545
2023-08-17 10:20:58 +02:00
Timm Bäder
b56ab41d54 Revert "[clang][Interp] Basic support for bit fields"
This reverts commit 8065b1cc133c9e4b6900d5c11220617180706b94.

This breaks builders. I forgot this depends
on https://reviews.llvm.org/D155548
2023-08-10 09:17:07 +02:00
Timm Bäder
8065b1cc13 [clang][Interp] Basic support for bit fields
Differential Revision: https://reviews.llvm.org/D155270
2023-08-10 08:50:10 +02:00
Timm Bäder
f6ee4e3f55 [clang][ExprConst] Add RHS source range to div by zero diags
Differential Revision: https://reviews.llvm.org/D157074
2023-08-09 15:48:50 +02:00
Paul Robinson
4e8cae4aec Fix build break on pickier MSVC configurations [NFC]
clang::StreamingDiagnostic doesn't have a uint8_t overload,
which makes MSVC without /permissive- decide it's ambiguous.
Pre-approved in replies to D153276.
2023-08-08 12:44:47 -04:00
Timm Bäder
925ec544cf Revert "[clang][ExprConst] Add RHS source range to div by zero diags"
This reverts commit 74c141a467caf9ebb4835458bc4ffbedb172a63a.

Looks like this breaks a whole bunch of unexpected tests:
https://lab.llvm.org/buildbot/#/builders/109/builds/70813
2023-08-08 18:16:35 +02:00
Timm Bäder
74c141a467 [clang][ExprConst] Add RHS source range to div by zero diags
Differential Revision: https://reviews.llvm.org/D157074
2023-08-08 17:59:13 +02:00
Timm Bäder
ff80fc0ea2 [clang][Interp] Implement __builtin_isnan()
The previous version was using llvm::reverse(CallExpr::arguments()),
which causes problems when clang is compiled with GCC.

Differential Revision: https://reviews.llvm.org/D155369
2023-07-29 10:48:10 +02:00
Timm Bäder
9016514c6b Revert "[clang][Interp] Implement __builtin_isnan()"
This reverts commit 8ad9dcb3869394e3a4ecb01631646237074723fe.

This breaks builders:
https://lab.llvm.org/buildbot/#/builders/139/builds/46363/steps/6/logs/FAIL__Clang__constant-builtins-fmin_cpp

Revert while I figure out what's going wrong.
2023-07-28 20:59:38 +02:00
Timm Bäder
8ad9dcb386 [clang][Interp] Implement __builtin_isnan()
Differential Revision: https://reviews.llvm.org/D155369
2023-07-28 20:49:45 +02:00
Timm Bäder
9092acc510 [clang][Interp] Emit proper diagnostic when comparing unrelated pointers
Differential Revision: https://reviews.llvm.org/D149172
2023-07-28 13:23:48 +02:00
Timm Bäder
ae4849f967 [clang][Interp] PointerToIntegral casts
Differential Revision: https://reviews.llvm.org/D150946
2023-07-26 13:39:37 +02:00
Timm Bäder
8c0246c7f5 [clang][Interp] Reject reinterpret_casts
Add a new InvalidCast op for this purpose and emit a diagnostic.

Differential Revision: https://reviews.llvm.org/D153276
2023-07-26 09:56:29 +02:00
Timm Bäder
c7251385c8 [clang][Interp] Check pointers for live-ness when returning them
We might be trying to return a pointer or reference to a local variable,
which doesn't work.

Differential Revision: https://reviews.llvm.org/D154795
2023-07-26 09:52:09 +02:00
Timm Bäder
7a3ad8ed77 [clang] Provide source range to 'invalid subexpr in const expr' diags
Differential Revision: https://reviews.llvm.org/D150566
2023-07-26 09:12:11 +02:00
Timm Bäder
7d876c62a3 [clang][Interp] Add 'Invalid' opcode and use it for throw stmts
We will use this opcode for conditionally executed statements that are
invalid in a constant expression.

Differential Revision: https://reviews.llvm.org/D150364
2023-07-26 08:59:53 +02:00
Timm Bäder
8a4bbeb916 [clang][Interp] Remove args from called functions in more cases
When calling functions in the checkingPotentialConstantExpression mode,
we cannot have arguments (including This + RVO pointers) for the
toplevel callee, but the functions called from within can work just
fine, or at least we succeed in pushing their arguments on the stack, so
we must also succeed in removing them again.

Differential Revision: https://reviews.llvm.org/D150358
2023-07-26 08:47:54 +02:00
Timm Bäder
6d2e141e5c [clang][Interp] Handle lambda static invokers
Differential Revision: https://reviews.llvm.org/D150111
2023-07-26 08:42:16 +02:00
Timm Bäder
8cb6e476cc [clang][Interp] Handle PtrMemOps
Differential Revision: https://reviews.llvm.org/D144164
2023-07-26 07:04:21 +02:00
Timm Bäder
eaadbcd5e0 [clang][Interp] Implement __builtin_strcmp
Make our Function class keep a list of parameter offsets so we can
simply get a parameter by index when evaluating builtin functions.

Differential Revision: https://reviews.llvm.org/D149816
2023-07-20 15:46:04 +02:00