courbet updated this revision to Diff 175710. courbet marked 2 inline comments as done. courbet added a comment.
expand types in all qualifiers, not only type traits. Repository: rC Clang CHANGES SINCE LAST ACTION https://reviews.llvm.org/D54903/new/ https://reviews.llvm.org/D54903 Files: include/clang/AST/NestedNameSpecifier.h lib/AST/NestedNameSpecifier.cpp lib/Sema/SemaTemplate.cpp test/SemaCXX/static-assert.cpp
Index: test/SemaCXX/static-assert.cpp =================================================================== --- test/SemaCXX/static-assert.cpp +++ test/SemaCXX/static-assert.cpp @@ -68,3 +68,74 @@ }; static_assert(first_trait<X>::value && second_trait<X>::value, "message"); // expected-error{{static_assert failed due to requirement 'second_trait<X>::value' "message"}} + +namespace std { + +template <class Tp, Tp v> +struct integral_constant { + static const Tp value = v; + typedef Tp value_type; + typedef integral_constant type; +}; + +template <class Tp, Tp v> +const Tp integral_constant<Tp, v>::value; + +typedef integral_constant<bool, true> true_type; +typedef integral_constant<bool, false> false_type; + +template <class Tp> +struct is_const : public false_type {}; +template <class Tp> +struct is_const<Tp const> : public true_type {}; + +// We do not define is_same in terms of integral_constant to check that both implementations are supported. +template <typename T, typename U> +struct is_same { + static const bool value = false; +}; + +template <typename T> +struct is_same<T, T> { + static const bool value = true; +}; + +} // namespace std + +struct ExampleTypes { + using T = int; + using U = float; +}; + +static_assert(std::is_same<ExampleTypes::T, ExampleTypes::U>::value, "message"); // expected-error{{static_assert failed due to requirement 'std::is_same<int, float>::value' "message"}} +static_assert(std::is_const<ExampleTypes::T>::value, "message"); // expected-error{{static_assert failed due to requirement 'std::is_const<int>::value' "message"}} + +struct BI_tag {}; +struct RAI_tag : BI_tag {}; +struct MyIterator { + using tag = BI_tag; +}; +struct MyContainer { + using iterator = MyIterator; +}; +template <class Container> +void foo() { + static_assert(std::is_same<RAI_tag, typename Container::iterator::tag>::value, "message"); // expected-error{{static_assert failed due to requirement 'std::is_same<RAI_tag, BI_tag>::value' "message"}} +} +template void foo<MyContainer>(); // expected-note {{in instantiation of function template specialization 'foo<MyContainer>' requested here}} + +namespace ns { +template <typename T, int v> +struct NestedTemplates1 { + struct NestedTemplates2 { + template <typename U> + struct NestedTemplates3 : public std::is_same<T, U> {}; + }; +}; +} // namespace ns + +template <typename T, typename U, int a> +void foo2() { + static_assert(::ns::NestedTemplates1<T, a>::NestedTemplates2::template NestedTemplates3<U>::value, "message"); // expected-error{{static_assert failed due to requirement '::ns::NestedTemplates1<int, 3>::NestedTemplates2::NestedTemplates3<float>::value' "message"}} +} +template void foo2<int, float, 3>(); // expected-note {{in instantiation of function template specialization 'foo2<int, float, 3>' requested here}} Index: lib/Sema/SemaTemplate.cpp =================================================================== --- lib/Sema/SemaTemplate.cpp +++ lib/Sema/SemaTemplate.cpp @@ -3052,6 +3052,23 @@ 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 &PrintPolicy) { + const auto *DR = dyn_cast<DeclRefExpr>(FailedCond); + if (DR && DR->getQualifier()) { + // If this is a qualified name, expand the template arguments in nested + // qualifiers. + DR->getQualifier()->print(OS, PrintPolicy, true); + OS << DR->getDecl()->getName(); + return; + } + FailedCond->printPretty(OS, nullptr, PrintPolicy); +} + std::pair<Expr *, std::string> Sema::findFailedBooleanCondition(Expr *Cond, bool AllowTopLevelCond) { Cond = lookThroughRangesV3Condition(PP, Cond); @@ -3093,7 +3110,7 @@ std::string Description; { llvm::raw_string_ostream Out(Description); - FailedCond->printPretty(Out, nullptr, getPrintingPolicy()); + prettyPrintFailedBooleanCondition(Out, FailedCond, getPrintingPolicy()); } return { FailedCond, Description }; } Index: lib/AST/NestedNameSpecifier.cpp =================================================================== --- lib/AST/NestedNameSpecifier.cpp +++ 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,16 @@ LLVM_FALLTHROUGH; case TypeSpec: { + if (ResolveTemplateArguments && getAsRecordDecl()) { + if (const auto *Record = + dyn_cast<ClassTemplateSpecializationDecl>(getAsRecordDecl())) { + // 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); @@ -319,8 +329,8 @@ // suppress that nested-name-specifier during printing. assert(!isa<ElaboratedType>(T) && "Elaborated type in nested-name-specifier"); - if (const TemplateSpecializationType *SpecType - = dyn_cast<TemplateSpecializationType>(T)) { + if (const TemplateSpecializationType *SpecType = + dyn_cast<TemplateSpecializationType>(T)) { // Print the template name without its corresponding // nested-name-specifier. SpecType->getTemplateName().print(OS, InnerPolicy, true); Index: include/clang/AST/NestedNameSpecifier.h =================================================================== --- include/clang/AST/NestedNameSpecifier.h +++ 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<int, MyClass>` instead of + /// `ns::SomeTemplate<Container::value_type, T>`. + void print(raw_ostream &OS, const PrintingPolicy &Policy, + bool ResolveTemplateArguments = false) const; void Profile(llvm::FoldingSetNodeID &ID) const { ID.AddPointer(Prefix.getOpaqueValue());
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits