[clang] f8a37a6 - [Clang] Fix compat diagnostic to detect a nontype template parameter has a placeholder type using getContainedAutoType()

Shafik Yaghmour via cfe-commits cfe-commits at lists.llvm.org
Sun Sep 18 12:12:36 PDT 2022


Author: Shafik Yaghmour
Date: 2022-09-18T11:54:32-07:00
New Revision: f8a37a6ce67470f9f6b3a4dacb65c637c2958b8d

URL: https://github.com/llvm/llvm-project/commit/f8a37a6ce67470f9f6b3a4dacb65c637c2958b8d
DIFF: https://github.com/llvm/llvm-project/commit/f8a37a6ce67470f9f6b3a4dacb65c637c2958b8d.diff

LOG: [Clang] Fix compat diagnostic to detect a nontype template parameter has a placeholder type using getContainedAutoType()

Based on the changes introduced by 15361a21e01026e74cb17011b702c7d1c881ae94 it
looks like C++17 compatibility diagnostic should have been checking
getContainedAutoType().

This fixes: https://github.com/llvm/llvm-project/issues/57369
  https://github.com/llvm/llvm-project/issues/57643
  https://github.com/llvm/llvm-project/issues/57793

Differential Revision: https://reviews.llvm.org/D132990

Added: 
    clang/test/SemaTemplate/gh57362.cpp
    clang/test/SemaTemplate/temp_arg_nontype_diagnostic_cxx17.cpp

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Sema/SemaTemplate.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 403d50b1c9d47..035536d18d9b8 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -146,6 +146,11 @@ Bug Fixes
 - A SubstTemplateTypeParmType can now represent the pack index for a
   substitution from an expanded pack.
   `Issue 56099 <https://github.com/llvm/llvm-project/issues/56099>`_
+- Fix `-Wpre-c++17-compat` crashing Clang when compiling C++20 code which
+  contains deduced template specializations. This Fixes
+  `Issue 57369 <https://github.com/llvm/llvm-project/issues/57369>`_
+  `Issue 57643 <https://github.com/llvm/llvm-project/issues/57643>`_
+  `Issue 57793 <https://github.com/llvm/llvm-project/issues/57793>`_
 
 
 Improvements to Clang's diagnostics

diff  --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index 6e56cccc5ebf6..adca91b0ae63e 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -1531,11 +1531,11 @@ NamedDecl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
 
   CheckValidDeclSpecifiers();
 
-  if (TInfo->getType()->isUndeducedType()) {
-    Diag(D.getIdentifierLoc(),
-         diag::warn_cxx14_compat_template_nontype_parm_auto_type)
-      << QualType(TInfo->getType()->getContainedAutoType(), 0);
-  }
+  if (const auto *T = TInfo->getType()->getContainedDeducedType())
+    if (isa<AutoType>(T))
+      Diag(D.getIdentifierLoc(),
+           diag::warn_cxx14_compat_template_nontype_parm_auto_type)
+          << QualType(TInfo->getType()->getContainedAutoType(), 0);
 
   assert(S->isTemplateParamScope() &&
          "Non-type template parameter not in template parameter scope!");

diff  --git a/clang/test/SemaTemplate/gh57362.cpp b/clang/test/SemaTemplate/gh57362.cpp
new file mode 100644
index 0000000000000..87bd8da0a91da
--- /dev/null
+++ b/clang/test/SemaTemplate/gh57362.cpp
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++20 -Wpre-c++17-compat %s
+// expected-no-diagnostics
+
+namespace GH57362 {
+template <int num>
+class TemplateClass {};
+
+template <TemplateClass nttp> // ok, no diagnostic expected
+void func() {}
+}

diff  --git a/clang/test/SemaTemplate/temp_arg_nontype_diagnostic_cxx17.cpp b/clang/test/SemaTemplate/temp_arg_nontype_diagnostic_cxx17.cpp
new file mode 100644
index 0000000000000..6afe143d7c30a
--- /dev/null
+++ b/clang/test/SemaTemplate/temp_arg_nontype_diagnostic_cxx17.cpp
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++17 -Wpre-c++17-compat %s
+
+template <decltype(auto) n> // expected-warning {{non-type template parameters declared with 'decltype(auto)' are incompatible with C++ standards before C++17}}
+struct B{};
+
+template <auto n> // expected-warning {{non-type template parameters declared with 'auto' are incompatible with C++ standards before C++17}}
+struct A{};


        


More information about the cfe-commits mailing list