aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/AST/ExprConstant.cpp
AgeCommit message (Collapse)AuthorFilesLines
2023-05-16[clang][AST] Print name instead of type when diagnosing uninitialized ↵Takuya Shimizu1-17/+16
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-08[Clang] Correctly handle allocation in template argumentsCorentin Jabot1-2/+10
Fixes #62462 Reviewed By: #clang-language-wg, erichkeane Differential Revision: https://reviews.llvm.org/D150036
2023-05-04[clang] Use -std=c++23 instead of -std=c++2bMark de Wever1-1/+1
During the ISO C++ Committee meeting plenary session the C++23 Standard has been voted as technical complete. This updates the reference to c++2b to c++23 and updates the __cplusplus macro. Drive-by fixes c++1z -> c++17 and c++2a -> c++20 when seen. Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D149553
2023-04-25Fix codegen for initialization of global atomicsAaron Ballman1-0/+1
This amends 2e275e24355cb224981f9beb2b026a3169fc7232. That commit added a null to pointer cast kind when determining whether the expression can be a valid constant initializer, but failed to update the constant expression evaluator to perform the evaluation. This commit updates the constant expression evaluator to handle that cast kind.
2023-04-17Constexpr evaluator should treat [[gnu::weak]] member pointer comparisons as ↵Takuya Shimizu1-2/+2
evaluation failure This patch fixes the wrong signal from the constexpr evaluator that [[gnu::weak]] member pointer comparison is valid, while it is emitting notes on them. I found a crashing case fixed by this change and added it as a test case: https://godbolt.org/z/8391fGjGn I noticed this while I was working on D146358. Differential Revision: https://reviews.llvm.org/D148419
2023-03-13[clang][AST] Improve diagnostic for `nullptr` constexpr function pointer callTakuya Shimizu1-0/+5
This patch improves diagnostic for clang constexpr evaluator by adding a check for nullptr in function pointer call evaluations. ex. ``` constexpr int foo(int (*bla)(void)) { return bla(); } static_assert(foo(nullptr) == 1); ``` BEFORE this patch, clang generates the following diagnostic for the code above: ``` <source>:5:15: error: static assertion expression is not an integral constant expression static_assert(foo(nullptr) == 1); ^~~~~~~~~~~~~~~~~ <source>:2:10: note: subexpression not valid in a constant expression return bla(); ^ <source>:5:15: note: in call to 'foo(nullptr)' static_assert(foo(nullptr) == 1); ^ 1 error generated. ``` AFTER this patch, subexpression not valid in a constant expression note is replaced with 'bla' evaluates to a null function pointer. Fixes https://github.com/llvm/llvm-project/issues/59872 Differential Revision: https://reviews.llvm.org/D145793
2023-03-07[clang] Fix single-element array initialization in constexprMariya Podchishchaeva1-1/+1
https://reviews.llvm.org/D130791 added an improvement that in case array element has a trivial constructor, it is evaluated once and the result is re-used for remaining elements. Make sure the constructor is evaluated for single-elements arrays too. Fixes https://github.com/llvm/llvm-project/issues/60803 Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D145486
2023-03-06[clang] Fix aggregate initialization inside lambda constexprMariya Podchishchaeva1-7/+10
Constant evaluator only considered access to `this` pointer to be possible if `this` poitners was captured. However `this` can also appear if there was a default member initializer. Fixes https://github.com/llvm/llvm-project/issues/60936 Reviewed By: shafik Differential Revision: https://reviews.llvm.org/D144866
2023-02-19Use APInt::getSignificantBits instead of APInt::getMinSignedBits (NFC)Kazu Hirata1-1/+1
Note that getMinSignedBits has been soft-deprecated in favor of getSignificantBits.
2023-02-19Use APInt::count{l,r}_{zero,one} (NFC)Kazu Hirata1-4/+4
2023-02-19Use APInt::popcount instead of APInt::countPopulation (NFC)Kazu Hirata1-2/+2
This is for consistency with the C++20-style bit manipulation functions in <bit>.
2023-02-17[WebAssembly] Initial support for reference type externref in clangPaulo Matos1-0/+2
This patch introduces a new type __externref_t that denotes a WebAssembly opaque reference type. It also implements builtin __builtin_wasm_ref_null_extern(), that returns a null value of __externref_t. This lays the ground work for further builtins and reference types. Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D122215
2023-02-05Revert "[clang][WebAssembly] Initial support for reference type externref in ↵Vitaly Buka1-2/+0
clang" Very likely breaks stage 3 of msan build bot. Good: 764c88a50ac76a2df2d051a0eb5badc6867aabb6 https://lab.llvm.org/buildbot/#/builders/74/builds/17058 Looks unrelated: 48b5a06dfcab12cf093a1a3df42cb5b684e2be4c Bad: 48b5a06dfcab12cf093a1a3df42cb5b684e2be4c https://lab.llvm.org/buildbot/#/builders/74/builds/17059 This reverts commit eb66833d19573df97034a81279eda31b8d19815b.
2023-01-31[Clang] Add machinery to catch overflow in unary minus outside of a constant ↵Shafik Yaghmour1-4/+10
expression context We provide several diagnostics for various undefined behaviors due to signed integer overflow outside of a constant expression context. We were missing the machinery to catch overflows due to unary minus. Fixes: https://github.com/llvm/llvm-project/issues/31643 Differential Revision: https://reviews.llvm.org/D142867
2023-01-31[clang][WebAssembly] Initial support for reference type externref in clangPaulo Matos1-0/+2
This patch introduces a new type __externref_t that denotes a WebAssembly opaque reference type. It also implements builtin __builtin_wasm_ref_null_extern(), that returns a null value of __externref_t. This lays the ground work for further builtins and reference types. Differential Revision: https://reviews.llvm.org/D122215
2023-01-29[Clang] Treat `std::forward_like` as builtinAlexander Shaposhnikov1-0/+1
This diff extends D123345 by adding support for std::forward_like. Test plan: ninja check-clang check-clang-tools check-llvm Differential revision: https://reviews.llvm.org/D142430
2023-01-19[clang] Don't short-circuit constant evaluation for array or record typesCharles Magahern1-1/+1
FastEvaluateAsRValue returns `true` without setting a result value for when a given constant expression is an array or record type. Clang attributes must be able to support constant expressions that are array or record types, so proceed with the slower path for evaluation in the case where `FastEvaluateAsRValue` does not yield an evaluation result. Differential Revision: https://reviews.llvm.org/D141745
2023-01-19[clang][Interp][NFCI] Pull IsConstantContext into StateTimm Bäder1-6/+1
This way we can check for this flag in the new interpreter as well.
2023-01-14[clang] Remove remaining uses of llvm::Optional (NFC)Kazu Hirata1-2/+0
This patch removes several "using" declarations and #include "llvm/ADT/Optional.h". This is part of an effort to migrate from llvm::Optional to std::optional: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2023-01-14[clang] Use std::optional instead of llvm::Optional (NFC)Kazu Hirata1-39/+41
This patch replaces (llvm::|)Optional< with std::optional<. I'll post a separate patch to remove #include "llvm/ADT/Optional.h". This is part of an effort to migrate from llvm::Optional to std::optional: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2023-01-12[clang] Reland parenthesized aggregate init patchesAlan Zhao1-16/+62
This commit relands the patches for implementing P0960R3 and P1975R0, which describe initializing aggregates via a parenthesized list. The relanded commits are: * 40c52159d3ee - P0960R3 and P1975R0: Allow initializing aggregates from a parenthesized list of values * c77a91bb7ba7 - Remove overly restrictive aggregate paren init logic * 32d7aae04fdb - Fix a clang crash on invalid code in C++20 mode This patch also fixes a crash in the original implementation. Previously, if the input tried to call an implicitly deleted copy or move constructor of a union, we would then try to initialize the union by initializing it's first element with a reference to a union. This behavior is incorrect (we should fail to initialize) and if the type of the first element has a constructor with a single template typename parameter, then Clang will explode. This patch fixes that issue by checking that constructor overload resolution did not result in a deleted function before attempting parenthesized aggregate initialization. Additionally, this patch also includes D140159, which contains some minor fixes made in response to code review comments in the original implementation that were made after that patch was submitted. Co-authored-by: Sheng <ox59616e@gmail.com> Fixes #54040, Fixes #59675 Reviewed By: ilya-biryukov Differential Revision: https://reviews.llvm.org/D141546
2023-01-12[Clang] Diagnose undefined behavior in a constant expression while ↵Shafik Yaghmour1-4/+5
evaluating a compound assignment with remainder as operand Currently we don't diagnose overflow in a constant expression for the case of compound assignment with remainder as a operand. In handleIntIntBinOp the arguments LHS and Result can be the same source but in the check for remainder in this function we assigned to Result before checking for overflow. In all the other operations the check is done before Result is assigned to. Differential Revision: https://reviews.llvm.org/D140455
2023-01-09Move from llvm::makeArrayRef to ArrayRef deduction guides - clang/ partserge-sans-paille1-5/+5
This is a follow-up to https://reviews.llvm.org/D140896, split into several parts as it touches a lot of files. Differential Revision: https://reviews.llvm.org/D141139
2023-01-06[Sema] Fix crash when evaluating nested call with value-dependent argPierre van Houtryve1-5/+0
Fix an edge case `ExprConstant.cpp`'s `EvaluateWithSubstitution` when called by `CheckEnableIf` The assertion in `CallStackFrame::getTemporary` could fail during evaluation of nested calls to a function using `enable_if` when the second argument was a value-dependent expression. This caused a temporary to be created for the second argument with a given version during the evaluation of the inner call, but we bailed out when evaluating the second argument of the outer call due to the expression being value-dependent. After bailing out, we tried to clean up the argument's value slot but it caused an assertion to trigger in `getTemporary` as a temporary for the second argument existed, but only for the inner call and not the outer call. See the test case for a more complete description of the issue. Reviewed By: ahatanak Differential Revision: https://reviews.llvm.org/D139713
2023-01-04[clang] Revert parentesized aggregate initalization patchesAlan Zhao1-71/+16
This feature causes clang to crash when compiling Chrome - see https://crbug.com/1405031 and https://github.com/llvm/llvm-project/issues/59675 Revert "[clang] Fix a clang crash on invalid code in C++20 mode." This reverts commit 32d7aae04fdb58e65a952f281ff2f2c3f396d98f. Revert "[clang] Remove overly restrictive aggregate paren init logic" This reverts commit c77a91bb7ba793ec3a6a5da3743ed55056291658. Revert "[clang][C++20] P0960R3 and P1975R0: Allow initializing aggregates from a parenthesized list of values" This reverts commit 40c52159d3ee337dbed14e4c73b5616ea354c337.
2022-12-27[clang] Use a StringRef instead of a raw char pointer to store builtin and ↵serge-sans-paille1-10/+10
call information This avoids recomputing string length that is already known at compile time. It has a slight impact on preprocessing / compile time, see https://llvm-compile-time-tracker.com/compare.php?from=3f36d2d579d8b0e8824d9dd99bfa79f456858f88&to=e49640c507ddc6615b5e503144301c8e41f8f434&stat=instructions:u This a recommit of e953ae5bbc313fd0cc980ce021d487e5b5199ea4 and the subsequent fixes caa713559bd38f337d7d35de35686775e8fb5175 and 06b90e2e9c991e211fecc97948e533320a825470. The above patchset caused some version of GCC to take eons to compile clang/lib/Basic/Targets/AArch64.cpp, as spotted in aa171833ab0017d9732e82b8682c9848ab25ff9e. The fix is to make BuiltinInfo tables a compilation unit static variable, instead of a private static variable. Differential Revision: https://reviews.llvm.org/D139881
2022-12-25Revert "[clang] Use a StringRef instead of a raw char pointer to store ↵Vitaly Buka1-10/+10
builtin and call information" Revert "Fix lldb option handling since e953ae5bbc313fd0cc980ce021d487e5b5199ea4 (part 2)" Revert "Fix lldb option handling since e953ae5bbc313fd0cc980ce021d487e5b5199ea4" GCC build hangs on this bot https://lab.llvm.org/buildbot/#/builders/37/builds/19104 compiling CMakeFiles/obj.clangBasic.dir/Targets/AArch64.cpp.d The bot uses GNU 11.3.0, but I can reproduce locally with gcc (Debian 12.2.0-3) 12.2.0. This reverts commit caa713559bd38f337d7d35de35686775e8fb5175. This reverts commit 06b90e2e9c991e211fecc97948e533320a825470. This reverts commit e953ae5bbc313fd0cc980ce021d487e5b5199ea4.
2022-12-24[clang] Use a StringRef instead of a raw char pointer to store builtin and ↵serge-sans-paille1-10/+10
call information This avoids recomputing string length that is already known at compile time. It has a slight impact on preprocessing / compile time, see https://llvm-compile-time-tracker.com/compare.php?from=3f36d2d579d8b0e8824d9dd99bfa79f456858f88&to=e49640c507ddc6615b5e503144301c8e41f8f434&stat=instructions:u This is a recommit of 719d98dfa841c522d8d452f0685e503538415a53 that into account a GGC issue (probably https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92181) when dealing with intiailizer_list and constant expressions. Workaround this by avoiding initializer list, at the expense of a temporary plain old array. Differential Revision: https://reviews.llvm.org/D139881
2022-12-23Revert "[clang] Use a StringRef instead of a raw char pointer to store ↵serge-sans-paille1-10/+10
builtin and call information" There are still remaining issues with GCC 12, see for instance https://lab.llvm.org/buildbot/#/builders/93/builds/12669 This reverts commit 5ce4e92264102de21760c94db9166afe8f71fcf6.
2022-12-23[clang] Use a StringRef instead of a raw char pointer to store builtin and ↵serge-sans-paille1-10/+10
call information This avoids recomputing string length that is already known at compile time. It has a slight impact on preprocessing / compile time, see https://llvm-compile-time-tracker.com/compare.php?from=3f36d2d579d8b0e8824d9dd99bfa79f456858f88&to=e49640c507ddc6615b5e503144301c8e41f8f434&stat=instructions:u This is a recommit of 719d98dfa841c522d8d452f0685e503538415a53 with a change to llvm/utils/TableGen/OptParserEmitter.cpp to cope with GCC bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108158 Differential Revision: https://reviews.llvm.org/D139881
2022-12-23Revert "[clang] Use a StringRef instead of a raw char pointer to store ↵serge-sans-paille1-10/+10
builtin and call information" Failing builds: https://lab.llvm.org/buildbot#builders/9/builds/19030 This is GCC specific and has been reported upstream: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108158 This reverts commit 719d98dfa841c522d8d452f0685e503538415a53.
2022-12-23[clang] Use a StringRef instead of a raw char pointer to store builtin and ↵serge-sans-paille1-10/+10
call information This avoids recomputing string length that is already known at compile time. It has a slight impact on preprocessing / compile time, see https://llvm-compile-time-tracker.com/compare.php?from=3f36d2d579d8b0e8824d9dd99bfa79f456858f88&to=e49640c507ddc6615b5e503144301c8e41f8f434&stat=instructions:u Differential Revision: https://reviews.llvm.org/D139881
2022-12-14[clang][C++20] P0960R3 and P1975R0: Allow initializing aggregates from a ↵Alan Zhao1-16/+71
parenthesized list of values This patch implements P0960R3, which allows initialization of aggregates via parentheses. As an example: ``` struct S { int i, j; }; S s1(1, 1); int arr1[2](1, 2); ``` This patch also implements P1975R0, which fixes the wording of P0960R3 for single-argument parenthesized lists so that statements like the following are allowed: ``` S s2(1); S s3 = static_cast<S>(1); S s4 = (S)1; int (&&arr2)[] = static_cast<int[]>(1); int (&&arr3)[2] = static_cast<int[2]>(1); ``` This patch was originally authored by @0x59616e and completed by @ayzhao. Fixes #54040, Fixes #54041 Co-authored-by: Sheng <ox59616e@gmail.com> Full write up : https://discourse.llvm.org/t/c-20-rfc-suggestion-desired-regarding-the-implementation-of-p0960r3/63744 Reviewed By: ilya-biryukov Differential Revision: https://reviews.llvm.org/D129531
2022-12-12Add missing check for constant evaluation of a comparison of a pointerRichard Smith1-13/+45
to member naming a weak member to nullptr. This fixes a miscompile where constant evaluation would incorrectly determine that a weak member function pointer is never null. In passing, also improve the diagnostics for constant evaluation of some nearby cases.
2022-12-09[AST] Use std::optional in ExprConstant.cpp (NFC)Kazu Hirata1-1/+2
This is part of an effort to migrate from llvm::Optional to std::optional: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-04[clang] Use std::nullopt instead of None in comments (NFC)Kazu Hirata1-1/+1
This is part of an effort to migrate from llvm::Optional to std::optional: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-03[AST] Use std::nullopt instead of None (NFC)Kazu Hirata1-19/+19
This patch mechanically replaces None with std::nullopt where the compiler would warn if None were deprecated. The intent is to reduce the amount of manual work required in migrating from Optional to std::optional. This is part of an effort to migrate from llvm::Optional to std::optional: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-02Use CTAD on llvm::SaveAndRestoreJan Svoboda1-2/+2
Reviewed By: dblaikie Differential Revision: https://reviews.llvm.org/D139229
2022-11-28[Clang] Permit static constexpr variables in constexpr functionsCorentin Jabot1-2/+4
This implement the C++23 paper P2647R1 (adopted in Kona) Reviewed By: #clang-language-wg, erichkeane Differential Revision: https://reviews.llvm.org/D138851
2022-11-23Use std::nullopt_t instead of NoneType (NFC)Kazu Hirata1-2/+2
This patch replaces those occurrences of NoneType that would trigger an error if the definition of NoneType were missing in None.h. To keep this patch focused, I am deliberately not replacing None with std::nullopt in this patch or updating comments. They will be addressed in subsequent patches. This is part of an effort to migrate from llvm::Optional to std::optional: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716 Differential Revision: https://reviews.llvm.org/D138539
2022-11-17[clang] Short-circuit evaluation in ::EvaluateAsConstantExprTimm Bäder1-0/+9
Use FastEvaluateAsRValue() in EvaluateAsConstantExpr() as well, to short-circuit evaluation of simple integrals. Differential Revision: https://reviews.llvm.org/D138115
2022-11-17[clang] Missed rounding mode use in constant evaluationSerge Pavlov1-8/+3
Integer-to-float conversion was handled in constant evaluator with default rounding mode. This change fixes the behavior and the conversion is made using rounding mode stored in ImplicitCastExpr node. Differential Revision: https://reviews.llvm.org/D137719
2022-11-15[ObjC] Fix an assertion failure in EvaluateLValueAkira Hatanaka1-1/+1
Look through parentheses when determining whether the expression is a @selector expression.
2022-10-27[clang] Implement -fstrict-flex-arrays=3Bill Wendling1-9/+23
The -fstrict-flex-arrays=3 is the most restrictive type of flex arrays. No number, including 0, is allowed in the FAM. In the cases where a "0" is used, the resulting size is the same as if a zero-sized object were substituted. This is needed for proper _FORTIFY_SOURCE coverage in the Linux kernel, among other reasons. So while the only reason for specifying a zero-length array at the end of a structure is for specify a FAM, treating it as such will cause _FORTIFY_SOURCE not to work correctly; __builtin_object_size will report -1 instead of 0 for a destination buffer size to keep any kernel internals from using the deprecated members as fake FAMs. For example: struct broken { int foo; int fake_fam[0]; struct something oops; }; There have been bugs where the above struct was created because "oops" was added after "fake_fam" by someone not realizing. Under __FORTIFY_SOURCE, doing: memcpy(p->fake_fam, src, len); raises no warnings when __builtin_object_size(p->fake_fam, 1) returns -1 and may stomp on "oops." Omitting a warning when using the (invalid) zero-length array is how GCC treats -fstrict-flex-arrays=3. A warning in that situation is likely an irritant, because requesting this option level is explicitly requesting this behavior. Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101836 Differential Revision: https://reviews.llvm.org/D134902
2022-10-23[clang] Fix time profile in "isIntegerConstantExpr"Evgeny Shulgin1-3/+1
The time profiler in `Expr::isIntegerConstantExpr` used to call `Loc->printToString`, it was inconsistent with other time profiles in the file and caused segfaults if `Loc` was `nullptr`. Fixes https://github.com/llvm/llvm-project/issues/58551 Reviewed By: dyung, jloser Differential Revision: https://reviews.llvm.org/D136549
2022-10-21[clang] Add time profile for constant evaluationEvgeny Shulgin1-0/+51
Add time profiler for various constexpr evaluation events so that slow event could be visible on the visualized flame chart. Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D136022
2022-10-21[Clang] Add __has_constexpr_builtin supportEvgeny Shulgin1-25/+37
The `__has_constexpr_builtin` macro can be used to check whether the builtin in constant-evaluated by Clang frontend. Reviewed By: aaron.ballman, shafik Differential Revision: https://reviews.llvm.org/D136036
2022-10-10[Clang] Support constexpr builtin fminEvgeny Shulgin1-0/+18
Support constexpr version of __builtin_fmin and its variations. Reviewed By: jcranmer-intel Differential Revision: https://reviews.llvm.org/D135493
2022-10-07[Clang] Support constexpr builtin fmaxEvgeny Shulgin1-0/+18
Support constexpr version of __builtin_fmax and its variations. Reviewed By: jcranmer-intel Differential Revision: https://reviews.llvm.org/D134369
2022-10-06[clang][NFC] Use enum for -fstrict-flex-arraysBill Wendling1-3/+5
Use enums for the strict flex arrays flag so that it's more readable. Differential Revision: https://reviews.llvm.org/D135107