-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathattributes.cpp
158 lines (132 loc) · 5.45 KB
/
attributes.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//===- Attribute.cpp ------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Example clang plugin which adds an an annotation to file-scope declarations
// with the 'example' attribute.
//
//===----------------------------------------------------------------------===//
#include "clang/AST/ASTContext.h"
#include "clang/AST/Attr.h"
#include "clang/Sema/ParsedAttr.h"
#include "clang/Sema/Sema.h"
#include "clang/Sema/SemaDiagnostic.h"
#include "llvm/IR/Attributes.h"
#include "clang/Frontend/CompilerInstance.h"
using namespace clang;
namespace kslicer
{
std::string GetRangeSourceCode(const clang::SourceRange a_range, const clang::CompilerInstance& compiler);
}
//clang::CompilerInstance* g_pCompilerInstance = nullptr; // check that we set this in kslicer_main.cpp
namespace {
struct SetterAttrInfo : public ParsedAttrInfo {
SetterAttrInfo() {
OptArgs = 1;
static constexpr Spelling S[] = {{ParsedAttr::AS_CXX11, "kslicer::setter"}}; // {ParsedAttr::AS_CXX11, "setter"},
Spellings = S;
}
bool diagAppertainsToDecl(Sema &S, const ParsedAttr &Attr, const Decl *D) const override
{
// This attribute appertains to functions only.
if (!isa<FunctionDecl>(D)) {
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type_str) << Attr << "functions";
return false;
}
return true;
}
AttrHandling handleDeclAttribute(Sema &S, Decl *D, const ParsedAttr &Attr) const override
{
// Attach an annotate attribute to the Decl.
D->addAttr(AnnotateAttr::Create(S.Context, "setter", nullptr, 0, Attr.getRange()));
return AttributeApplied;
}
};
// size
struct SizeAttrInfo : public ParsedAttrInfo {
SizeAttrInfo() {
// Can take up to 3 optional arguments
OptArgs = 3;
static constexpr Spelling S[] = {{ParsedAttr::AS_GNU, "size"}, // __attribute__((size("a_dataSize")))
{ParsedAttr::AS_CXX11, "size"}, // [[size("a_dataSize")]]
{ParsedAttr::AS_CXX11, "kslicer::size"}}; // [[kslicer::size("a_dataSize")]]
Spellings = S;
}
bool diagAppertainsToDecl(Sema &S, const ParsedAttr &Attr,
const Decl *D) const override {
// This attribute appertains to function parameter only
if (!isa<ParmVarDecl>(D)) {
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type_str) << Attr << "function parameters";
return false;
}
return true;
}
AttrHandling handleDeclAttribute(Sema &S, Decl *D, const ParsedAttr &Attr) const override
{
// Check if the decl is at file scope.
if (!D->getDeclContext()->isFileContext()) {
unsigned ID = S.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error, "'kslicer::size' attribute only allowed at file scope");
S.Diag(Attr.getLoc(), ID);
return AttributeNotApplied;
}
auto argInfo = Attr.getInfo();
auto argNum = Attr.getNumArgs();
//auto argText = kslicer::GetRangeSourceCode(Attr.getRange(), *g_pCompilerInstance);
// If there are arguments, the first argument should be a string literal.
if (argNum > 0)
{
//auto *Arg0 = Attr.getArgAsExpr(0);
//StringLiteral *Literal = dyn_cast<StringLiteral>(Arg0->IgnoreParenCasts());
//if (!Literal) {
// unsigned ID = S.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error, "first argument to the 'size' " "attribute must be a string literal");
// S.Diag(Attr.getLoc(), ID);
// return AttributeNotApplied;
//}
//std::string data = Literal->getString().str();
SmallVector<Expr *, 16> ArgsBuf;
for (unsigned i = 0; i < Attr.getNumArgs(); i++) {
ArgsBuf.push_back(Attr.getArgAsExpr(i));
D->addAttr(AnnotateAttr::Create(S.Context, "size", ArgsBuf.data(), ArgsBuf.size(), Attr.getRange()));
}
}
return AttributeApplied;
}
};
struct ThreadLocalInfo : public ParsedAttrInfo {
ThreadLocalInfo() {
OptArgs = 1;
static constexpr Spelling S[] = {{ParsedAttr::AS_CXX11, "threadlocal"}};
Spellings = S;
}
bool diagAppertainsToDecl(Sema &S, const ParsedAttr &Attr, const Decl *D) const override
{
// This attribute appertains to functions only.
if (!isa<VarDecl>(D)) \
{
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type_str) << Attr << " is for variables-arrays only";
return false;
}
const clang::VarDecl *varDecl = llvm::dyn_cast<clang::VarDecl>(D);
clang::QualType varType = varDecl->getType();
if (!varType->isArrayType())
{
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type_str) << Attr << " is for arrays only";
return false;
}
return true;
}
AttrHandling handleDeclAttribute(Sema &S, Decl *D, const ParsedAttr &Attr) const override
{
// Attach an annotate attribute to the Decl.
D->addAttr(AnnotateAttr::Create(S.Context, "threadlocal", nullptr, 0, Attr.getRange()));
return AttributeApplied;
}
};
} // namespace
static ParsedAttrInfoRegistry::Add<SetterAttrInfo> G_SETTER_ATTR("setter", "");
static ParsedAttrInfoRegistry::Add<SizeAttrInfo> G_SIZE_ATTR ("size", "");
static ParsedAttrInfoRegistry::Add<ThreadLocalInfo> G_THRL_ATTR ("threadlocal", "");