Index: lib/Sema/SemaTemplate.cpp =================================================================== --- lib/Sema/SemaTemplate.cpp +++ lib/Sema/SemaTemplate.cpp @@ -3052,6 +3052,69 @@ return Cond; } +// Returns true if `RecordName` is the name of an std type trait. +static bool isTypeTraitName(const llvm::StringRef RecordName) { + return +#define TYPE_TRAIT_1(Spelling, Name, Key) RecordName == (#Spelling + 2) || +#define TYPE_TRAIT_2(Spelling, Name, Key) RecordName == (#Spelling + 2) || +#define TYPE_TRAIT_N(Spelling, Name, Key) RecordName == (#Spelling + 2) || +#include "clang/Basic/TokenKinds.def" +#undef TYPE_TRAIT_1 +#undef TYPE_TRAIT_2 +#undef TYPE_TRAIT_N + false; +} + +// If `Record` is a type trait, pretty prints the condition with actual types +// and returns true. +static bool prettyPrintTypeTrait(const NestedNameSpecifier *NNS, + llvm::raw_string_ostream &OS, + const PrintingPolicy &PrintPolicy) { + // We are looking for a type. + if (NNS == nullptr || NNS->getKind() != NestedNameSpecifier::TypeSpec) + return false; + const RecordDecl *Record = NNS->getAsRecordDecl(); + if (Record == nullptr) + return false; + // In namespace "::std". + if (!Record->isInStdNamespace()) + return false; + // Now check whether the record name is one of the known type traits. + if (!isTypeTraitName(Record->getName())) + return false; + // Print the type trait with resolved template parameters. + const auto *TmplDecl = dyn_cast(Record); + assert(TmplDecl && + "std type_traits should be ClassTemplateSpecializationDecl"); + OS << "std::" << Record->getName() << "<"; + ArrayRef Args = TmplDecl->getTemplateArgs().asArray(); + Args.front().print(PrintPolicy, OS); + for (const auto &Arg : Args.drop_front()) { + OS << ", "; + Arg.print(PrintPolicy, OS); + } + OS << ">"; + 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 &PrintPolicy) { + if (const auto *DR = dyn_cast(FailedCond)) { + const auto *Var = dyn_cast(DR->getDecl()); + // This might be `std::some_type_trait::value`. + if (Var && Var->isStaticDataMember() && Var->getName() == "value" && + prettyPrintTypeTrait(DR->getQualifier(), OS, PrintPolicy)) { + OS << "::value"; + return; + } + } + FailedCond->printPretty(OS, nullptr, PrintPolicy); +} + std::pair Sema::findFailedBooleanCondition(Expr *Cond, bool AllowTopLevelCond) { Cond = lookThroughRangesV3Condition(PP, Cond); @@ -3093,7 +3156,7 @@ std::string Description; { llvm::raw_string_ostream Out(Description); - FailedCond->printPretty(Out, nullptr, getPrintingPolicy()); + prettyPrintFailedBooleanCondition(Out, FailedCond, getPrintingPolicy()); } return { FailedCond, Description }; } Index: test/SemaCXX/static-assert.cpp =================================================================== --- test/SemaCXX/static-assert.cpp +++ test/SemaCXX/static-assert.cpp @@ -68,3 +68,44 @@ }; 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{{static_assert failed due to requirement 'std::is_same::value' "message"}} +static_assert(std::is_const::value, "message"); // expected-error{{static_assert failed due to requirement 'std::is_const::value' "message"}}