Author: courbet Date: Mon Dec 3 23:59:57 2018 New Revision: 348239 URL: http://llvm.org/viewvc/llvm-project?rev=348239&view=rev Log: [WIP][Sema] Improve static_assert diagnostics for type traits.
Summary: In our codebase, `static_assert(std::some_type_trait<Ts...>::value, "msg")` (where `some_type_trait` is an std type_trait and `Ts...` is the appropriate template parameters) account for 11.2% of the `static_assert`s. In these cases, the `Ts` are typically not spelled out explicitly, e.g. `static_assert(std::is_same<SomeT::TypeT, typename SomeDependentT::value_type>::value, "message");` The diagnostic when the assert fails is typically not very useful, e.g. `static_assert failed due to requirement 'std::is_same<SomeT::TypeT, typename SomeDependentT::value_type>::value' "message"` This change makes the diagnostic spell out the types explicitly , e.g. `static_assert failed due to requirement 'std::is_same<int, float>::value' "message"` See tests for more examples. After this is submitted, I intend to handle `static_assert(!std::some_type_trait<Ts...>::value, "msg")`, which is another 6.6% of static_asserts. Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D54903 Added: cfe/trunk/test/SemaCXX/static-assert-cxx17.cpp Modified: cfe/trunk/include/clang/AST/NestedNameSpecifier.h cfe/trunk/lib/AST/NestedNameSpecifier.cpp cfe/trunk/lib/Sema/SemaTemplate.cpp cfe/trunk/test/SemaCXX/static-assert.cpp Modified: cfe/trunk/include/clang/AST/NestedNameSpecifier.h URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/NestedNameSpecifier.h?rev=348239&r1=348238&r2=348239&view=diff ============================================================================== --- cfe/trunk/include/clang/AST/NestedNameSpecifier.h (original) +++ cfe/trunk/include/clang/AST/NestedNameSpecifier.h Mon Dec 3 23:59:57 2018 @@ -212,9 +212,12 @@ public: /// 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()); Modified: cfe/trunk/lib/AST/NestedNameSpecifier.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/NestedNameSpecifier.cpp?rev=348239&r1=348238&r2=348239&view=diff ============================================================================== --- cfe/trunk/lib/AST/NestedNameSpecifier.cpp (original) +++ cfe/trunk/lib/AST/NestedNameSpecifier.cpp Mon Dec 3 23:59:57 2018 @@ -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 @@ bool NestedNameSpecifier::containsUnexpa /// 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 @@ NestedNameSpecifier::print(raw_ostream & LLVM_FALLTHROUGH; case TypeSpec: { + const auto *Record = + dyn_cast_or_null<ClassTemplateSpecializationDecl>(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); Modified: cfe/trunk/lib/Sema/SemaTemplate.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplate.cpp?rev=348239&r1=348238&r2=348239&view=diff ============================================================================== --- cfe/trunk/lib/Sema/SemaTemplate.cpp (original) +++ cfe/trunk/lib/Sema/SemaTemplate.cpp Mon Dec 3 23:59:57 2018 @@ -3052,6 +3052,28 @@ static Expr *lookThroughRangesV3Conditio 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<DeclRefExpr>(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<VarTemplateSpecializationDecl>(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<Expr *, std::string> Sema::findFailedBooleanCondition(Expr *Cond, bool AllowTopLevelCond) { Cond = lookThroughRangesV3Condition(PP, Cond); @@ -3093,7 +3115,7 @@ Sema::findFailedBooleanCondition(Expr *C std::string Description; { llvm::raw_string_ostream Out(Description); - FailedCond->printPretty(Out, nullptr, getPrintingPolicy()); + prettyPrintFailedBooleanCondition(Out, FailedCond, getPrintingPolicy()); } return { FailedCond, Description }; } Added: cfe/trunk/test/SemaCXX/static-assert-cxx17.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/static-assert-cxx17.cpp?rev=348239&view=auto ============================================================================== --- cfe/trunk/test/SemaCXX/static-assert-cxx17.cpp (added) +++ cfe/trunk/test/SemaCXX/static-assert-cxx17.cpp Mon Dec 3 23:59:57 2018 @@ -0,0 +1,47 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++1z -triple=x86_64-linux-gnu + +template <typename U, typename V> +struct S1 { + static constexpr const bool value = false; +}; + +template <typename U, typename V> +inline constexpr bool global_inline_var = S1<U, V>::value; + +template <typename T> +struct S2 { + template <typename U, typename V> + static inline constexpr bool var = global_inline_var<U, V>; +}; + +template <typename U, typename V> +void foo() { + static_assert(S1<U, V>::value); + // expected-error@-1{{static_assert failed due to requirement 'S1<int, float>::value'}} +} +template void foo<int, float>(); +// expected-note@-1{{in instantiation of function template specialization 'foo<int, float>' requested here}} + +template <typename U, typename V> +void foo2() { + static_assert(global_inline_var<U, V>); + // expected-error@-1{{static_assert failed due to requirement 'global_inline_var<int, float>'}} +} +template void foo2<int, float>(); +// expected-note@-1{{in instantiation of function template specialization 'foo2<int, float>' requested here}} + +template <typename T, typename U, typename V> +void foo3() { + static_assert(T::template var<U, V>); + // expected-error@-1{{static_assert failed due to requirement 'S2<long>::var<int, float>'}} +} +template void foo3<S2<long>, int, float>(); +// expected-note@-1{{in instantiation of function template specialization 'foo3<S2<long>, int, float>' requested here}} + +template <typename T> +void foo4() { + static_assert(S1<T[sizeof(T)], int[4]>::value, ""); + // expected-error@-1{{static_assert failed due to requirement 'S1<float [4], int [4]>::value'}} +}; +template void foo4<float>(); +// expected-note@-1{{in instantiation of function template specialization 'foo4<float>' requested here}} Modified: cfe/trunk/test/SemaCXX/static-assert.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/static-assert.cpp?rev=348239&r1=348238&r2=348239&view=diff ============================================================================== --- cfe/trunk/test/SemaCXX/static-assert.cpp (original) +++ cfe/trunk/test/SemaCXX/static-assert.cpp Mon Dec 3 23:59:57 2018 @@ -68,3 +68,100 @@ template<typename T> struct second_trait }; 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@-1{{static_assert failed due to requirement 'std::is_same<int, float>::value' "message"}} +static_assert(std::is_const<ExampleTypes::T>::value, "message"); +// expected-error@-1{{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@-1{{static_assert failed due to requirement 'std::is_same<RAI_tag, BI_tag>::value' "message"}} +} +template void foo<MyContainer>(); +// expected-note@-1{{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@-1{{static_assert failed due to requirement '::ns::NestedTemplates1<int, 3>::NestedTemplates2::NestedTemplates3<float>::value' "message"}} +} +template void foo2<int, float, 3>(); +// expected-note@-1{{in instantiation of function template specialization 'foo2<int, float, 3>' requested here}} + +template <class T> +void foo3(T t) { + static_assert(std::is_const<T>::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<decltype(t)>::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 <class T> +void foo4(T t) { + static_assert(std::is_const<typename T::iterator>::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<int>' requested here}} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits