-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathcsharpparser.cpp
More file actions
209 lines (168 loc) · 5.53 KB
/
Copy pathcsharpparser.cpp
File metadata and controls
209 lines (168 loc) · 5.53 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
#include <csharpparser/csharpparser.h>
#include <boost/filesystem.hpp>
#include <model/buildaction.h>
#include <model/buildaction-odb.hxx>
#include <model/buildsourcetarget.h>
#include <model/buildsourcetarget-odb.hxx>
#include <model/file.h>
#include <model/file-odb.hxx>
#include <parser/sourcemanager.h>
#include <util/logutil.h>
#include <util/odbtransaction.h>
#include <util/threadpool.h>
#include <util/filesystem.h>
#include <memory>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
namespace cc
{
namespace parser
{
CsharpParser::CsharpParser(ParserContext& ctx_): AbstractParser(ctx_)
{
_threadNum = _ctx.options["jobs"].as<int>();
}
bool CsharpParser::acceptProjectBuildPath(const std::string& buildPath_)
{
return fs::is_directory(buildPath_);
}
bool CsharpParser::parse()
{
bool success = true;
std::vector<std::string> paths = _ctx.options["input"].as<std::vector<std::string>>();
if (!paths.empty())
{
LOG(debug) << "C# parser parse path: " << paths.size();
success = success && parseProjectBuildPath(paths);
}
else
{
LOG(error) << "No input directories provided for C# parser!";
success = false;
}
return success;
}
bool CsharpParser::parseProjectBuildPath(
const std::vector<std::string>& paths_ //,
)
{
namespace ch = std::chrono;
std::future<std::string> log;
fs::path csharp_path = util::findCurrentExecutableDir() + "/../lib/csharp/";
/*
* Concatenate the command parameters to pass to the C# parser.
* 1) C# parser binary
* 2) Database connection string
* 3) Project build directory path
* 4) CC lib directory path
* 5) Thread number
* 6+) Source directories
*/
std::string command("./CSharpParser ");
command.append("'");
command.append(_ctx.options["database"].as<std::string>());
command.append("' '");
command.append(csharp_path.string());
command.append("' ");
command.append(std::to_string(_ctx.options["jobs"].as<int>()));
for (auto p : paths_)
{
if (fs::is_directory(p))
{
command.append(" '");
command.append(p);
command.append("' ");
}
}
LOG(debug) << "CSharpParser command: " << command;
ch::steady_clock::time_point begin = ch::steady_clock::now();
int result = bp::system(command, bp::start_dir(csharp_path), bp::std_out > log);
ch::steady_clock::time_point current = ch::steady_clock::now();
float elapsed_time = ch::duration_cast<ch::milliseconds>(current - begin).count();
LOG(debug) << "CSharp Parse time: " << elapsed_time << " ms";
std::string line;
std::stringstream log_str(log.get());
int countFull = 0, countPart = 0;
while(std::getline(log_str, line, '\n'))
{
line.erase(std::remove(line.begin(), line.end(), '\r'), line.end()); // remove \r
if (line.empty()) continue;
std::string prefix = "JSON_READY:"; // check if json on console
if (line.find(prefix) == 0)
{
std::string jsonString = line.substr(prefix.length()); // cut the prefix
try
{
std::stringstream jsonStream(jsonString);
boost::property_tree::ptree pt;
boost::property_tree::read_json(jsonStream, pt);
bool fullyParsed = pt.get<bool>("fullyParsed");
std::string filepath = pt.get<std::string>("filePath");
std::string targetDll = pt.get<std::string>("targetDll");
bool isError = !fullyParsed;
addSource(filepath, targetDll, isError);
if (fullyParsed) { countFull++; }
else { countPart++; }
}
catch (const boost::property_tree::json_parser::json_parser_error& e)
{
LOG(warning) << "Failed to parse JSON output from C# parser: " << e.what() << " | Line: " << jsonString;
}
catch (const boost::property_tree::ptree_error& e)
{
LOG(warning) << "Missing expected JSON field from C# parser: " << e.what() << " | Line: " << jsonString;
}
}
else
{
LOG(debug) << "[C#]: " << line;
}
}
ch::steady_clock::time_point after = ch::steady_clock::now();
elapsed_time =
ch::duration_cast<ch::milliseconds>(after - current).count();
LOG(debug) << "C# source manage time: " << elapsed_time << " ms";
LOG(info) << "Number of files fully parsed: " << countFull <<
", partially parsed: " << countPart << ", total: " << countFull+countPart;
return result == 0;
}
void CsharpParser::addSource(const std::string& filepath_, const std::string& targetDll_, bool error_)
{
util::OdbTransaction transaction(_ctx.db);
model::BuildActionPtr buildAction(new model::BuildAction);
buildAction->command = "dotnet build ";
buildAction->type = model::BuildAction::Compile;
model::BuildSource buildSource;
buildSource.file = _ctx.srcMgr.getFile(filepath_);
buildSource.file->parseStatus = error_
? model::File::PSPartiallyParsed
: model::File::PSFullyParsed;
buildSource.file->type = "CS";
buildSource.action = buildAction;
_ctx.srcMgr.updateFile(*buildSource.file);
_ctx.srcMgr.persistFiles();
transaction([&, this] {
_ctx.db->persist(buildAction);
_ctx.db->persist(buildSource);
});
}
CsharpParser::~CsharpParser()
{
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wreturn-type-c-linkage"
extern "C"
{
boost::program_options::options_description getOptions()
{
boost::program_options::options_description description("C# Plugin");
return description;
}
std::shared_ptr<CsharpParser> make(ParserContext& ctx_)
{
return std::make_shared<CsharpParser>(ctx_);
}
}
#pragma clang diagnostic pop
} // parser
} // cc