Skip to content

Commit d8e0b0d

Browse files
authored
[clang] Diagnose use of deprecated template alias (#97619)
Issue a warning diagnostic when a template alias with a deprecated attribute is used.
1 parent 83c2bfd commit d8e0b0d

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,19 @@ Improvements to Clang's diagnostics
744744

745745
- Clang now diagnoses dangling assignments for pointer-like objects (annotated with `[[gsl::Pointer]]`) under `-Wdangling-assignment-gsl` (off by default)
746746
Fixes #GH63310.
747+
748+
- Clang now diagnoses uses of alias templates with a deprecated attribute. (Fixes #GH18236).
749+
750+
.. code-block:: c++
751+
752+
template <typename T>
753+
struct NoAttr {
754+
};
755+
756+
template <typename T>
757+
using UsingWithAttr __attribute__((deprecated)) = NoAttr<T>;
758+
759+
UsingWithAttr<int> objUsingWA; // warning: 'UsingWithAttr' is deprecated
747760

748761
Improvements to Clang's time-trace
749762
----------------------------------

clang/lib/Sema/SemaAvailability.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ ShouldDiagnoseAvailabilityOfDecl(Sema &S, const NamedDecl *D,
107107
break;
108108
}
109109

110+
// For alias templates, get the underlying declaration.
111+
if (const auto *ADecl = dyn_cast<TypeAliasTemplateDecl>(D)) {
112+
D = ADecl->getTemplatedDecl();
113+
Result = D->getAvailability(Message);
114+
}
115+
110116
// Forward class declarations get their attributes from their definition.
111117
if (const auto *IDecl = dyn_cast<ObjCInterfaceDecl>(D)) {
112118
if (IDecl->getDefinition()) {

clang/lib/Sema/SemaTemplate.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3344,6 +3344,10 @@ QualType Sema::CheckTemplateIdType(TemplateName Name,
33443344
*this, /*PointOfInstantiation=*/TemplateLoc,
33453345
/*Entity=*/AliasTemplate,
33463346
/*TemplateArgs=*/TemplateArgLists.getInnermost());
3347+
3348+
// Diagnose uses of this alias.
3349+
(void)DiagnoseUseOfDecl(AliasTemplate, TemplateLoc);
3350+
33473351
if (Inst.isInvalid())
33483352
return QualType();
33493353

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
2+
//
3+
// This test checks that a deprecated attribute on an alias
4+
// template triggers a warning diagnostic when it is used.
5+
6+
template <typename T>
7+
struct NoAttr {
8+
void foo() {}
9+
};
10+
11+
// expected-note@+2 7{{'UsingWithAttr' has been explicitly marked deprecated here}}
12+
template <typename T>
13+
using UsingWithAttr __attribute__((deprecated)) = NoAttr<T>;
14+
15+
// expected-note@+1 {{'UsingInstWithAttr' has been explicitly marked deprecated here}}
16+
using UsingInstWithAttr __attribute__((deprecated)) = NoAttr<int>;
17+
18+
// expected-note@+1 {{'TDWithAttr' has been explicitly marked deprecated here}}
19+
typedef NoAttr<int> TDWithAttr __attribute__((deprecated));
20+
21+
// expected-warning@+1 {{'UsingWithAttr' is deprecated}}
22+
typedef UsingWithAttr<int> TDUsingWithAttr;
23+
24+
typedef NoAttr<int> TDNoAttr;
25+
26+
// expected-note@+1 {{'UsingTDWithAttr' has been explicitly marked deprecated here}}
27+
using UsingTDWithAttr __attribute__((deprecated)) = TDNoAttr;
28+
29+
struct S {
30+
NoAttr<float> f1;
31+
// expected-warning@+1 {{'UsingWithAttr' is deprecated}}
32+
UsingWithAttr<float> f2;
33+
};
34+
35+
// expected-warning@+1 {{'UsingWithAttr' is deprecated}}
36+
void foo(NoAttr<short> s1, UsingWithAttr<short> s2) {
37+
}
38+
39+
// expected-note@+2 {{'UsingWithCPPAttr' has been explicitly marked deprecated here}}
40+
template <typename T>
41+
using UsingWithCPPAttr [[deprecated]] = NoAttr<T>;
42+
43+
// expected-note@+1 {{'UsingInstWithCPPAttr' has been explicitly marked deprecated here}}
44+
using UsingInstWithCPPAttr [[deprecated("Do not use this")]] = NoAttr<int>;
45+
46+
void bar() {
47+
NoAttr<int> obj; // Okay
48+
49+
// expected-warning@+2 {{'UsingWithAttr' is deprecated}}
50+
// expected-note@+1 {{in instantiation of template type alias 'UsingWithAttr' requested here}}
51+
UsingWithAttr<int> objUsingWA;
52+
53+
// expected-warning@+2 {{'UsingWithAttr' is deprecated}}
54+
// expected-note@+1 {{in instantiation of template type alias 'UsingWithAttr' requested here}}
55+
NoAttr<UsingWithAttr<int>> s;
56+
57+
// expected-note@+1 {{'DepInt' has been explicitly marked deprecated here}}
58+
using DepInt [[deprecated]] = int;
59+
// expected-warning@+3 {{'UsingWithAttr' is deprecated}}
60+
// expected-warning@+2 {{'DepInt' is deprecated}}
61+
// expected-note@+1 {{in instantiation of template type alias 'UsingWithAttr' requested here}}
62+
using X = UsingWithAttr<DepInt>;
63+
64+
// expected-warning@+2 {{'UsingWithAttr' is deprecated}}
65+
// expected-note@+1 {{in instantiation of template type alias 'UsingWithAttr' requested here}}
66+
UsingWithAttr<int>().foo();
67+
68+
// expected-warning@+1 {{'UsingInstWithAttr' is deprecated}}
69+
UsingInstWithAttr objUIWA;
70+
71+
// expected-warning@+1 {{'TDWithAttr' is deprecated}}
72+
TDWithAttr objTDWA;
73+
74+
// expected-warning@+1 {{'UsingTDWithAttr' is deprecated}}
75+
UsingTDWithAttr objUTDWA;
76+
77+
// expected-warning@+2 {{'UsingWithCPPAttr' is deprecated}}
78+
// expected-note@+1 {{in instantiation of template type alias 'UsingWithCPPAttr' requested here}}
79+
UsingWithCPPAttr<int> objUsingWCPPA;
80+
81+
// expected-warning@+1 {{'UsingInstWithCPPAttr' is deprecated: Do not use this}}
82+
UsingInstWithCPPAttr objUICPPWA;
83+
}

0 commit comments

Comments
 (0)