From 6a72a16cd26cbbba30598119eb033e6535d1a9ca Mon Sep 17 00:00:00 2001 From: Clement Courbet Date: Mon, 10 Dec 2018 08:53:17 +0000 Subject: [PATCH] Revert r348741 "[Sema] Further improvements to to static_assert diagnostics." Seems to break build bots. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@348742 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Sema/Sema.h | 6 ++- lib/Sema/SemaDeclCXX.cpp | 3 +- lib/Sema/SemaTemplate.cpp | 77 +++++++++++++--------------- test/PCH/cxx-static_assert.cpp | 4 +- test/Sema/static-assert.c | 2 +- test/SemaCXX/static-assert-cxx17.cpp | 9 ---- test/SemaCXX/static-assert.cpp | 12 +---- 7 files changed, 48 insertions(+), 65 deletions(-) diff --git a/include/clang/Sema/Sema.h b/include/clang/Sema/Sema.h index e67e918e99..8e29447f1f 100644 --- a/include/clang/Sema/Sema.h +++ b/include/clang/Sema/Sema.h @@ -2861,7 +2861,11 @@ public: /// Find the failed Boolean condition within a given Boolean /// constant expression, and describe it with a string. - std::pair findFailedBooleanCondition(Expr *Cond); + /// + /// \param AllowTopLevelCond Whether to allow the result to be the + /// complete top-level condition. + std::pair + findFailedBooleanCondition(Expr *Cond, bool AllowTopLevelCond); /// Emit diagnostics for the diagnose_if attributes on Function, ignoring any /// non-ArgDependent DiagnoseIfAttrs. diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp index 831e74a2bf..e408c04379 100644 --- a/lib/Sema/SemaDeclCXX.cpp +++ b/lib/Sema/SemaDeclCXX.cpp @@ -13878,7 +13878,8 @@ Decl *Sema::BuildStaticAssertDeclaration(SourceLocation StaticAssertLoc, Expr *InnerCond = nullptr; std::string InnerCondDescription; std::tie(InnerCond, InnerCondDescription) = - findFailedBooleanCondition(Converted.get()); + findFailedBooleanCondition(Converted.get(), + /*AllowTopLevelCond=*/false); if (InnerCond) { Diag(StaticAssertLoc, diag::err_static_assert_requirement_failed) << InnerCondDescription << !AssertMessage diff --git a/lib/Sema/SemaTemplate.cpp b/lib/Sema/SemaTemplate.cpp index c7d67cde11..56302d6248 100644 --- a/lib/Sema/SemaTemplate.cpp +++ b/lib/Sema/SemaTemplate.cpp @@ -3052,42 +3052,30 @@ static Expr *lookThroughRangesV3Condition(Preprocessor &PP, Expr *Cond) { return Cond; } -namespace { - -// A PrinterHelper that prints more helpful diagnostics for some sub-expressions -// within failing boolean expression, such as substituting template parameters -// for actual types. -class FailedBooleanConditionPrinterHelper : public PrinterHelper { -public: - explicit FailedBooleanConditionPrinterHelper(const PrintingPolicy &Policy) - : Policy(Policy) {} - - bool handledStmt(Stmt *E, raw_ostream &OS) override { - const auto *DR = dyn_cast(E); - if (DR && DR->getQualifier()) { - // If this is a qualified name, expand the template arguments in nested - // qualifiers. - DR->getQualifier()->print(OS, Policy, true); - // Then print the decl itself. - const ValueDecl *VD = DR->getDecl(); - OS << VD->getName(); - if (const auto *IV = dyn_cast(VD)) { - // This is a template variable, print the expanded template arguments. - printTemplateArgumentList(OS, IV->getTemplateArgs().asArray(), Policy); - } - return true; +// Print a diagnostic for the failing static_assert expression. Defaults to +// pretty-printing the expression. +static void prettyPrintFailedBooleanCondition(llvm::raw_string_ostream &OS, + const Expr *FailedCond, + const PrintingPolicy &Policy) { + const auto *DR = dyn_cast(FailedCond); + if (DR && DR->getQualifier()) { + // If this is a qualified name, expand the template arguments in nested + // qualifiers. + DR->getQualifier()->print(OS, Policy, true); + // Then print the decl itself. + const ValueDecl *VD = DR->getDecl(); + OS << VD->getName(); + if (const auto *IV = dyn_cast(VD)) { + // This is a template variable, print the expanded template arguments. + printTemplateArgumentList(OS, IV->getTemplateArgs().asArray(), Policy); } - return false; + return; } - -private: - const PrintingPolicy &Policy; -}; - -} // end anonymous namespace + FailedCond->printPretty(OS, nullptr, Policy); +} std::pair -Sema::findFailedBooleanCondition(Expr *Cond) { +Sema::findFailedBooleanCondition(Expr *Cond, bool AllowTopLevelCond) { Cond = lookThroughRangesV3Condition(PP, Cond); // Separate out all of the terms in a conjunction. @@ -3099,6 +3087,11 @@ Sema::findFailedBooleanCondition(Expr *Cond) { for (Expr *Term : Terms) { Expr *TermAsWritten = Term->IgnoreParenImpCasts(); + // Literals are uninteresting. + if (isa(TermAsWritten) || + isa(TermAsWritten)) + continue; + // The initialization of the parameter from the argument is // a constant-evaluated context. EnterExpressionEvaluationContext ConstantEvaluated( @@ -3111,18 +3104,18 @@ Sema::findFailedBooleanCondition(Expr *Cond) { break; } } - if (!FailedCond) - FailedCond = Cond->IgnoreParenImpCasts(); - // Literals are uninteresting. - if (isa(FailedCond) || isa(FailedCond)) - return {nullptr, ""}; + if (!FailedCond) { + if (!AllowTopLevelCond) + return { nullptr, "" }; + + FailedCond = Cond->IgnoreParenImpCasts(); + } std::string Description; { llvm::raw_string_ostream Out(Description); - FailedBooleanConditionPrinterHelper Helper(getPrintingPolicy()); - FailedCond->printPretty(Out, &Helper, getPrintingPolicy()); + prettyPrintFailedBooleanCondition(Out, FailedCond, getPrintingPolicy()); } return { FailedCond, Description }; } @@ -3206,7 +3199,9 @@ QualType Sema::CheckTemplateIdType(TemplateName Name, Expr *FailedCond; std::string FailedDescription; std::tie(FailedCond, FailedDescription) = - findFailedBooleanCondition(TemplateArgs[0].getSourceExpression()); + findFailedBooleanCondition( + TemplateArgs[0].getSourceExpression(), + /*AllowTopLevelCond=*/true); // Remove the old SFINAE diagnostic. PartialDiagnosticAt OldDiag = @@ -9654,7 +9649,7 @@ Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, Expr *FailedCond; std::string FailedDescription; std::tie(FailedCond, FailedDescription) = - findFailedBooleanCondition(Cond); + findFailedBooleanCondition(Cond, /*AllowTopLevelCond=*/true); Diag(FailedCond->getExprLoc(), diag::err_typename_nested_not_found_requirement) diff --git a/test/PCH/cxx-static_assert.cpp b/test/PCH/cxx-static_assert.cpp index be6c0ab2a7..8049525fb5 100644 --- a/test/PCH/cxx-static_assert.cpp +++ b/test/PCH/cxx-static_assert.cpp @@ -3,7 +3,7 @@ // Test with pch. // RUN: %clang_cc1 -std=c++11 -emit-pch -o %t %s -// RUN: %clang_cc1 -include-pch %t -verify -std=c++11 %s +// RUN: %clang_cc1 -include-pch %t -verify -std=c++11 %s #ifndef HEADER #define HEADER @@ -14,7 +14,7 @@ template struct T { #else -// expected-error@12 {{static_assert failed due to requirement '1 == 2' "N is not 2!"}} +// expected-error@12 {{static_assert failed "N is not 2!"}} T<1> t1; // expected-note {{in instantiation of template class 'T<1>' requested here}} T<2> t2; diff --git a/test/Sema/static-assert.c b/test/Sema/static-assert.c index e8cfb1fa58..87fa0504b2 100644 --- a/test/Sema/static-assert.c +++ b/test/Sema/static-assert.c @@ -38,5 +38,5 @@ struct A { typedef UNION(unsigned, struct A) U1; UNION(char[2], short) u2 = { .one = { 'a', 'b' } }; -typedef UNION(char, short) U3; // expected-error {{static_assert failed due to requirement 'sizeof(char) == sizeof(short)' "type size mismatch"}} +typedef UNION(char, short) U3; // expected-error {{static_assert failed "type size mismatch"}} typedef UNION(float, 0.5f) U4; // expected-error {{expected a type}} diff --git a/test/SemaCXX/static-assert-cxx17.cpp b/test/SemaCXX/static-assert-cxx17.cpp index 67b3541bea..7dcdb89719 100644 --- a/test/SemaCXX/static-assert-cxx17.cpp +++ b/test/SemaCXX/static-assert-cxx17.cpp @@ -45,12 +45,3 @@ void foo4() { }; template void foo4(); // expected-note@-1{{in instantiation of function template specialization 'foo4' requested here}} - - -template -void foo5() { - static_assert(!!(global_inline_var)); - // expected-error@-1{{static_assert failed due to requirement '!!(global_inline_var)'}} -} -template void foo5(); -// expected-note@-1{{in instantiation of function template specialization 'foo5' requested here}} diff --git a/test/SemaCXX/static-assert.cpp b/test/SemaCXX/static-assert.cpp index b43d56a922..38f82091ae 100644 --- a/test/SemaCXX/static-assert.cpp +++ b/test/SemaCXX/static-assert.cpp @@ -15,14 +15,14 @@ class C { }; template struct T { - static_assert(N == 2, "N is not 2!"); // expected-error {{static_assert failed due to requirement '1 == 2' "N is not 2!"}} + static_assert(N == 2, "N is not 2!"); // expected-error {{static_assert failed "N is not 2!"}} }; T<1> t1; // expected-note {{in instantiation of template class 'T<1>' requested here}} T<2> t2; template struct S { - static_assert(sizeof(T) > sizeof(char), "Type not big enough!"); // expected-error {{static_assert failed due to requirement 'sizeof(char) > sizeof(char)' "Type not big enough!"}} + static_assert(sizeof(T) > sizeof(char), "Type not big enough!"); // expected-error {{static_assert failed "Type not big enough!"}} }; S s1; // expected-note {{in instantiation of template class 'S' requested here}} @@ -111,14 +111,6 @@ static_assert(std::is_same::value, "message"); // expected-error@-1{{static_assert failed due to requirement 'std::is_same::value' "message"}} static_assert(std::is_const::value, "message"); // expected-error@-1{{static_assert failed due to requirement 'std::is_const::value' "message"}} -static_assert(!std::is_const::value, "message"); -// expected-error@-1{{static_assert failed due to requirement '!std::is_const::value' "message"}} -static_assert(!(std::is_const::value), "message"); -// expected-error@-1{{static_assert failed due to requirement '!(std::is_const::value)' "message"}} -static_assert(std::is_const::value == false, "message"); -// expected-error@-1{{static_assert failed due to requirement 'std::is_const::value == false' "message"}} -static_assert(!(std::is_const::value == true), "message"); -// expected-error@-1{{static_assert failed due to requirement '!(std::is_const::value == true)' "message"}} struct BI_tag {}; struct RAI_tag : BI_tag {}; -- 2.40.0