85 Commits

Author SHA1 Message Date
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
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 Baeder
c45466cd9a
[clang][Interp] Only emit function_param_value_unknown in C++11 (#67990)
This is also what the current interpreter does.
2023-10-24 09:28:30 +02:00
Timm Baeder
2bed2a7a5c
[clang][Interp] Emit dummy values for unknown C variables (#66749)
We can't load anything from them, but we can still take their address or
return them as lvalues.
2023-10-07 16:00:19 +02:00
Timm Baeder
f58d54ab96
[clang][Interp] Diagnose uninitialized bases (#67131) 2023-10-03 06:46:31 +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
4b5fe9c42d [clang][Interp] Check floating results for NaNs
Differential Revision: https://reviews.llvm.org/D156506
2023-09-11 12:21:36 +02:00
Timm Baeder
fedc982731
[clang][Diagnostics] Add source range to uninitialized diagnostics (#65896)
Before:

```
array.cpp:319:10: note: read of uninitialized object is not allowed in a constant expression
  319 |    return aaa;
      |           ^
```

After:

```
array.cpp:319:10: note: read of uninitialized object is not allowed in a constant expression
  319 |    return aaa;
      |           ^~~
```
2023-09-11 07:03:18 +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
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
81fb216245 [clang][Diagnostics] Provide source range to invalid casts in const expr
Differential Revision: https://reviews.llvm.org/D153241
2023-07-28 12:28:45 +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
c54ff51be9 [clang][Interp] Emit correct diagnostic for uninitialized reads
Differential Revision: https://reviews.llvm.org/D154758
2023-07-09 16:27:49 +02:00
Timm Bäder
0d40973644 [clang][Interp][NFC] Move CastFP to Interp.h
It's not a Check* function, so try to stay consistent and move this to
the header.
2023-07-04 17:35:28 +02:00
Timm Bäder
df5213c442 [clang][Interp][NFC] Return a const pointer from Pointer::getRecord() 2023-07-04 17:21:51 +02:00
Timm Bäder
044be8f5d1 [clang][Interp][NFC] Add some missing const qualifiers 2023-07-04 16:16:17 +02:00
Timm Bäder
1c8c5a89c5 [clang][Interp][NFC] Merge two if statements 2023-07-04 16:16:17 +02:00
Takuya Shimizu
dfb85c3ce0 [Clang][Interp] Diagnose uninitialized ctor of global record arrays
This patch adds a check for uninitialized subobjects of global variables that are record arrays.
e.g. `constexpr Foo f[2];`

Reviewed By: tbaeder

Differential Revision: https://reviews.llvm.org/D152548
2023-06-21 19:03:01 +09:00
Timm Bäder
459f495f49 [clang][Interp] Check inc/dec family of ops for initialization
Differential Revision: https://reviews.llvm.org/D149846
2023-06-16 15:19:23 +02:00
Timm Bäder
da836b36bc [clang][Interp] Track frame depth
Save the depth of each InterpFrame and bail out if we're too deep.

Differential Revision: https://reviews.llvm.org/D148614
2023-05-31 12:08:03 +02:00
Timm Bäder
40c26ec48c [clang][Interp] Fix diagnosing uninitialized ctor record arrays
Differential Revision: https://reviews.llvm.org/D143334
2023-05-31 11:39:40 +02:00
Takuya Shimizu
456d072405 Reland: [clang][AST] Print name instead of type when diagnosing uninitialized subobject in constexpr variables
This patch improves the diagnostic on uninitialized subobjects in constexpr variables by modifying the diagnostic message to display the subobject's name instead of its type.

Fixes https://github.com/llvm/llvm-project/issues/58601
Differential Revision: https://reviews.llvm.org/D146358
2023-05-24 21:31:25 +09:00
Erich Keane
34e49d3e85 Revert "[clang][AST] Print name instead of type when diagnosing uninitialized subobject in constexpr variables"
This reverts commit 0e167fc0a2147c9b673b8afd5fea001b0d127781.

This patch causes its assertion to fire when doing AST-dump, so
reverting so that the author has time to fix this.
2023-05-18 06:44:39 -07:00
Takuya Shimizu
0e167fc0a2 [clang][AST] Print name instead of type when diagnosing uninitialized subobject in constexpr variables
This patch improves the diagnostic on uninitialized subobjects in constexpr variables by modifying the diagnostic message to display the subobject's name instead of its type.

Fixes https://github.com/llvm/llvm-project/issues/58601
Differential Revision: https://reviews.llvm.org/D146358
2023-05-16 21:49:57 +09:00
Timm Bäder
db3dcdc08c [clang][Interp] Fix initializing base class members
For the given test case, we were trying to initialize a member of C,
which doesn't have any. Get the proper base pointer instead and
initialize the members there.

Differential Revision: https://reviews.llvm.org/D143466
2023-04-03 13:51:04 +02:00
Timm Bäder
569222e172 [clang][Interp] Only check constructors for global variables
Local variables may be partially initialized.

Differential Revision: https://reviews.llvm.org/D140723
2023-03-02 09:13:47 +01:00