MatchVerifier.h
9.9 KB
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
//===- unittest/AST/MatchVerifier.h - AST unit test support ---------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// Provides MatchVerifier, a base class to implement gtest matchers that
// verify things that can be matched on the AST.
//
// Also implements matchers based on MatchVerifier:
// LocationVerifier and RangeVerifier to verify whether a matched node has
// the expected source location or source range.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_UNITTESTS_AST_MATCHVERIFIER_H
#define LLVM_CLANG_UNITTESTS_AST_MATCHVERIFIER_H
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Tooling/Tooling.h"
#include "Language.h"
#include "gtest/gtest.h"
namespace clang {
namespace ast_matchers {
/// \brief Base class for verifying some property of nodes found by a matcher.
template <typename NodeType>
class MatchVerifier : public MatchFinder::MatchCallback {
public:
template <typename MatcherType>
testing::AssertionResult match(const std::string &Code,
const MatcherType &AMatcher) {
std::vector<std::string> Args;
return match(Code, AMatcher, Args, Lang_CXX);
}
template <typename MatcherType>
testing::AssertionResult match(const std::string &Code,
const MatcherType &AMatcher,
Language L) {
std::vector<std::string> Args;
return match(Code, AMatcher, Args, L);
}
template <typename MatcherType>
testing::AssertionResult match(const std::string &Code,
const MatcherType &AMatcher,
std::vector<std::string>& Args,
Language L);
template <typename MatcherType>
testing::AssertionResult match(const Decl *D, const MatcherType &AMatcher);
protected:
void run(const MatchFinder::MatchResult &Result) override;
virtual void verify(const MatchFinder::MatchResult &Result,
const NodeType &Node) {}
void setFailure(const Twine &Result) {
Verified = false;
VerifyResult = Result.str();
}
void setSuccess() {
Verified = true;
}
private:
bool Verified;
std::string VerifyResult;
};
/// \brief Runs a matcher over some code, and returns the result of the
/// verifier for the matched node.
template <typename NodeType> template <typename MatcherType>
testing::AssertionResult MatchVerifier<NodeType>::match(
const std::string &Code, const MatcherType &AMatcher,
std::vector<std::string>& Args, Language L) {
MatchFinder Finder;
Finder.addMatcher(AMatcher.bind(""), this);
std::unique_ptr<tooling::FrontendActionFactory> Factory(
tooling::newFrontendActionFactory(&Finder));
StringRef FileName;
switch (L) {
case Lang_C:
Args.push_back("-std=c99");
FileName = "input.c";
break;
case Lang_C89:
Args.push_back("-std=c89");
FileName = "input.c";
break;
case Lang_CXX:
Args.push_back("-std=c++98");
FileName = "input.cc";
break;
case Lang_CXX11:
Args.push_back("-std=c++11");
FileName = "input.cc";
break;
case Lang_CXX14:
Args.push_back("-std=c++14");
FileName = "input.cc";
break;
case Lang_CXX17:
Args.push_back("-std=c++17");
FileName = "input.cc";
break;
case Lang_CXX2a:
Args.push_back("-std=c++2a");
FileName = "input.cc";
break;
case Lang_OpenCL:
FileName = "input.cl";
break;
case Lang_OBJCXX:
FileName = "input.mm";
break;
}
// Default to failure in case callback is never called
setFailure("Could not find match");
if (!tooling::runToolOnCodeWithArgs(Factory->create(), Code, Args, FileName))
return testing::AssertionFailure() << "Parsing error";
if (!Verified)
return testing::AssertionFailure() << VerifyResult;
return testing::AssertionSuccess();
}
/// \brief Runs a matcher over some AST, and returns the result of the
/// verifier for the matched node.
template <typename NodeType> template <typename MatcherType>
testing::AssertionResult MatchVerifier<NodeType>::match(
const Decl *D, const MatcherType &AMatcher) {
MatchFinder Finder;
Finder.addMatcher(AMatcher.bind(""), this);
setFailure("Could not find match");
Finder.match(*D, D->getASTContext());
if (!Verified)
return testing::AssertionFailure() << VerifyResult;
return testing::AssertionSuccess();
}
template <typename NodeType>
void MatchVerifier<NodeType>::run(const MatchFinder::MatchResult &Result) {
const NodeType *Node = Result.Nodes.getNodeAs<NodeType>("");
if (!Node) {
setFailure("Matched node has wrong type");
} else {
// Callback has been called, default to success.
setSuccess();
verify(Result, *Node);
}
}
template <>
inline void MatchVerifier<ast_type_traits::DynTypedNode>::run(
const MatchFinder::MatchResult &Result) {
BoundNodes::IDToNodeMap M = Result.Nodes.getMap();
BoundNodes::IDToNodeMap::const_iterator I = M.find("");
if (I == M.end()) {
setFailure("Node was not bound");
} else {
// Callback has been called, default to success.
setSuccess();
verify(Result, I->second);
}
}
/// \brief Verify whether a node has the correct source location.
///
/// By default, Node.getSourceLocation() is checked. This can be changed
/// by overriding getLocation().
template <typename NodeType>
class LocationVerifier : public MatchVerifier<NodeType> {
public:
void expectLocation(unsigned Line, unsigned Column) {
ExpectLine = Line;
ExpectColumn = Column;
}
protected:
void verify(const MatchFinder::MatchResult &Result,
const NodeType &Node) override {
SourceLocation Loc = getLocation(Node);
unsigned Line = Result.SourceManager->getSpellingLineNumber(Loc);
unsigned Column = Result.SourceManager->getSpellingColumnNumber(Loc);
if (Line != ExpectLine || Column != ExpectColumn) {
std::string MsgStr;
llvm::raw_string_ostream Msg(MsgStr);
Msg << "Expected location <" << ExpectLine << ":" << ExpectColumn
<< ">, found <";
Loc.print(Msg, *Result.SourceManager);
Msg << '>';
this->setFailure(Msg.str());
}
}
virtual SourceLocation getLocation(const NodeType &Node) {
return Node.getLocation();
}
private:
unsigned ExpectLine, ExpectColumn;
};
/// \brief Verify whether a node has the correct source range.
///
/// By default, Node.getSourceRange() is checked. This can be changed
/// by overriding getRange().
template <typename NodeType>
class RangeVerifier : public MatchVerifier<NodeType> {
public:
void expectRange(unsigned BeginLine, unsigned BeginColumn,
unsigned EndLine, unsigned EndColumn) {
ExpectBeginLine = BeginLine;
ExpectBeginColumn = BeginColumn;
ExpectEndLine = EndLine;
ExpectEndColumn = EndColumn;
}
protected:
void verify(const MatchFinder::MatchResult &Result,
const NodeType &Node) override {
SourceRange R = getRange(Node);
SourceLocation Begin = R.getBegin();
SourceLocation End = R.getEnd();
unsigned BeginLine = Result.SourceManager->getSpellingLineNumber(Begin);
unsigned BeginColumn = Result.SourceManager->getSpellingColumnNumber(Begin);
unsigned EndLine = Result.SourceManager->getSpellingLineNumber(End);
unsigned EndColumn = Result.SourceManager->getSpellingColumnNumber(End);
if (BeginLine != ExpectBeginLine || BeginColumn != ExpectBeginColumn ||
EndLine != ExpectEndLine || EndColumn != ExpectEndColumn) {
std::string MsgStr;
llvm::raw_string_ostream Msg(MsgStr);
Msg << "Expected range <" << ExpectBeginLine << ":" << ExpectBeginColumn
<< '-' << ExpectEndLine << ":" << ExpectEndColumn << ">, found <";
Begin.print(Msg, *Result.SourceManager);
Msg << '-';
End.print(Msg, *Result.SourceManager);
Msg << '>';
this->setFailure(Msg.str());
}
}
virtual SourceRange getRange(const NodeType &Node) {
return Node.getSourceRange();
}
private:
unsigned ExpectBeginLine, ExpectBeginColumn, ExpectEndLine, ExpectEndColumn;
};
/// \brief Verify whether a node's dump contains a given substring.
class DumpVerifier : public MatchVerifier<ast_type_traits::DynTypedNode> {
public:
void expectSubstring(const std::string &Str) {
ExpectSubstring = Str;
}
protected:
void verify(const MatchFinder::MatchResult &Result,
const ast_type_traits::DynTypedNode &Node) override {
std::string DumpStr;
llvm::raw_string_ostream Dump(DumpStr);
Node.dump(Dump, *Result.SourceManager);
if (Dump.str().find(ExpectSubstring) == std::string::npos) {
std::string MsgStr;
llvm::raw_string_ostream Msg(MsgStr);
Msg << "Expected dump substring <" << ExpectSubstring << ">, found <"
<< Dump.str() << '>';
this->setFailure(Msg.str());
}
}
private:
std::string ExpectSubstring;
};
/// \brief Verify whether a node's pretty print matches a given string.
class PrintVerifier : public MatchVerifier<ast_type_traits::DynTypedNode> {
public:
void expectString(const std::string &Str) {
ExpectString = Str;
}
protected:
void verify(const MatchFinder::MatchResult &Result,
const ast_type_traits::DynTypedNode &Node) override {
std::string PrintStr;
llvm::raw_string_ostream Print(PrintStr);
Node.print(Print, Result.Context->getPrintingPolicy());
if (Print.str() != ExpectString) {
std::string MsgStr;
llvm::raw_string_ostream Msg(MsgStr);
Msg << "Expected pretty print <" << ExpectString << ">, found <"
<< Print.str() << '>';
this->setFailure(Msg.str());
}
}
private:
std::string ExpectString;
};
} // end namespace ast_matchers
} // end namespace clang
#endif