Skip to content

Commit 65f8e2d

Browse files
committed
[Temp] fix stubs
1 parent 7444333 commit 65f8e2d

20 files changed

+152
-118
lines changed

server/src/ReturnTypesFetcher.cpp

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,32 @@
33
#include "fetchers/Fetcher.h"
44
#include "testgens/BaseTestGen.h"
55

6-
void ReturnTypesFetcher::fetch(ProgressWriter *const progressWriter,
7-
const CollectionUtils::FileSet &allFiles) {
8-
tests::TestsMap testsMap;
9-
for (const auto &filePath : allFiles) {
10-
testsMap[filePath];
11-
}
12-
Fetcher(Fetcher::Options::Value::RETURN_TYPE_NAMES_ONLY,
13-
testGen->getTargetBuildDatabase()->compilationDatabase, testsMap, nullptr, nullptr,
14-
testGen->compileCommandsJsonPath, false)
15-
.fetchWithProgress(progressWriter, "Fetching return types for functions", true);
16-
for (auto const &[sourceFilePath, test] : testsMap) {
17-
for (const auto& [methodName, methodDescription]: test.methods) {
18-
auto returnTypedefName = PrinterUtils::getReturnMangledName(methodDescription.name);
19-
if (CollectionUtils::containsKey(methodDescription.functionPointers, returnTypedefName)) {
20-
types::Type returnTypeCopy = methodDescription.returnType;
21-
returnTypeCopy.replaceUsedType(returnTypedefName);
22-
testGen->methodNameToReturnTypeMap[methodName] = returnTypeCopy;
23-
} else {
24-
testGen->methodNameToReturnTypeMap[methodName] = methodDescription.returnType;
6+
void ReturnTypesFetcher::fetch(const ProgressWriter *progressWriter) {
7+
const std::shared_ptr<CompilationDatabase> buildDatabases[] = {
8+
testGen->getTargetBuildDatabase()->compilationDatabase,
9+
testGen->getProjectBuildDatabase()->compilationDatabase};
10+
for (const auto &buildDatabase: buildDatabases) {
11+
tests::TestsMap testsMap;
12+
for (const auto &filePath: buildDatabase->getAllFiles()) {
13+
testsMap[filePath];
14+
}
15+
Fetcher(Fetcher::Options::Value::RETURN_TYPE_NAMES_ONLY,
16+
buildDatabase, testsMap, nullptr, nullptr,
17+
testGen->compileCommandsJsonPath, false)
18+
.fetchWithProgress(progressWriter, "Fetching return types for functions", true);
19+
for (auto const &[sourceFilePath, test]: testsMap) {
20+
for (const auto &[methodName, methodDescription]: test.methods) {
21+
if (testGen->methodNameToReturnTypeMap.count(methodName)) {
22+
continue;
23+
}
24+
auto returnTypedefName = PrinterUtils::getReturnMangledName(methodDescription.name);
25+
if (CollectionUtils::containsKey(methodDescription.functionPointers, returnTypedefName)) {
26+
types::Type returnTypeCopy = methodDescription.returnType;
27+
returnTypeCopy.replaceUsedType(returnTypedefName);
28+
testGen->methodNameToReturnTypeMap[methodName] = returnTypeCopy;
29+
} else {
30+
testGen->methodNameToReturnTypeMap[methodName] = methodDescription.returnType;
31+
}
2532
}
2633
}
2734
}

server/src/ReturnTypesFetcher.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class ReturnTypesFetcher {
1818
explicit ReturnTypesFetcher(BaseTestGen *testGen) : testGen(testGen) {
1919
}
2020

21-
void fetch(ProgressWriter *const progressWriter, const CollectionUtils::FileSet &allFiles);
21+
void fetch(const ProgressWriter *progressWriter);
2222
};
2323

2424

server/src/Server.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,10 @@ Status Server::TestsGenServiceImpl::ProcessBaseTestRequest(BaseTestGen &testGen,
206206
types::TypesHandler typesHandler{testGen.types, sizeContext};
207207
testGen.progressWriter->writeProgress("Generating stub files", 0.0);
208208
StubGen stubGen(testGen);
209-
210-
Synchronizer synchronizer(&testGen, &sizeContext);
211-
synchronizer.synchronize(typesHandler);
212-
209+
{
210+
Synchronizer synchronizer(&testGen, &sizeContext);
211+
synchronizer.synchronize(typesHandler);
212+
}
213213
std::shared_ptr<LineInfo> lineInfo = nullptr;
214214
auto lineTestGen = dynamic_cast<LineTestGen *>(&testGen);
215215

@@ -253,9 +253,10 @@ Status Server::TestsGenServiceImpl::ProcessBaseTestRequest(BaseTestGen &testGen,
253253
}
254254
}
255255
auto generator = std::make_shared<KleeGenerator>(&testGen, typesHandler, pathSubstitution);
256-
257-
ReturnTypesFetcher returnTypesFetcher{&testGen};
258-
returnTypesFetcher.fetch(testGen.progressWriter, synchronizer.getTargetSourceFiles());
256+
{
257+
ReturnTypesFetcher returnTypesFetcher(&testGen);
258+
returnTypesFetcher.fetch(testGen.progressWriter);
259+
}
259260
LOG_S(DEBUG) << "Temporary build directory path: " << testGen.serverBuildDir;
260261
generator->buildKleeFiles(testGen.tests, lineInfo);
261262
generator->handleFailedFunctions(testGen.tests);
@@ -562,8 +563,10 @@ Status Server::TestsGenServiceImpl::ProcessProjectStubsRequest(BaseTestGen *test
562563
testGen->compileCommandsJsonPath, false);
563564

564565
fetcher.fetchWithProgress(testGen->progressWriter, logMessage);
565-
Synchronizer synchronizer(testGen, &sizeContext);
566-
synchronizer.synchronize(typesHandler);
566+
{
567+
Synchronizer synchronizer(testGen, &sizeContext);
568+
synchronizer.synchronize(typesHandler);
569+
}
567570
stubsWriter->writeResponse(testGen->synchronizedStubs, testGen->projectContext.testDirPath);
568571
return Status::OK;
569572
}

server/src/Synchronizer.cpp

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,11 @@ bool Synchronizer::isProbablyOutdatedWrappers(const fs::path &srcFilePath) const
8888
}
8989

9090
CollectionUtils::FileSet Synchronizer::getOutdatedSourcePaths() const {
91-
auto allFiles = getTargetSourceFiles();
92-
auto outdatedSources = CollectionUtils::filterOut(getTargetSourceFiles(), [this](fs::path const &sourcePath) {
93-
return !isProbablyOutdatedWrappers(sourcePath);
94-
});
91+
auto allFiles = testGen->getTargetSourceFiles();
92+
auto outdatedSources = CollectionUtils::filterOut(testGen->getTargetSourceFiles(),
93+
[this](fs::path const &sourcePath) {
94+
return !isProbablyOutdatedWrappers(sourcePath);
95+
});
9596
return outdatedSources;
9697
}
9798

@@ -227,7 +228,7 @@ Synchronizer::createStubsCompilationDatabase(StubSet &stubFiles,
227228
void Synchronizer::synchronizeWrappers(const CollectionUtils::FileSet &outdatedSourcePaths,
228229
const types::TypesHandler &typesHandler) const {
229230
auto sourceFilesNeedToRegenerateWrappers = outdatedSourcePaths;
230-
for (fs::path const &sourceFilePath : getTargetSourceFiles()) {
231+
for (fs::path const &sourceFilePath : testGen->getTargetSourceFiles()) {
231232
if (!CollectionUtils::contains(sourceFilesNeedToRegenerateWrappers, sourceFilePath)) {
232233
auto wrapperFilePath =
233234
Paths::getWrapperFilePath(testGen->projectContext, sourceFilePath);
@@ -247,27 +248,19 @@ void Synchronizer::synchronizeWrappers(const CollectionUtils::FileSet &outdatedS
247248
});
248249
}
249250

250-
const CollectionUtils::FileSet &Synchronizer::getTargetSourceFiles() const {
251-
return testGen->getTargetBuildDatabase()->compilationDatabase->getAllFiles();
252-
}
253-
254-
const CollectionUtils::FileSet &Synchronizer::getProjectSourceFiles() const {
255-
return testGen->getProjectBuildDatabase()->compilationDatabase->getAllFiles();
256-
}
257-
258251
StubSet Synchronizer::getStubsFiles() const {
259252
return getStubSetFromSources(testGen->getProjectBuildDatabase()->compilationDatabase->getAllFiles());
260253
}
261254

262255
void Synchronizer::prepareDirectory(const fs::path &stubDirectory) {
263256
fs::create_directories(stubDirectory);
264-
for (const auto &entry : fs::recursive_directory_iterator(stubDirectory)) {
257+
for (const auto &entry: fs::recursive_directory_iterator(stubDirectory)) {
265258
if (entry.is_regular_file()) {
266259
fs::path stubPath = entry.path();
267260
if (!Paths::isHeaderFile(stubPath)) {
268261
fs::path sourcePath =
269-
Paths::stubPathToSourcePath(testGen->projectContext, stubPath);
270-
if (!CollectionUtils::contains(getProjectSourceFiles(), sourcePath)) {
262+
Paths::stubPathToSourcePath(testGen->projectContext, stubPath);
263+
if (!CollectionUtils::contains(testGen->getProjectSourceFiles(), sourcePath)) {
271264
LOG_S(DEBUG) << "Found extra file in stub directory: " << stubPath
272265
<< ". Removing it.";
273266
fs::remove(stubPath);

server/src/Synchronizer.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,33 @@ class Synchronizer {
2727

2828
[[nodiscard]] std::unordered_set<StubOperator, HashUtils::StubHash> getOutdatedStubs() const;
2929

30-
long long getFileOutdatedTime(const fs::path &filePath) const;
30+
[[nodiscard]] long long getFileOutdatedTime(const fs::path &filePath) const;
3131

32-
bool isProbablyOutdatedStubs(const fs::path &srcFilePath) const;
32+
[[nodiscard]] bool isProbablyOutdatedStubs(const fs::path &srcFilePath) const;
3333

34-
bool isProbablyOutdatedWrappers(const fs::path &srcFilePath) const;
34+
[[nodiscard]] bool isProbablyOutdatedWrappers(const fs::path &srcFilePath) const;
3535

36-
bool removeStubIfSourceAbsent(const StubOperator &stub) const;
36+
[[nodiscard]] bool removeStubIfSourceAbsent(const StubOperator &stub) const;
3737

3838
void synchronizeStubs(std::unordered_set<StubOperator, HashUtils::StubHash> &outdatedStubs,
3939
const types::TypesHandler &typesHandler);
40+
4041
void synchronizeWrappers(const CollectionUtils::FileSet &outdatedSourcePaths,
4142
const types::TypesHandler &typesHandler) const;
4243

4344
std::shared_ptr<CompilationDatabase>
4445
createStubsCompilationDatabase(
45-
std::unordered_set<StubOperator, HashUtils::StubHash> &stubFiles,
46-
const fs::path &ccJsonStubDirPath) const;
46+
std::unordered_set<StubOperator, HashUtils::StubHash> &stubFiles,
47+
const fs::path &ccJsonStubDirPath) const;
4748

48-
void prepareDirectory(fs::path const& stubDirectory);
49+
void prepareDirectory(fs::path const &stubDirectory);
4950

5051
static std::unordered_set<StubOperator, HashUtils::StubHash>
5152

5253
getStubSetFromSources(const CollectionUtils::FileSet &paths);
54+
55+
[[nodiscard]] std::unordered_set<StubOperator, HashUtils::StubHash> getStubsFiles() const;
56+
5357
public:
5458
static std::unordered_set<StubOperator, HashUtils::StubHash>
5559

@@ -60,10 +64,6 @@ class Synchronizer {
6064
Synchronizer(BaseTestGen *testGen, types::TypesHandler::SizeContext *sizeContext);
6165

6266
void synchronize(const types::TypesHandler &typesHandler);
63-
64-
[[nodiscard]] const CollectionUtils::FileSet &getTargetSourceFiles() const;
65-
[[nodiscard]] const CollectionUtils::FileSet &getProjectSourceFiles() const;
66-
[[nodiscard]] std::unordered_set<StubOperator, HashUtils::StubHash> getStubsFiles() const;
6767
};
6868

6969

server/src/Tests.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,11 @@ std::shared_ptr<ArrayValueView> KTestObjectParser::multiArrayView(const std::vec
209209
return std::make_shared<ArrayValueView>(views);
210210
}
211211

212-
std::shared_ptr<FunctionPointerView> KTestObjectParser::functionPointerView(const std::optional<std::string> &scopeName,
213-
const std::string &methodName, const std::string &paramName) {
212+
std::shared_ptr<FunctionPointerView> KTestObjectParser::functionPointerView(
213+
const std::optional <std::string> &scopeName,
214+
const std::string &methodName, const std::string &paramName) {
214215
std::string value =
215-
StubsUtils::getFunctionPointerStubName(scopeName, methodName, paramName).substr(1);
216+
StubsUtils::getFunctionPointerStubName(scopeName, methodName, paramName, false).substr(1);
216217
return std::make_shared<FunctionPointerView>(value);
217218
}
218219

@@ -1165,20 +1166,20 @@ void KTestObjectParser::processStubParamValue(
11651166
const std::unordered_map<std::string, types::Type> &methodNameToReturnTypeMap,
11661167
std::vector<RawKleeParam> &rawKleeParams) {
11671168
for (const auto &kleeParam : rawKleeParams) {
1168-
if (StringUtils::endsWith(kleeParam.paramName, PrinterUtils::KLEE_SYMBOLIC_SUFFIX)) {
1169-
std::string methodName = kleeParam.paramName.substr(
1170-
0, kleeParam.paramName.size() - PrinterUtils::KLEE_SYMBOLIC_SUFFIX.size());
1169+
std::string methodName = StubsUtils::tryGetMethodNameFromStubSymbolic(kleeParam.paramName);
1170+
if (!methodName.empty()) {
11711171
if (!CollectionUtils::containsKey(methodNameToReturnTypeMap, methodName)) {
11721172
LOG_S(WARNING) << "Method name \"" << methodName << "\" was not fetched, skipping";
11731173
continue;
11741174
}
1175-
auto type = typesHandler.getReturnTypeToCheck(methodNameToReturnTypeMap.at(methodName));
1176-
Tests::TypeAndVarName typeAndVarName{ type, kleeParam.paramName };
1175+
auto type = types::Type::createArray(
1176+
typesHandler.getReturnTypeToCheck(methodNameToReturnTypeMap.at(methodName)));
1177+
Tests::TypeAndVarName typeAndVarName{type, kleeParam.paramName};
11771178
auto testParamView =
1178-
testParameterView(kleeParam, typeAndVarName, types::PointerUsage::PARAMETER,
1179-
testCaseDescription.objects, testCaseDescription.lazyReferences);
1179+
testParameterView(kleeParam, typeAndVarName, types::PointerUsage::PARAMETER,
1180+
testCaseDescription.objects, testCaseDescription.lazyReferences);
11801181
testCaseDescription.stubValues.emplace_back(kleeParam.paramName, 0, testParamView);
1181-
testCaseDescription.stubValuesTypes.emplace_back(type, kleeParam.paramName, 0);
1182+
testCaseDescription.stubValuesTypes.emplace_back(type, kleeParam.paramName, std::nullopt);
11821183
}
11831184
}
11841185
}

server/src/fetchers/Fetcher.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class Fetcher {
8181
std::shared_ptr<FileToStringSet> structsToDeclare = std::make_shared<FileToStringSet>();
8282
std::shared_ptr<FileToStringSet> structsDeclared = std::make_shared<FileToStringSet>();
8383
public:
84-
std::shared_ptr<FileToStringSet> getStructsToDeclare() const;
84+
[[nodiscard]] std::shared_ptr<FileToStringSet> getStructsToDeclare() const;
8585
private:
8686
// For functions
8787
bool fetchFunctionBodies;

server/src/printers/KleeConstraintsPrinter.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,12 @@ void KleeConstraintsPrinter::genConstraintsForStruct(const ConstraintsState &sta
125125
access,
126126
field.type,
127127
isStruct ? state.endString : false,
128-
state.depth + 1 };
129-
std::string stubFunctionName = StubsUtils::getFunctionPointerAsStructFieldStubName(curStruct.name, field.name);
128+
state.depth + 1};
129+
std::string stubFunctionName = StubsUtils::getFunctionPointerAsStructFieldStubName(curStruct.name, field.name,
130+
false);
130131
switch (typesHandler->getTypeKind(field.type)) {
131-
case TypeKind::STRUCT_LIKE:
132-
genConstraintsForStruct(newState);
132+
case TypeKind::STRUCT_LIKE:
133+
genConstraintsForStruct(newState);
133134
break;
134135
case TypeKind::ENUM:
135136
genConstraintsForEnum(newState);

server/src/printers/KleePrinter.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -419,13 +419,15 @@ void KleePrinter::genParamsDeclarations(
419419
bool KleePrinter::genParamDeclaration(const Tests::MethodDescription &testMethod,
420420
const Tests::MethodParam &param) {
421421
std::string stubFunctionName =
422-
StubsUtils::getFunctionPointerStubName(testMethod.isClassMethod() ? std::make_optional(testMethod.classObj->name) : std::nullopt,
423-
testMethod.name, param.name);
422+
StubsUtils::getFunctionPointerStubName(
423+
testMethod.isClassMethod() ? std::make_optional(testMethod.classObj->name) : std::nullopt,
424+
testMethod.name, param.name, false);
424425
if (types::TypesHandler::isPointerToFunction(param.type)) {
425426
strDeclareVar(getTypedefFunctionPointer(testMethod.name, param.name, false), param.name,
426427
stubFunctionName, param.alignment);
427428
} else if (types::TypesHandler::isArrayOfPointersToFunction(param.type)) {
428-
strDeclareArrayOfFunctionPointerVar(getTypedefFunctionPointer(testMethod.name, param.name, false), param.name, stubFunctionName);
429+
strDeclareArrayOfFunctionPointerVar(getTypedefFunctionPointer(testMethod.name, param.name, false), param.name,
430+
stubFunctionName);
429431
} else if (types::TypesHandler::isObjectPointerType(param.type)) {
430432
return genPointerParamDeclaration(param);
431433
} else if (types::TypesHandler::isArrayType(param.type)) {

server/src/printers/Printer.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -388,15 +388,17 @@ namespace printer {
388388
const types::TypesHandler &typesHandler,
389389
const std::string &prefix,
390390
const std::string &suffix,
391-
const std::string &methodName,
392-
const std::string &nameForStub,
391+
const std::string &parentMethodName,
393392
bool makeStatic) {
394393
auto methodCopy = method;
395394
methodCopy.name = method.name;
396395

397-
std::string stubSymbolicVarName = StubsUtils::getStubSymbolicVarName(nameForStub);
396+
std::string stubSymbolicVarName = StubsUtils::getStubSymbolicVarName(methodCopy.name, parentMethodName);
397+
if (methodCopy.name != StubsUtils::tryGetMethodNameFromStubSymbolic(stubSymbolicVarName)) {
398+
LOG_S(WARNING) << "Can't generate symbolic value for \"" << stubSymbolicVarName << "\" function";
399+
return ss;
400+
}
398401
if (!types::TypesHandler::omitMakeSymbolic(method.returnType)) {
399-
stubSymbolicVarName = StubsUtils::getStubSymbolicVarName(methodName + "_" + nameForStub);
400402
strDeclareArrayVar(types::Type::createArray(method.returnType), stubSymbolicVarName,
401403
types::PointerUsage::PARAMETER);
402404
}
@@ -541,14 +543,15 @@ namespace printer {
541543
}
542544
}
543545

544-
void printer::Printer::writeExternForSymbolicStubs(const Tests::MethodDescription& testMethod) {
546+
void printer::Printer::writeExternForSymbolicStubs(const Tests::MethodDescription &testMethod) {
545547
std::unordered_map<std::string, std::string> symbolicNamesToTypesMap;
546-
for (const auto& testCase: testMethod.testCases) {
548+
for (const auto &testCase: testMethod.testCases) {
547549
for (size_t i = 0; i < testCase.stubValues.size(); i++) {
548-
symbolicNamesToTypesMap[testCase.stubValues[i].name] = testCase.stubValuesTypes[i].type.usedType();
550+
std::string utype = testCase.stubValuesTypes[i].type.usedType();
551+
symbolicNamesToTypesMap[testCase.stubValues[i].name + "[]"] = utype.substr(0, utype.size() - 1);
549552
}
550553
}
551-
for (const auto& [name, type]: symbolicNamesToTypesMap) {
554+
for (const auto &[name, type]: symbolicNamesToTypesMap) {
552555
strDeclareVar("extern \"C\" " + type, name);
553556
}
554557
}
@@ -564,7 +567,7 @@ namespace printer {
564567
strTypedefFunctionPointer(*fInfo, typedefName);
565568
}
566569
strStubForMethod(tests::Tests::MethodDescription::fromFunctionInfo(*fInfo), *typesHandler,
567-
stubName, "stub", methodName, fInfo->name, makeStatic);
570+
stubName, "stub", methodName, makeStatic);
568571
}
569572

570573
void

server/src/printers/Printer.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,8 @@ namespace printer {
178178
const types::TypesHandler &typesHandler,
179179
const std::string &prefix,
180180
const std::string &suffix,
181-
const std::string &methodName,
182-
const std::string &nameForStub,
183-
bool makeStatic = false);
181+
const std::string &parentMethodName,
182+
bool makeStatic);
184183

185184
Stream strKleeMakeSymbolic(SRef varName, bool needAmpersand, SRef pseudoName);
186185

0 commit comments

Comments
 (0)