<div dir="ltr">This triggered failure in libcxx tests.<div><a href="http://bb.pgr.jp/builders/bootstrap-clang-libcxx-lld-i686-linux/builds/97">http://bb.pgr.jp/builders/bootstrap-clang-libcxx-lld-i686-linux/builds/97</a><br><br><div class="gmail_quote"><div dir="ltr">On Fri, Sep 15, 2017 at 8:40 AM Douglas Gregor via cfe-commits <<a href="mailto:cfe-commits@lists.llvm.org">cfe-commits@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: dgregor<br>
Date: Thu Sep 14 16:38:42 2017<br>
New Revision: 313315<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=313315&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=313315&view=rev</a><br>
Log:<br>
Diagnostic specific failed condition in a static_assert.<br>
<br>
When a static_assert fails, dig out a specific condition to diagnose,<br>
using the same logic that we use to find the enable_if condition to<br>
diagnose.<br>
<br>
Modified:<br>
cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td<br>
cfe/trunk/include/clang/Sema/Sema.h<br>
cfe/trunk/lib/Sema/SemaDeclCXX.cpp<br>
cfe/trunk/lib/Sema/SemaTemplate.cpp<br>
cfe/trunk/test/SemaCXX/static-assert.cpp<br>
<br>
Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=313315&r1=313314&r2=313315&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=313315&r1=313314&r2=313315&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)<br>
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Thu Sep 14 16:38:42 2017<br>
@@ -1219,6 +1219,8 @@ def warn_messaging_unqualified_id : Warn<br>
def err_static_assert_expression_is_not_constant : Error<<br>
"static_assert expression is not an integral constant expression">;<br>
def err_static_assert_failed : Error<"static_assert failed%select{ %1|}0">;<br>
+def err_static_assert_requirement_failed : Error<<br>
+ "static_assert failed due to requirement '%0'%select{ %2|}1">;<br>
def ext_static_assert_no_message : ExtWarn<<br>
"static_assert with no message is a C++17 extension">, InGroup<CXX17>;<br>
def warn_cxx14_compat_static_assert_no_message : Warning<<br>
<br>
Modified: cfe/trunk/include/clang/Sema/Sema.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=313315&r1=313314&r2=313315&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=313315&r1=313314&r2=313315&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/include/clang/Sema/Sema.h (original)<br>
+++ cfe/trunk/include/clang/Sema/Sema.h Thu Sep 14 16:38:42 2017<br>
@@ -2783,6 +2783,14 @@ public:<br>
EnableIfAttr *CheckEnableIf(FunctionDecl *Function, ArrayRef<Expr *> Args,<br>
bool MissingImplicitThis = false);<br>
<br>
+ /// Find the failed Boolean condition within a given Boolean<br>
+ /// constant expression, and describe it with a string.<br>
+ ///<br>
+ /// \param AllowTopLevelCond Whether to allow the result to be the<br>
+ /// complete top-level condition.<br>
+ std::pair<Expr *, std::string><br>
+ findFailedBooleanCondition(Expr *Cond, bool AllowTopLevelCond);<br>
+<br>
/// Emit diagnostics for the diagnose_if attributes on Function, ignoring any<br>
/// non-ArgDependent DiagnoseIfAttrs.<br>
///<br>
<br>
Modified: cfe/trunk/lib/Sema/SemaDeclCXX.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclCXX.cpp?rev=313315&r1=313314&r2=313315&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclCXX.cpp?rev=313315&r1=313314&r2=313315&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/lib/Sema/SemaDeclCXX.cpp (original)<br>
+++ cfe/trunk/lib/Sema/SemaDeclCXX.cpp Thu Sep 14 16:38:42 2017<br>
@@ -13296,8 +13296,20 @@ Decl *Sema::BuildStaticAssertDeclaration<br>
llvm::raw_svector_ostream Msg(MsgBuffer);<br>
if (AssertMessage)<br>
AssertMessage->printPretty(Msg, nullptr, getPrintingPolicy());<br>
- Diag(StaticAssertLoc, diag::err_static_assert_failed)<br>
- << !AssertMessage << Msg.str() << AssertExpr->getSourceRange();<br>
+<br>
+ Expr *InnerCond = nullptr;<br>
+ std::string InnerCondDescription;<br>
+ std::tie(InnerCond, InnerCondDescription) =<br>
+ findFailedBooleanCondition(Converted.get(),<br>
+ /*AllowTopLevelCond=*/false);<br>
+ if (InnerCond) {<br>
+ Diag(StaticAssertLoc, diag::err_static_assert_requirement_failed)<br>
+ << InnerCondDescription << !AssertMessage<br>
+ << Msg.str() << InnerCond->getSourceRange();<br>
+ } else {<br>
+ Diag(StaticAssertLoc, diag::err_static_assert_failed)<br>
+ << !AssertMessage << Msg.str() << AssertExpr->getSourceRange();<br>
+ }<br>
Failed = true;<br>
}<br>
}<br>
<br>
Modified: cfe/trunk/lib/Sema/SemaTemplate.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplate.cpp?rev=313315&r1=313314&r2=313315&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplate.cpp?rev=313315&r1=313314&r2=313315&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/lib/Sema/SemaTemplate.cpp (original)<br>
+++ cfe/trunk/lib/Sema/SemaTemplate.cpp Thu Sep 14 16:38:42 2017<br>
@@ -2863,11 +2863,9 @@ static Expr *lookThroughRangesV3Conditio<br>
return Cond;<br>
}<br>
<br>
-/// Find the failed subexpression within enable_if, and describe it<br>
-/// with a string.<br>
-static std::pair<Expr *, std::string><br>
-findFailedEnableIfCondition(Sema &S, Expr *Cond) {<br>
- Cond = lookThroughRangesV3Condition(S.PP, Cond);<br>
+std::pair<Expr *, std::string><br>
+Sema::findFailedBooleanCondition(Expr *Cond, bool AllowTopLevelCond) {<br>
+ Cond = lookThroughRangesV3Condition(PP, Cond);<br>
<br>
// Separate out all of the terms in a conjunction.<br>
SmallVector<Expr *, 4> Terms;<br>
@@ -2876,27 +2874,37 @@ findFailedEnableIfCondition(Sema &S, Exp<br>
// Determine which term failed.<br>
Expr *FailedCond = nullptr;<br>
for (Expr *Term : Terms) {<br>
+ Expr *TermAsWritten = Term->IgnoreParenImpCasts();<br>
+<br>
+ // Literals are uninteresting.<br>
+ if (isa<CXXBoolLiteralExpr>(TermAsWritten) ||<br>
+ isa<IntegerLiteral>(TermAsWritten))<br>
+ continue;<br>
+<br>
// The initialization of the parameter from the argument is<br>
// a constant-evaluated context.<br>
EnterExpressionEvaluationContext ConstantEvaluated(<br>
- S, Sema::ExpressionEvaluationContext::ConstantEvaluated);<br>
+ *this, Sema::ExpressionEvaluationContext::ConstantEvaluated);<br>
<br>
bool Succeeded;<br>
- if (Term->EvaluateAsBooleanCondition(Succeeded, S.Context) &&<br>
+ if (Term->EvaluateAsBooleanCondition(Succeeded, Context) &&<br>
!Succeeded) {<br>
- FailedCond = Term->IgnoreParenImpCasts();<br>
+ FailedCond = TermAsWritten;<br>
break;<br>
}<br>
}<br>
<br>
- if (!FailedCond)<br>
+ if (!FailedCond) {<br>
+ if (!AllowTopLevelCond)<br>
+ return { nullptr, "" };<br>
+<br>
FailedCond = Cond->IgnoreParenImpCasts();<br>
+ }<br>
<br>
std::string Description;<br>
{<br>
llvm::raw_string_ostream Out(Description);<br>
- FailedCond->printPretty(Out, nullptr,<br>
- PrintingPolicy(S.Context.getLangOpts()));<br>
+ FailedCond->printPretty(Out, nullptr, getPrintingPolicy());<br>
}<br>
return { FailedCond, Description };<br>
}<br>
@@ -2980,8 +2988,9 @@ QualType Sema::CheckTemplateIdType(Templ<br>
Expr *FailedCond;<br>
std::string FailedDescription;<br>
std::tie(FailedCond, FailedDescription) =<br>
- findFailedEnableIfCondition(<br>
- *this, TemplateArgs[0].getSourceExpression());<br>
+ findFailedBooleanCondition(<br>
+ TemplateArgs[0].getSourceExpression(),<br>
+ /*AllowTopLevelCond=*/true);<br>
<br>
// Remove the old SFINAE diagnostic.<br>
PartialDiagnosticAt OldDiag =<br>
@@ -9513,7 +9522,7 @@ Sema::CheckTypenameType(ElaboratedTypeKe<br>
Expr *FailedCond;<br>
std::string FailedDescription;<br>
std::tie(FailedCond, FailedDescription) =<br>
- findFailedEnableIfCondition(*this, Cond);<br>
+ findFailedBooleanCondition(Cond, /*AllowTopLevelCond=*/true);<br>
<br>
Diag(FailedCond->getExprLoc(),<br>
diag::err_typename_nested_not_found_requirement)<br>
<br>
Modified: cfe/trunk/test/SemaCXX/static-assert.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/static-assert.cpp?rev=313315&r1=313314&r2=313315&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/static-assert.cpp?rev=313315&r1=313314&r2=313315&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/test/SemaCXX/static-assert.cpp (original)<br>
+++ cfe/trunk/test/SemaCXX/static-assert.cpp Thu Sep 14 16:38:42 2017<br>
@@ -51,3 +51,20 @@ StaticAssertProtected<X> sap2; // expect<br>
<br>
static_assert(true); // expected-warning {{C++17 extension}}<br>
static_assert(false); // expected-error-re {{failed{{$}}}} expected-warning {{extension}}<br>
+<br>
+<br>
+// Diagnostics for static_assert with multiple conditions<br>
+template<typename T> struct first_trait {<br>
+ static const bool value = false;<br>
+};<br>
+<br>
+template<><br>
+struct first_trait<X> {<br>
+ static const bool value = true;<br>
+};<br>
+<br>
+template<typename T> struct second_trait {<br>
+ static const bool value = false;<br>
+};<br>
+<br>
+static_assert(first_trait<X>::value && second_trait<X>::value, "message"); // expected-error{{static_assert failed due to requirement 'second_trait<X>::value' "message"}}<br>
<br>
<br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits</a><br>
</blockquote></div></div></div>