Skip to content

[clang] Diagnose use of deprecated template alias #97619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 22, 2024

Conversation

premanandrao
Copy link
Contributor

@premanandrao premanandrao commented Jul 3, 2024

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

Fixes #18236.

Issue a warning diagnostic when a template alias with a
deprecated attribute is used.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jul 3, 2024
@llvmbot
Copy link
Member

llvmbot commented Jul 3, 2024

@llvm/pr-subscribers-clang

Author: None (premanandrao)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/97619.diff

3 Files Affected:

  • (modified) clang/lib/Sema/SemaAvailability.cpp (+6)
  • (modified) clang/lib/Sema/SemaTemplate.cpp (+4)
  • (added) clang/test/SemaTemplate/alias-template-deprecated.cpp (+58)
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@+2 5{{'UsingWithAttr' has been explicitly marked deprecated here}}
+template <typename T>
+using UsingWithAttr __attribute__((deprecated)) = NoAttr<T>;
+
+// expected-note@+1 {{'UsingInstWithAttr' has been explicitly marked deprecated here}}
+using UsingInstWithAttr __attribute__((deprecated)) = NoAttr<int>;
+
+// expected-note@+1 {{'TDWithAttr' has been explicitly marked deprecated here}}
+typedef NoAttr<int> TDWithAttr __attribute__((deprecated));
+
+// expected-warning@+1 {{'UsingWithAttr' is deprecated}}
+typedef UsingWithAttr<int> TDUsingWithAttr;
+
+typedef NoAttr<int> TDNoAttr;
+
+// expected-note@+1 {{'UsingTDWithAttr' has been explicitly marked deprecated here}}
+using UsingTDWithAttr __attribute__((deprecated)) = TDNoAttr;
+
+struct S {
+  NoAttr<float> f1;
+  // expected-warning@+1 {{'UsingWithAttr' is deprecated}}
+  UsingWithAttr<float> f2;
+};
+
+// expected-warning@+1 {{'UsingWithAttr' is deprecated}}
+void foo(NoAttr<short> s1, UsingWithAttr<short> s2) {
+}
+
+void bar() {
+  NoAttr<int> obj; // Okay
+
+  // expected-warning@+2 {{'UsingWithAttr' is deprecated}}
+  // expected-note@+1 {{in instantiation of template type alias 'UsingWithAttr' requested here}}
+  UsingWithAttr<int> objUsingWA;
+
+  // expected-warning@+2 {{'UsingWithAttr' is deprecated}}
+  // expected-note@+1 {{in instantiation of template type alias 'UsingWithAttr' requested here}}
+  UsingWithAttr<int>().foo();
+
+  // expected-warning@+1 {{'UsingInstWithAttr' is deprecated}}
+  UsingInstWithAttr objUIWA;
+
+  // expected-warning@+1 {{'TDWithAttr' is deprecated}}
+  TDWithAttr objTDWA;
+
+  // expected-warning@+1 {{'UsingTDWithAttr' is deprecated}}
+  UsingTDWithAttr objUTDWA;
+}

@mizvekov
Copy link
Contributor

mizvekov commented Jul 3, 2024

For reference, see https://reviews.llvm.org/D136533 for similar expansion of diagnostics for uses of declarations, and how that lead to trouble in MacOS land.

I'd suggest building libc++ on MacOS, to be sure this won't cause regressions and a future revert.

I have a MacOS machine now, so I should be able to try to revive that patch at some point as well.

@premanandrao
Copy link
Contributor Author

For reference, see https://reviews.llvm.org/D136533 for similar expansion of diagnostics for uses of declarations, and how that lead to trouble in MacOS land.
I'd suggest building libc++ on MacOS, to be sure this won't cause regressions and a future revert.

Thanks for pointing out the other PR. I don't have a MacOS system handy right now, but I can get to one after the weekend.

I have a MacOS machine now, so I should be able to try to revive that patch at some point as well.

Sounds good.

@cor3ntin
Copy link
Contributor

@ldionne

@ldionne
Copy link
Member

ldionne commented Jul 12, 2024

I just ran the bootstrapping build with this patch applied and ran check-runtimes, and nothing failed. This might not be sufficient (perhaps I missed something important), but at first glance it doesn't seem to break anything on our side.

@premanandrao
Copy link
Contributor Author

premanandrao commented Jul 12, 2024

I just ran the bootstrapping build with this patch applied and ran check-runtimes, and nothing failed. This might not be sufficient (perhaps I missed something important), but at first glance it doesn't seem to break anything on our side

Thank you SO much!

Would you mind sharing (pointers to) instructions on how you built this? I was running into errors about __remove_cv_t (and others) at my end; this was without any of my changes, so it was a sort of a reference build I was trying.

@ldionne
Copy link
Member

ldionne commented Jul 16, 2024

@premanandrao There are instructions here: https://libcxx.llvm.org/BuildingLibcxx.html#bootstrapping-build

You can also use e.g. run-buildbot for this job:

bootstrapping-build)

You will probably need to modify a few things like maybe remove the mentions of ccache.


// expected-note@+2 5{{'UsingWithAttr' has been explicitly marked deprecated here}}
template <typename T>
using UsingWithAttr __attribute__((deprecated)) = NoAttr<T>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend adding a test that uses the C++11 [[deprecated]] attribute syntax.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @tahonermann! Done.

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this, the changes should also come with a release note in clang/docs/ReleaseNotes.rst so users know about the improvement. Changes generally LG, but I did have some test suggestions.

UsingWithCPPAttr<int> objUsingWCPPA;

// expected-warning@+1 {{'UsingInstWithCPPAttr' is deprecated: Do not use this}}
UsingInstWithCPPAttr objUICPPWA;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some more test cases I'd like to see:

NoAttr<UsingWithAttr<int>> s; // diag

using Foo [[deprecated]] = int;
using X = UsingWithAttr<Foo>; // two diagnostics, one for Foo and one for UsingWithAttr

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @AaronBallman! Added your test cases and added a release note.

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Copy link
Contributor

@mizvekov mizvekov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM as well.

@AaronBallman AaronBallman merged commit d8e0b0d into llvm:main Jul 22, 2024
8 checks passed
hubot pushed a commit to v8/v8 that referenced this pull request Jul 24, 2024
llvm/llvm-project#97619 makes this trigger
-Wdeprecated-declarations, so this is causing Chrome's ToT bots to blow
up.

Bug: chromium:354996246
Change-Id: I9521c0e1e0f66d7456d8a6aa397cd9bb8c7c48ef
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/5734993
Reviewed-by: Nico Hartmann <[email protected]>
Commit-Queue: Nico Hartmann <[email protected]>
Auto-Submit: Alan Zhao <[email protected]>
Reviewed-by: Michael Lippautz <[email protected]>
Cr-Commit-Position: refs/heads/main@{#95205}
yuxuanchen1997 pushed a commit that referenced this pull request Jul 25, 2024
Issue a warning diagnostic when a template alias with a deprecated
attribute is used.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support attribute deprecated for template alias
7 participants