This revision was automatically updated to reflect the committed changes. Closed by commit rC351484: Add -Wctad-maybe-unsupported to diagnose CTAD on types with no user defined… (authored by EricWF, committed by ).
Repository: rC Clang CHANGES SINCE LAST ACTION https://reviews.llvm.org/D56731/new/ https://reviews.llvm.org/D56731 Files: include/clang/Basic/DiagnosticGroups.td include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaInit.cpp test/SemaCXX/cxx1z-class-template-argument-deduction.cpp
Index: include/clang/Basic/DiagnosticSemaKinds.td =================================================================== --- include/clang/Basic/DiagnosticSemaKinds.td +++ include/clang/Basic/DiagnosticSemaKinds.td @@ -2129,6 +2129,12 @@ "class template argument deduction is incompatible with C++ standards " "before C++17%select{|; for compatibility, use explicit type name %1}0">, InGroup<CXXPre17Compat>, DefaultIgnore; +def warn_ctad_maybe_unsupported : Warning< + "%0 may not intend to support class template argument deduction">, + InGroup<CTADMaybeUnsupported>, DefaultIgnore; +def note_suppress_ctad_maybe_unsupported : Note< + "add a deduction guide to suppress this warning">; + // C++14 deduced return types def err_auto_fn_deduction_failure : Error< Index: include/clang/Basic/DiagnosticGroups.td =================================================================== --- include/clang/Basic/DiagnosticGroups.td +++ include/clang/Basic/DiagnosticGroups.td @@ -1050,3 +1050,5 @@ // A group for cross translation unit static analysis related warnings. def CrossTU : DiagGroup<"ctu">; + +def CTADMaybeUnsupported : DiagGroup<"ctad-maybe-unsupported">; Index: test/SemaCXX/cxx1z-class-template-argument-deduction.cpp =================================================================== --- test/SemaCXX/cxx1z-class-template-argument-deduction.cpp +++ test/SemaCXX/cxx1z-class-template-argument-deduction.cpp @@ -409,6 +409,86 @@ } +#pragma clang diagnostic push +#pragma clang diagnostic warning "-Wctad-maybe-unsupported" +namespace test_implicit_ctad_warning { + +template <class T> +struct Tag {}; + +template <class T> +struct NoExplicit { // expected-note {{add a deduction guide to suppress this warning}} + NoExplicit(T) {} + NoExplicit(T, int) {} +}; + +// expected-warning@+1 {{'NoExplicit' may not intend to support class template argument deduction}} +NoExplicit ne(42); + +template <class U> +struct HasExplicit { + HasExplicit(U) {} + HasExplicit(U, int) {} +}; +template <class U> HasExplicit(U, int) -> HasExplicit<Tag<U>>; + +HasExplicit he(42); + +// Motivating examples from (taken from Stephan Lavavej's 2018 Cppcon talk) +template <class T, class U> +struct AmateurPair { // expected-note {{add a deduction guide to suppress this warning}} + T first; + U second; + explicit AmateurPair(const T &t, const U &u) {} +}; +// expected-warning@+1 {{'AmateurPair' may not intend to support class template argument deduction}} +AmateurPair p1(42, "hello world"); // deduces to Pair<int, char[12]> + +template <class T, class U> +struct AmateurPair2 { // expected-note {{add a deduction guide to suppress this warning}} + T first; + U second; + explicit AmateurPair2(T t, U u) {} +}; +// expected-warning@+1 {{'AmateurPair2' may not intend to support class template argument deduction}} +AmateurPair2 p2(42, "hello world"); // deduces to Pair2<int, const char*> + +template <class T, class U> +struct ProPair { + T first; U second; + explicit ProPair(T const& t, U const& u) {} +}; +template<class T1, class T2> +ProPair(T1, T2) -> ProPair<T1, T2>; +ProPair p3(42, "hello world"); // deduces to ProPair<int, const char*> +static_assert(__is_same(decltype(p3), ProPair<int, const char*>)); + +// Test that user-defined explicit guides suppress the warning even if they +// aren't used as candidates. +template <class T> +struct TestExplicitCtor { + TestExplicitCtor(T) {} +}; +template <class T> +explicit TestExplicitCtor(TestExplicitCtor<T> const&) -> TestExplicitCtor<void>; +TestExplicitCtor<int> ce1{42}; +TestExplicitCtor ce2 = ce1; +static_assert(__is_same(decltype(ce2), TestExplicitCtor<int>), ""); + +struct allow_ctad_t { + allow_ctad_t() = delete; +}; + +template <class T> +struct TestSuppression { + TestSuppression(T) {} +}; +TestSuppression(allow_ctad_t)->TestSuppression<void>; +TestSuppression ta("abc"); +static_assert(__is_same(decltype(ta), TestSuppression<const char *>), ""); +} +#pragma clang diagnostic pop + #else // expected-no-diagnostics Index: lib/Sema/SemaInit.cpp =================================================================== --- lib/Sema/SemaInit.cpp +++ lib/Sema/SemaInit.cpp @@ -9264,9 +9264,14 @@ OverloadCandidateSet Candidates(Kind.getLocation(), OverloadCandidateSet::CSK_Normal); OverloadCandidateSet::iterator Best; + + bool HasAnyDeductionGuide = false; + auto tryToResolveOverload = [&](bool OnlyListConstructors) -> OverloadingResult { Candidates.clear(OverloadCandidateSet::CSK_Normal); + HasAnyDeductionGuide = false; + for (auto I = Guides.begin(), E = Guides.end(); I != E; ++I) { NamedDecl *D = (*I)->getUnderlyingDecl(); if (D->isInvalidDecl()) @@ -9278,6 +9283,9 @@ if (!GD) continue; + if (!GD->isImplicit()) + HasAnyDeductionGuide = true; + // C++ [over.match.ctor]p1: (non-list copy-initialization from non-class) // For copy-initialization, the candidate functions are all the // converting constructors (12.3.1) of that class. @@ -9430,5 +9438,15 @@ Diag(TSInfo->getTypeLoc().getBeginLoc(), diag::warn_cxx14_compat_class_template_argument_deduction) << TSInfo->getTypeLoc().getSourceRange() << 1 << DeducedType; + + // Warn if CTAD was used on a type that does not have any user-defined + // deduction guides. + if (!HasAnyDeductionGuide) { + Diag(TSInfo->getTypeLoc().getBeginLoc(), + diag::warn_ctad_maybe_unsupported) + << TemplateName; + Diag(Template->getLocation(), diag::note_suppress_ctad_maybe_unsupported); + } + return DeducedType; }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits