1//===- Attribute.cpp ------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Example clang plugin which adds an an annotation to file-scope declarations
10// with the 'example' attribute.
11//
12// This plugin is used by clang/test/Frontend/plugin-attribute tests.
13//
14//===----------------------------------------------------------------------===//
15
16#include "clang/AST/ASTContext.h"
17#include "clang/AST/Attr.h"
18#include "clang/Sema/ParsedAttr.h"
19#include "clang/Sema/Sema.h"
20#include "clang/Sema/SemaDiagnostic.h"
21#include "llvm/IR/Attributes.h"
22using namespace clang;
23
24namespace {
25
26struct ExampleAttrInfo : public ParsedAttrInfo {
27 ExampleAttrInfo() {
28 // Can take up to 15 optional arguments, to emulate accepting a variadic
29 // number of arguments. This just illustrates how many arguments a
30 // `ParsedAttrInfo` can hold, we will not use that much in this example.
31 OptArgs = 15;
32 // GNU-style __attribute__(("example")) and C++/C23-style [[example]] and
33 // [[plugin::example]] supported.
34 static constexpr Spelling S[] = {{.Syntax: ParsedAttr::AS_GNU, .NormalizedFullName: "example"},
35 {.Syntax: ParsedAttr::AS_C23, .NormalizedFullName: "example"},
36 {.Syntax: ParsedAttr::AS_CXX11, .NormalizedFullName: "example"},
37 {.Syntax: ParsedAttr::AS_CXX11, .NormalizedFullName: "plugin::example"}};
38 Spellings = S;
39 }
40
41 bool diagAppertainsToDecl(Sema &S, const ParsedAttr &Attr,
42 const Decl *D) const override {
43 // This attribute appertains to functions only.
44 if (!isa<FunctionDecl>(Val: D)) {
45 S.Diag(Loc: Attr.getLoc(), DiagID: diag::warn_attribute_wrong_decl_type_str)
46 << Attr << Attr.isRegularKeywordAttribute() << "functions";
47 return false;
48 }
49 return true;
50 }
51
52 AttrHandling handleDeclAttribute(Sema &S, Decl *D,
53 const ParsedAttr &Attr) const override {
54 // Check if the decl is at file scope.
55 if (!D->getDeclContext()->isFileContext()) {
56 unsigned ID = S.getDiagnostics().getCustomDiagID(
57 L: DiagnosticsEngine::Error,
58 FormatString: "'example' attribute only allowed at file scope");
59 S.Diag(Loc: Attr.getLoc(), DiagID: ID);
60 return AttributeNotApplied;
61 }
62 // We make some rules here:
63 // 1. Only accept at most 3 arguments here.
64 // 2. The first argument must be a string literal if it exists.
65 if (Attr.getNumArgs() > 3) {
66 unsigned ID = S.getDiagnostics().getCustomDiagID(
67 L: DiagnosticsEngine::Error,
68 FormatString: "'example' attribute only accepts at most three arguments");
69 S.Diag(Loc: Attr.getLoc(), DiagID: ID);
70 return AttributeNotApplied;
71 }
72 // If there are arguments, the first argument should be a string literal.
73 if (Attr.getNumArgs() > 0) {
74 auto *Arg0 = Attr.getArgAsExpr(Arg: 0);
75 StringLiteral *Literal =
76 dyn_cast<StringLiteral>(Val: Arg0->IgnoreParenCasts());
77 if (!Literal) {
78 unsigned ID = S.getDiagnostics().getCustomDiagID(
79 L: DiagnosticsEngine::Error, FormatString: "first argument to the 'example' "
80 "attribute must be a string literal");
81 S.Diag(Loc: Attr.getLoc(), DiagID: ID);
82 return AttributeNotApplied;
83 }
84 SmallVector<Expr *, 16> ArgsBuf;
85 for (unsigned i = 0; i < Attr.getNumArgs(); i++) {
86 ArgsBuf.push_back(Elt: Attr.getArgAsExpr(Arg: i));
87 }
88 D->addAttr(A: AnnotateAttr::Create(Ctx&: S.Context, Annotation: "example", Args: ArgsBuf.data(),
89 ArgsSize: ArgsBuf.size(), Range: Attr.getRange()));
90 } else {
91 // Attach an annotate attribute to the Decl.
92 D->addAttr(A: AnnotateAttr::Create(Ctx&: S.Context, Annotation: "example", Args: nullptr, ArgsSize: 0,
93 Range: Attr.getRange()));
94 }
95 return AttributeApplied;
96 }
97};
98
99} // namespace
100
101static ParsedAttrInfoRegistry::Add<ExampleAttrInfo> X("example", "");
102