114 Commits

Author SHA1 Message Date
Timm Bäder
ad7aeb0ff5 Revert "[clang][Interp] Fix CheckCallable for undefined-and-not-constexpr fns"
This reverts commit d00b35534d068510025d22e5bd9c4fdac45757fb.

This breaks the ms-constexpr test:
https://lab.llvm.org/buildbot/#/builders/144/builds/2605
2024-07-18 15:30:12 +02:00
Timm Bäder
d00b35534d [clang][Interp] Fix CheckCallable for undefined-and-not-constexpr fns 2024-07-18 15:22:02 +02:00
Timm Bäder
fc65a9603b [clang][Interp] Run record destructors when deallocating dynamic memory 2024-07-18 15:07:29 +02:00
Timm Bäder
e94e72a0c2 Reapply "[clang][Interp] Implement dynamic memory allocation handling (#70306)"
This reverts commit 48d703e7f56282ce5d690e45a129a4a7fd040ee6.
2024-07-17 11:44:13 +02:00
Timm Bäder
6484655f9d [clang][Interp] Allow initialization of extern variables via ctors 2024-07-15 13:12:52 +02:00
Timm Bäder
9ac2b89652 [clang][Interp] Diagnose volatile reads 2024-07-15 09:37:45 +02:00
Timm Bäder
48d703e7f5 Revert "[clang][Interp] Implement dynamic memory allocation handling (#70306)"
This reverts commit fa133d3151b5e428b1c5819d29b0ad28a90882a2.

It looks like this has some more serious problems:
https://lab.llvm.org/buildbot/#/builders/39/builds/528

As well as build failures on MacOS.
2024-07-14 21:17:47 +02:00
Timm Bäder
ba3dcec16b Revert "[clang][Interp] Fix a build failure on Windows"
This reverts commit 27f5c00c607e08b3cdf37f3bf9f4ccf156de2ab4.
2024-07-14 21:17:39 +02:00
Timm Bäder
27f5c00c60 [clang][Interp] Fix a build failure on Windows
The usual ambiguous APInt constructor:

https://lab.llvm.org/buildbot/#/builders/141/builds/764
2024-07-14 20:15:20 +02:00
Timm Baeder
fa133d3151
[clang][Interp] Implement dynamic memory allocation handling (#70306)
Implement handling for new/delete/new[]/delete[] expressions via a new
`DynamicAllocator` class.

This introduces four new opcodes:
 - `Alloc` - Allocates one element (`new int(14)`)
- `AllocN` - Allocates N elements of the given primitive (`new
int[100]`)
- `AllocCN` - Allocates N elements of the given (composite) descriptor
(`new S[100]`)
 - `Free` - de-allocates memory allocates using any of the above.
2024-07-14 19:58:17 +02:00
Timm Bäder
33af112f99 [clang][Interp] Fix modifying const objects in functions calls in ctors
The current frame might not be a constructor for the object we're
initializing, but a parent frame might.
2024-07-14 17:47:11 +02:00
Timm Bäder
40f4bd18f2 [clang][Interp] Allow reading mutable members if they were created...
... in this evaluation.
2024-06-29 21:10:24 +02:00
Timm Bäder
37698d9248 [clang][Interp][NFC] Use CheckLoad() in Inc/Dec ops
CheckLoad checks more things than we did before.
2024-06-29 21:10:24 +02:00
Timm Bäder
50b1534118 [clang][Interp] Don't diagnose non-const reads from the evaluating decl 2024-06-27 18:01:11 +02:00
Timm Bäder
90e4eb8a47 [clang][Interp][NFC] Add more source ranges to diagnostics 2024-06-26 11:44:55 +02:00
Timm Bäder
ce938fcbde [clang][Interp] Diagnose functions without body like undefined ones
We only get a "reached end of constexpr function" diagnostic
otherwise.
2024-06-06 19:41:41 +02:00
Timm Baeder
a86c1e7175
[clang][Interp] Member Pointers (#91303)
This adds a `MemberPointer` class along with a `PT_MemberPtr` primitive
type.

A `MemberPointer` has a `Pointer` Base as well as a `Decl*` (could be
`ValueDecl*`?) decl it points to.
For the actual logic, this mainly changes the way we handle `PtrMemOp`s
in `VisitBinaryOperator`.
2024-06-06 11:17:48 +02:00
Timm Bäder
d8c8c8c7b9 [clang][Interp] Diagnose dummy assignments differently
Incremental change here, but a step in the right direction. Before,
an assignment to a dummy variable was diagnosed as a "read of a
non-const variable".
2024-05-24 16:40:38 +02:00
Timm Bäder
84729c9df3 [clang][Interp] Don't diagnose ObjCIvarDecls as invalid reads 2024-05-23 13:47:54 +02:00
Timm Bäder
3c67c227f2 [clang][Interp] Test non-active union access 2024-05-22 17:31:34 +02:00
Timm Bäder
5cb2ea5704 [clang][Interp][NFC] Remove VarDecl requirement in diagnoseUnknownDecl()
We can call diagnoseNonConstVariable() for all ValueDecls just fine.
2024-05-22 12:14:56 +02:00
Timm Bäder
7f3ac51b94 [clang][Interp] Only accept constant variables in c++98 2024-05-15 09:49:57 +02:00
Timm Bäder
64c3997939 [clang][Interp] Allow initializing static class members
We need to handle this when registering global variables.
2024-04-11 19:05:29 +02:00
Timm Baeder
1709eac58f
[clang][Interp] Integral pointers (#84159)
This turns the current `Pointer` class into a discriminated union of
`BlockPointer` and `IntPointer`. The former is what `Pointer` currently
is while the latter is just an integer value and an optional
`Descriptor*`.

The `Pointer` then has type check functions like
`isBlockPointer()`/`isIntegralPointer()`/`asBlockPointer()`/`asIntPointer()`,
which can be used to access its data.

Right now, the `IntPointer` and `BlockPointer` structs do not have any
methods of their own and everything is instead implemented in Pointer
(like it was before) and the functions now just either assert for the
right type or decide what to do based on it.

This also implements bitcasts by decaying the pointer to an integral
pointer.

`test/AST/Interp/const-eval.c` is a new test testing all kinds of stuff
related to this. It still has a few tests `#ifdef`-ed out but that
mostly depends on other unimplemented things like
`__builtin_constant_p`.
2024-04-10 12:53:54 +02:00
Timm Bäder
447691333f [clang][Interp] Don't suppress diagnostics for undefined+external funcs
Calling them should still generate a diagnostic.
2024-03-15 16:43:43 +01:00
Timm Bäder
4f69c4b158 [clang][Interp] Don't diagnose reading const ints in C++98
We _can_ read them, even in C++98.
2024-03-15 16:43:43 +01:00
Timm Bäder
ddfc7e2254 [clang][Interp] Emit more dummy pointers in C++ mode 2024-02-29 10:04:42 +01:00
Timm Bäder
8c5e9cf737 [clang][Interp] Implement nullability argument checking
Implement constexpr checking for null pointers being passed to
arguments annotated as nonnull.
2024-02-26 06:19:25 +01:00
Timm Bäder
13acb3af5a [clang][Interp] Don't diagnose alread invalid function decls
They have already been diagnosed before. Also improve that test case.
2024-02-23 10:53:31 +01:00
Timm Bäder
91ebd010aa [clang][Interp] Remove dereference()
This function tried to be smart about the dereferenced value,
but it ended up hurting more than it helped. At least in the current
state, where we still try get the correct output.

I might add something similar back later.
2024-02-21 15:25:55 +01:00
Timm Bäder
9563746d35 [clang][Interp] Diagnose uninitialized global variables explicitly
There used to be some diagnostic differences between the new
interpreter and the old one.
2024-02-20 14:20:17 +01:00
Timm Bäder
17d8a84c32 [clang][Interp] Fix handling PointerToIntegral casts
We need to always emit the diagnostic, but still do the cast.
This is also what the current interpreter does.
2024-02-16 14:31:42 +01:00
Timm Bäder
6a8ab12930 [clang][Interp] Properly emit call ops to invalid functions
Just like everywhere else, we can't just abort compilation because
a function is invalid. We need to emit the Call op and let later
interpretation handle the failure.

This fixes a long standing FIXME comment.
2024-02-15 20:17:51 +01:00
Timm Bäder
d53515afef [clang][Interp] Fix variadic member functions
For variadic member functions, the way we calculated the instance
pointer and RVO pointer offsts on the stack was incorrect, due
to Func->getArgSize() not returning the full size of all the
passed arguments. When calling variadic functions, we need
to pass the size of the passed (variadic) arguments to the Call*
ops, so they can use that information to properly check the
instance pointer, etc.

This patch adds a bit of code duplication in Interp.h, which I
will get rid of in later cleanup NFC patches.
2024-02-15 05:59:53 +01:00
Timm Bäder
82950a695d [clang][Interp] Protect ArrayElemPtr ops from dummy pointers
Change the semantics of Pointer::isDummy() to check for a null
Pointee and returnd false in that case. Then call CheckDummy()
in ArrayElemPtr{,Pop} to protect those ops from operating on
dummy pointers and enable a few tests in test/Sema/ that now
work with the new constant interpreter.
2024-02-06 12:29:35 +01:00
Timm Bäder
0be39155bd [clang][Interp] Protect stores against dummy pointers 2024-02-02 09:18:06 +01:00
Timm Baeder
47df391296
[clang][Interp] Handle std::move etc. builtins (#70772) 2024-01-31 15:57:23 +01:00
Timm Baeder
5bb99edcb6
[clang][Interp] Add inline descriptor to global variables (#72892)
Some time ago, I did a similar patch for local variables.

Initializing global variables can fail as well:
```c++
constexpr int a = 1/0;
static_assert(a == 0);
```
... would succeed in the new interpreter, because we never saved the
fact that `a` has not been successfully initialized.
2024-01-31 08:03:37 +01:00
Timm Baeder
4e7cf1b1ed
[clang][Interp] Add an EvaluationResult class (#71315)
Add an `EvaluationResult` class. This contains the result either as a
`Pointer` or as a `APValue`.

This way, we can inspect the result of the evaluation and diagnose
problems with it (e.g. uninitialized fields in global initializers or
pointers pointing to things they shouldn't point to).
2024-01-19 10:08:03 +01:00
cor3ntin
e90e43fb9c
[Clang][NFC] Rename CXXMethodDecl::isPure -> is VirtualPure (#78463)
To avoid any possible confusion with the notion of pure function and the
gnu::pure attribute.
2024-01-18 15:30:58 +01:00
Timm Baeder
30d458626d
[clang][Interp] Fix diagnosing non-const variables pre-C++11 (#76718)
In CheckConstant(), consider that in C++98 const variables may not be read at all, and diagnose that accordingly.
2024-01-18 15:15:05 +01:00
Timm Baeder
fc2766c1d4
[clang][Interp] Diagnose reads from non-const global variables (#71919)
This fixes a long-standing FIXME item.

Unfortunately it changes the diagnostic output of the tests added in
`cxx23.cpp`, but they were wrong before and are wrong after, so no big
deal.
2024-01-13 05:51:03 +01:00
Timm Baeder
e3993e044e
[clang][Interp] Implement __builtin_addressof (#77303)
We don't need to do anything here, since the input is already a Pointer.
The only complexity is that we pre-classify the parameters as PT_Ptr,
but they might end up being of a different pointer type, e.g. PT_FnPtr.
2024-01-11 09:02:24 +01:00
Timm Baeder
c15e5836d4
[clang][Interp] Fix nullptr array dereferencing (#75798)
The attached test case would cause an assertion failure in Pointer.h
when operating on a null pointer.
2024-01-08 08:33:15 +01:00
Timm Baeder
a8977005b6
[clang][Interp] Don't diagnose undefined functions when checking... (#75051)
... for a potential constant expression. They are not defined now, but
might be defined later when the function is actually called.
2023-12-13 12:14:53 +01:00
Timm Baeder
965d301dff
[clang][Interp] Implement __builtin_classify_type (#71972)
This adds some infrastructure for unevaluated builtin calls, and uses the implementation from ExprConstant.cpp
2023-11-17 16:13:23 +01:00
Timm Bäder
8ff81deeaa [clang][Interp][NFC] Remove some redundant code
The needsRuntimeArgPop() stuff is now handled by the
cleanupAfterFunctionCall() function.
2023-11-12 06:37:51 +01:00
Takuya Shimizu
b88a9f9670
[clang][ExprConst] Fix crash on uninitialized array subobject (#67817)
https://reviews.llvm.org/D146358 was assuming that all subobjects have
their own name (`SubobjectDecl`), but it was not true for array
elements.

Fixes https://github.com/llvm/llvm-project/issues/67317
2023-10-27 14:11:27 +09: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