Skip to content

Commit e8a5e79

Browse files
authored
Changes for using UTBotCPP on scalapack project. (#560)
* Skip .f and .f.o files. * Check only Definition params. * Use custom print for headers and wrappers code generation. Need this cause of unusual definition of some functions. * Some code beauty changes. * Some code beauty changes. * Skip .f and .f.o files. * Check only Definition params. * Use custom print for headers and wrappers code generation. Need this cause of unusual definition of some functions. * Some code beauty changes. * Some code beauty changes. * Add filter for old style function definition. * fix clang-10 fail. * fix clang-10 fail. * Add exception * Fix error in auto_installation_check. * Add comment with old style code definition example. * Move check in separate function.
1 parent ee4f9df commit e8a5e79

File tree

8 files changed

+73
-7
lines changed

8 files changed

+73
-7
lines changed

.github/workflows/publish-utbot.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ jobs:
9191

9292
auto_installation_check:
9393
needs: publish
94-
runs-on: ubuntu-latest
94+
runs-on: ubuntu-20.04
9595
steps:
9696
- name: Checkout repository
9797
uses: actions/checkout@v3

server/src/Paths.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ namespace Paths {
5151
return m.first == base.end();
5252
}
5353

54+
bool skipFile(const fs::path &file) {
55+
return file.string().substr(file.string().size() - 4) == ".f.o";
56+
}
57+
5458
fs::path longestCommonPrefixPath(const fs::path &a, const fs::path &b) {
5559
if (a == b) {
5660
return a;

server/src/Paths.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ namespace Paths {
101101

102102
bool isSubPathOf(const fs::path &base, const fs::path &sub);
103103

104+
bool skipFile(const fs::path &file);
105+
104106
fs::path longestCommonPrefixPath(const fs::path &a, const fs::path &b);
105107

106108
static inline fs::path replaceExtension(const fs::path &path, const std::string &newExt) {

server/src/building/BuildDatabase.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,9 @@ bool BuildDatabase::ObjectFileInfo::is32bits() const {
432432
}
433433

434434
fs::path BuildDatabase::TargetInfo::getOutput() const {
435+
if (commands.empty()){
436+
throw CompilationDatabaseException("There are no targets");
437+
}
435438
return commands[0].getOutput();
436439
}
437440

server/src/building/ProjectBuildDatabse.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "loguru.h"
77
#include "utils/StringUtils.h"
88
#include "utils/CompilationUtils.h"
9+
#include "Paths.h"
910

1011
static std::string tryConvertToFullPath(const std::string &possibleFilePath, const fs::path &dirPath) {
1112
fs::path fullFilePath = Paths::getCCJsonFileFullPath(possibleFilePath, dirPath);
@@ -68,6 +69,10 @@ void ProjectBuildDatabase::initObjects(const nlohmann::json &compileCommandsJson
6869
fs::path jsonFile = compileCommand.at("file").get<std::string>();
6970
fs::path sourceFile = Paths::getCCJsonFileFullPath(jsonFile, directory);
7071

72+
if (!Paths::isSourceFile(sourceFile)){
73+
continue;
74+
}
75+
7176
std::vector<std::string> jsonArguments;
7277
if (compileCommand.contains("command")) {
7378
std::string command = compileCommand.at("command");
@@ -125,7 +130,9 @@ void ProjectBuildDatabase::initObjects(const nlohmann::json &compileCommandsJson
125130
objectFileInfos[outputFile] = objectInfo;
126131
}
127132
const fs::path &sourcePath = objectInfo->getSourcePath();
133+
128134
sourceFileInfos[sourcePath].emplace_back(objectInfo);
135+
129136
}
130137
for (auto &[sourceFile, objectInfos]: sourceFileInfos) {
131138
std::sort(objectInfos.begin(), objectInfos.end(), BuildDatabase::ObjectFileInfo::conflictPriorityMore);
@@ -164,6 +171,9 @@ void ProjectBuildDatabase::initInfo(const nlohmann::json &linkCommandsJson) {
164171
for (nlohmann::json const &jsonFile: linkCommand.at("files")) {
165172
auto filename = jsonFile.get<std::string>();
166173
fs::path currentFile = Paths::getCCJsonFileFullPath(filename, command.getDirectory());
174+
if (Paths::skipFile(currentFile)){
175+
continue;
176+
}
167177
targetInfo->addFile(currentFile);
168178
if (Paths::isObjectFile(currentFile)) {
169179
if (!CollectionUtils::containsKey(objectFileInfos, currentFile)) {

server/src/clang-utils/SourceToHeaderMatchCallback.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,12 @@ void SourceToHeaderMatchCallback::generateInternal(const FunctionDecl *decl) con
202202

203203
std::string curDecl = getRenamedDeclarationAsString(decl, policy, decoratedName);
204204
std::string wrapperDecl = getRenamedDeclarationAsString(decl, policy, wrapperName);
205+
206+
if (IsOldStyleDefinition(curDecl)){
207+
curDecl = getOldStyleDeclarationAsString(decl, decoratedName);
208+
wrapperDecl = getOldStyleDeclarationAsString(decl, wrapperName);
209+
}
210+
205211
*internalStream << "extern \"C\" " << wrapperDecl << ";\n";
206212
*internalStream << "static " << curDecl << " {\n";
207213
printReturn(decl, wrapperName, internalStream);
@@ -253,6 +259,9 @@ void SourceToHeaderMatchCallback::generateWrapper(const FunctionDecl *decl) cons
253259
std::string name = decl->getNameAsString();
254260
std::string wrapperName = PrinterUtils::wrapperName(name, projectContext, sourceFilePath);
255261
std::string wrapperDecl = getRenamedDeclarationAsString(decl, policy, wrapperName);
262+
if (IsOldStyleDefinition(wrapperDecl)){
263+
wrapperDecl = getOldStyleDeclarationAsString(decl, wrapperName);
264+
}
256265

257266
*wrapperStream << wrapperDecl << " {\n";
258267
printReturn(decl, name, wrapperStream);
@@ -362,3 +371,39 @@ void SourceToHeaderMatchCallback::renameDecl(const NamedDecl *decl, const std::s
362371
DeclarationName wrapperDeclarationName{ &info };
363372
const_cast<NamedDecl *>(decl)->setDeclName(wrapperDeclarationName);
364373
}
374+
375+
std::string SourceToHeaderMatchCallback::getOldStyleDeclarationAsString(const FunctionDecl *decl, std::string const &name) const{
376+
std::string result;
377+
llvm::raw_string_ostream resultStream{ result };
378+
std::string funcReturnType = decl->getFunctionType()->getReturnType().getAsString();
379+
std::vector<std::string> parameters = CollectionUtils::transformTo<std::vector<std::string>>(
380+
decl->parameters(), [](ParmVarDecl *param) { return param->getNameAsString(); });
381+
382+
std::vector<std::string> param_types = CollectionUtils::transformTo<std::vector<std::string>>(
383+
decl->parameters(), [](ParmVarDecl *param) { return param->getType().getAsString(); });
384+
resultStream << funcReturnType << ' ' << name << '(';
385+
386+
for (int i = 0; i < parameters.size(); i++){
387+
if (i != 0){
388+
resultStream << ", ";
389+
}
390+
resultStream << param_types[i] << ' ' << parameters[i];
391+
}
392+
resultStream << ")";
393+
resultStream.flush();
394+
return result;
395+
}
396+
397+
/*Example of old style definition
398+
int sum(a, b)
399+
int a;
400+
int b;
401+
{
402+
return a + b;
403+
}
404+
*/
405+
bool SourceToHeaderMatchCallback::IsOldStyleDefinition(std::string const &definition) const{
406+
std::regex normStyle ("\\([a-zA-Z0-9*&_()\\[\\]]+ [a-zA-Z0-9*&_()\\[\\]]+[,) ]");
407+
bool res = std::regex_search(definition, normStyle);
408+
return !res;
409+
}

server/src/clang-utils/SourceToHeaderMatchCallback.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ class SourceToHeaderMatchCallback : public clang::ast_matchers::MatchFinder::Mat
8787
void renameDecl(const clang::NamedDecl *decl, const std::string &name) const;
8888

8989
std::string decorate(std::string_view name) const;
90+
91+
std::string getOldStyleDeclarationAsString(const clang::FunctionDecl *decl, std::string const &name) const;
92+
93+
bool IsOldStyleDefinition(std::string const &definition) const;
9094
};
9195

9296

server/src/fetchers/FunctionDeclsMatchCallback.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,7 @@ void FunctionDeclsMatchCallback::run(const MatchFinder::MatchResult &Result) {
9090
}
9191

9292
const auto paramsFromDefinition = FS->parameters();
93-
const auto paramsFromDeclaration = FSFromHeader->parameters();
94-
for (size_t i = 0; i < paramsFromDeclaration.size(); ++i) {
95-
const auto &declParam = paramsFromDeclaration[i];
93+
for (size_t i = 0; i < paramsFromDefinition.size(); ++i) {
9694
const auto &defParam = paramsFromDefinition[i];
9795
std::string name = NameDecorator::decorate(defParam->getNameAsString());
9896
std::string mangledName = PrinterUtils::getParamMangledName(name, methodName);
@@ -102,9 +100,9 @@ void FunctionDeclsMatchCallback::run(const MatchFinder::MatchResult &Result) {
102100
if (name == methodDescription.name) {
103101
name = mangledName;
104102
}
105-
auto paramType = ParamsHandler::getType(defParam->getType(), declParam->getType(), sourceManager);
106-
addFunctionPointer(methodDescription.functionPointers, declParam->getFunctionType(),
107-
declParam->getType(), name, sourceManager, paramType);
103+
auto paramType = ParamsHandler::getType(defParam->getType(), defParam->getType(), sourceManager);
104+
addFunctionPointer(methodDescription.functionPointers, defParam->getFunctionType(),
105+
defParam->getType(), name, sourceManager, paramType);
108106
auto alignment = AlignmentFetcher::fetch(defParam);
109107
bool hasIncompleteType = ClangUtils::isIncomplete(defParam->getType());
110108
methodDescription.params.emplace_back(paramType, name, alignment, hasIncompleteType);

0 commit comments

Comments
 (0)