[llvm-branch-commits] [cfe-branch] r355898 - Merging r355743:
Hans Wennborg via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Tue Mar 12 01:29:08 PDT 2019
Author: hans
Date: Tue Mar 12 01:29:08 2019
New Revision: 355898
URL: http://llvm.org/viewvc/llvm-project?rev=355898&view=rev
Log:
Merging r355743:
------------------------------------------------------------------------
r355743 | ericwf | 2019-03-08 23:06:48 +0100 (Fri, 08 Mar 2019) | 26 lines
[8.0 Regression] Fix handling of `__builtin_constant_p` inside template 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
------------------------------------------------------------------------
Modified:
cfe/branches/release_80/ (props changed)
cfe/branches/release_80/lib/AST/ExprConstant.cpp
cfe/branches/release_80/test/SemaCXX/constant-expression-cxx1y.cpp
cfe/branches/release_80/test/SemaCXX/enable_if.cpp
Propchange: cfe/branches/release_80/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Tue Mar 12 01:29:08 2019
@@ -1,4 +1,4 @@
/cfe/branches/type-system-rewrite:134693-134817
-/cfe/trunk:351334,351340,351344,351360,351457,351459,351531,351579-351580,352040,352079,352099,352102,352105,352119,352156,352221-352222,352229,352307,352323,352463,352539,352610,352672,352822,353142,353393,353402,353411,353431,353493,353495,353656,353943,353976,354035,354074,354147,354351,354721,354723,354937,354968,355489,355491
+/cfe/trunk:351334,351340,351344,351360,351457,351459,351531,351579-351580,352040,352079,352099,352102,352105,352119,352156,352221-352222,352229,352307,352323,352463,352539,352610,352672,352822,353142,353393,353402,353411,353431,353493,353495,353656,353943,353976,354035,354074,354147,354351,354721,354723,354937,354968,355489,355491,355743
/cfe/trunk/test:170344
/cfe/trunk/test/SemaTemplate:126920
Modified: cfe/branches/release_80/lib/AST/ExprConstant.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_80/lib/AST/ExprConstant.cpp?rev=355898&r1=355897&r2=355898&view=diff
==============================================================================
--- cfe/branches/release_80/lib/AST/ExprConstant.cpp (original)
+++ cfe/branches/release_80/lib/AST/ExprConstant.cpp Tue Mar 12 01:29:08 2019
@@ -10985,6 +10985,7 @@ bool Expr::EvaluateAsConstantExpr(EvalRe
const ASTContext &Ctx) const {
EvalInfo::EvaluationMode EM = EvalInfo::EM_ConstantExpression;
EvalInfo Info(Ctx, Result, EM);
+ Info.InConstantContext = true;
if (!::Evaluate(Result.Val, Info, this))
return false;
@@ -11625,6 +11626,7 @@ bool Expr::EvaluateWithSubstitution(APVa
const Expr *This) const {
Expr::EvalStatus Status;
EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantExpressionUnevaluated);
+ Info.InConstantContext = true;
LValue ThisVal;
const LValue *ThisPtr = nullptr;
@@ -11708,6 +11710,7 @@ bool Expr::isPotentialConstantExprUneval
EvalInfo Info(FD->getASTContext(), Status,
EvalInfo::EM_PotentialConstantExpressionUnevaluated);
+ Info.InConstantContext = true;
// Fabricate a call stack frame to give the arguments a plausible cover story.
ArrayRef<const Expr*> Args;
Modified: cfe/branches/release_80/test/SemaCXX/constant-expression-cxx1y.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_80/test/SemaCXX/constant-expression-cxx1y.cpp?rev=355898&r1=355897&r2=355898&view=diff
==============================================================================
--- cfe/branches/release_80/test/SemaCXX/constant-expression-cxx1y.cpp (original)
+++ cfe/branches/release_80/test/SemaCXX/constant-expression-cxx1y.cpp Tue Mar 12 01:29:08 2019
@@ -1135,3 +1135,27 @@ constexpr bool indirect_builtin_constant
return __builtin_constant_p(*__s);
}
constexpr bool n = indirect_builtin_constant_p("a");
+
+__attribute__((enable_if(indirect_builtin_constant_p("a") == n, "OK")))
+int test_in_enable_if() { return 0; }
+int n2 = test_in_enable_if();
+
+template <bool n = indirect_builtin_constant_p("a")>
+int test_in_template_param() { return 0; }
+int n3 = test_in_template_param();
+
+void test_in_case(int n) {
+ switch (n) {
+ case indirect_builtin_constant_p("abc"):
+ break;
+ }
+}
+enum InEnum1 {
+ ONE = indirect_builtin_constant_p("abc")
+};
+enum InEnum2 : int {
+ TWO = indirect_builtin_constant_p("abc")
+};
+enum class InEnum3 {
+ THREE = indirect_builtin_constant_p("abc")
+};
Modified: cfe/branches/release_80/test/SemaCXX/enable_if.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_80/test/SemaCXX/enable_if.cpp?rev=355898&r1=355897&r2=355898&view=diff
==============================================================================
--- cfe/branches/release_80/test/SemaCXX/enable_if.cpp (original)
+++ cfe/branches/release_80/test/SemaCXX/enable_if.cpp Tue Mar 12 01:29:08 2019
@@ -514,3 +514,11 @@ namespace TypeOfFn {
static_assert(is_same<__typeof__(foo)*, decltype(&foo)>::value, "");
}
+
+namespace InConstantContext {
+void foo(const char *s) __attribute__((enable_if(((void)__builtin_constant_p(*s), true), "trap"))) {}
+
+void test() {
+ InConstantContext::foo("abc");
+}
+} // namespace InConstantContext
More information about the llvm-branch-commits
mailing list