1482 Commits

Author SHA1 Message Date
Eli Friedman
dc90bf0329 [clang] Fix const eval of constexpr-unknown relational comparisons. (#150088)
Like in other places, ignore the reference type of the base. (It might
make sense to refactor this at some point.)

Fixes #150015.

(cherry picked from commit bba846773c7dfce0f95b8846672d8dd5fa8912be)
2025-07-29 09:57:43 +02:00
Eli Friedman
ece444008a [clang] Fix potential constant expression checking with constexpr-unknown.
071765749a70b22fb62f2efc07a3f242ff5b4c52 improved constexpr-unknown
diagnostics, but potential constant expression checking broke in the
process: we produce diagnostics in more cases. Suppress the diagnostics
as appropriate.

This fix affects -Winvalid-constexpr and the enable_if attribute. (The
-Winvalid-constexpr diagnostic isn't really important right now, but it
will become important if we allow constexpr-unknown with pre-C++23
standards.)

Fixes #149041.  Fixes #149188.
(cherry picked from commit 6a60f18997d62b0e2842a921fcb6beb3e52ed823)
2025-07-28 10:59:44 +02:00
Eli Friedman
2067574f54 [clang] Fix pointer comparisons between pointers to constexpr-unknown (#147663)
A constexpr-unknown reference can be equal to an arbitrary value, except
values allocated during constant evaluation. Fix the handling.

The standard is unclear exactly which pointer comparisons count as
"unknown" in this context; for example, in some cases we could use
alignment to prove two constexpr-unknown references are not equal. I
decided to ignore all the cases involving variables not allocated during
constant evaluation.

While looking at this, I also spotted that there might be issues with
lifetimes, but I didn't try to address it.

(cherry picked from commit 20c8e3c2a4744524396cc473a370cfb7855850b6)
2025-07-17 17:56:24 +02:00
Owen Pan
2f1673eaa0 Follow up on #147623 2025-07-11 18:15:27 -07:00
Connector Switch
3231cb4b19
[clang] Fix copy/paste error in vector __builtin_elementwise_{add,sub}_sat implementation. (#147973)
Closes #147891.
2025-07-11 19:45:16 +08:00
Alex Sepkowski
7c16a31aa5
Address a handful of C4146 compiler warnings where literals can be replaced with std::numeric_limits (#147623)
This PR addresses instances of compiler warning C4146 that can be
replaced with std::numeric_limits. Specifically, these are cases where a
literal such as '-1ULL' was used to assign a value to a uint64_t
variable. The intent is much cleaner if we use the appropriate
std::numeric_limits value<Type>::max() for these cases.


Addresses #147439
2025-07-09 16:13:28 -07:00
Eli Friedman
aa27d4e0c3
[clang] Implement consteval for captured structured bindings. (#147615)
127bf44385424891eb04cff8e52d3f157fc2cb7c implemented most of the
infrastructure for capturing structured bindings in lambdas, but missed
one piece: constant evaluation of such lambdas. Refactor the code to
handle this case.

Fixes #145956.
2025-07-09 12:09:35 -07:00
kadir çetinkaya
f72e53f350
[clang][CompundLiteralExpr] Don't defer evaluation for CLEs (#137163)
Previously we would defer evaluation of CLEs until LValue to RValue
conversions, which would result in creating values within wrong scope
and triggering use-after-frees.

This patch instead eagerly evaluates CLEs, within the scope requiring
them. This requires storing an extra pointer for CLE expressions with
static storage.

Fixes https://github.com/llvm/llvm-project/issues/137165
2025-07-08 16:00:40 +02:00
Eli Friedman
071765749a
[clang] Improve constexpr-unknown diagnostics. (#146288)
APValue::ConstexprUnknown() constructs a broken LValue that doesn't have
an lvalue path, which confuses later error handling. It turns out we
don't actually use the result of createConstexprUnknownAPValues for
anything, so just stop using it. Just construct the LValue directly when
we need it.

Make findCompleteObject emit errors more aggressively; allowing it to
succeed for constexpr-unknown objects leads to weird states where it
succeeds, but doesn't return a well-formed object.

Delete the check for constexpr-unknown in dynamic_cast handling: it's
not necessary, and breaks with the other changes in this patch.

These changes allow us to produce proper diagnostics when something
fails to be evaluated, instead of just printing a generic top-level
error without any notes.
2025-07-07 23:35:10 -07:00
Henrik G. Olsson
37eb465710
Reland "[Modules] Record whether VarDecl initializers contain side effects" (#145447)
This reverts commit 329ae86 and adds an early exit for EvaluateInPlace when the expression's type is null.
2025-06-23 20:20:15 -07:00
Rahul Joshi
6cf656eca7
[NFC][Clang][AST] Drop llvm:: in front of ArrayRef/MutableArrayRef (#145207) 2025-06-23 13:10:42 -07:00
Aaron Ballman
9eef4d1c5f
Remove delayed typo expressions (#143423)
This removes the delayed typo correction functionality from Clang
(regular typo correction still remains) due to fragility of the
solution.

An RFC was posted here:
https://discourse.llvm.org/t/rfc-removing-support-for-delayed-typo-correction/86631
and while that RFC was asking for folks to consider stepping up to be
maintainers, and we did have a few new contributors show some interest,
experiments show that it's likely worth it to remove this functionality
entirely and focus efforts on improving regular typo correction.

This removal fixes ~20 open issues (quite possibly more), improves
compile time performance by roughly .3-.4%
(https://llvm-compile-time-tracker.com/?config=Overview&stat=instructions%3Au&remote=AaronBallman&sortBy=date),
and does not appear to regress diagnostic behavior in a way we wouldn't
find acceptable.

Fixes #142457
Fixes #139913
Fixes #138850
Fixes #137867
Fixes #137860
Fixes #107840
Fixes #93308
Fixes #69470
Fixes #59391
Fixes #58172
Fixes #46215
Fixes #45915
Fixes #45891
Fixes #44490
Fixes #36703
Fixes #32903
Fixes #23312
Fixes #69874
2025-06-13 06:45:40 -04:00
Dmitry Polukhin
9797b5fcfb
[C++20][Modules] Fix false compilation error with constexpr (#143168)
Use declaresSameEntity when evaluating constexpr to avoid resetting
computed union value due to using different instances of the merged
field decl.
2025-06-11 10:35:06 +01:00
Eli Friedman
609023213d
[clang] Check constexpr int->enum conversions consistently. (#143034)
In 8de51375f12d91675a18d17f262276e65f43fbe0 and related patches, we
added some code to avoid triggering -Wenum-constexpr-conversion in some
cases. This isn't necessary anymore because -Wenum-constexpr-conversion
doesn't exist anymore. And the checks are subtly wrong: they exclude
cases where we actually do need to check the conversion. This patch gets
rid of the unnecessary checks.
2025-06-06 08:57:11 -07:00
Vincent
49386f40dd
[Clang] Run destructors of variables declared in the second part of a for loop during constant evaluation (#140278)
Within the condition statement of the for block, the destructor doesn't
get called when evaluating compile time constants.

Resolves #139818
2025-06-05 23:50:20 +02:00
Corentin Jabot
1be7c6fb40
[Clang] Fix constant eval of assignment operators with an explicit object parameter (#142964)
Fixes #142835
2025-06-05 19:30:25 +02:00
Eli Friedman
97885213bd
[clang] Don't evaluate the initializer of constexpr-unknown parameters. (#142498)
If we see a parameter of reference type that isn't part of the frame,
don't try to evaluate its default argument. Just treat it as a
constexpr-unknown value.

Fixes #141114.  Fixes #141858.
2025-06-03 09:51:37 -07:00
David Green
3a42cbd47d [AArch64] Rename AArch64SVEACLETypes.def and add base SVE_TYPE. 2025-05-28 12:26:54 +01:00
Cassandra Beckley
5a4571133a
[HLSL] Implement SpirvType and SpirvOpaqueType (#134034)
This implements the design proposed by [Representing SpirvType in
Clang's Type System](https://github.com/llvm/wg-hlsl/pull/181). It
creates `HLSLInlineSpirvType` as a new `Type` subclass, and
`__hlsl_spirv_type` as a new builtin type template to create such a
type.

This new type is lowered to the `spirv.Type` target extension type, as
described in [Target Extension Types for Inline SPIR-V and Decorated
Types](https://github.com/llvm/wg-hlsl/blob/main/proposals/0017-inline-spirv-and-decorated-types.md).
2025-05-27 11:40:54 -04:00
Timm Baeder
f17b9a77aa
[clang][ExprConst][NFC] Only call getExprLoc() once (#141473)
It's potentially costly, so only do it once.
2025-05-26 13:45:22 +02:00
Eli Friedman
021443cd2a
[clang] fix constexpr-unknown handling of self-references. (#132990)
Usually, in constant evaluation, references which are local to the
evaluation have to be initialized before they're accessed. However,
there's one funny special case: the initializer of a reference can refer
to itself. This generally ends up being undefined behavior if it's used
in an evaluated context, but it isn't otherwise forbidden.

In constant evaluation, this splits into two cases: global variables,
and local variables in constexpr functions. This patch handles both of
those cases. (Local variables tends to trip other errors in most cases,
but if you try hard enough, you can get an accepts-invalid.)

Fixes #131330 .
2025-05-19 11:27:51 -07:00
Shafik Yaghmour
136f2ba2a7
[Clang][AST] Fix HandleLValueBase to deal with references (#140105)
Since P2280R4 Unknown references and pointers was implemented,
HandleLValueBase now has to deal with referneces:

D.MostDerivedType->getAsCXXRecordDecl()

will return a nullptr if D.MostDerivedType is a ReferenceType. The fix
is to use getNonReferenceType() to obtain the Pointee Type if we have a
reference.

Fixes: https://github.com/llvm/llvm-project/issues/139452
2025-05-15 16:04:37 -07:00
Timm Baeder
9ca8248a91
[clang] Save ShuffleVectorExpr args as ConstantExpr (#139709)
The passed indices have to be constant integers anyway, which we verify
before creating the ShuffleVectorExpr. Use the value we create there and
save the indices using a ConstantExpr instead. This way, we don't have
to evaluate the args every time we call getShuffleMaskIdx().
2025-05-14 16:13:01 +02:00
JJ Marr
b77109ff8c
Better diagnostics when assertion fails in consteval (#130458)
Take this piece of code:
```cpp
#include <cassert>

consteval int square(int x) {
  int result = x * x;
  assert(result == 42);
  return result;
}

void test() {
  auto val = square(2);
}
```
The assertion will fail, and `clang++` will output
(https://godbolt.org/z/hjz3KbTTv):
```cpp
<source>:10:14: error: call to consteval function 'square' is not a constant expression
   10 |   auto val = square(2);
      |              ^
<source>:5:3: note: non-constexpr function '__assert_fail' cannot be used in a constant expression
    5 |   assert(result == 42);
      |   ^
/usr/include/assert.h:95:9: note: expanded from macro 'assert'
   95 |       : __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION))
      |         ^
<source>:10:14: note: in call to 'square(2)'
   10 |   auto val = square(2);
      |              ^~~~~~~~~
/usr/include/assert.h:69:13: note: declared here
   69 | extern void __assert_fail (const char *__assertion, const char *__file,
      |             ^
1 error generated.
Compiler returned: 1
```
This is confusing because it implies that the issue was using an
assertion in a constant-evaluted context, and not that the assertion
failed (`assert()` is OK in constant evaluation). This PR changes the
error message to:
```cpp
test.cpp:10:14: error: call to consteval function 'square' is not a constant expression
   10 |   auto val = square(2);
      |              ^
test.cpp:5:3: note: assertion failed in consteval context: 'result == 42'
    5 |   assert(result == 42);
      |   ^
/nix/store/lw21wr626v5sdcaxxkv2k4zf1121hfc9-glibc-2.40-36-dev/include/assert.h:102:9: note: expanded from macro 'assert'
  102 |       : __assert_fail (#expr, __ASSERT_FILE, __ASSERT_LINE,             \
      |         ^
test.cpp:10:14: note: in call to 'square(2)'
   10 |   auto val = square(2);
      |              ^~~~~~~~~
1 error generated.```
2025-05-14 08:47:38 +02:00
Timm Bäder
cc281584a1 Revert "[clang][ExprConst] Check for array size of initlists (#138673)"
This reverts commit d35ad58859c97521edab7b2eddfa9fe6838b9a5e.

This breaks the clang build:
https://lab.llvm.org/buildbot/#/builders/132/builds/1033

/home/buildbot-worker/bbroot/clang-riscv-rva23-evl-vec-2stage/stage2/lib/Target/RISCV/RISCVGenGlobalISel.inc:1512:44: note: cannot allocate array; evaluated array bound 2431270 exceeds the limit (1048576); use '-fconstexpr-steps' to increase this limit
2025-05-09 09:28:26 +02:00
Timm Baeder
d35ad58859
[clang][ExprConst] Check for array size of initlists (#138673)
Fixes #138653
2025-05-09 09:03:20 +02:00
Timm Baeder
dc28f9d087
[clang][ExprConstant] Bail out on invalid lambda capture inits (#138832)
Fixes https://github.com/llvm/llvm-project/issues/138824
2025-05-08 05:22:11 +02:00
Timm Baeder
ecc73a6f1c
[clang][bytecode] Use bytecode interpreter in EvaluateCharRangeAsString (#138461)
This was always using the ast walker.
2025-05-05 09:06:34 +02:00
cor3ntin
cb068dcec1
[Clang] Fix handling of reference types in tryEvaluateBuiltinObjectSize (#138247)
The order of operation was slightly incorrect, as we were checking for
incomplete types *before* handling reference types.

Fixes #129397

---------

Co-authored-by: Erich Keane <ekeane@nvidia.com>
2025-05-02 18:45:24 +02:00
Akira Hatanaka
feaa5aa840
Fix a crash in constant evaluation of ExtVectorElementExprs (#136771)
Handle the case where the base expression is a pointer to a vector type.

rdar://149223362
2025-04-24 08:47:29 -07:00
Henrik G. Olsson
de1af6b727
Eval string one past end reland (#137091)
Relands #137078 after updating clang/test/AST/ByteCode/cxx20.cpp to
account for diagnostic outputs that differ between Linux and macOS.
2025-04-23 20:27:12 -07:00
Henrik G. Olsson
93705c3a76
Revert "[ConstEval] Fix crash when comparing strings past the end" (#137088)
Reverts llvm/llvm-project#137078
2025-04-23 16:48:46 -07:00
Henrik G. Olsson
55160e6a89
[ConstEval] Fix crash when comparing strings past the end (#137078)
When `ArePotentiallyOverlappingStringLiterals`, added in
https://github.com/llvm/llvm-project/pull/109208, compares string
literals it drops the front of the string with the greatest offset from
its base pointer. The number of characters dropped is equal to the
difference between the two strings' offsets from their base pointers.
This would trigger an assert when the resulting offset is past the end
of the object. Not only are one-past-the-end pointers legal constructs,
the compiler should not crash even when faced with illegal constructs.

rdar://149865910
2025-04-23 16:41:21 -07:00
Kazu Hirata
c2d6c7cea7
[clang] Use llvm::append_range (NFC) (#136448) 2025-04-19 12:21:14 -07:00
Timm Baeder
ecbd2d5e14
[clang][ExprConst] Diagnose ptr subs with non-zero offset (#135938)
The attached test case was missing the note.
2025-04-17 17:27:19 +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
Aaron Ballman
00c43ae235
[C2y] Implement WG14 N3369 and N3469 (_Countof) (#133125)
C2y adds the `_Countof` operator which returns the number of elements in
an array. As with `sizeof`, `_Countof` either accepts a parenthesized
type name or an expression. Its operand must be (of) an array type. When
passed a constant-size array operand, the operator is a constant
expression which is valid for use as an integer constant expression.

This is being exposed as an extension in earlier C language modes, but
not in C++. C++ already has `std::extent` and `std::size` to cover these
needs, so the operator doesn't seem to get the user enough benefit to
warrant carrying this as an extension.

Fixes #102836
2025-03-27 13:23:16 -04:00
marius doerner
4067581aea
[clang] Placement new error when modifying consts (#132460)
Raise an error when placement new is used to modify a const-qualified
variable in a constexpr function.

Fixes #131432
2025-03-25 20:39:12 +01:00
Matheus Izvekov
1416566449
Reland: [clang] NFC: Clear some uses of MemberPointerType::getClass (#132317)
Relands Original PR: https://github.com/llvm/llvm-project/pull/131965
Addresses
https://github.com/llvm/llvm-project/pull/131965#issuecomment-2741619498
* Fixes isIncompleteType for injected classes

This clears up some uses of getClass on MemberPointerType when
equivalent uses of getMostRecentCXXRecordDecl would be just as simple or
simpler.
    
This is split-off from a larger patch which removes getClass, in order
to facilitate review.
2025-03-21 10:54:24 -03:00
Timm Baeder
1667a2afd8
[clang][ExprConst] Check record base classes for valid structs (#132270)
In error cases, the base might be None.

Fixes https://github.com/llvm/llvm-project/issues/132257
2025-03-21 07:37:18 +01:00
Matheus Izvekov
335a4614de
Revert "[clang] NFC: Clear some uses of MemberPointerType::getClass" (#132281)
Reverts llvm/llvm-project#131965

Reverted due to issue reported here:
https://github.com/llvm/llvm-project/pull/131965#issuecomment-2741619498
2025-03-20 17:54:21 -03:00
Matheus Izvekov
fd7be0d2e9
[clang] NFC: Clear some uses of MemberPointerType::getClass (#131965) 2025-03-19 21:36:10 -03:00
Ayokunle Amodu
02744c5010
[clang][diagnostics] Update note_constexpr_invalid_cast to use enum_select and adjust its uses (#130868)
Handles #123121

This patch updates `note_constexpr_invalid_cast` diagnostic to use
`enum_select` instead of `select,` improving readability and reducing
reliance on magic numbers in caller sites.
2025-03-19 12:36:08 -07:00
cor3ntin
bc8b19c757
[Clang] Introduce a trait to determine the structure binding size (#131515)
Introduce a trait to determine the number of bindings that would be
produced by

```cpp

   auto [...p] = expr;

```

This is necessary to implement P2300
(https://eel.is/c++draft/exec#snd.concepts-5), but can also be used to
implement a general get<N> function that supports aggregates

`__builtin_structured_binding_size` is a unary type trait that evaluates
to the number of bindings in a decomposition

If the argument cannot be decomposed, a sfinae-friendly error is
produced.

A type is considered a valid tuple if `std::tuple_size_v<T>` is a valid
expression, even if there is no valid `std::tuple_element`
specialization or suitable `get` function for that type.


Fixes #46049
2025-03-18 20:50:56 +01:00
cor3ntin
911b200ce3
[Clang] Constant Expressions inside of GCC' asm strings (#131003)
Implements GCC's constexpr string ASM extension
https://gcc.gnu.org/onlinedocs/gcc/Asm-constexprs.html
2025-03-17 20:10:46 +01:00
Sarah Spall
f9568e8d23
[HLSL] Make memory representation of boolean vectors in HLSL, vectors of i32. Add support for boolean swizzling. (#123977)
Make the memory representation of boolean vectors in HLSL, vectors of
i32.
Allow boolean swizzling for boolean vectors in HLSL.
Add tests for boolean vectors and boolean vector swizzling.
Closes #91639
2025-03-11 13:54:09 -07:00
Younan Zhang
f4218753ad
[Clang] Implement P0963R3 "Structured binding declaration as a condition" (#130228)
This implements the R2 semantics of P0963.

The R1 semantics, as outlined in the paper, were introduced in Clang 6.
In addition to that, the paper proposes swapping the evaluation order of
condition expressions and the initialization of binding declarations
(i.e. std::tuple-like decompositions).
2025-03-11 15:41:56 +08:00
Timm Baeder
35f273ab07
[clang][NFC] Clean up Expr::EvaluateAsConstantExpr (#130498)
The Info.EnableNewConstInterp case is already handled above.
2025-03-10 08:56:23 +01:00
Eli Friedman
42d49a7724
[clang] Reject constexpr-unknown values as constant expressions more consistently (#129952)
Perform the check for constexpr-unknown values in the same place we
perform checks for other values which don't count as constant
expressions.

While I'm here, also fix a rejects-valid with a reference that doesn't
have an initializer. This diagnostic was also covering up some of the
bugs here.

The existing behavior with -fexperimental-new-constant-interpreter seems
to be correct, but the diagnostics are slightly different; it would be
helpful if someone could check on that as a followup.

Followup to #128409.

Fixes #129844. Fixes #129845.
2025-03-09 18:38:55 -07:00
Yingwei Zheng
27757fb874
[Clang] Treat constexpr-unknown value as invalid in EvaluateAsInitializer (#128409)
It is an alternative to
https://github.com/llvm/llvm-project/pull/127525.
Close https://github.com/llvm/llvm-project/issues/127475.
2025-03-05 14:01:24 +08:00