From: Richard Smith Date: Mon, 10 Sep 2018 20:31:03 +0000 (+0000) Subject: Enhance -Wc++14-compat for class template argument deduction to list the X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5bd39424d48a329650b0652fcb3441fa9c97b855;p=clang Enhance -Wc++14-compat for class template argument deduction to list the deduced type (if known). git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@341858 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 3ff595c8e9..b253a883d6 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -2153,7 +2153,8 @@ def note_deduction_guide_access : Note< "deduction guide declared %0 by intervening access specifier">; def warn_cxx14_compat_class_template_argument_deduction : Warning< "class template argument deduction is incompatible with C++ standards " - "before C++17">, InGroup, DefaultIgnore; + "before C++17%select{|; for compatibility, use explicit type name %1}0">, + InGroup, DefaultIgnore; // C++14 deduced return types def err_auto_fn_deduction_failure : Error< diff --git a/lib/Sema/SemaInit.cpp b/lib/Sema/SemaInit.cpp index 18a7aab1f5..a25818f0fd 100644 --- a/lib/Sema/SemaInit.cpp +++ b/lib/Sema/SemaInit.cpp @@ -9139,13 +9139,13 @@ QualType Sema::DeduceTemplateSpecializationFromInitializer( return QualType(); } - Diag(TSInfo->getTypeLoc().getBeginLoc(), - diag::warn_cxx14_compat_class_template_argument_deduction) - << TSInfo->getTypeLoc().getSourceRange(); - // Can't deduce from dependent arguments. - if (Expr::hasAnyTypeDependentArguments(Inits)) + if (Expr::hasAnyTypeDependentArguments(Inits)) { + Diag(TSInfo->getTypeLoc().getBeginLoc(), + diag::warn_cxx14_compat_class_template_argument_deduction) + << TSInfo->getTypeLoc().getSourceRange() << 0; return Context.DependentTy; + } // FIXME: Perform "exact type" matching first, per CWG discussion? // Or implement this via an implied 'T(T) -> T' deduction guide? @@ -9348,5 +9348,10 @@ QualType Sema::DeduceTemplateSpecializationFromInitializer( // C++ [dcl.type.class.deduct]p1: // The placeholder is replaced by the return type of the function selected // by overload resolution for class template deduction. - return SubstAutoType(TSInfo->getType(), Best->Function->getReturnType()); + QualType DeducedType = + SubstAutoType(TSInfo->getType(), Best->Function->getReturnType()); + Diag(TSInfo->getTypeLoc().getBeginLoc(), + diag::warn_cxx14_compat_class_template_argument_deduction) + << TSInfo->getTypeLoc().getSourceRange() << 1 << DeducedType; + return DeducedType; } diff --git a/test/SemaCXX/cxx14-compat.cpp b/test/SemaCXX/cxx14-compat.cpp index 8b3dee8c82..d70f477cd0 100644 --- a/test/SemaCXX/cxx14-compat.cpp +++ b/test/SemaCXX/cxx14-compat.cpp @@ -16,7 +16,7 @@ namespace [[]] NS_with_attr {} // expected-warning {{incompatible with C++ stand enum { e [[]] }; // expected-warning {{incompatible with C++ standards before C++17}} template struct X {}; -X x; // expected-warning {{class template argument deduction is incompatible with C++ standards before C++17}} +X x; // expected-warning {{class template argument deduction is incompatible with C++ standards before C++17; for compatibility, use explicit type name 'X'}} template class> struct Y {}; Y yx; // ok, not class template argument deduction