1 //===- Attribute.cpp ------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // Example clang plugin which adds an an annotation to file-scope declarations
10 // with the 'example' attribute.
12 //===----------------------------------------------------------------------===//
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/Attr.h"
16 #include "clang/Sema/ParsedAttr.h"
17 #include "clang/Sema/Sema.h"
18 #include "clang/Sema/SemaDiagnostic.h"
19 #include "llvm/IR/Attributes.h"
20 using namespace clang
;
24 struct ExampleAttrInfo
: public ParsedAttrInfo
{
26 // Can take up to 15 optional arguments, to emulate accepting a variadic
27 // number of arguments. This just illustrates how many arguments a
28 // `ParsedAttrInfo` can hold, we will not use that much in this example.
30 // GNU-style __attribute__(("example")) and C++-style [[example]] and
31 // [[plugin::example]] supported.
32 static constexpr Spelling S
[] = {{ParsedAttr::AS_GNU
, "example"},
33 {ParsedAttr::AS_CXX11
, "example"},
34 {ParsedAttr::AS_CXX11
, "plugin::example"}};
38 bool diagAppertainsToDecl(Sema
&S
, const ParsedAttr
&Attr
,
39 const Decl
*D
) const override
{
40 // This attribute appertains to functions only.
41 if (!isa
<FunctionDecl
>(D
)) {
42 S
.Diag(Attr
.getLoc(), diag::warn_attribute_wrong_decl_type_str
)
43 << Attr
<< "functions";
49 AttrHandling
handleDeclAttribute(Sema
&S
, Decl
*D
,
50 const ParsedAttr
&Attr
) const override
{
51 // Check if the decl is at file scope.
52 if (!D
->getDeclContext()->isFileContext()) {
53 unsigned ID
= S
.getDiagnostics().getCustomDiagID(
54 DiagnosticsEngine::Error
,
55 "'example' attribute only allowed at file scope");
56 S
.Diag(Attr
.getLoc(), ID
);
57 return AttributeNotApplied
;
59 // We make some rules here:
60 // 1. Only accept at most 3 arguments here.
61 // 2. The first argument must be a string literal if it exists.
62 if (Attr
.getNumArgs() > 3) {
63 unsigned ID
= S
.getDiagnostics().getCustomDiagID(
64 DiagnosticsEngine::Error
,
65 "'example' attribute only accepts at most three arguments");
66 S
.Diag(Attr
.getLoc(), ID
);
67 return AttributeNotApplied
;
69 // If there are arguments, the first argument should be a string literal.
70 if (Attr
.getNumArgs() > 0) {
71 auto *Arg0
= Attr
.getArgAsExpr(0);
72 StringLiteral
*Literal
=
73 dyn_cast
<StringLiteral
>(Arg0
->IgnoreParenCasts());
75 unsigned ID
= S
.getDiagnostics().getCustomDiagID(
76 DiagnosticsEngine::Error
, "first argument to the 'example' "
77 "attribute must be a string literal");
78 S
.Diag(Attr
.getLoc(), ID
);
79 return AttributeNotApplied
;
81 SmallVector
<Expr
*, 16> ArgsBuf
;
82 for (unsigned i
= 0; i
< Attr
.getNumArgs(); i
++) {
83 ArgsBuf
.push_back(Attr
.getArgAsExpr(i
));
85 D
->addAttr(AnnotateAttr::Create(S
.Context
, "example", ArgsBuf
.data(),
86 ArgsBuf
.size(), Attr
.getRange()));
88 // Attach an annotate attribute to the Decl.
89 D
->addAttr(AnnotateAttr::Create(S
.Context
, "example", nullptr, 0,
92 return AttributeApplied
;
98 static ParsedAttrInfoRegistry::Add
<ExampleAttrInfo
> X("example", "");