-
Notifications
You must be signed in to change notification settings - Fork 4
/
NacroVerifier.cpp
189 lines (166 loc) · 5.69 KB
/
NacroVerifier.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
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/Expr.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendAction.h"
#include "clang/Frontend/FrontendPluginRegistry.h"
#include "llvm/ADT/IntervalMap.h"
#include "llvm/Support/raw_ostream.h"
#include "NacroVerifier.h"
#include <vector>
using namespace clang;
namespace llvm {
template<>
struct IntervalMapHalfOpenInfo<FullSourceLoc> {
static inline bool isUnfairComparison(const FullSourceLoc& LHS,
const FullSourceLoc& RHS) {
return (!LHS.hasManager() || !RHS.hasManager()) ||
(&LHS.getManager() != &RHS.getManager());
}
/// startLess - Return true if x is not in [a;b).
static inline
bool startLess(const FullSourceLoc &x, const FullSourceLoc &a) {
if(isUnfairComparison(x, a)) {
return x.getRawEncoding() < a.getRawEncoding();
} else {
auto& SM = x.getManager();
return SM.isBeforeInTranslationUnit(cast<const SourceLocation>(x),
cast<const SourceLocation>(a));
}
}
/// stopLess - Return true if x is not in [a;b).
static inline
bool stopLess(const FullSourceLoc &b, const FullSourceLoc &x) {
if(isUnfairComparison(b, x)) {
return b.getRawEncoding() <= x.getRawEncoding();
} else {
auto& SM = x.getManager();
return SM.isBeforeInTranslationUnit(cast<const SourceLocation>(b),
cast<const SourceLocation>(x)) ||
b == x;
}
}
/// adjacent - Return true when the intervals [x;a) and [b;y) can coalesce.
static inline
bool adjacent(const FullSourceLoc &a, const FullSourceLoc &b) {
return a == b;
}
/// nonEmpty - Return true if [a;b) is non-empty.
static inline
bool nonEmpty(const FullSourceLoc &a, const FullSourceLoc &b) {
if(isUnfairComparison(a, b)) {
return a.getRawEncoding() < b.getRawEncoding();
} else {
auto& SM = a.getManager();
return SM.isBeforeInTranslationUnit(cast<const SourceLocation>(a),
cast<const SourceLocation>(b));
}
}
};
} // end namespace clang
namespace {
struct NacroRuleDepot {
using IntervalTy
= llvm::IntervalMap<FullSourceLoc, NacroRule*,
llvm::IntervalMapImpl::NodeSizer<FullSourceLoc, NacroRule*>::LeafSize,
llvm::IntervalMapHalfOpenInfo<FullSourceLoc>>;
typename IntervalTy::Allocator Allocator;
IntervalTy Intervals;
NacroRuleDepot()
: Allocator(),
Intervals(Allocator) {}
inline
operator bool() const {
return !Intervals.empty();
}
};
} // end anonymous namespace
static NacroRuleDepot NacroRules;
void NacroVerifier::AddNacroRule(NacroRule* Rule) {
auto SR = Rule->getSourceRange();
auto B = SR.getBegin(), E = SR.getEnd();
NacroRules.Intervals.insert(FullSourceLoc(B, SM), FullSourceLoc(E, SM),
Rule);
}
namespace {
/// Try to warn the following use case
/// ```
/// #define hello(arg) { \
/// int x = 0; \
/// return arg + x - 87; \
/// }
/// int foo(int x) {
/// hello(x)
/// }
/// ```
struct NacroDeclRefChecker
: public RecursiveASTVisitor<NacroDeclRefChecker> {
explicit NacroDeclRefChecker(ASTContext& Context)
: Ctx(Context),
SM(Ctx.getSourceManager()),
Diag(Ctx.getDiagnostics()) {
ErrDiagID = Diag.getCustomDiagID(DiagnosticsEngine::Error,
"a potential declaration leak detected");
RefNoteDiagID = Diag.getCustomDiagID(DiagnosticsEngine::Note,
"the reference to '%0' "
"that comes from outside a nacro");
DeclNoteDiagID = Diag.getCustomDiagID(DiagnosticsEngine::Note,
"is bind to declaration "
"within a nacro");
}
bool VisitDeclRefExpr(DeclRefExpr* DRE) {
if(!NacroRules) return true;
auto DRELoc
= FullSourceLoc(SM.getSpellingLoc(DRE->getLocation()), SM);
auto* D = DRE->getDecl();
auto DLoc
= FullSourceLoc(SM.getSpellingLoc(D->getLocation()), SM);
// Throw an error if one of DRELoc or DLoc is in
// a nacro but the other is not
auto* RefR = NacroRules.Intervals.lookup(DRELoc);
auto* DeclR = NacroRules.Intervals.lookup(DLoc);
if(RefR != DeclR &&
DeclR != nullptr) {
Diag.Report(DRE->getLocation(), ErrDiagID);
Diag.Report(DRE->getLocation(), RefNoteDiagID)
<< DRE->getNameInfo().getName().getAsString();
Diag.Report(DLoc, DeclNoteDiagID);
}
return true;
}
private:
ASTContext& Ctx;
SourceManager& SM;
DiagnosticsEngine& Diag;
unsigned ErrDiagID;
unsigned RefNoteDiagID,
DeclNoteDiagID;
};
struct NacroVerifierImpl : public ASTConsumer {
explicit NacroVerifierImpl(ASTContext& Ctx)
: DeclRefChecker(Ctx) {}
void HandleTranslationUnit(ASTContext& Ctx) override {
DeclRefChecker.TraverseAST(Ctx);
}
private:
NacroDeclRefChecker DeclRefChecker;
};
struct NacroVerifierImplAction : public PluginASTAction {
std::unique_ptr<clang::ASTConsumer> CreateASTConsumer(
clang::CompilerInstance &Compiler, llvm::StringRef InFile) override {
return std::unique_ptr<clang::ASTConsumer>(
new NacroVerifierImpl(Compiler.getASTContext()));
}
bool ParseArgs(const CompilerInstance &CI,
const std::vector<std::string>& args) override {
return true;
}
ActionType getActionType() override {
return PluginASTAction::AddBeforeMainAction;
}
};
} // end anonymous namespace
static
FrontendPluginRegistry::Add<NacroVerifierImplAction>
X("nacro-verifier", "Nacro verifier driver");