-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNodeMatcherPlugin.cpp
91 lines (78 loc) · 2.89 KB
/
NodeMatcherPlugin.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
#include "clang/Frontend/ASTUnit.h"
#include "clang/Frontend/FrontendPluginRegistry.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/MemoryBuffer.h"
#include "Query.h"
#include "QueryParser.h"
#include "QuerySession.h"
using namespace clang;
using namespace clang::ast_matchers;
using namespace clang::tooling;
using namespace llvm;
namespace {
cl::list<std::string> QueryFiles(cl::Positional,
cl::desc("Specify the clang-query script files"),
cl::value_desc("filenames"));
std::vector<std::unique_ptr<MemoryBuffer>> FileContents;
class NodeMatcherConsumer : public ASTConsumer {
private:
CompilerInstance &CI;
public:
NodeMatcherConsumer(CompilerInstance &CI) : CI(CI) {}
void HandleTranslationUnit(ASTContext &Context) override {
for (size_t i = 0; i < FileContents.size(); ++i) {
StringRef FileContent = FileContents.at(i)->getBuffer();
clang::query::QuerySession QS(Context);
while (!FileContent.empty()) {
clang::query::QueryRef Q = clang::query::QueryParser::parse(FileContent, QS);
if (!Q->run(llvm::outs(), QS)) {
llvm::errs() << "Error: Query execution failed in " << QueryFiles[i] << "\n";
return;
}
FileContent = Q->RemainingContent;
}
}
}
};
class NodeMatcherAction : public PluginASTAction {
protected:
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
StringRef) override {
return std::make_unique<NodeMatcherConsumer>(CI);
}
bool ParseArgs(const CompilerInstance &CI,
const std::vector<std::string> &args) override {
std::vector<const char*> ccargs;
ccargs.push_back("QueryPlugin");
for (const auto& arg : args) {
ccargs.push_back(arg.c_str());
}
cl::ParseCommandLineOptions(ccargs.size(), ccargs.data());
if (QueryFiles.empty()) {
llvm::errs() << "Error: No query file specified, pass at least one query file with -fplugin-arg-NodeMatcherPlugin-/path/to/file\n";
return false;
}
FileContents.reserve(QueryFiles.size());
for (const auto& QueryFile : QueryFiles) {
auto Buffer = llvm::MemoryBuffer::getFile(QueryFile);
if (!Buffer) {
llvm::errs() << "Error: Cannot open " << QueryFile << ": "
<< Buffer.getError().message() << "\n";
return false;
}
FileContents.push_back(std::move(Buffer.get()));
}
return true;
}
PluginASTAction::ActionType getActionType() override {
return AddAfterMainAction;
}
};
}
static FrontendPluginRegistry::Add<NodeMatcherAction>
X("NodeMatcherPlugin", "Run clang-query scripts and generate warnings for matches");