-
Notifications
You must be signed in to change notification settings - Fork 5
/
Driver.cpp
178 lines (164 loc) · 6.79 KB
/
Driver.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
// Sources for the ASTFrontendAction-ASTConsumer-MatchCallback "triad":
// Also has info on how to use FrontendActionFactory and ClangTool
// https://github.com/peter-can-talk/cppnow-2017/blob/master/code/mccabe/mccabe.cpp
// and the corresponding conference video
// https://www.youtube.com/watch?v=E6i8jmiy8MY
// Information about ASTFrontendAction, ASTConsumer from official Clang doc.
// https://clang.llvm.org/docs/RAVFrontendAction.html
// Information about ASTFrontendAction, ASTContext and Clang AST in general:
// https://jonasdevlieghere.com/understanding-the-clang-ast/
// clang includes
#include "clang/Frontend/FrontendAction.h"
#include "clang/Tooling/Tooling.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Path.h"
// standard includes
#include <iostream> // should be removed
#include <fstream> // file stream
// JSON library
#include <nlohmann/json.hpp>
//
#include "cxx-langstat/AnalysisRegistry.h"
#include "cxx-langstat/Utils.h"
#include "cxx-langstat/Driver.h"
#include "cxx-langstat/Stats.h"
using namespace clang;
using namespace clang::ast_matchers;
using namespace clang::tooling;
using StringRef = llvm::StringRef;
using ASTContext = clang::ASTContext;
using ordered_json = nlohmann::ordered_json;
//-----------------------------------------------------------------------------
// Consumes the AST, i.e. does computations on it
class Consumer : public ASTConsumer {
public:
Consumer(StringRef InFile, AnalysisRegistry* Registry) :
InFile(InFile),
Registry(Registry){
}
// Called when AST for TU is ready/has been parsed
// Assumes -emit-features in on
void HandleTranslationUnit(ASTContext& Context){
ordered_json AllAnalysesFeatures;
Registry->createFreshAnalyses();
Stage Stage = Registry->Options.Stage;
for(const auto& an : Registry->Analyses){ // ref to unique_ptr bad?
auto AnalysisShorthand = an->getShorthand();
// Analyze clang AST and get features
AllAnalysesFeatures[AnalysisShorthand]
=an->getFeatures(InFile, Context);
}
// Write to file if -emit-features is active
auto OutputFile = Registry->getCurrentOutputFile();
std::cout << "Writing features to file: "<<OutputFile<<"\n";
std::ofstream o(OutputFile);
o << AllAnalysesFeatures.dump(4) << '\n';
Registry->destroyAnalyses();
}
public:
StringRef InFile;
AnalysisRegistry* Registry;
};
// Responsible for steering when what is executed
class Action : public ASTFrontendAction {
public:
Action(AnalysisRegistry* Registry) : Registry(Registry){
// std::cout << "Creating AST Action" << std::endl;
}
// Called at start of processing a single input
bool BeginSourceFileAction(CompilerInstance& CI) {
std::cout
<< "Starting to process \033[32m" << getCurrentFile().str()
<< "\033[0m. AST=" << isCurrentFileAST() << ".\n";
return true;
}
// Called after frontend is initialized, but before per-file processing
virtual std::unique_ptr<ASTConsumer> CreateASTConsumer(
CompilerInstance &CI, StringRef InFile){
// std::cout << "Creating AST Consumer" << std::endl;
return std::make_unique<Consumer>(getCurrentFile(), Registry);
}
//
void EndSourceFileAction(){
// std::cout << "Finished processing " << getCurrentFile().str() << ".\n";
}
AnalysisRegistry* Registry;
};
// Responsible for building Actions
class Factory : public clang::tooling::FrontendActionFactory {
public:
Factory(AnalysisRegistry* Reg) : Registry(Reg){}
//
std::unique_ptr<FrontendAction> create() override {
return std::make_unique<Action>(Registry);
}
AnalysisRegistry* Registry;
};
//-----------------------------------------------------------------------------
//
int CXXLangstatMain(std::vector<std::string> InputFiles,
std::vector<std::string> OutputFiles, Stage Stage, std::string Analyses,
std::string BuildPath, std::shared_ptr<CompilationDatabase> db){
// Create custom options object for registry
CXXLangstatOptions Opts(Stage, OutputFiles, Analyses);
AnalysisRegistry* Registry = new AnalysisRegistry(Opts);
if(Stage == emit_features){
// https://clang.llvm.org/doxygen/CommonOptionsParser_8cpp_source.html @ 109
// Read in database found in dir specified by -p or a parent path
std::string ErrorMessage;
if(!BuildPath.empty()){
std::cout << "READING BUILD PATH\n";
std::cout << "BUILD PATH: " << BuildPath << std::endl;
db = CompilationDatabase::autoDetectFromDirectory(BuildPath, ErrorMessage);
if(!ErrorMessage.empty()){
std::cout << ErrorMessage << std::endl;
exit(1);
}
std::cout << "FOUND COMPILE COMMANDS:" << std::endl;
for(auto cc : db->getAllCompileCommands()){
for(auto s : cc.CommandLine)
std::cout << s << " ";
std::cout << std::endl;
}
}
if(db){
ClangTool Tool(*db, InputFiles);
// Tool is run for every file specified in source path list
Tool.run(std::make_unique<Factory>(Registry).get());
} else {
std::cout << "The tool couldn't run due to missing compilation database "
"Please try one of the following:\n"
" When analyzing .ast files, append \"--\", to indicate no CDB is necessary\n"
" When analyzing .cpp files, append \"-- <compile flags>\" or \"-p <build path>\"\n";
}
}
// Process features stored on disk to statistics
else if(Stage == emit_statistics){
ordered_json AllFilesAllStatistics;
ordered_json Summary;
for(auto File : InputFiles){
std::cout << "Reading features from file: " << File << "...";
ordered_json j;
std::ifstream i(File);
i >> j;
std::cout << "Done\n";
ordered_json OneFileAllStatistics;
Registry->createFreshAnalyses();
for(const auto& an : Registry->Analyses){ // ref to unique_ptr bad?
auto AnalysisShorthand = an->getShorthand();
for(const auto& [statdesc, stats] : an->getStatistics(j[AnalysisShorthand]).items()){
OneFileAllStatistics[statdesc] = stats;
}
}
Summary = add(std::move(Summary), OneFileAllStatistics);
AllFilesAllStatistics[File] = OneFileAllStatistics;
Registry->destroyAnalyses();
}
std::ofstream o(Registry->Options.OutputFiles[0]);
o << AllFilesAllStatistics.dump(4) << std::endl;
o << Summary.dump(4) << std::endl;
}
delete Registry;
return 0;
}