aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/AST/ExprConstant.cpp
AgeCommit message (Collapse)AuthorFilesLines
2019-05-13[c++20] P1064R0: Allow virtual function calls in constant expressionRichard Smith1-21/+207
evaluation. llvm-svn: 360559
2019-05-12Reject attempts to call non-static member functions on objects outsideRichard Smith1-33/+122
their lifetime in constant expressions. This is undefined behavior per [class.cdtor]p2. We continue to allow this for objects whose values are not visible within the constant evaluation, because there's no way we can tell whether the access is defined or not, existing code relies on the ability to make such calls, and every other compiler allows such calls. This reinstates r360499, reverted in r360531. llvm-svn: 360538
2019-05-12Fix handling of objects under construction during constant expressionRichard Smith1-88/+153
evaluation. It's not enough to just track the LValueBase that we're evaluating, we need to also track the path to the objects whose constructors are running. This reinstates r360464 (reverted in r360531) with a workaround for an MSVC bug that previously caused the Windows bots to fail. llvm-svn: 360537
2019-05-11Revert rL360499 and rL360464 from cfe/trunk:Simon Pilgrim1-267/+113
Reject attempts to call non-static member functions on objects outside their lifetime in constant expressions. This is undefined behavior per [class.cdtor]p2. We continue to allow this for objects whose values are not visible within the constant evaluation, because there's no way we can tell whether the access is defined or not, existing code relies on the ability to make such calls, and every other compiler allows such calls. ........ Fix handling of objects under construction during constant expression evaluation. It's not enough to just track the LValueBase that we're evaluating, we need to also track the path to the objects whose constructors are running. ........ Fixes windows buildbots llvm-svn: 360531
2019-05-11Reject attempts to call non-static member functions on objects outsideRichard Smith1-33/+122
their lifetime in constant expressions. This is undefined behavior per [class.cdtor]p2. We continue to allow this for objects whose values are not visible within the constant evaluation, because there's no way we can tell whether the access is defined or not, existing code relies on the ability to make such calls, and every other compiler allows such calls. llvm-svn: 360499
2019-05-10Fix handling of objects under construction during constant expressionRichard Smith1-88/+153
evaluation. It's not enough to just track the LValueBase that we're evaluating, we need to also track the path to the objects whose constructors are running. llvm-svn: 360464
2019-05-10Improve interface of APValuePathEntry.Richard Smith1-42/+25
llvm-svn: 360463
2019-05-10Delete write-only HasQualifiers after rC360370Fangrui Song1-2/+0
llvm-svn: 360408
2019-05-09DR1872: don't allow any calls to virtual functions in constantRichard Smith1-6/+8
evaluation. Not even in cases where we would not actually perform virtual dispatch. llvm-svn: 360370
2019-05-04Disallow the operand of __builtin_constant_p from modifying enclosingRichard Smith1-15/+28
state when it's encountered while evaluating a constexpr function. We attempt to follow GCC trunk's behavior here, but it is somewhat inscrutible, so our behavior is only approximately the same for now. Specifically, we only permit modification of objects whose lifetime began within the operand of the __builtin_constant_p. GCC appears to have effectively the same restriction, but also some unknown restriction based on where and how the local state of the constexpr function is mentioned within the operand (see added testcases). llvm-svn: 359958
2019-05-03Remove else-after-returnDavid Blaikie1-5/+4
llvm-svn: 359913
2019-05-02Fix -Wunsequenced false-positives in code controlled by a branch onRichard Smith1-3/+6
__builtin_constant_p. If the operand of __builtin_constant_p is not constant and has side-effects, then code controlled by a branch on it is unreachable and we should not emit runtime behavior warnings in such code. llvm-svn: 359844
2019-04-27Reinstate r359059, reverted in r359361, with a fix to properly preventRichard Smith1-36/+43
us emitting the operand of __builtin_constant_p if it has side-effects. Original commit message: Fix interactions between __builtin_constant_p and constexpr to match current trunk GCC. GCC permits information from outside the operand of __builtin_constant_p (but in the same constant evaluation context) to be used within that operand; clang now does so too. A few other minor deviations from GCC's behavior showed up in my testing and are also fixed (matching GCC): * Clang now supports nullptr_t as the argument type for __builtin_constant_p * Clang now returns true from __builtin_constant_p if called with a null pointer * Clang now returns true from __builtin_constant_p if called with an integer cast to pointer type llvm-svn: 359367
2019-04-27Revert Fix interactions between __builtin_constant_p and constexpr to match ↵Jorge Gorbe Moya1-35/+28
current trunk GCC. This reverts r359059 (git commit 0b098754b73f3b96d00ecb1c7605760b11c90298) llvm-svn: 359361
2019-04-24[Builtins] Implement __builtin_is_constant_evaluated for use in C++2aEric Fiselier1-0/+4
Summary: This patch implements `__builtin_is_constant_evaluated` as specifier by [P0595R2](https://wg21.link/p0595r2). It is built on the back of Bill Wendling's work for `__builtin_constant_p()`. More tests to come, but early feedback is appreciated. I plan to implement warnings for common mis-usages like those belowe in a following patch: ``` void foo(int x) { if constexpr (std::is_constant_evaluated())) { // condition is always `true`. Should use plain `if` instead. foo_constexpr(x); } else { foo_runtime(x); } } ``` Reviewers: rsmith, MaskRay, bruno, void Reviewed By: rsmith Subscribers: dexonsmith, zoecarver, fdeazeve, kristina, cfe-commits Differential Revision: https://reviews.llvm.org/D55500 llvm-svn: 359067
2019-04-24Fix interactions between __builtin_constant_p and constexpr to matchRichard Smith1-28/+35
current trunk GCC. GCC permits information from outside the operand of __builtin_constant_p (but in the same constant evaluation context) to be used within that operand; clang now does so too. A few other minor deviations from GCC's behavior showed up in my testing and are also fixed (matching GCC): * Clang now supports nullptr_t as the argument type for __builtin_constant_p * Clang now returns true from __builtin_constant_p if called with a null pointer * Clang now returns true from __builtin_constant_p if called with an integer cast to pointer type llvm-svn: 359059
2019-03-08[8.0 Regression] Fix handling of `__builtin_constant_p` inside template ↵Eric Fiselier1-0/+3
arguments, enumerators, case statements, and the enable_if attribute. Summary: The following code is accepted by Clang 7 and prior but rejected by the upcoming 8 release and in trunk [1] ``` // error {{never produces a constant expression}} void foo(const char* s) __attribute__((enable_if(__builtin_constant_p(*s) == false, "trap"))) {} void test() { foo("abc"); } ``` Prior to Clang 8, the call to `__builtin_constant_p` was a constant expression returning false. Currently, it's not a valid constant expression. The bug is caused because we failed to set `InConstantContext` when attempting to evaluate unevaluated constant expressions. [1] https://godbolt.org/z/ksAjmq Reviewers: rsmith, hans, sbenza Reviewed By: rsmith Subscribers: kristina, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D59038 llvm-svn: 355743
2019-03-08[ObjC] Emit a boxed expression as a compile-time constant if theAkira Hatanaka1-0/+4
expression inside the parentheses is a valid UTF-8 string literal. Previously clang emitted an expression like @("abc") as a message send to stringWithUTF8String. This commit makes clang emit the boxed expression as a compile-time constant instead. This commit also has the effect of silencing the nullable-to-nonnull conversion warning clang started emitting after r317727, which originally motivated this commit (see https://oleb.net/2018/@keypath). rdar://problem/42684601 Differential Revision: https://reviews.llvm.org/D58729 llvm-svn: 355662
2019-03-06Inline asm constraints: allow ICE-like pointers for the "n" constraint (PR40890)Hans Wennborg1-6/+5
Apparently GCC allows this, and there's code relying on it (see bug). The idea is to allow expression that would have been allowed if they were cast to int. So I based the code on how such a cast would be done (the CK_PointerToIntegral case in IntExprEvaluator::VisitCastExpr()). Differential Revision: https://reviews.llvm.org/D58821 llvm-svn: 355491
2019-03-06[Fixed Point Arithmetic] Fixed Point and Integer ConversionsLeonard Chan1-0/+30
This patch includes the necessary code for converting between a fixed point type and integer. This also includes constant expression evaluation for conversions with these types. Differential Revision: https://reviews.llvm.org/D56900 llvm-svn: 355462
2019-02-21[Fixed Point Arithmetic] Fixed Point ComparisonsLeonard Chan1-0/+16
This patch implements fixed point comparisons with other fixed point types and integers. This also provides constant expression evaluation for them. Differential Revision: https://reviews.llvm.org/D57219 llvm-svn: 354621
2019-02-14[Builtins] Treat `bcmp` as a builtin.Clement Courbet1-5/+11
Summary: This makes it consistent with `memcmp` and `__builtin_bcmp`. Also see the discussion in https://reviews.llvm.org/D56593. Reviewers: jyknight Subscribers: kristina, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D58120 llvm-svn: 354023
2019-02-09Fix buildbot failure from r353569.Eli Friedman1-1/+8
I assumed lvalue-to-rvalue conversions of array type would never happen, but apparently clang-tidy tries in some cases. llvm-svn: 353598
2019-02-08[Sema] Make string literal init an rvalue.Eli Friedman1-44/+25
This allows substantially simplifying the expression evaluation code, because we don't have to special-case lvalues which are actually string literal initialization. This currently throws away an optimization where we would avoid creating an array APValue for string literal initialization. If we really want to optimize this case, we should fix APValue so it can store simple arrays more efficiently, like llvm::ConstantDataArray. This shouldn't affect the memory usage for other string literals. (Not sure if this is a blocker; I don't think string literal init is common enough for this to be a serious issue, but I could be wrong.) The change to test/CodeGenObjC/encode-test.m is a weird side-effect of these changes: we currently don't constant-evaluate arrays in C, so the strlen call shouldn't be folded, but lvalue string init managed to get around that check. I this this is fine. Fixes https://bugs.llvm.org/show_bug.cgi?id=40430 . llvm-svn: 353569
2019-01-30Add a new builtin: __builtin_dynamic_object_sizeErik Pilkington1-0/+1
This builtin has the same UI as __builtin_object_size, but has the potential to be evaluated dynamically. It is meant to be used as a drop-in replacement for libraries that use __builtin_object_size when a dynamic checking mode is enabled. For instance, __builtin_object_size fails to provide any extra checking in the following function: void f(size_t alloc) { char* p = malloc(alloc); strcpy(p, "foobar"); // expands to __builtin___strcpy_chk(p, "foobar", __builtin_object_size(p, 0)) } This is an overflow if alloc < 7, but because LLVM can't fold the object size intrinsic statically, it folds __builtin_object_size to -1. With __builtin_dynamic_object_size, alloc is passed through to __builtin___strcpy_chk. rdar://32212419 Differential revision: https://reviews.llvm.org/D56760 llvm-svn: 352665
2019-01-19Update the file headers across all of the LLVM projects in the monorepoChandler Carruth1-4/+3
to reflect the new license. We understand that people may be surprised that we're moving the header entirely to discuss the new license. We checked this carefully with the Foundation's lawyer and we believe this is the correct approach. Essentially, all code in the project is now made available by the LLVM project under our new license, so you will see that the license headers include that license only. Some of our contributors have contributed code under our old license, and accordingly, we have retained a copy of our old license notice in the top-level files in each project and repository. llvm-svn: 351636
2019-01-18[Fixed Point Arithmetic] Fixed Point Addition Constant Expression EvaluationLeonard Chan1-49/+148
This patch includes logic for constant expression evaluation of fixed point additions. Differential Revision: https://reviews.llvm.org/D55868 llvm-svn: 351593
2019-01-16[Fixed Point Arithmetic] Add APFixedPoint to APValueLeonard Chan1-0/+3
This adds APFixedPoint to the union of values that can be represented with an APValue. Differential Revision: https://reviews.llvm.org/D56746 llvm-svn: 351368
2019-01-11[AST] Remove ASTContext from getThisType (NFC)Brian Gesiak1-1/+1
Summary: https://reviews.llvm.org/D54862 removed the usages of `ASTContext&` from within the `CXXMethodDecl::getThisType` method. Remove the parameter altogether, as well as all usages of it. This does not result in any functional change because the parameter was unused since https://reviews.llvm.org/D54862. Test Plan: check-clang Reviewers: akyrtzi, mikael Reviewed By: mikael Subscribers: mehdi_amini, dexonsmith, cfe-commits Differential Revision: https://reviews.llvm.org/D56509 llvm-svn: 350914
2018-12-18[ExprConstant] Handle compound assignment when LHS has integral type and RHS ↵Tan S. B.1-7/+19
has floating point type Fixes PR39858 Differential Revision: https://reviews.llvm.org/D55413 llvm-svn: 349444
2018-12-14[Clang] Add __builtin_launderEric Fiselier1-1/+2
Summary: This patch adds `__builtin_launder`, which is required to implement `std::launder`. Additionally GCC provides `__builtin_launder`, so thing brings Clang in-line with GCC. I'm not exactly sure what magic `__builtin_launder` requires, but based on previous discussions this patch applies a `@llvm.invariant.group.barrier`. As noted in previous discussions, this may not be enough to correctly handle vtables. Reviewers: rnk, majnemer, rsmith Reviewed By: rsmith Subscribers: kristina, Romain-Geissler-1A, erichkeane, amharc, jroelofs, cfe-commits, Prazek Differential Revision: https://reviews.llvm.org/D40218 llvm-svn: 349195
2018-12-12[ExprConstant] Improve memchr/memcmp for type mismatch and multibyte element ↵Hubert Tong1-22/+133
types Summary: `memchr` and `memcmp` operate upon the character units of the object representation; that is, the `size_t` parameter expresses the number of character units. The constant folding implementation is updated in this patch to account for multibyte element types in the arrays passed to `memchr`/`memcmp` and, in the case of `memcmp`, to account for the possibility that the arrays may have differing element types (even when they are byte-sized). Actual inspection of the object representation is not implemented. Comparisons are done only between elements with the same object size; that is, `memchr` will fail when inspecting at least one character unit of a multibyte element. The integer types are assumed to have two's complement representation with 0 for `false`, 1 for `true`, and no padding bits. `memcmp` on multibyte elements will only be able to fold in cases where enough elements are equal for the answer to be 0. Various tests are added to guard against incorrect folding for cases that miscompile on some system or other prior to this patch. At the same time, the unsigned 32-bit `wchar_t` testing in `test/SemaCXX/constexpr-string.cpp` is restored. Reviewers: rsmith, aaron.ballman, hfinkel Reviewed By: rsmith Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D55510 llvm-svn: 348938
2018-12-10[constexpr][c++2a] Try-catch blocks in constexpr functionsBruno Cardoso Lopes1-0/+3
Implement support for try-catch blocks in constexpr functions, as proposed in http://wg21.link/P1002 and voted in San Diego for c++20. The idea is that we can still never throw inside constexpr, so the catch block is never entered. A try-catch block like this: try { f(); } catch (...) { } is then morally equivalent to just { f(); } Same idea should apply for function/constructor try blocks. rdar://problem/45530773 Differential Revision: https://reviews.llvm.org/D55097 llvm-svn: 348789
2018-12-10Misc typos fixes in ./lib folderRaphael Isemann1-3/+3
Summary: Found via `codespell -q 3 -I ../clang-whitelist.txt -L uint,importd,crasher,gonna,cant,ue,ons,orign,ned` Reviewers: teemperor Reviewed By: teemperor Subscribers: teemperor, jholewinski, jvesely, nhaehnle, whisperity, jfb, cfe-commits Differential Revision: https://reviews.llvm.org/D55475 llvm-svn: 348755
2018-12-01Specify constant context in constant emitterBill Wendling1-0/+1
The constant emitter may need to evaluate the expression in a constant context. For exasmple, global initializer lists. llvm-svn: 348070
2018-11-30Revert "Revert r347417 "Re-Reinstate 347294 with a fix for the failures.""Fangrui Song1-45/+102
It seems the two failing tests can be simply fixed after r348037 Fix 3 cases in Analysis/builtin-functions.cpp Delete the bad CodeGen/builtin-constant-p.c for now llvm-svn: 348053
2018-11-30Revert r347417 "Re-Reinstate 347294 with a fix for the failures."Fangrui Song1-102/+45
Kept the "indirect_builtin_constant_p" test case in test/SemaCXX/constant-expression-cxx1y.cpp while we are investigating why the following snippet fails: extern char extern_var; struct { int a; } a = {__builtin_constant_p(extern_var)}; llvm-svn: 348039
2018-11-30[ExprConstant] Try fixing __builtin_constant_p after D54355 (rC347417)Fangrui Song1-1/+0
Summary: Reinstate the original behavior (Success(false, E)) before D54355 when this branch is taken. This fixes spurious error of the following snippet: extern char extern_var; struct { int a; } a = {__builtin_constant_p(extern_var)}; llvm-svn: 348037
2018-11-28Re-commit r347417 "Re-Reinstate 347294 with a fix for the failures."Hans Wennborg1-45/+103
This was reverted in r347656 due to me thinking it caused a miscompile of Chromium. Turns out it was the Chromium code that was broken. llvm-svn: 347756
2018-11-27Revert r347417 "Re-Reinstate 347294 with a fix for the failures."Hans Wennborg1-103/+45
This caused a miscompile in Chrome (see crbug.com/908372) that's illustrated by this small reduction: static bool f(int *a, int *b) { return !__builtin_constant_p(b - a) || (!(b - a)); } int arr[] = {1,2,3}; bool g() { return f(arr, arr + 3); } $ clang -O2 -S -emit-llvm a.cc -o - g() should return true, but after r347417 it became false for some reason. This also reverts the follow-up commits. r347417: > Re-Reinstate 347294 with a fix for the failures. > > Don't try to emit a scalar expression for a non-scalar argument to > __builtin_constant_p(). > > Third time's a charm! r347446: > The result of is.constant() is unsigned. r347480: > A __builtin_constant_p() returns 0 with a function type. r347512: > isEvaluatable() implies a constant context. > > Assume that we're in a constant context if we're asking if the expression can > be compiled into a constant initializer. This fixes the issue where a > __builtin_constant_p() in a compound literal was diagnosed as not being > constant, even though it's always possible to convert the builtin into a > constant. r347531: > A "constexpr" is evaluated in a constant context. Make sure this is reflected > if a __builtin_constant_p() is a part of a constexpr. llvm-svn: 347656
2018-11-26A "constexpr" is evaluated in a constant context. Make sure this is reflectedBill Wendling1-0/+2
if a __builtin_constant_p() is a part of a constexpr. llvm-svn: 347531
2018-11-24isEvaluatable() implies a constant context.Bill Wendling1-2/+4
Assume that we're in a constant context if we're asking if the expression can be compiled into a constant initializer. This fixes the issue where a __builtin_constant_p() in a compound literal was diagnosed as not being constant, even though it's always possible to convert the builtin into a constant. llvm-svn: 347512
2018-11-21Re-Reinstate 347294 with a fix for the failures.Bill Wendling1-43/+97
Don't try to emit a scalar expression for a non-scalar argument to __builtin_constant_p(). Third time's a charm! llvm-svn: 347417
2018-11-21Revert r347364 again, the fix was incomplete.Nico Weber1-97/+43
llvm-svn: 347389
2018-11-20Reinstate 347294 with a fix for the failures.Bill Wendling1-43/+97
EvaluateAsInt() is sometimes called in a constant context. When that's the case, we need to specify it as so. llvm-svn: 347364
2018-11-20Revert 347294, it turned many bots on lab.llvm.org:8011/console red.Nico Weber1-61/+24
llvm-svn: 347314
2018-11-20Use is.constant intrinsic for __builtin_constant_pBill Wendling1-24/+61
Summary: A __builtin_constant_p may end up with a constant after inlining. Use the is.constant intrinsic if it's a variable that's in a context where it may resolve to a constant, e.g., an argument to a function after inlining. Reviewers: rsmith, shafik Subscribers: jfb, kristina, cfe-commits, nickdesaulniers, jyknight Differential Revision: https://reviews.llvm.org/D54355 llvm-svn: 347294
2018-11-12PR39628 Treat all non-zero values as 'true' in bool compound-assignmentRichard Smith1-2/+3
in constant evaluation, not just odd values. llvm-svn: 346699
2018-11-09Compound literals, enums, et al require const exprBill Wendling1-0/+2
Summary: Compound literals, enums, file-scoped arrays, etc. require their initializers and size specifiers to be constant. Wrap the initializer expressions in a ConstantExpr so that we can easily check for this later on. Reviewers: rsmith, shafik Reviewed By: rsmith Subscribers: cfe-commits, jyknight, nickdesaulniers Differential Revision: https://reviews.llvm.org/D53921 llvm-svn: 346455
2018-11-08[OpenCL] Add support of cl_intel_device_side_avc_motion_estimation extensionAndrew Savonichev1-0/+3
Summary: Documentation can be found at https://www.khronos.org/registry/OpenCL/extensions/intel/cl_intel_device_side_avc_motion_estimation.txt Patch by Kristina Bessonova Reviewers: Anastasia, yaxunl, shafik Reviewed By: Anastasia Subscribers: arphaman, sidorovd, AlexeySotkin, krisb, bader, asavonic, cfe-commits Differential Revision: https://reviews.llvm.org/D51484 llvm-svn: 346392