[PATCH] D59038: [8.0 Regression] Fix handling of `__builtin_constant_p` inside the enable_if attribute.
Eric Fiselier via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Mar 6 12:15:34 PST 2019
EricWF created this revision.
EricWF added reviewers: rsmith, hans, sbenza.
Herald added a subscriber: kristina.
Herald added a project: clang.
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
Repository:
rC Clang
https://reviews.llvm.org/D59038
Files:
lib/AST/ExprConstant.cpp
test/SemaCXX/constant-expression-cxx1y.cpp
test/SemaCXX/enable_if.cpp
Index: test/SemaCXX/enable_if.cpp
===================================================================
--- test/SemaCXX/enable_if.cpp
+++ test/SemaCXX/enable_if.cpp
@@ -514,3 +514,11 @@
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
Index: test/SemaCXX/constant-expression-cxx1y.cpp
===================================================================
--- test/SemaCXX/constant-expression-cxx1y.cpp
+++ test/SemaCXX/constant-expression-cxx1y.cpp
@@ -1135,3 +1135,7 @@
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();
Index: lib/AST/ExprConstant.cpp
===================================================================
--- lib/AST/ExprConstant.cpp
+++ lib/AST/ExprConstant.cpp
@@ -11767,6 +11767,7 @@
const Expr *This) const {
Expr::EvalStatus Status;
EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantExpressionUnevaluated);
+ Info.InConstantContext = true;
LValue ThisVal;
const LValue *ThisPtr = nullptr;
@@ -11850,6 +11851,7 @@
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;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D59038.189550.patch
Type: text/x-patch
Size: 1691 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190306/ec9e58b6/attachment.bin>
More information about the cfe-commits
mailing list