Skip to content

Commit eb4b3a2

Browse files
committed
Include attributes on LabelStmts (forwarding from decls) in AST dumps
We apply this to add a test of the new label attribute instantiation support.
1 parent a6b8309 commit eb4b3a2

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

clang/examples/Attribute/Attribute.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ struct ExampleAttrInfo : public ParsedAttrInfo {
4141
bool diagAppertainsToDecl(Sema &S, const ParsedAttr &Attr,
4242
const Decl *D) const override {
4343
// This attribute appertains to functions only.
44-
if (!isa<FunctionDecl>(D)) {
44+
if (!isa<FunctionDecl>(D) && !isa<LabelDecl>(D)) {
4545
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type_str)
46-
<< Attr << Attr.isRegularKeywordAttribute() << "functions";
46+
<< Attr << Attr.isRegularKeywordAttribute() << "functions or labels";
4747
return false;
4848
}
4949
return true;
@@ -52,7 +52,7 @@ struct ExampleAttrInfo : public ParsedAttrInfo {
5252
AttrHandling handleDeclAttribute(Sema &S, Decl *D,
5353
const ParsedAttr &Attr) const override {
5454
// Check if the decl is at file scope.
55-
if (!D->getDeclContext()->isFileContext()) {
55+
if (!D->getDeclContext()->isFileContext() && !isa<LabelDecl>(D)) {
5656
unsigned ID = S.getDiagnostics().getCustomDiagID(
5757
DiagnosticsEngine::Error,
5858
"'example' attribute only allowed at file scope");

clang/include/clang/AST/ASTNodeTraverser.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,11 @@ class ASTNodeTraverser
800800
Visit(A);
801801
}
802802

803+
void VisitLabelStmt(const LabelStmt *Node) {
804+
for (const auto *A : Node->getDecl()->getAttrs())
805+
Visit(A);
806+
}
807+
803808
void VisitCXXCatchStmt(const CXXCatchStmt *Node) {
804809
Visit(Node->getExceptionDecl());
805810
}

clang/test/Frontend/plugin-attribute.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ void fn2() __attribute__((example("somestring", 1, 2.0))) {
1818
}
1919
template <int N> void template_fn() __attribute__((example("template", N))) {
2020
__attribute__((example("def", N + 1))) for (int i = 0; i < 9; ++i) {}
21+
__attribute__((example("ghi", N + 2)))
22+
label1:
23+
for (int i = 0; i < 9; ++i) {}
2124
}
2225
void fn3() { template_fn<5>(); }
2326
// CHECK: -AttributedStmt 0x{{[0-9a-z]+}} {{<line:[0-9]+:[0-9]+(, col:[0-9]+)?>}}
@@ -44,6 +47,11 @@ void fn3() { template_fn<5>(); }
4447
// CHECK: -IntegerLiteral 0x{{[0-9a-z]+}} {{<line:[0-9]+:[0-9]+(, col:[0-9]+)?>}} 'int' 5
4548
// CHECK: -IntegerLiteral 0x{{[0-9a-z]+}} {{<col:[0-9]+(, col:[0-9]+)?>}} 'int' 1
4649
// CHECK: -AnnotateAttr 0x{{[0-9a-z]+}} {{<line:[0-9]+:[0-9]+(, col:[0-9]+)?>}} "example"
50+
// CHECK: -StringLiteral 0x{{[0-9a-z]+}} {{<col:[0-9]+(, col:[0-9]+)?>}} 'const char[{{[0-9]+}}]' lvalue "ghi"
51+
// CHECK: -BinaryOperator 0x{{[0-9a-z]+}} {{<col:[0-9]+(, col:[0-9]+)?>}} 'int' '+'
52+
// CHECK: -IntegerLiteral 0x{{[0-9a-z]+}} {{<line:[0-9]+:[0-9]+(, col:[0-9]+)?>}} 'int' 5
53+
// CHECK: -IntegerLiteral 0x{{[0-9a-z]+}} {{<col:[0-9]+(, col:[0-9]+)?>}} 'int' 2
54+
// CHECK: -AnnotateAttr 0x{{[0-9a-z]+}} {{<line:[0-9]+:[0-9]+(, col:[0-9]+)?>}} "example"
4755
// CHECK: -StringLiteral 0x{{[0-9a-z]+}} {{<col:[0-9]+(, col:[0-9]+)?>}} 'const char[{{[0-9]+}}]' lvalue "template"
4856
// CHECK: -IntegerLiteral 0x{{[0-9a-z]+}} {{<col:[0-9]+(, col:[0-9]+)?>}} 'int' 5
4957

0 commit comments

Comments
 (0)