-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkslicer_utils.cpp
294 lines (247 loc) · 9.8 KB
/
kslicer_utils.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
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
#include "kslicer.h"
#include "initial_pass.h"
#include "template_rendering.h"
#include <algorithm>
#include <cctype>
#include <string>
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::string kslicer::GetRangeSourceCode(const clang::SourceRange a_range, const clang::CompilerInstance& compiler)
{
const clang::SourceManager& sm = compiler.getSourceManager();
const clang::LangOptions& lopt = compiler.getLangOpts();
clang::SourceLocation b(a_range.getBegin()), _e(a_range.getEnd());
clang::SourceLocation e(clang::Lexer::getLocForEndOfToken(_e, 0, sm, lopt));
if(e < b)
return std::string("");
else
return std::string(sm.getCharacterData(b), sm.getCharacterData(e));
}
std::string kslicer::GetRangeSourceCode(const clang::SourceRange a_range, const clang::SourceManager& sm)
{
clang::LangOptions lopt;
clang::SourceLocation b(a_range.getBegin()), _e(a_range.getEnd());
clang::SourceLocation e(clang::Lexer::getLocForEndOfToken(_e, 0, sm, lopt));
return std::string(sm.getCharacterData(b), sm.getCharacterData(e));
}
uint64_t kslicer::GetHashOfSourceRange(const clang::SourceRange& a_range)
{
const uint32_t hash1 = a_range.getBegin().getRawEncoding();
const uint32_t hash2 = a_range.getEnd().getRawEncoding();
return (uint64_t(hash1) << 32) | uint64_t(hash2);
}
void kslicer::PrintError(const std::string& a_msg, const clang::SourceRange& a_range, const clang::SourceManager& a_sm)
{
const auto beginLoc = a_range.getBegin();
const auto inFileLoc = a_sm.getPresumedLoc(beginLoc);
const auto fileName = std::string(a_sm.getFilename(beginLoc));
const auto line = inFileLoc.getLine();
const auto col = inFileLoc.getColumn();
std::string code = GetRangeSourceCode(a_range, a_sm);
std::cout << fileName.c_str() << ":" << line << ":" << col << ": error: " << a_msg << std::endl;
std::cout << "--> " << code.c_str() << std::endl;
}
void kslicer::PrintWarning(const std::string& a_msg, const clang::SourceRange& a_range, const clang::SourceManager& a_sm)
{
const auto beginLoc = a_range.getBegin();
const auto inFileLoc = a_sm.getPresumedLoc(beginLoc);
const auto fileName = std::string(a_sm.getFilename(beginLoc));
const auto line = inFileLoc.getLine();
const auto col = inFileLoc.getColumn();
std::string code = GetRangeSourceCode(a_range, a_sm);
std::cout << fileName.c_str() << ":" << line << ":" << col << ": warning: " << a_msg << " --> " << code.c_str() << std::endl;
}
void kslicer::ExtractTypeAndVarNameFromConstructor(clang::CXXConstructExpr* constructExpr, clang::ASTContext* astContext, std::string& varName, std::string& typeName)
{
// (0) очищаем строки
//
varName = "";
typeName = "";
// (1) Получаем имя типа
//
clang::CXXConstructorDecl* ctor = constructExpr->getConstructor();
typeName = ctor->getNameInfo().getName().getAsString();
// (2) Получаем имя переменной
clang::DynTypedNodeList parents = astContext->getParents(*constructExpr);
for (const clang::DynTypedNode& parent : parents) {
if (const clang::VarDecl* varDecl = parent.get<clang::VarDecl>()) {
varName = varDecl->getNameAsString();
break;
}
}
}
const clang::Expr* kslicer::RemoveImplicitCast(const clang::Expr* nextNode)
{
if(nextNode == nullptr)
return nullptr;
while(clang::isa<clang::ImplicitCastExpr>(nextNode))
{
auto cast = dyn_cast<const clang::ImplicitCastExpr>(nextNode);
nextNode = cast->getSubExpr();
}
return nextNode;
}
clang::Expr* kslicer::RemoveImplicitCast(clang::Expr* nextNode)
{
if(nextNode == nullptr)
return nullptr;
while(clang::isa<clang::ImplicitCastExpr>(nextNode))
{
auto cast = dyn_cast<clang::ImplicitCastExpr>(nextNode);
nextNode = cast->getSubExpr();
}
return nextNode;
}
std::string kslicer::CutOffFileExt(const std::string& a_filePath)
{
const size_t lastindex = a_filePath.find_last_of(".");
if(lastindex != std::string::npos)
return a_filePath.substr(0, lastindex);
else
return a_filePath;
}
std::string kslicer::CutOffStructClass(const std::string& a_typeName)
{
auto spacePos = a_typeName.find_last_of(" ");
if(spacePos != std::string::npos)
return a_typeName.substr(spacePos+1);
return a_typeName;
}
void MakeAbsolutePathRelativeTo(std::filesystem::path& a_filePath, const std::filesystem::path& a_folderPath)
{
a_filePath = std::filesystem::relative(a_filePath, a_folderPath);
}
std::string ToLowerCase(std::string a_str)
{
std::transform(a_str.begin(), a_str.end(), a_str.begin(), [](unsigned char c){ return std::tolower(c); });
return a_str;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::unordered_map<std::string, std::string> ReadCommandLineParams(int argc, const char** argv, std::filesystem::path& fileName,
std::vector<std::string>& allFiles,
std::vector<std::string>& ignoreFiles,
std::vector<std::string>& processFiles,
std::vector<std::string>& cppIncludes)
{
std::unordered_map<std::string, std::string> cmdLineParams;
for(int i=0; i<argc; i++)
{
std::string key(argv[i]);
const bool isDefine = key.size() > 1 && key.substr(0,2) == "-D";
const bool isKey = key.size() > 0 && key[0] == '-';
if(key == "-ignore")
ignoreFiles.push_back(argv[i+1]);
else if (key == "-process")
processFiles.push_back(argv[i+1]);
else if (key == "-cpp_include")
cppIncludes.push_back(argv[i+1]);
else if(key != "-v" && !isDefine && isKey) // exclude special "-IfoldePath" form, exclude "-v"
{
if(i != argc-1) // not last argument
{
cmdLineParams[key] = argv[i+1];
i++;
}
else
cmdLineParams[key] = "";
}
else if(key.find(".cpp") != std::string::npos)
allFiles.push_back(key);
}
if(allFiles.size() == 0)
{
std::cout << "[main]: no input file is specified " << std::endl;
exit(0);
}
else if(allFiles.size() == 1)
fileName = allFiles[0];
else
{
fileName = allFiles[0];
// merge files to a single temporary file
auto folderPath = fileName.parent_path();
auto fileName2 = fileName.filename();
fileName2.replace_extension("");
fileName2.concat("_temp.cpp");
auto fileNameT = folderPath / fileName2;
std::cout << "[kslicer]: merging input files to temporary file " << fileName2 << std::endl;
std::ofstream fout(fileNameT);
for(auto file : allFiles)
{
fout << "////////////////////////////////////////////////////" << std::endl;
fout << "//// input file: " << file << std::endl;
fout << "////////////////////////////////////////////////////" << std::endl;
std::ifstream fin(file);
std::string line;
while (std::getline(fin, line))
fout << line.c_str() << std::endl;
}
fout.close();
fileName = fileNameT;
std::cout << "[kslicer]: merging finished" << std::endl;
}
return cmdLineParams;
}
std::vector<const char*> ExcludeSlicerParams(int argc, const char** argv, const std::unordered_map<std::string,std::string>& params, const char* a_mainFileName)
{
std::unordered_set<std::string> values;
for(auto p : params)
values.insert(p.second);
bool foundDSlicer = false;
bool foundMainFile = false;
std::vector<const char*> argsForClang; // exclude our input from cmdline parameters and pass the rest to clang
argsForClang.reserve(argc);
for(int i=1;i<argc;i++)
{
if(params.find(argv[i]) == params.end() && values.find(argv[i]) == values.end())
argsForClang.push_back(argv[i]);
if(std::string(argv[i]) == "-DKERNEL_SLICER")
foundDSlicer = true;
else if(std::string(argv[i]) == a_mainFileName)
foundMainFile = true;
}
if(!foundMainFile)
argsForClang.insert(argsForClang.begin(), a_mainFileName);
if(!foundDSlicer)
argsForClang.push_back("-DKERNEL_SLICER");
return argsForClang;
}
const char* GetClangToolingErrorCodeMessage(int code)
{
if(code == 0)
return "OK";
else if (code == 1)
return "ERROR";
else
return "SKIPPED_FILES";
}
void ReadThreadsOrderFromStr(const std::string& threadsOrderStr, uint32_t threadsOrder[3])
{
auto size = std::min<size_t>(threadsOrderStr.size(), 3);
for(size_t symbId = 0; symbId < size; symbId++)
{
switch(threadsOrderStr[symbId])
{
case 'x':
case 'X':
case '0':
threadsOrder[symbId] = 0;
break;
case 'y':
case 'Y':
case '1':
threadsOrder[symbId] = 1;
break;
case 'z':
case 'Z':
case '2':
threadsOrder[symbId] = 2;
break;
default:
break;
};
}
}