Index: cfe/trunk/include/clang/AST/NestedNameSpecifier.h =================================================================== --- cfe/trunk/include/clang/AST/NestedNameSpecifier.h +++ cfe/trunk/include/clang/AST/NestedNameSpecifier.h @@ -212,9 +212,12 @@ /// parameter pack (for C++11 variadic templates). bool containsUnexpandedParameterPack() const; - /// Print this nested name specifier to the given output - /// stream. - void print(raw_ostream &OS, const PrintingPolicy &Policy) const; + /// Print this nested name specifier to the given output stream. If + /// `ResolveTemplateArguments` is true, we'll print actual types, e.g. + /// `ns::SomeTemplate` instead of + /// `ns::SomeTemplate`. + void print(raw_ostream &OS, const PrintingPolicy &Policy, + bool ResolveTemplateArguments = false) const; void Profile(llvm::FoldingSetNodeID &ID) const { ID.AddPointer(Prefix.getOpaqueValue()); Index: cfe/trunk/lib/AST/NestedNameSpecifier.cpp =================================================================== --- cfe/trunk/lib/AST/NestedNameSpecifier.cpp +++ cfe/trunk/lib/AST/NestedNameSpecifier.cpp @@ -16,6 +16,7 @@ #include "clang/AST/ASTContext.h" #include "clang/AST/Decl.h" #include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclTemplate.h" #include "clang/AST/PrettyPrinter.h" #include "clang/AST/TemplateName.h" #include "clang/AST/Type.h" @@ -270,9 +271,8 @@ /// Print this nested name specifier to the given output /// stream. -void -NestedNameSpecifier::print(raw_ostream &OS, - const PrintingPolicy &Policy) const { +void NestedNameSpecifier::print(raw_ostream &OS, const PrintingPolicy &Policy, + bool ResolveTemplateArguments) const { if (getPrefix()) getPrefix()->print(OS, Policy); @@ -305,6 +305,15 @@ LLVM_FALLTHROUGH; case TypeSpec: { + const auto *Record = + dyn_cast_or_null(getAsRecordDecl()); + if (ResolveTemplateArguments && Record) { + // Print the type trait with resolved template parameters. + Record->printName(OS); + printTemplateArgumentList(OS, Record->getTemplateArgs().asArray(), + Policy); + break; + } const Type *T = getAsType(); PrintingPolicy InnerPolicy(Policy); Index: cfe/trunk/lib/Sema/SemaTemplate.cpp =================================================================== --- cfe/trunk/lib/Sema/SemaTemplate.cpp +++ cfe/trunk/lib/Sema/SemaTemplate.cpp @@ -3052,6 +3052,28 @@ return Cond; } +// 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; + } + FailedCond->printPretty(OS, nullptr, Policy); +} + std::pair Sema::findFailedBooleanCondition(Expr *Cond, bool AllowTopLevelCond) { Cond = lookThroughRangesV3Condition(PP, Cond); @@ -3093,7 +3115,7 @@ std::string Description; { llvm::raw_string_ostream Out(Description); - FailedCond->printPretty(Out, nullptr, getPrintingPolicy()); + prettyPrintFailedBooleanCondition(Out, FailedCond, getPrintingPolicy()); } return { FailedCond, Description }; } Index: cfe/trunk/test/SemaCXX/static-assert-cxx17.cpp =================================================================== --- cfe/trunk/test/SemaCXX/static-assert-cxx17.cpp +++ cfe/trunk/test/SemaCXX/static-assert-cxx17.cpp @@ -0,0 +1,47 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++1z -triple=x86_64-linux-gnu + +template +struct S1 { + static constexpr const bool value = false; +}; + +template +inline constexpr bool global_inline_var = S1::value; + +template +struct S2 { + template + static inline constexpr bool var = global_inline_var; +}; + +template +void foo() { + static_assert(S1::value); + // expected-error@-1{{static_assert failed due to requirement 'S1::value'}} +} +template void foo(); +// expected-note@-1{{in instantiation of function template specialization 'foo' requested here}} + +template +void foo2() { + static_assert(global_inline_var); + // expected-error@-1{{static_assert failed due to requirement 'global_inline_var'}} +} +template void foo2(); +// expected-note@-1{{in instantiation of function template specialization 'foo2' requested here}} + +template +void foo3() { + static_assert(T::template var); + // expected-error@-1{{static_assert failed due to requirement 'S2::var'}} +} +template void foo3, int, float>(); +// expected-note@-1{{in instantiation of function template specialization 'foo3, int, float>' requested here}} + +template +void foo4() { + static_assert(S1::value, ""); + // expected-error@-1{{static_assert failed due to requirement 'S1::value'}} +}; +template void foo4(); +// expected-note@-1{{in instantiation of function template specialization 'foo4' requested here}} Index: cfe/trunk/test/SemaCXX/static-assert.cpp =================================================================== --- cfe/trunk/test/SemaCXX/static-assert.cpp +++ cfe/trunk/test/SemaCXX/static-assert.cpp @@ -68,3 +68,100 @@ }; static_assert(first_trait::value && second_trait::value, "message"); // expected-error{{static_assert failed due to requirement 'second_trait::value' "message"}} + +namespace std { + +template +struct integral_constant { + static const Tp value = v; + typedef Tp value_type; + typedef integral_constant type; +}; + +template +const Tp integral_constant::value; + +typedef integral_constant true_type; +typedef integral_constant false_type; + +template +struct is_const : public false_type {}; +template +struct is_const : public true_type {}; + +// We do not define is_same in terms of integral_constant to check that both implementations are supported. +template +struct is_same { + static const bool value = false; +}; + +template +struct is_same { + static const bool value = true; +}; + +} // namespace std + +struct ExampleTypes { + using T = int; + using U = float; +}; + +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"}} + +struct BI_tag {}; +struct RAI_tag : BI_tag {}; +struct MyIterator { + using tag = BI_tag; +}; +struct MyContainer { + using iterator = MyIterator; +}; +template +void foo() { + static_assert(std::is_same::value, "message"); + // expected-error@-1{{static_assert failed due to requirement 'std::is_same::value' "message"}} +} +template void foo(); +// expected-note@-1{{in instantiation of function template specialization 'foo' requested here}} + +namespace ns { +template +struct NestedTemplates1 { + struct NestedTemplates2 { + template + struct NestedTemplates3 : public std::is_same {}; + }; +}; +} // namespace ns + +template +void foo2() { + static_assert(::ns::NestedTemplates1::NestedTemplates2::template NestedTemplates3::value, "message"); + // expected-error@-1{{static_assert failed due to requirement '::ns::NestedTemplates1::NestedTemplates2::NestedTemplates3::value' "message"}} +} +template void foo2(); +// expected-note@-1{{in instantiation of function template specialization 'foo2' requested here}} + +template +void foo3(T t) { + static_assert(std::is_const::value, "message"); + // expected-error-re@-1{{static_assert failed due to requirement 'std::is_const<(lambda at {{.*}}static-assert.cpp:{{[0-9]*}}:{{[0-9]*}})>::value' "message"}} + static_assert(std::is_const::value, "message"); + // expected-error-re@-1{{static_assert failed due to requirement 'std::is_const<(lambda at {{.*}}static-assert.cpp:{{[0-9]*}}:{{[0-9]*}})>::value' "message"}} +} +void callFoo3() { + foo3([]() {}); + // expected-note@-1{{in instantiation of function template specialization 'foo3<(lambda at }} +} + +template +void foo4(T t) { + static_assert(std::is_const::value, "message"); + // expected-error@-1{{type 'int' cannot be used prior to '::' because it has no members}} +} +void callFoo4() { foo4(42); } +// expected-note@-1{{in instantiation of function template specialization 'foo4' requested here}}