Skip to content

Commit 5a5ff13

Browse files
committed
Add support for template-instantiations of AnnotateAttr
1 parent 2398a73 commit 5a5ff13

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

clang/lib/Sema/SemaTemplateInstantiate.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,6 +1537,7 @@ namespace {
15371537
NamedDecl *FirstQualifierInScope = nullptr,
15381538
bool AllowInjectedClassName = false);
15391539

1540+
const AnnotateAttr *TransformAnnotateAttr(const AnnotateAttr *AA);
15401541
const CXXAssumeAttr *TransformCXXAssumeAttr(const CXXAssumeAttr *AA);
15411542
const LoopHintAttr *TransformLoopHintAttr(const LoopHintAttr *LH);
15421543
const NoInlineAttr *TransformStmtNoInlineAttr(const Stmt *OrigS,
@@ -2125,6 +2126,19 @@ TemplateInstantiator::TransformTemplateParmRefExpr(DeclRefExpr *E,
21252126
Arg, PackIndex);
21262127
}
21272128

2129+
const AnnotateAttr *
2130+
TemplateInstantiator::TransformAnnotateAttr(const AnnotateAttr *AA) {
2131+
SmallVector<Expr *> Args;
2132+
for (Expr *Arg : AA->args()) {
2133+
ExprResult Res = getDerived().TransformExpr(Arg);
2134+
if (!Res.isUsable())
2135+
return AA;
2136+
Args.push_back(Res.get());
2137+
}
2138+
return AnnotateAttr::CreateImplicit(getSema().Context, AA->getAnnotation(),
2139+
Args.data(), Args.size(), AA->getRange());
2140+
}
2141+
21282142
const CXXAssumeAttr *
21292143
TemplateInstantiator::TransformCXXAssumeAttr(const CXXAssumeAttr *AA) {
21302144
ExprResult Res = getDerived().TransformExpr(AA->getAssumption());

clang/test/Frontend/plugin-attribute.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@
55
//--- good_attr.cpp
66
// expected-no-diagnostics
77
void fn1a() __attribute__((example)) {
8-
__attribute__((example)) for (int i = 0; i < 10; ++i) {}
8+
__attribute__((example)) for (int i = 0; i < 9; ++i) {}
99
}
1010
[[example]] void fn1b() {
11-
[[example]] for (int i = 0; i < 10; ++i) {}
11+
[[example]] for (int i = 0; i < 9; ++i) {}
1212
}
1313
[[plugin::example]] void fn1c() {
14-
[[plugin::example]] for (int i = 0; i < 10; ++i) {}
14+
[[plugin::example]] for (int i = 0; i < 9; ++i) {}
1515
}
1616
void fn2() __attribute__((example("somestring", 1, 2.0))) {
17-
__attribute__((example("abc", 3, 4.0))) for (int i = 0; i < 10; ++i) {}
17+
__attribute__((example("abc", 3, 4.0))) for (int i = 0; i < 9; ++i) {}
1818
}
19+
template <int N> void template_fn() __attribute__((example("template", N))) {
20+
__attribute__((example("def", N + 1))) for (int i = 0; i < 9; ++i) {}
21+
}
22+
void fn3() { template_fn<5>(); }
1923
// CHECK: -AttributedStmt 0x{{[0-9a-z]+}} {{<line:[0-9]+:[0-9]+(, col:[0-9]+)?>}}
2024
// CHECK: -AnnotateAttr 0x{{[0-9a-z]+}} {{<col:[0-9]+(, col:[0-9]+)?>}} "example"
2125
// CHECK: -AnnotateAttr 0x{{[0-9a-z]+}} {{<line:[0-9]+:[0-9]+(, col:[0-9]+)?>}} "example"
@@ -34,6 +38,14 @@ void fn2() __attribute__((example("somestring", 1, 2.0))) {
3438
// CHECK: -StringLiteral 0x{{[0-9a-z]+}} {{<col:[0-9]+(, col:[0-9]+)?>}} 'const char[{{[0-9]+}}]' lvalue "somestring"
3539
// CHECK: -IntegerLiteral 0x{{[0-9a-z]+}} {{<col:[0-9]+(, col:[0-9]+)?>}} 'int' 1
3640
// CHECK: -FloatingLiteral 0x{{[0-9a-z]+}} {{<col:[0-9]+(, col:[0-9]+)?>}} 'double' 2.000000e+00
41+
// CHECK: -AnnotateAttr 0x{{[0-9a-z]+}} {{<col:[0-9]+(, col:[0-9]+)?>}} Implicit "example"
42+
// CHECK: -StringLiteral 0x{{[0-9a-z]+}} {{<col:[0-9]+(, col:[0-9]+)?>}} 'const char[{{[0-9]+}}]' lvalue "def"
43+
// CHECK: -BinaryOperator 0x{{[0-9a-z]+}} {{<col:[0-9]+(, col:[0-9]+)?>}} 'int' '+'
44+
// CHECK: -IntegerLiteral 0x{{[0-9a-z]+}} {{<line:[0-9]+:[0-9]+(, col:[0-9]+)?>}} 'int' 5
45+
// CHECK: -IntegerLiteral 0x{{[0-9a-z]+}} {{<col:[0-9]+(, col:[0-9]+)?>}} 'int' 1
46+
// CHECK: -AnnotateAttr 0x{{[0-9a-z]+}} {{<line:[0-9]+:[0-9]+(, col:[0-9]+)?>}} "example"
47+
// CHECK: -StringLiteral 0x{{[0-9a-z]+}} {{<col:[0-9]+(, col:[0-9]+)?>}} 'const char[{{[0-9]+}}]' lvalue "template"
48+
// CHECK: -IntegerLiteral 0x{{[0-9a-z]+}} {{<col:[0-9]+(, col:[0-9]+)?>}} 'int' 5
3749

3850
//--- bad_attr.cpp
3951
int var1 __attribute__((example("otherstring"))) = 1; // expected-warning {{'example' attribute only applies to functions}}

0 commit comments

Comments
 (0)