-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindClassDecls.cpp
285 lines (235 loc) · 8.74 KB
/
FindClassDecls.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendAction.h"
#include "clang/Tooling/Tooling.h"
#include "clang/Lex/LiteralSupport.h"
// Command line support
#include "clang/Tooling/CommonOptionsParser.h"
#include "llvm/Support/CommandLine.h"
#include <algorithm>
#include <vector>
using namespace clang;
namespace test {
#include "pError.h"
class FindNamedClassVisitor
: public RecursiveASTVisitor<FindNamedClassVisitor> {
public:
explicit FindNamedClassVisitor(ASTContext *Context) : Context(Context) {}
/**********************************************************************************/
// R12_5: The sizeof operator shall not have an operand which is a function
// parameter declared as “array of type”
bool VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *expr) {
// Only Sizeof operator
if (expr->getKind() != UETT_SizeOf) {
return true;
}
// Except sizeof(type)
// Wipe out type is argument
if (expr->isArgumentType() != false) {
return true;
}
// expr->dumpColor();
//
Expr *arg = expr->getArgumentExpr();
// Copy from clang/lib/Sema/SemaExpr.cpp
if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(arg->IgnoreParens())) {
if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(DeclRef->getFoundDecl())) {
QualType OType = PVD->getOriginalType();
QualType Type = PVD->getType();
if (Type->isPointerType() && OType->isArrayType()) {
pError(Context, DeclRef,
"R12_5: Using sizeof on function parameter!");
}
}
}
return true;
}
/**********************************************************************************/
// Code that can fetch token source code
bool VisitIntegerLiteral(IntegerLiteral *il) {
// il->dumpColor();
using std::string;
const SourceManager &sm = Context->getSourceManager();
const SourceLocation spellingLoc = sm.getSpellingLoc(il->getLocStart());
const string lexem = srcLocToString(spellingLoc);
// const NumericLiteralParser nlp(lexem, spellingLoc,
// CI->getPreprocessor());
// llvm::outs() << lexem << "\n";
/* if ((nlp.isUnsigned && lexem.find("u") != string::npos) ||
(nlp.isFloat && lexem.find("f") != string::npos) ||
((nlp.isLong || nlp.isLongLong) && lexem.find("l") != string::npos)) {
reportError(expr->getLocEnd());
}
*/
return true;
}
/**********************************************************************************/
// R12_3: The comma operator should not be used
bool VisitBinaryOperator(BinaryOperator *bo) {
if (bo->getOpcode() != BO_Comma) {
return true;
}
// bo->dumpColor();
pError(Context, bo, "R12_3: Using comma operator!");
return true;
}
/*********************************************************************************/
// Rule 16_2 A switch label shall only be used when the most closely-enclosing
// compound statement is the body of a switch statement
// TODO
// Rule 16_3 An unconditional break statement shall terminate every
// switch-clause
//
bool VisitSwitchStmt(SwitchStmt *ss) {
CompoundStmt *body = dyn_cast<CompoundStmt>(ss->getBody());
// There is always a compoundStmt even a empty switch
if (body == nullptr) {
return true;
}
std::vector<SwitchCase *> cases;
// Record most closely-enclosing cases i.e body of the switch
//
for (CompoundStmt::body_iterator bi = body->body_begin();
bi != body->body_end(); bi++) {
//(*bi)->dumpColor();
if (isa<SwitchCase>(*bi)) {
SwitchCase *sc = dyn_cast<SwitchCase>(*bi);
cases.push_back(sc);
// If continuous case: case 1: case 2: case 3:
while (isa<SwitchCase>(sc->getSubStmt())) {
sc = dyn_cast<SwitchCase>(sc->getSubStmt());
cases.push_back(sc);
}
}
}
for (const SwitchCase *sc = ss->getSwitchCaseList(); sc != nullptr;
sc = sc->getNextSwitchCase()) {
//
// sc->dumpColor();
if (std::find(cases.begin(), cases.end(), sc) == cases.end()) {
pError(Context, sc,
"R16_2: Switch case is not used in most closely-enclosing of "
"switch body");
}
}
/*******************************************************************************/
// Rule 16_4 Every switch statement shall have a default label
bool hasDefault = false;
for (const SwitchCase *sc = ss->getSwitchCaseList(); sc != nullptr;
sc = sc->getNextSwitchCase()) {
if (isa<DefaultStmt>(sc)) {
hasDefault = true;
}
}
if (!hasDefault) {
pError(Context, ss,
"R16_4 Every switch statement shall have a default label");
}
/*******************************************************************************/
// Rule 16_5 A default label shall appear as either the first or the last
// switch label of a switch statement
//
// We check if any SwitchCase is default except first and end
//
// We just count the label and store in member
// We let treaverse*() do the traverse AST work for us
// See VisitSwitchCase() do the other jobs
{
// Reset class member in every switch
labelTotal = 0;
labelCount = 0;
for (const SwitchCase *sc = ss->getSwitchCaseList(); sc != nullptr;
sc = sc->getNextSwitchCase()) {
labelTotal++;
}
} // end of R16_5
/*******************************************************************************/
// R16_6 Every switch statement shall have at least two switch-clauses
{
int labelCount = 0;
for (const SwitchCase *sc = ss->getSwitchCaseList(); sc != nullptr;
sc = sc->getNextSwitchCase()) {
labelCount++;
}
if (labelCount < 2) {
pError(Context, ss,
"R16_6 Every switch statement shall have at least two "
"switch-clauses");
}
}
/*******************************************************************************/
// R16_7 A switch-expression shall not have essentially Boolean type
// Copy from clang/lib/Sema/SemaStmt.cpp
{
Expr *cond = ss->getCond();
// cond->dumpColor();
QualType CondType = cond->getType();
// No need to check isTypeDependent() -> C has no template
if (cond->isKnownToHaveBooleanValue()) {
pError(Context, ss,
"R16_7 A switch-expression shall not have essentially Boolean "
"type");
}
}
return true;
} // end visitSwitch
bool VisitSwitchCase(SwitchCase *sc) {
labelCount++;
// Defautlt statment can only appear in first or last
if (labelCount != 1 && labelCount != labelTotal) {
if (isa<DefaultStmt>(sc)) {
pError(Context, sc,
"R16_5: A default label shall appear as either the first "
"or the last switch label");
}
}
return true;
}
private:
ASTContext *Context;
int labelCount, labelTotal;
std::string srcLocToString(const SourceLocation start) {
const clang::SourceManager &sm = Context->getSourceManager();
const clang::LangOptions lopt = Context->getLangOpts();
const SourceLocation spellingLoc = sm.getSpellingLoc(start);
unsigned tokenLength =
clang::Lexer::MeasureTokenLength(spellingLoc, sm, lopt);
return std::string(sm.getCharacterData(spellingLoc),
sm.getCharacterData(spellingLoc) + tokenLength);
}
};
class FindNamedClassConsumer : public clang::ASTConsumer {
public:
explicit FindNamedClassConsumer(ASTContext *Context) : Visitor(Context) {}
virtual void HandleTranslationUnit(clang::ASTContext &Context) {
Visitor.TraverseDecl(Context.getTranslationUnitDecl());
}
private:
FindNamedClassVisitor Visitor;
};
class FindNamedClassAction : public clang::ASTFrontendAction {
public:
virtual std::unique_ptr<clang::ASTConsumer>
CreateASTConsumer(clang::CompilerInstance &Compiler, llvm::StringRef InFile) {
return std::unique_ptr<clang::ASTConsumer>(
new FindNamedClassConsumer(&Compiler.getASTContext()));
}
};
} // end of namespace test
// Handling input
static llvm::cl::OptionCategory MyToolCategory("my-tool options");
static llvm::cl::extrahelp
CommonHelp(tooling::CommonOptionsParser::HelpMessage);
int main(int argc, const char **argv) {
tooling::CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
tooling::ClangTool Tool(OptionsParser.getCompilations(),
OptionsParser.getSourcePathList());
return Tool.run(
tooling::newFrontendActionFactory<test::FindNamedClassAction>().get());
/* if (argc > 1) {
clang::tooling::runToolOnCode(new test::FindNamedClassAction, argv[1]);
}
*/
return 0;
}