aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/AST/ExprConstant.cpp
AgeCommit message (Collapse)AuthorFilesLines
2023-10-02[C++] Implement "Deducing this" (P0847R7)Corentin Jabot1-11/+19
This patch implements P0847R7 (partially), CWG2561 and CWG2653. Reviewed By: aaron.ballman, #clang-language-wg Differential Revision: https://reviews.llvm.org/D140828
2023-09-29[clang] Fix a crash from nested ArrayInitLoopExpr (#67722)isuckatcs1-0/+13
This patch makes sure that everything is cleaned up properly when ExprConstant evaluates an ArrayInitLoopExpr. Fixes #57135
2023-09-29Remove unsed parameter; NFCAaron Ballman1-2/+1
2023-09-27Revert "[clang][SemaCXX] Diagnose tautological uses of consteval if and ↵Sam McCall1-0/+16
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[clang][SemaCXX] Diagnose tautological uses of consteval if and ↵Takuya Shimizu1-16/+0
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-14[clang] Bail out when handling union access with virtual inheritanceAntonio Frighetto1-5/+12
An assertion issue that arose when handling union member access with virtual base class has been addressed. As pointed out by @zygoloid, there is no need for further derived-to-base analysis in this instance, so we can bail out upon encountering a virtual base class. Minor refinement on the function name as we might not be handling a union. Reported-By: ormris Fixes: https://github.com/llvm/llvm-project/issues/65982
2023-09-11[clang][Diagnostics] Add source range to uninitialized diagnostics (#65896)Timm Baeder1-2/+4
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-05[AST] Use correct APSInt width when evaluating string literalsSergei Barannikov1-3/+2
The width of the APSInt values should be the width of an element. getCharByteWidth returns the size of an element in _host_ bytes, which makes the width N times greater, where N is the ratio between target's CHAR_BIT and host's CHAR_BIT. This is NFC for in-tree targets because all of them have CHAR_BIT == 8. Reviewed By: cor3ntin, aaron.ballman Differential Revision: https://reviews.llvm.org/D154773
2023-09-04Add support of Windows Trace Logging macrosRichard Dzenis1-0/+3
Consider the following code: #include <windows.h> #include <TraceLoggingActivity.h> #include <TraceLoggingProvider.h> #include <winmeta.h> TRACELOGGING_DEFINE_PROVIDER( g_hMyComponentProvider, "SimpleTraceLoggingProvider", // {0205c616-cf97-5c11-9756-56a2cee02ca7} (0x0205c616,0xcf97,0x5c11,0x97,0x56,0x56,0xa2,0xce,0xe0,0x2c,0xa7)); void test() { TraceLoggingFunction(g_hMyComponentProvider); } int main() { TraceLoggingRegister(g_hMyComponentProvider); test(); TraceLoggingUnregister(g_hMyComponentProvider); } It compiles with MSVC, but clang-cl reports an error: C:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\shared/TraceLoggingActivity.h(377,30): note: expanded from macro '_tlgThisFunctionName' #define _tlgThisFunctionName __FUNCTION__ ^ .\tl.cpp(14,5): error: cannot initialize an array element of type 'char' with an lvalue of type 'const char[5]' TraceLoggingFunction(g_hMyComponentProvider); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The second commit is not needed to support above code, however, during isolated tests in ms_predefined_expr.cpp I found that MSVC accepts code with constexpr, whereas clang-cl does not. I see that in most places PredefinedExpr is supported in constant evaluation, so I didn't wrap my code with ``if(MicrosoftExt)``. Reviewed By: cor3ntin Differential Revision: https://reviews.llvm.org/D158591
2023-08-28[AST] Fix crash in evaluation of OpaqueExpr in ClangdIlya Biryukov1-1/+2
The code mistakenly returns the value as evaluated whenever the temporary is allocated, but not yet evaluated. I could only reproduce in Clangd and could not come up with an example that would crash the compiler. Clangd runs more evaluations for hover than the language would allow. Reviewed By: kadircet Differential Revision: https://reviews.llvm.org/D158985
2023-08-27[AST] Modernize EvalResult (NFC)Kazu Hirata1-2/+2
2023-08-25[clang] Fix crash in __builtin_strncmp and other related builtin functionsShafik Yaghmour1-2/+2
The implementation of __builtin_strncmp and other related builtins function use getExtValue() to evaluate the size argument. This can cause a crash when the value does not fit into an int64_t value, which is can be expected since the type of the argument is size_t. The fix is to switch to using getZExtValue(). This fixes: https://github.com/llvm/llvm-project/issues/64876 Differential Revision: https://reviews.llvm.org/D158557
2023-08-25[clang][ExprConstant] Improve error message of compound assignment against ↵Takuya Shimizu1-0/+4
uninitialized object BEFORE this patch, compound assignment operator against uninitialized object such as uninit += 1 was diagnosed as subexpression not valid This patch clarifies the reason for the error by saying that uninit is an uninitialized object. Fixes https://github.com/llvm/llvm-project/issues/51536 Reviewed By: shafik, tbaeder Differential Revision: https://reviews.llvm.org/D157855
2023-08-24[Clang] Always constant-evaluate operands of comparisons to nullptrCorentin Jabot1-0/+4
even if we know what the result is going to be. There may be side effects we ought not to ignore, Fixes #64923 Reviewed By: ChuanqiXu Differential Revision: https://reviews.llvm.org/D158601
2023-08-19[clang][Diagnostics] Provide source range to integer-overflow warningsTakuya Shimizu1-2/+2
BEFORE: ``` overflow.cpp:1:21: warning: overflow in expression; result is -2147483648 with type 'int' [-Winteger-overflow] 1 | int x = __INT_MAX__ + 1 + 3; | ^ overflow.cpp:2:9: warning: overflow in expression; result is -2147483648 with type 'int' [-Winteger-overflow] 2 | int a = -(1 << 31) + 1; | ^ ``` AFTER: ``` overflow.cpp:1:21: warning: overflow in expression; result is -2147483648 with type 'int' [-Winteger-overflow] 1 | int x = __INT_MAX__ + 1 + 3; | ~~~~~~~~~~~~^~~ overflow.cpp:2:9: warning: overflow in expression; result is -2147483648 with type 'int' [-Winteger-overflow] 2 | int a = -(1 << 31) + 1; | ^~~~~~~~~~ ``` Reviewed By: tbaeder Differential Revision: https://reviews.llvm.org/D157383
2023-08-17[clang][SVE] Rename isVLSTBuiltinType, NFCJianjian GUAN1-1/+1
Since we also have VLST for rvv now, it is not clear to keep using `isVLSTBuiltinType`, so I added prefix SVE to it. Reviewed By: paulwalker-arm Differential Revision: https://reviews.llvm.org/D158045
2023-08-16[clang][ExprConst] Use call source range for 'in call to' diagsTimm Bäder1-26/+30
Differential Revision: https://reviews.llvm.org/D156604
2023-08-14[clang] Enable constexpr on LZCNT/POPCNT MS extension intrinsicsAlejandro Aguirre1-3/+16
As discussed on #46593 - this enables us to use __lzcnt / __popcnt intrinsics inside constexpr code. Differential Revision: https://reviews.llvm.org/D157420
2023-08-09[clang][ExprConst] Add RHS source range to div by zero diagsTimm Bäder1-4/+5
Differential Revision: https://reviews.llvm.org/D157074
2023-08-08Revert "[clang][ExprConst] Add RHS source range to div by zero diags"Timm Bäder1-9/+9
This reverts commit 74c141a467caf9ebb4835458bc4ffbedb172a63a. Looks like this breaks a whole bunch of unexpected tests: https://lab.llvm.org/buildbot/#/builders/109/builds/70813
2023-08-08[clang][ExprConst] Add RHS source range to div by zero diagsTimm Bäder1-9/+9
Differential Revision: https://reviews.llvm.org/D157074
2023-08-08[clang][ExprConstant] Fix crash on uninitialized base class subobjectTakuya Shimizu1-3/+10
This patch fixes the reported regression caused by D146358 through adding notes about an uninitialized base class when we diagnose uninitialized constructor. This also changes the wording from the old one in order to make it clear that the uninitialized subobject is a base class and its constructor is not called. Wording changes: BEFORE: `subobject of type 'Base' is not initialized` AFTER: `constructor of base class 'Base' is not called` Fixes https://github.com/llvm/llvm-project/issues/63496 Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D153969
2023-08-02[clang] allow const structs/unions/arrays to be constant expressions for CNick Desaulniers1-14/+0
For code like: struct foo { ... }; struct bar { struct foo foo; }; const struct foo my_foo = { ... }; struct bar my_bar = { .foo = my_foo }; Eli Friedman points out the relevant part of the C standard seems to have some flexibility in what is considered a constant expression: 6.6 paragraph 10: An implementation may accept other forms of constant expressions. GCC 8 added support for these, so clang not supporting them has been a constant thorn in the side of source code portability within the Linux kernel. Fixes: https://github.com/llvm/llvm-project/issues/44502 Reviewed By: efriedma Differential Revision: https://reviews.llvm.org/D76096
2023-07-31[clang][ExprConstant] Print template arguments when describing stack frameTakuya Shimizu1-2/+6
This patch adds additional printing of template argument list when the described function is a template specialization. This can be useful when handling complex template functions in constexpr evaluator. Reviewed By: cjdb, dblaikie Differential Revision: https://reviews.llvm.org/D154366
2023-07-28[Clang] Improve the handling of large arrays evaluation.Corentin Jabot1-10/+49
This is a temporary fix (for clang 17) that caps the size of any array we try to constant evaluate: There are 2 limits: * We cap to UINT_MAX the size of ant constant evaluated array, because the constant evaluator does not support size_t. * We cap to `-fconstexpr-steps` elements the size of each individual array and dynamic array allocations. This works out because the number of constexpr steps already limits how many array elements can be initialized, which makes this new limit conservatively generous. This ensure that the compiler does not crash when attempting to constant-fold valid programs. If the limit is reached by a given array, constant evaluation will fail, and the program will be ill-formed, until a bigger limit is given. Or, constant folding will fail and the array will be evaluated at runtime. Fixes #63562 Reviewed By: efriedma Differential Revision: https://reviews.llvm.org/D155955
2023-07-28[clang][Diagnostics] Provide source range to invalid casts in const exprTimm Bäder1-1/+1
Differential Revision: https://reviews.llvm.org/D153241
2023-07-26[clang] Provide source range to 'invalid subexpr in const expr' diagsTimm Bäder1-2/+2
Differential Revision: https://reviews.llvm.org/D150566
2023-07-24[clang][ConstantEmitter] have tryEmitPrivate[ForVarInit] try ↵Nick Desaulniers1-3/+5
ConstExprEmitter fast-path first As suggested by @efriedma in: https://reviews.llvm.org/D76096#4370369 This should speed up evaluating whether an expression is constant or not, but due to the complexity of these two different implementations, we may start getting different answers for edge cases for which we do not yet have test cases in-tree (or perhaps even performance regressions for some cases). As such, contributors have carte blanche to revert if necessary. For additional historical context about ExprConstant vs CGExprConstant, here's snippets from a private conversation on discord: ndesaulniers: why do we have clang/lib/AST/ExprConstant.cpp and clang/lib/CodeGen/CGExprConstant.cpp? Does clang constant fold during ast walking/creation AND during LLVM codegen? efriedma: originally, clang needed to handle two things: integer constant expressions (the "5" in "int x[5];"), and constant global initializers (the "5" in "int x = 5;"). pre-C++11, the two could be handled mostly separately; so we had the code for integer constants in AST/, and the code for globals in CodeGen/. C++11 constexpr sort of destroyed that separation, though. so now we do both kinds of constant evaluation on the AST, then CGExprConstant translates the result of that evaluation to LLVM IR. but we kept around some bits of the old cgexprconstant to avoid performance/memory usage regressions on large arrays. Reviewed By: efriedma Differential Revision: https://reviews.llvm.org/D151587
2023-07-20[Clang] Implement P2741R3 - user-generated static_assert messagesCorentin Jabot1-1/+43
Reviewed By: #clang-language-wg, aaron.ballman Differential Revision: https://reviews.llvm.org/D154290
2023-07-12[NFC] Initialize class member pointers to nullptr.Sindhu Chittireddy1-1/+1
Fix clang-format issues in surrounding code. Differential revision: https://reviews.llvm.org/D153892
2023-07-09[clang][ConstExpr][NFC] Make Frame::describe() constTimm Bäder1-2/+2
2023-07-07[AST] Stop evaluate constant expression if the condition expression which in ↵yronglin1-5/+6
switch statement contains errors This fix issue: https://github.com/llvm/llvm-project/issues/63453 ``` constexpr int foo(unsigned char c) { switch (f) { case 0: return 7; default: break; } return 0; } static_assert(foo('d')); ``` Reviewed By: aaron.ballman, erichkeane, hokein Differential Revision: https://reviews.llvm.org/D153296
2023-07-05[clang][NFC] Move two declarations closer to their point of useTimm Bäder1-3/+3
2023-07-02[C11] Correct global atomic pointer initialization from an integer constantAaron Ballman1-0/+2
This is a follow-up to 2e275e24355cb224981f9beb2b026a3169fc7232 and 1395cde24b3641e284bb1daae7d56c189a2635e3 which corrects a missed case: initializing an _Atomic(T *) from a null pointer constant in the form of the integer literal 0. Fixes https://github.com/llvm/llvm-project/issues/63550 Differential Revision: https://reviews.llvm.org/D154284
2023-06-28[clang][ExprConstant] Fix display of syntactically-invalid note for member ↵Takuya Shimizu1-19/+46
function calls This patch makes the display of member function calls more true to the user-written code by making use of the syntactical structure of the function calls. This patch also changes the display of conventional value-based printing from arrow operator to dot operator. This avoids the syntactical invalidness in notes previously caused by the display of & operator (lack of parentheses and reference of rvalue) Fixes https://github.com/llvm/llvm-project/issues/57081 Reviewed By: cjdb Differential Revision: https://reviews.llvm.org/D151720
2023-06-26[Clang] Implement P2738R1 - constexpr cast from void*Corentin Jabot1-8/+16
Reviewed By: #clang-language-wg, erichkeane Differential Revision: https://reviews.llvm.org/D153702
2023-06-25[llvm] Add missing StringExtras.h includesElliot Goodrich1-0/+1
In preparation for removing the `#include "llvm/ADT/StringExtras.h"` from the header to source file of `llvm/Support/Error.h`, first add in all the missing includes that were previously included transitively through this header.
2023-06-22[CLANG] Fix uninitialized scalar field issuesManna, Soumi1-1/+1
Reviewed By: erichkeane, steakhal, tahonermann, shafik Differential Revision: https://reviews.llvm.org/D150744
2023-06-18[clang] Add __builtin_isfpclassSerge Pavlov1-0/+10
A new builtin function __builtin_isfpclass is added. It is called as: __builtin_isfpclass(<floating point value>, <test>) and returns an integer value, which is non-zero if the floating point argument falls into one of the classes specified by the second argument, and zero otherwise. The set of classes is an integer value, where each value class is represented by a bit. There are ten data classes, as defined by the IEEE-754 standard, they are represented by bits: 0x0001 (__FPCLASS_SNAN) - Signaling NaN 0x0002 (__FPCLASS_QNAN) - Quiet NaN 0x0004 (__FPCLASS_NEGINF) - Negative infinity 0x0008 (__FPCLASS_NEGNORMAL) - Negative normal 0x0010 (__FPCLASS_NEGSUBNORMAL) - Negative subnormal 0x0020 (__FPCLASS_NEGZERO) - Negative zero 0x0040 (__FPCLASS_POSZERO) - Positive zero 0x0080 (__FPCLASS_POSSUBNORMAL) - Positive subnormal 0x0100 (__FPCLASS_POSNORMAL) - Positive normal 0x0200 (__FPCLASS_POSINF) - Positive infinity They have corresponding builtin macros to facilitate using the builtin function: if (__builtin_isfpclass(x, __FPCLASS_NEGZERO | __FPCLASS_POSZERO) { // x is any zero. } The data class encoding is identical to that used in llvm.is.fpclass function. Differential Revision: https://reviews.llvm.org/D152351
2023-06-14[Clang] Show type in enum out of range diagnosticDimitry Andric1-4/+5
When the diagnostic for an out of range enum value is printed, it currently does not show the actual enum type in question, for example: v8/src/base/bit-field.h:43:29: error: integer value 7 is outside the valid range of values [0, 3] for this enumeration type [-Wenum-constexpr-conversion] static constexpr T kMax = static_cast<T>(kNumValues - 1); ^ This can make it cumbersome to find the cause for the problem. Add the enum type to the diagnostic message, to make it easier. Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D152788
2023-06-08[ExprConstant] fix ICE from Evaluate on _Atomic structsNick Desaulniers1-0/+1
Fixes two crashes reported by @efriedma and @ahatanak, as per @ahatanak's analysis. Link: https://reviews.llvm.org/D151587#4397446 Link: https://reviews.llvm.org/D151587#4399684 Reviewed By: ahatanak, aaron.ballman Differential Revision: https://reviews.llvm.org/D152303
2023-06-07[clang] Implement P2564 "consteval must propagate up"Corentin Jabot1-8/+7
Reviewed By: aaron.ballman, #clang-language-wg Differential Revision: https://reviews.llvm.org/D151094
2023-05-28[NFC][CLANG] Fix issue with dereference null return value in ↵Manna, Soumi1-2/+1
EvaluateBuiltinClassifyType() This patch uses cast instead of dyn_cast which will assert if the type doesn't match. Reviewed By: erichkeane Differential Revision: https://reviews.llvm.org/D151469
2023-05-26[Clang][Attribute] Improve the AST/diagnoses fidelity of alignas and _Alignasyronglin1-2/+1
- Fix diagnoses when the argument to `alignas` or `_Alignas` is an incomplete type. Before: ``` ./alignas.cpp:1:15: error: invalid application of 'alignof' to an incomplete type 'void' class alignas(void) Foo {}; ~^~~~~ 1 error generated. ``` Now: ``` ./alignas.cpp:1:15: error: invalid application of 'alignas' to an incomplete type 'void' class alignas(void) Foo {}; ~^~~~~ 1 error generated. ``` - Improve the AST fidelity of `alignas` and `_Alignas` attribute. Before: ``` AlignedAttr 0x13f07f278 <col:7> alignas `-ConstantExpr 0x13f07f258 <col:15, col:21> 'unsigned long' |-value: Int 8 `-UnaryExprOrTypeTraitExpr 0x13f07f118 <col:15, col:21> 'unsigned long' alignof 'void *' ``` Now: ``` AlignedAttr 0x14288c608 <col:7> alignas 'void *' ``` Reviewed By: erichkeane Differential Revision: https://reviews.llvm.org/D150528
2023-05-24Reland: [clang][AST] Print name instead of type when diagnosing ↵Takuya Shimizu1-17/+16
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-23Reland: [clang][ExprConstant] fix __builtin_object_size for flexible array ↵Nick Desaulniers1-1/+15
members As reported by @kees, GCC treats __builtin_object_size of structures containing flexible array members (aka arrays with incomplete type) not just as the sizeof the underlying type, but additionally the size of the members in a designated initializer list. Fixes: https://github.com/llvm/llvm-project/issues/62789 Reviewed By: erichkeane, efriedma Differential Revision: https://reviews.llvm.org/D150892
2023-05-23[NFC][CLANG] Fix static code analyzer concernsManna, Soumi1-0/+2
Reported by Static Code Analyzer Tool, Coverity: Dereference null return value Inside "ExprConstant.cpp" file, in <unnamed>::RecordExprEvaluator::VisitCXXStdInitializerListExpr(clang::CXXStdInitializerListExpr const *): Return value of function which returns null is dereferenced without checking. bool RecordExprEvaluator::VisitCXXStdInitializerListExpr( const CXXStdInitializerListExpr *E) { // returned_null: getAsConstantArrayType returns nullptr (checked 81 out of 93 times). //var_assigned: Assigning: ArrayType = nullptr return value from getAsConstantArrayType. const ConstantArrayType *ArrayType = Info.Ctx.getAsConstantArrayType(E->getSubExpr()->getType()); LValue Array; //Condition !EvaluateLValue(E->getSubExpr(), Array, this->Info, false), taking false branch. if (!EvaluateLValue(E->getSubExpr(), Array, Info)) return false; // Get a pointer to the first element of the array. //Dereference null return value (NULL_RETURNS) //dereference: Dereferencing a pointer that might be nullptr ArrayType when calling addArray. Array.addArray(Info, E, ArrayType); This patch adds an assert for unexpected type for array initializer. Reviewed By: erichkeane Differential Revision: https://reviews.llvm.org/D151040
2023-05-23Revert "[clang][ExprConstant] fix __builtin_object_size for flexible array ↵Krasimir Georgiev1-8/+1
members" This reverts commit 57c5c1ab2a188b7962c9de5ac0f95e3c7441940a. Causes an assertion failure: https://reviews.llvm.org/D150892#4363080
2023-05-22[clang][ExprConstant] fix __builtin_object_size for flexible array membersNick Desaulniers1-1/+8
As reported by @kees, GCC treats __builtin_object_size of structures containing flexible array members (aka arrays with incomplete type) not just as the sizeof the underlying type, but additionally the size of the members in a designated initializer list. Fixes: https://github.com/llvm/llvm-project/issues/62789 Reviewed By: erichkeane Differential Revision: https://reviews.llvm.org/D150892
2023-05-18Revert "[clang][AST] Print name instead of type when diagnosing ↵Erich Keane1-16/+17
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.