99 Commits

Author SHA1 Message Date
Timm Bäder
8e69052b0e [clang][Interp] Handle ArrayTypeTraitExprs 2024-03-16 12:46:28 +01:00
Timm Bäder
2cd19df792 [clang][Interp] Allow visiting extern variables
If the variable is additionally const(expr), visit them like normal
but omit the initializer.
2024-03-14 15:39:28 +01:00
Timm Bäder
a99b912c9b [clang][Interp] Create dummy pointers for external variables
This way we can use their address, which is necessary in some
scenarios. This requires us to create different descriptors
for dummy arrays so we can get the diagnostics right.
2024-03-14 14:35:44 +01:00
Timm Bäder
12fdabc790 [clang][Interp] Diagnose dummy pointers used in Inc/Dec ops
For example for unknown parameter decls.
2024-03-06 16:57:02 +01:00
Timm Bäder
57f599d644 [clang][Interp] Improve handling of external variables
Further down in this function, we assert that the variable has
an initializer, which didn't work for external declarations.
2024-03-02 17:40:16 +01:00
Timm Bäder
e59681d963 [clang][Interp] Allow inc/dec on boolean values
The warnings or errors are emitted in Sema, but we still need to
do the operation and provide a reasonable result.
2024-03-01 15:40:45 +01:00
Timm Bäder
17a1b8f848 [clang][Interp] Fix nullptr - nullptr expressions
They can happen and we used to run into an assertion.
2024-02-19 16:30:57 +01:00
Timm Bäder
9b718c0d5d [clang][Interp] Handle CXXUuidofExprs
Allocate storage and initialize it with the given APValue contents.
2024-02-13 13:41:06 +01:00
Timm Bäder
66f73100b8 [clang][Interp] Fix ltor conversion for pointer types
This special case is wrong, we need to handle pointer types here
just like anything else.
2024-02-13 08:44:15 +01:00
Timm Bäder
a97ff2d35a [clang][Interp] Add missing static_assert message
This breaks builders:
https://lab.llvm.org/buildbot/#/builders/139/builds/58960
2024-02-07 13:41:27 +01:00
Timm Bäder
61c7a69fa0 [clang][Interp] Fix sizeof of reference types 2024-02-07 13:32:07 +01:00
Timm Bäder
6dfb31adf6 [clang][Interp][NFC] Simplify test case
By checking using verify={ref,expected},both.
2024-02-06 11:39:08 +01:00
Timm Bäder
5e8626c920 [clang][Interp] Handle ObjCBoolLiteralExprs
Emit them just like the others, but these are integer typed.
2024-02-05 18:21:30 +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
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
e80b943647
[clang][Interp] Fix discarded integral and floating casts (#77295)
We need to handle this at the CastExpr level.
2024-01-10 20:19:04 +01:00
Timm Baeder
f5b378b0d6
[clang] Use new interpreter in EvaluateAsConstantExpr if requested (#70763)
EvaluateAsConstantExpr() uses ::EvaluateInPlace() directly, which does
not use the new interpreter if requested. Do it here, which is the same
pattern we use in EvaluateAsInitializer.
2023-11-15 08:29:22 +01:00
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
b44763c5e6
[clang][Interp] Fix scalar inits of void type (#69868) 2023-10-24 09:18:39 +02:00
Aaron Ballman
84a3aadf0f Diagnose use of VLAs in C++ by default
Reapplication of 7339c0f782d5c70e0928f8991b0c05338a90c84c with a fix
for a crash involving arrays without a size expression.

Clang supports VLAs in C++ as an extension, but we currently only warn
on their use when you pass -Wvla, -Wvla-extension, or -pedantic.
However, VLAs as they're expressed in C have been considered by WG21
and rejected, are easy to use accidentally to the surprise of users
(e.g., https://ddanilov.me/default-non-standard-features/), and they
have potential security implications beyond constant-size arrays
(https://wiki.sei.cmu.edu/confluence/display/c/ARR32-C.+Ensure+size+arguments+for+variable+length+arrays+are+in+a+valid+range).
C++ users should strongly consider using other functionality such as
std::vector instead.

This seems like sufficiently compelling evidence to warn users about
VLA use by default in C++ modes. This patch enables the -Wvla-extension
diagnostic group in C++ language modes by default, and adds the warning
group to -Wall in GNU++ language modes. The warning is still opt-in in
C language modes, where support for VLAs is somewhat less surprising to
users.

RFC: https://discourse.llvm.org/t/rfc-diagnosing-use-of-vlas-in-c/73109
Fixes https://github.com/llvm/llvm-project/issues/62836
Differential Revision: https://reviews.llvm.org/D156565
2023-10-20 13:10:03 -04:00
Aaron Ballman
f5043f46c0 Revert "Diagnose use of VLAs in C++ by default"
This reverts commit 7339c0f782d5c70e0928f8991b0c05338a90c84c.

Breaks bots:
https://lab.llvm.org/buildbot/#/builders/139/builds/51875
https://lab.llvm.org/buildbot/#/builders/164/builds/45262
2023-10-20 10:00:18 -04:00
Aaron Ballman
7339c0f782 Diagnose use of VLAs in C++ by default
Clang supports VLAs in C++ as an extension, but we currently only warn
on their use when you pass -Wvla, -Wvla-extension, or -pedantic.
However, VLAs as they're expressed in C have been considered by WG21
and rejected, are easy to use accidentally to the surprise of users
(e.g., https://ddanilov.me/default-non-standard-features/), and they
have potential security implications beyond constant-size arrays
(https://wiki.sei.cmu.edu/confluence/display/c/ARR32-C.+Ensure+size+arguments+for+variable+length+arrays+are+in+a+valid+range).
C++ users should strongly consider using other functionality such as
std::vector instead.

This seems like sufficiently compelling evidence to warn users about
VLA use by default in C++ modes. This patch enables the -Wvla-extension
diagnostic group in C++ language modes by default, and adds the warning
group to -Wall in GNU++ language modes. The warning is still opt-in in
C language modes, where support for VLAs is somewhat less surprising to
users.

RFC: https://discourse.llvm.org/t/rfc-diagnosing-use-of-vlas-in-c/73109
Fixes https://github.com/llvm/llvm-project/issues/62836
Differential Revision: https://reviews.llvm.org/D156565
2023-10-20 09:50:21 -04:00
Timm Baeder
12107386ca
[clang][Interp] Handle CXXScalarValueInitExprs (#67147)
Handle those by just zero-initializing the result.
2023-10-10 12:45:09 +02:00
Timm Bäder
3542dd8f91 [clang][Interp][NFC] Move int128 tests to their own file 2023-10-10 09:54:08 +02:00
Timm Baeder
ef9666aa81
[clang][Interp] Fix value truncation when casting int128 to smaller size (#67961)
Before this patch, we would run into an assertion in
`APInt::get{S,Z}ExtValue()` because the `AllOnes` value had more than 64
active bits.
2023-10-03 06:05:39 +02:00
Timm Baeder
2d4de5acfa
[clang][Interp] Implement IntegralAP::comp (#67954) 2023-10-02 17:41:14 +02:00
Timm Bäder
8245ca994b [clang][Interp] Don't disable int128 tests everywhere. 2023-10-02 09:55:35 +02:00
Timm Bäder
c77b2ad00b [clang][Interp] Disable int128 tests on targets that don't have int128
This broke build bots.
2023-10-01 19:18:48 +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
Aaron Ballman
9243b1bfdb Revert "[clang-cl] Fix value of __FUNCTION__ and __FUNC__ in MSVC mode for c++. (#66120)"
This reverts commit 265568c136f94b108790e9be73cd8071e714aad1.

See https://github.com/llvm/llvm-project/issues/66114#issuecomment-1732319259
2023-09-27 14:26:49 -04:00
Sam McCall
880fa7faa9 Revert "[clang][SemaCXX] Diagnose tautological uses of consteval if and is_constant_evaluated"
This reverts commit 491b2810fb7fe5f080fa9c4f5945ed0a6909dc92.

This change broke valid code and generated incorrect diagnostics, see
https://reviews.llvm.org/D155064
2023-09-27 18:58:01 +02:00
Takuya Shimizu
491b2810fb [clang][SemaCXX] Diagnose tautological uses of consteval if and is_constant_evaluated
This patch makes clang diagnose extensive cases of consteval if and is_constant_evaluated usage that are tautologically true or false.
This introduces a new IsRuntimeEvaluated boolean flag to Sema::ExpressionEvaluationContextRecord that means the immediate appearance of if consteval or is_constant_evaluated are tautologically false(e.g. inside if !consteval {} block or non-constexpr-qualified function definition body)
This patch also pushes new expression evaluation context when parsing the condition of if constexpr and initializer of constexpr variables so that Sema can be aware that the use of consteval if and is_consteval are tautologically true in if constexpr condition and constexpr variable initializers.
BEFORE this patch, the warning for is_constant_evaluated was emitted from constant evaluator. This patch moves the warning logic to Sema in order to diagnose tautological use of is_constant_evaluated in the same way as consteval if.

This patch separates initializer evaluation context from InitializerScopeRAII.
This fixes a bug that was happening when user takes address of function address in initializers of non-local variables.

Fixes https://github.com/llvm/llvm-project/issues/43760
Fixes https://github.com/llvm/llvm-project/issues/51567

Reviewed By: cor3ntin, ldionne
Differential Revision: https://reviews.llvm.org/D155064
2023-09-27 09:26:06 +09:00
Zahira Ammarguellat
265568c136
[clang-cl] Fix value of __FUNCTION__ and __FUNC__ in MSVC mode for c++. (#66120)
Predefined macro FUNCTION (and __FUNC__) in clang is not returning the
same string than MS for templated functions.

See https://godbolt.org/z/88n1rGs3b

For this test case MSVC is returning:
function: TestClass<class UnitTestNative>::TestClass
func: TestClass

---------

Co-authored-by: Reid Kleckner <rnk@google.com>
2023-09-22 08:45:53 -04:00
Timm Bäder
52a55a7178 [clang][Interp] Allow zero-init of primitives with an empty init list
Differential Revision: https://reviews.llvm.org/D158595
2023-09-18 11:09:30 +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
d61ba038a9 [clang][Interp][NFC] Call delegate() for the RHS of a comma op 2023-09-14 13:36:51 +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
c4becd50ad [clang][Interp] Support __null
Differential Revision: https://reviews.llvm.org/D155552
2023-08-17 10:28:37 +02:00
Timm Bäder
21aa8a220c [clang][Interp] Fix ignoring Integral- and IntegralToBoolean casts
Differential Revision: https://reviews.llvm.org/D154474
2023-08-01 15:21:20 +02:00
Timm Bäder
65dca4cbcb [clang][Interp] Fix tests for ignored expressions
We didn't call the function explicitly in a static_assert() statement
and the checkPotentialConstantExpression() invocation quietly aborted
early because of the missing initializer for A::a.

Fix this by calling ignoredExprs() explicitly.

Differential Revision: https://reviews.llvm.org/D149965
2023-08-01 14:58:46 +02:00
Timm Bäder
eec6931d77 [clang][Interp] Fix ignoring String- and CharacterLiterals
Differential Revision: https://reviews.llvm.org/D149960
2023-08-01 12:23:57 +02:00
Timm Bäder
0d2e9b6e46 [clang][Interp][NFC] Ignore ArraySubScriptExpr like the other exprs
Instead of evaluating the entire thing and then pop'ing the value from
the stack, just pass the discard() on.
2023-08-01 12:14:20 +02:00
Timm Bäder
c0a36a157d [clang][Interp] Fix ignoring CompoundLiteralExprs
Differential Revision: https://reviews.llvm.org/D149837
2023-08-01 11:48:04 +02:00
Timm Bäder
b864592915 [clang][Interp] Fix ignoring TypeTraitExprs
Differential Revision: https://reviews.llvm.org/D149834
2023-08-01 10:58:08 +02:00
Timm Bäder
c7b400f4a8 [clang][Interp] Fix ignoring SubstNonTypeTemplateParmExpr
Ignore the expressions and re-do the tests without all the "result
ignored" expected warnings. Those are expected, given the nature of the
tests.

Differential Revision: https://reviews.llvm.org/D149831
2023-08-01 10:25:16 +02:00
Timm Bäder
6aac11bad9 [clang][Interp] Evaluate Base when discarding a MemberExpr
The Base expression might cause side-effects, so we still have to
evaluate it.

Differential Revision: https://reviews.llvm.org/D149828
2023-08-01 10:16:31 +02:00