[clang] [clang] Diagnose use of deprecated template alias (PR #97619)

via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 3 11:37:05 PDT 2024


https://github.com/premanandrao created https://github.com/llvm/llvm-project/pull/97619

Issue a warning diagnostic when a template alias with a deprecated attribute is used.

>From e1076942f07b1d2de857688c88de6ff966861650 Mon Sep 17 00:00:00 2001
From: Premanand M Rao <premanand.m.rao at intel.com>
Date: Wed, 3 Jul 2024 11:20:42 -0700
Subject: [PATCH] [clang] Diagnose use of deprecated template alias

Issue a warning diagnostic when a template alias with a
deprecated attribute is used.
---
 clang/lib/Sema/SemaAvailability.cpp           |  6 ++
 clang/lib/Sema/SemaTemplate.cpp               |  4 ++
 .../alias-template-deprecated.cpp             | 58 +++++++++++++++++++
 3 files changed, 68 insertions(+)
 create mode 100644 clang/test/SemaTemplate/alias-template-deprecated.cpp

diff --git a/clang/lib/Sema/SemaAvailability.cpp b/clang/lib/Sema/SemaAvailability.cpp
index df83bbfb7aac8..17566c226ec80 100644
--- a/clang/lib/Sema/SemaAvailability.cpp
+++ b/clang/lib/Sema/SemaAvailability.cpp
@@ -107,6 +107,12 @@ ShouldDiagnoseAvailabilityOfDecl(Sema &S, const NamedDecl *D,
     break;
   }
 
+  // For alias templates, get the underlying declaration.
+  if (const auto *ADecl = dyn_cast<TypeAliasTemplateDecl>(D)) {
+    D = ADecl->getTemplatedDecl();
+    Result = D->getAvailability(Message);
+  }
+
   // Forward class declarations get their attributes from their definition.
   if (const auto *IDecl = dyn_cast<ObjCInterfaceDecl>(D)) {
     if (IDecl->getDefinition()) {
diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index 6879a9a274b5c..0e7663b82f437 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -4539,6 +4539,10 @@ QualType Sema::CheckTemplateIdType(TemplateName Name,
         *this, /*PointOfInstantiation=*/TemplateLoc,
         /*Entity=*/AliasTemplate,
         /*TemplateArgs=*/TemplateArgLists.getInnermost());
+
+    // Diagnose uses of this alias.
+    (void)DiagnoseUseOfDecl(AliasTemplate, TemplateLoc);
+
     if (Inst.isInvalid())
       return QualType();
 
diff --git a/clang/test/SemaTemplate/alias-template-deprecated.cpp b/clang/test/SemaTemplate/alias-template-deprecated.cpp
new file mode 100644
index 0000000000000..10242d7a810b0
--- /dev/null
+++ b/clang/test/SemaTemplate/alias-template-deprecated.cpp
@@ -0,0 +1,58 @@
+// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
+//
+// This test checks that a deprecated attribute on an alias
+// template triggers a warning diagnostic when it is used.
+
+template <typename T>
+struct NoAttr {
+  void foo() {}
+};
+
+// expected-note at +2 5{{'UsingWithAttr' has been explicitly marked deprecated here}}
+template <typename T>
+using UsingWithAttr __attribute__((deprecated)) = NoAttr<T>;
+
+// expected-note at +1 {{'UsingInstWithAttr' has been explicitly marked deprecated here}}
+using UsingInstWithAttr __attribute__((deprecated)) = NoAttr<int>;
+
+// expected-note at +1 {{'TDWithAttr' has been explicitly marked deprecated here}}
+typedef NoAttr<int> TDWithAttr __attribute__((deprecated));
+
+// expected-warning at +1 {{'UsingWithAttr' is deprecated}}
+typedef UsingWithAttr<int> TDUsingWithAttr;
+
+typedef NoAttr<int> TDNoAttr;
+
+// expected-note at +1 {{'UsingTDWithAttr' has been explicitly marked deprecated here}}
+using UsingTDWithAttr __attribute__((deprecated)) = TDNoAttr;
+
+struct S {
+  NoAttr<float> f1;
+  // expected-warning at +1 {{'UsingWithAttr' is deprecated}}
+  UsingWithAttr<float> f2;
+};
+
+// expected-warning at +1 {{'UsingWithAttr' is deprecated}}
+void foo(NoAttr<short> s1, UsingWithAttr<short> s2) {
+}
+
+void bar() {
+  NoAttr<int> obj; // Okay
+
+  // expected-warning at +2 {{'UsingWithAttr' is deprecated}}
+  // expected-note at +1 {{in instantiation of template type alias 'UsingWithAttr' requested here}}
+  UsingWithAttr<int> objUsingWA;
+
+  // expected-warning at +2 {{'UsingWithAttr' is deprecated}}
+  // expected-note at +1 {{in instantiation of template type alias 'UsingWithAttr' requested here}}
+  UsingWithAttr<int>().foo();
+
+  // expected-warning at +1 {{'UsingInstWithAttr' is deprecated}}
+  UsingInstWithAttr objUIWA;
+
+  // expected-warning at +1 {{'TDWithAttr' is deprecated}}
+  TDWithAttr objTDWA;
+
+  // expected-warning at +1 {{'UsingTDWithAttr' is deprecated}}
+  UsingTDWithAttr objUTDWA;
+}



More information about the cfe-commits mailing list