aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/AST/ExprConstant.cpp
AgeCommit message (Collapse)AuthorFilesLines
2024-10-11[clang][ExprConstant] Remove an outdated TODO comment (#111959)Timm Baeder1-8/+5
Seems like passing the quantities directly seems to work fine.
2024-10-03[OpenACC] Implement 'tile' attribute AST (#110999)Erich Keane1-0/+8
The 'tile' clause shares quite a bit of the rules with 'collapse', so a followup patch will add those tests/behaviors. This patch deals with adding the AST node. The 'tile' clause takes a series of integer constant expressions, or *. The asterisk is now represented by a new OpenACCAsteriskSizeExpr node, else this clause is very similar to others.
2024-10-02[clang][x86] Add constexpr support for ADC/SBB + ADX intrinsics (#110668)Simon Pilgrim1-0/+32
ADC and ADX use the same internal intrinsics - for testing I've taken the same approach as the generic builtin overflow tests, putting the intrinsics in a constexpr test wrapper and comparing the carry/result value pair. I've added the addcarry/subborrow intrinsics to the clang language extension list - I'm not sure if we want to add all ISA intrinsics to the list (although we can if people think it useful?), but I felt we should at least include the baseline x86 intrinsics.
2024-10-01[AMDGPU] Specify width and align for all AMDGPU builtin types. NFC. (#109656)Jay Foad1-1/+1
This will be used in ASTContext::getTypeInfo which needs this information for all builtin types, not just pointers.
2024-10-01[clang][x86] Add constexpr support for PDEP/PEXT intrinsics (#110535)Simon Pilgrim1-0/+30
2024-09-30[clang][x86] Add constexpr support for BZHI intrinsics (#110508)Simon Pilgrim1-0/+14
2024-09-30[clang][x86] Add constexpr support for LZCNT/TZCNT intrinsics (#110499)Simon Pilgrim1-0/+18
2024-09-28[clang][x86] Add constexpr support for BMI/TBM BEXTR intrinsics (#109577)Simon Pilgrim1-0/+24
This is an initial patch for constexpr handling of the BEXTR intrinsics - the plan is to support all x86 bit manipulation intrinsics eventually (and then SSE/AVX intrinsics), but I wanted to treat this as an initial test patch. Hopefully this will unstick #94161 as well.
2024-09-26[clang] implement current direction of CWG2765 for string literal ↵Richard Smith1-15/+115
comparisons in constant evaluation (#109208) Track the identity of each string literal object produced by evaluation with a global version number. Accept comparisons between literals of the same version, and between literals of different versions that cannot possibly be placed in overlapping storage. Treat the remaining comparisons as non-constant. --------- Co-authored-by: Timm Baeder <tbaeder@redhat.com> Co-authored-by: Aaron Ballman <aaron@aaronballman.com>
2024-09-17Revert "[clang][ExprConst] Allow comparisons with string literals (#106733)"Timm Bäder1-1/+1
This reverts commit 5d1d2f08c4a92580e7f6b3b6b77b2b6f6184e126. See the discussion in https://github.com/llvm/llvm-project/pull/106733 and https://github.com/llvm/llvm-project/issues/58754
2024-09-13[clang][ExprConst] Allow comparisons with string literals (#106733)Timm Baeder1-1/+1
Don't diagnose them, but literals still have distinct addresses. Fixes https://github.com/llvm/llvm-project/issues/58754
2024-09-11[HLSL] Allow truncation to scalar (#104844)Chris B1-1/+21
HLSL allows implicit conversions to truncate vectors to scalar pr-values. These conversions are scored as vector truncations and should warn appropriately. This change allows forming a truncation cast to a pr-value, but not an l-value. Truncating a vector to a scalar is performed by loading the first element of the vector and disregarding the remaining elements. Fixes #102964
2024-09-07[clang][bytecode] Allow continuing when discarded MemberExpr Base fails ↵Timm Baeder1-3/+3
(#107231) We don't need the value in this case, since we're discarding it anyway. Allow continuing the interpretation but note the side effect.
2024-09-05[Clang] Add __builtin_is_within_lifetime to implement P2641R4's ↵Mital Ashok1-3/+106
std::is_within_lifetime (#91895) [P2641R4](https://wg21.link/P2641R4) This new builtin function is declared `consteval`. Support for `-fexperimental-new-constant-interpreter` will be added in a later patch. --------- Co-authored-by: cor3ntin <corentinjabot@gmail.com>
2024-09-05[Clang] CWG2749: relational operators involving pointers to void (#93046)Mital Ashok1-10/+0
https://cplusplus.github.io/CWG/issues/2749.html This DR's effects are backported to C++98. Does not affect C where integral constant expressions cannot involve pointers. --------- Co-authored-by: Vlad Serebrennikov <serebrennikov.vladislav@gmail.com> Co-authored-by: cor3ntin <corentinjabot@gmail.com>
2024-09-02[clang] The ms-extension __noop should return zero in a constexpr context. ↵c8ef1-2/+2
(#106849) Fixes #106713.
2024-08-31[HLSL] Implement output parameter (#101083)Chris B1-0/+1
HLSL output parameters are denoted with the `inout` and `out` keywords in the function declaration. When an argument to an output parameter is constructed a temporary value is constructed for the argument. For `inout` pamameters the argument is initialized via copy-initialization from the argument lvalue expression to the parameter type. For `out` parameters the argument is not initialized before the call. In both cases on return of the function the temporary value is written back to the argument lvalue expression through an implicit assignment binary operator with casting as required. This change introduces a new HLSLOutArgExpr ast node which represents the output argument behavior. The OutArgExpr has three defined children: - An OpaqueValueExpr of the argument lvalue expression. - An OpaqueValueExpr of the copy-initialized parameter. - A BinaryOpExpr assigning the first with the value of the second. Fixes #87526 --------- Co-authored-by: Damyan Pepper <damyanp@microsoft.com> Co-authored-by: John McCall <rjmccall@gmail.com>
2024-08-30`__noop` not marked as constexpr #102064 (#105983)ofAlpaca1-0/+4
Fixes #102064
2024-08-26[NFC][clang][bytecode] Rename `clang::interp::State::getCtx` to ↵yronglin1-6/+6
`clang::interp::State::getASTContext` (#106071) The new constant interpreter's `clang::interp::InterpState` contains both `clang::interp::Context` and `clang::ASTContext`. So using `S.Ctx` and `S.getCtx()` was a bit confusing. This PR rename `getCtx()` to `getASTContext` to make things more clearer. Signed-off-by: yronglin <yronglin777@gmail.com>
2024-08-23[Clang] Implement P2747 constexpr placement new (#104586)cor3ntin1-23/+40
The implementation follows the resolution of CWG2922
2024-08-18[clang] fix divide by zero in ComplexExprEvaluator (#104666)c8ef1-3/+3
fix: #55390. --------- Co-authored-by: Sergei Barannikov <barannikov88@gmail.com>
2024-08-18[Clang] `constexpr` builtin floating point classification / comparison ↵Mital Ashok1-0/+48
functions (#94118) As per [P0533R9](https://wg21.link/P0533R9), the corresponding C++ `[c.math.fpclass]` standard library functions for the C macros are now `constexpr`. The only classification function that wasn't already `constexpr` was `__builtin_signbit`. The floating point comparison functions `__builtin_isgreater`, `__builtin_isgreaterequal`, `__builtin_isless`, `__builtin_islessequal`, `__builtin_islessgreater` and `__builtin_isunordered` are now `constexpr`. The C23 macro `iseqsig` is not currently supported because `__bulitin_iseqsig` doesn't exist yet (and C++26 is still currently based on C18). This also allows them to be constant folded in C, matching the behaviour of GCC.
2024-08-16[clang] Rename all AST/Interp stuff to AST/ByteCode (#104552)Timm Baeder1-3/+3
"Interp" clashes with the clang interpreter and people often confuse this.
2024-08-14[clang] Turn -Wenum-constexpr-conversion into a hard error (#102364)Carlos Galvez1-4/+2
The warning has been active for a few releases now, first only in user code, later in system headers, and finally as an error by default. Therefore, we believe it is now time to transition into a hard error, as required by the C++ Standard. The main affected C++ projects have by now fixed the error, or there's a pending patch for review that does it. Fixes #59036
2024-08-07[clang][ExprConst] allow single element access of vector object to be ↵Vikram Hegde1-4/+106
constant expression (#101126) This is a slightly updated version of https://github.com/llvm/llvm-project/pull/72607, originally authored by @yuanfang-chen
2024-08-05[HLSL] Implement intangible AST type (#97362)Helena Kotas1-0/+2
HLSL has a set of intangible types which are described in in the [draft HLSL Specification (**[Basic.types]**)](https://microsoft.github.io/hlsl-specs/specs/hlsl.pdf): There are special implementation-defined types such as handle types, which fall into a category of standard intangible types. Intangible types are types that have no defined object representation or value representation, as such the size is unknown at compile time. A class type T is an intangible class type if it contains an base classes or members of intangible class type, standard intangible type, or arrays of such types. Standard intangible types and intangible class types are collectively called intangible types([9](https://microsoft.github.io/hlsl-specs/specs/hlsl.html#Intangible)). This PR implements one standard intangible type `__hlsl_resource_t` and sets up the infrastructure that will make it easier to add more in the future, such as samplers or raytracing payload handles. The HLSL intangible types are declared in `clang/include/clang/Basic/HLSLIntangibleTypes.def` and this file is included with related macro definition in most places that require edits when a new type is added. The new types are added as keywords and not typedefs to make sure they cannot be redeclared, and they can only be declared in builtin implicit headers. The `__hlsl_resource_t` type represents a handle to a memory resource and it is going to be used in builtin HLSL buffer types like this: template <typename T> class RWBuffer { [[hlsl::contained_type(T)]] [[hlsl::is_rov(false)]] [[hlsl::resource_class(uav)]] __hlsl_resource_t Handle; }; Part 1/3 of llvm/llvm-project#90631. --------- Co-authored-by: Justin Bogner <mail@justinbogner.com>
2024-07-24[ExprConstant] Handle shift overflow the same way as other kinds of overflow ↵Eli Friedman1-9/+17
(#99579) We have a mechanism to allow folding expressions that aren't ICEs as an extension; use it more consistently. This ends up causing bad effects on diagnostics in a few cases, but that's not specific to shifts; it's a general issue with the way those uses handle overflow diagnostics.
2024-07-24[PAC] Define __builtin_ptrauth_type_discriminator (#100204)Akira Hatanaka1-0/+6
The builtin computes the discriminator for a type, which can be used to sign/authenticate function pointers and member function pointers. If the type passed to the builtin is a C++ member function pointer type, the result is the discriminator used to signed member function pointers of that type. If the type is a function, function pointer, or function reference type, the result is the discriminator used to sign functions of that type. It is ill-formed to use this builtin with any other type. A call to this function is an integer constant expression. Co-Authored-By: John McCall rjmccall@apple.com
2024-07-24[clang][ExprConst] Allow non-literal types in C++23 (#100062)Timm Baeder1-0/+4
Instead of diagnosing non-literal types in C++23, allow them and later diagnose them differently, e.g. because they have a non-constexpr constructor, destructor, etc. For this test: ```c++ struct NonLiteral { NonLiteral() {} }; constexpr int foo() { NonLiteral L; return 1; } // static_assert(foo() == 1); ``` The current diagnostics with c++20/c++23 are: ```console ~/code/llvm-project/build » clang -c array.cpp -std=c++20 array.cpp:91:14: error: variable of non-literal type 'NonLiteral' cannot be defined in a constexpr function before C++23 91 | NonLiteral L; | ^ array.cpp:87:8: note: 'NonLiteral' is not literal because it is not an aggregate and has no constexpr constructors other than copy or move constructors 87 | struct NonLiteral { | ^ 1 error generated. ------------------------------------------------------------ ~/code/llvm-project/build » clang -c array.cpp -std=c++23 (no output) ``` With the `static_assert` enabled, compiling with `-std=c++23` prints: ```console array.cpp:95:15: error: static assertion expression is not an integral constant expression 95 | static_assert(foo() == 1); | ^~~~~~~~~~ array.cpp:91:14: note: non-literal type 'NonLiteral' cannot be used in a constant expression 91 | NonLiteral L; | ^ array.cpp:95:15: note: in call to 'foo()' 95 | static_assert(foo() == 1); | ^~~~~ 1 error generated. ``` As mentioned in #60311, this is confusing. The output with c++20 suggests that using c++23 will make the problem go away, but it's diagnosed the same when running the function. With this commit, the output instead diagnoses _why_ the non-literal type can't be used: ```console array.cpp:95:15: error: static assertion expression is not an integral constant expression 95 | static_assert(foo() == 1); | ^~~~~~~~~~ array.cpp:91:14: note: non-constexpr constructor 'NonLiteral' cannot be used in a constant expression 91 | NonLiteral L; | ^ array.cpp:95:15: note: in call to 'foo()' 95 | static_assert(foo() == 1); | ^~~~~ array.cpp:88:3: note: declared here 88 | NonLiteral() {} | ^ 1 error generated. ``` Fixes #60311
2024-07-22Handle constant "pointers" for ↵James Y Knight1-10/+26
`__atomic_always_lock_free`/`__atomic_is_lock_free`. (#99340) The second argument passed to these builtins is used to validate whether the object's alignment is sufficient for atomic operations of the given size. Currently, the builtins can be folded at compile time only when the argument is 0/nullptr, or if the _type_ of the pointer guarantees appropriate alignment. This change allows the compiler to also evaluate non-null constant pointers, which enables callers to check a specified alignment, instead of only the type or an exact object. E.g.: `__atomic_is_lock_free(sizeof(T), (void*)4)` can be potentially evaluated to true at compile time, instead of generating a libcall. This is also supported by GCC, and used by libstdc++, and is also useful for libc++'s atomic_ref. Also helps with (but doesn't fix) issue #75081. This also fixes a crash bug, when the second argument was a non-pointer implicitly convertible to a pointer (such as an array, or a function).
2024-07-17[Clang] Fix some assertions not looking through type sugar (#92299)Mital Ashok1-1/+1
Fixes #92284 Co-authored-by: cor3ntin <corentinjabot@gmail.com>
2024-07-11[clang] Emit bad shift warnings (#70307)Budimir Aranđelović1-0/+7
Diagnose bad shifts and emit warnings
2024-07-06[Clang][ExprConstant] fix constant expression did not evaluate to integer ↵Zhikai Zeng1-3/+6
(#97146) fixes https://github.com/llvm/llvm-project/issues/96670 The cause is that we might return a lvalue here at https://github.com/llvm/llvm-project/blob/3e53c97d33210db68188e731e93ee48dbaeeae32/clang/lib/AST/ExprConstant.cpp#L15861-L15865 This PR will make sure we return a rvalue in `FastEvaluateAsRValue`.
2024-06-25Add support for __builtin_verbose_trap (#79230)Akira Hatanaka1-3/+18
The builtin causes the program to stop its execution abnormally and shows a human-readable description of the reason for the termination when a debugger is attached or in a symbolicated crash log. The motivation for the builtin is explained in the following RFC: https://discourse.llvm.org/t/rfc-adding-builtin-verbose-trap-string-literal/75845 clang's CodeGen lowers the builtin to `llvm.trap` and emits debugging information that represents an artificial inline frame whose name encodes the category and reason strings passed to the builtin.
2024-06-20[clang] Define ptrauth_sign_constant builtin. (#93904)Ahmed Bougacha1-0/+1
This is a constant-expression equivalent to ptrauth_sign_unauthenticated. Its constant nature lets us guarantee a non-attackable sequence is generated, unlike ptrauth_sign_unauthenticated which we generally discourage using. It being a constant also allows its usage in global initializers, though requiring constant pointers and discriminators. The value must be a constant expression of pointer type which evaluates to a non-null pointer. The key must be a constant expression of type ptrauth_key. The extra data must be a constant expression of pointer or integer type; if an integer, it will be coerced to ptrauth_extra_data_t. The result will have the same type as the original value. This can be used in constant expressions. Co-authored-by: John McCall <rjmccall@apple.com>
2024-06-20[clang] Define ptrauth_string_discriminator builtin. (#93903)Ahmed Bougacha1-0/+8
This exposes the ABI-stable hash function that allows computing a 16-bit discriminator from a constant string. This allows manually matching the implicit string discriminators computed in the ABI (e.g., from mangled names for vtable pointer/entry signing), as well as enabling the use of interesting discriminators when manually annotating specific pointers with the __ptrauth qualifier. The argument must be a string literal of char character type. The result has type ptrauth_extra_data_t. The result value is never zero and always within range for both the __ptrauth qualifier and ptrauth_blend_discriminator. This can be used in constant expressions. Co-authored-by: John McCall <rjmccall@apple.com>
2024-06-20[Clang] [Sema] Diagnose unknown std::initializer_list layout in SemaInit ↵Mital Ashok1-31/+20
(#95580) This checks if the layout of `std::initializer_list` is something Clang can handle much earlier and deduplicates the checks in CodeGen/CGExprAgg.cpp and AST/ExprConstant.cpp Also now diagnose `union initializer_list` (Fixes #95495), bit-field for the size (Fixes a crash that would happen during codegen if it were unnamed), base classes (that wouldn't be initialized) and polymorphic classes (whose vtable pointer wouldn't be initialized).
2024-06-20Reland [clang][Sema, Lex, Parse] Preprocessor embed in C and C++ (#95802)Mariya Podchishchaeva1-5/+58
This commit implements the entirety of the now-accepted [N3017 -Preprocessor Embed](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3017.htm) and its sister C++ paper [p1967](https://wg21.link/p1967). It implements everything in the specification, and includes an implementation that drastically improves the time it takes to embed data in specific scenarios (the initialization of character type arrays). The mechanisms used to do this are used under the "as-if" rule, and in general when the system cannot detect it is initializing an array object in a variable declaration, will generate EmbedExpr AST node which will be expanded by AST consumers (CodeGen or constant expression evaluators) or expand embed directive as a comma expression. This reverts commit https://github.com/llvm/llvm-project/commit/682d461d5a231cee54d65910e6341769419a67d7. --------- Co-authored-by: The Phantom Derpstorm <phdofthehouse@gmail.com> Co-authored-by: Aaron Ballman <aaron@aaronballman.com> Co-authored-by: cor3ntin <corentinjabot@gmail.com> Co-authored-by: H. Vetinari <h.vetinari@gmx.com>
2024-06-18[Clang][AMDGPU] Add a new builtin type for buffer rsrc (#94830)Shilei Tian1-0/+2
This patch adds a new builtin type for AMDGPU's buffer rsrc data type, which is effectively an AS 8 pointer. This is needed because we'd like to expose certain intrinsics to users via builtins which take buffer rsrc as argument.
2024-06-18[clang][Interp] Implement complex division (#94892)Timm Baeder1-33/+43
Share the implementation with the current interpreter.
2024-06-17[Clang] Disallow non-lvalue values in constant expressions to prevent ↵Oleksandr T1-0/+7
invalid pointer offset computation (#95479) Fixes #95366
2024-06-17[clang][Interp] Implement Complex-complex multiplication (#94891)Timm Baeder1-49/+57
Share the implementation for floating-point complex-complex multiplication with the current interpreter. This means we need a new opcode for this, but there's no good way around that.
2024-06-13[clang][ExprConst][NFC] Replace typecheck+castAs with getAsTimm Bäder1-2/+2
2024-06-12Revert "✨ [Sema, Lex, Parse] Preprocessor embed in C and C++ (and Obj-C ↵Vitaly Buka1-58/+5
and Obj-C++ by-proxy)" (#95299) Reverts llvm/llvm-project#68620 Introduce or expose a memory leak and UB, see llvm/llvm-project#68620
2024-06-12[clang][Sema, Lex, Parse] Preprocessor embed in C and C++ (and Obj-C and ↵The Phantom Derpstorm1-5/+58
Obj-C++ by-proxy) (#68620) This commit implements the entirety of the now-accepted [N3017 - Preprocessor Embed](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3017.htm) and its sister C++ paper [p1967](https://wg21.link/p1967). It implements everything in the specification, and includes an implementation that drastically improves the time it takes to embed data in specific scenarios (the initialization of character type arrays). The mechanisms used to do this are used under the "as-if" rule, and in general when the system cannot detect it is initializing an array object in a variable declaration, will generate EmbedExpr AST node which will be expanded by AST consumers (CodeGen or constant expression evaluators) or expand embed directive as a comma expression. --------- Co-authored-by: Aaron Ballman <aaron@aaronballman.com> Co-authored-by: cor3ntin <corentinjabot@gmail.com> Co-authored-by: H. Vetinari <h.vetinari@gmx.com> Co-authored-by: Podchishchaeva, Mariya <mariya.podchishchaeva@intel.com>
2024-06-11[clang] Replace X && isa<Y>(X) with isa_and_nonnull<Y>(X). NFC (#94987)Pavel Samolysov1-1/+1
This addresses a clang-tidy suggestion.
2024-06-08[clang] Report erroneous floating point results in _Complex math (#90588)Timm Baeder1-6/+21
Use handleFloatFloatBinOp to properly diagnose NaN results and divisions by zero. Fixes #84871
2024-04-29Squashed commit of the following:Pol Marcet Sardà1-2/+125
commit 8d41d93e3fceb3f3af77266f5a8388fc585150a5 Author: Pol Marcet Sardà <polmarcetsarda@gmail.com> Date: Sat Apr 20 12:19:49 2024 +0200 Address some misc comments; added a diagnostic and expanded macros in testing. commit 9493c0f290b558947d8b3ae8e1adf909b0fb9dcd Author: Pol Marcet Sardà <polmarcetsarda@gmail.com> Date: Sun Mar 31 18:18:45 2024 +0200 Following the review of sethp, I have made the following changes: -- Added diagnostic for the undefined shuffle of -1 -- Validated support for _BitInt -- A bunch of other minnor tweaks here and there commit 8273abc8d56ef8225cf4dba84f66a1e54a2ef036 Author: Pol Marcet Sardà <polmarcetsarda@gmail.com> Date: Thu Jan 4 12:31:08 2024 +0100 Fix typo in file name commit ff68f23921966c7d9605f91a47d6b481bf1d7a7b Author: Pol Marcet Sardà <polmarcetsarda@gmail.com> Date: Thu Jan 4 11:26:08 2024 +0100 Address suggestions from RKSimon commit c14783de45687c754253c0cbf8a7834c7f986d80 Author: Pol Marcet Sardà <polmarcetsarda@gmail.com> Date: Sat Dec 30 13:59:00 2023 +0100 [clang] Constexpr for __builtin_shufflevector and __builtin_convertvector Summary: This patch adds constexpr support for __builtin_shufflevector and __builtin_convertvector. A small oddity encountered was that the arg to the intrinsics may be an lvalue without any sort of implicit cast of any kind. I solved this through the EvaluateVectorOrLValue function, which treats the lvalue as if it was in an rvalue cast, which gets me the desired vector. Co-Authored-By: Seth Pellegrino <seth@codecopse.net>
2024-04-29[clang] Allow constexpr cast from `void*` in more cases (#89484)offsetof1-3/+4
[[expr.const]/5.14](https://eel.is/c++draft/expr.const#5.14) says that constexpr cast from <code>*cv* void\*</code> to `T*` is OK if the pointee type is similar to `T`, but Clang currently only permits the conversion if the types are the same except top-level cv-qualifiers. This patch also allows casting `(void*)nullptr`, implementing the resolution of [CWG2819](https://cplusplus.github.io/CWG/issues/2819). --------- Co-authored-by: Vlad Serebrennikov <serebrennikov.vladislav@gmail.com>
2024-04-25[NFC] Generalize ArraySections to work for OpenACC in the future (#89639)Erich Keane1-1/+1
OpenACC is going to need an array sections implementation that is a simpler version/more restrictive version of the OpenMP version. This patch moves `OMPArraySectionExpr` to `Expr.h` and renames it `ArraySectionExpr`, then adds an enum to choose between the two. This also fixes a couple of 'drive-by' issues that I discovered on the way, but leaves the OpenACC Sema parts reasonably unimplemented (no semantic analysis implementation), as that will be a followup patch.