From e883f1b319ed315f96652b3d0683e4b9ee137d91 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Tue, 6 Jun 2017 01:34:24 +0000 Subject: [PATCH] PR33318: Add missing full-expression checking to static_assert expression. This fixes missing lambda-captures for variables referenced only inside a static_assert (!), among other things. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@304760 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Parse/Parser.h | 3 ++- lib/Parse/ParseDeclCXX.cpp | 4 +++- lib/Sema/SemaDeclCXX.cpp | 8 ++++++++ test/SemaCXX/cxx1y-generic-lambdas-capturing.cpp | 4 ++++ test/SemaTemplate/deduction.cpp | 6 +++--- 5 files changed, 20 insertions(+), 5 deletions(-) diff --git a/include/clang/Parse/Parser.h b/include/clang/Parse/Parser.h index f4d5a661bd..a51d3a51d4 100644 --- a/include/clang/Parse/Parser.h +++ b/include/clang/Parse/Parser.h @@ -1461,7 +1461,8 @@ public: }; ExprResult ParseExpression(TypeCastState isTypeCast = NotTypeCast); - ExprResult ParseConstantExpressionInExprEvalContext(TypeCastState isTypeCast); + ExprResult ParseConstantExpressionInExprEvalContext( + TypeCastState isTypeCast = NotTypeCast); ExprResult ParseConstantExpression(TypeCastState isTypeCast = NotTypeCast); ExprResult ParseConstraintExpression(); // Expr that doesn't include commas. diff --git a/lib/Parse/ParseDeclCXX.cpp b/lib/Parse/ParseDeclCXX.cpp index 4c117f531e..1a4607a84c 100644 --- a/lib/Parse/ParseDeclCXX.cpp +++ b/lib/Parse/ParseDeclCXX.cpp @@ -840,7 +840,9 @@ Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){ return nullptr; } - ExprResult AssertExpr(ParseConstantExpression()); + EnterExpressionEvaluationContext ConstantEvaluated( + Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated); + ExprResult AssertExpr(ParseConstantExpressionInExprEvalContext()); if (AssertExpr.isInvalid()) { SkipMalformedDecl(); return nullptr; diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp index acacdc263c..3e2306361c 100644 --- a/lib/Sema/SemaDeclCXX.cpp +++ b/lib/Sema/SemaDeclCXX.cpp @@ -13232,6 +13232,14 @@ Decl *Sema::BuildStaticAssertDeclaration(SourceLocation StaticAssertLoc, } } + ExprResult FullAssertExpr = ActOnFinishFullExpr(AssertExpr, StaticAssertLoc, + /*DiscardedValue*/false, + /*IsConstexpr*/true); + if (FullAssertExpr.isInvalid()) + Failed = true; + else + AssertExpr = FullAssertExpr.get(); + Decl *Decl = StaticAssertDecl::Create(Context, CurContext, StaticAssertLoc, AssertExpr, AssertMessage, RParenLoc, Failed); diff --git a/test/SemaCXX/cxx1y-generic-lambdas-capturing.cpp b/test/SemaCXX/cxx1y-generic-lambdas-capturing.cpp index b08d58abd2..dc3adca62f 100644 --- a/test/SemaCXX/cxx1y-generic-lambdas-capturing.cpp +++ b/test/SemaCXX/cxx1y-generic-lambdas-capturing.cpp @@ -1376,3 +1376,7 @@ XT xt{}; } + +void PR33318(int i) { + [&](auto) { static_assert(&i != nullptr, ""); }(0); // expected-warning 2{{always true}} expected-note {{instantiation}} +} diff --git a/test/SemaTemplate/deduction.cpp b/test/SemaTemplate/deduction.cpp index 74eb5a6ee5..be86f18729 100644 --- a/test/SemaTemplate/deduction.cpp +++ b/test/SemaTemplate/deduction.cpp @@ -305,11 +305,11 @@ namespace nullptr_deduction { template struct X {}; template void f(X) { - static_assert(!v, ""); + static_assert(!v, ""); // expected-warning 2{{implicit conversion of nullptr constant to 'bool'}} } void g() { - f(X()); - f(X()); + f(X()); // expected-note {{instantiation of}} + f(X()); // expected-note {{instantiation of}} } template class X, typename T, typename U, int *P> -- 2.40.0