Skip to content

Commit a474688

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

19 files changed

+105
-91
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: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,18 +1167,19 @@ void KTestObjectParser::processStubParamValue(
11671167
for (const auto &kleeParam : rawKleeParams) {
11681168
if (StringUtils::endsWith(kleeParam.paramName, PrinterUtils::KLEE_SYMBOLIC_SUFFIX)) {
11691169
std::string methodName = kleeParam.paramName.substr(
1170-
0, kleeParam.paramName.size() - PrinterUtils::KLEE_SYMBOLIC_SUFFIX.size());
1170+
0, kleeParam.paramName.size() - PrinterUtils::KLEE_SYMBOLIC_SUFFIX.size());
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/Printer.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -389,14 +389,13 @@ namespace printer {
389389
const std::string &prefix,
390390
const std::string &suffix,
391391
const std::string &methodName,
392-
const std::string &nameForStub,
393392
bool makeStatic) {
394393
auto methodCopy = method;
395394
methodCopy.name = method.name;
396395

397-
std::string stubSymbolicVarName = StubsUtils::getStubSymbolicVarName(nameForStub);
396+
// TODO: may have same method.name for different classes in c++
397+
std::string stubSymbolicVarName = StubsUtils::getStubSymbolicVarName(method.name);
398398
if (!types::TypesHandler::omitMakeSymbolic(method.returnType)) {
399-
stubSymbolicVarName = StubsUtils::getStubSymbolicVarName(methodName + "_" + nameForStub);
400399
strDeclareArrayVar(types::Type::createArray(method.returnType), stubSymbolicVarName,
401400
types::PointerUsage::PARAMETER);
402401
}
@@ -541,14 +540,15 @@ namespace printer {
541540
}
542541
}
543542

544-
void printer::Printer::writeExternForSymbolicStubs(const Tests::MethodDescription& testMethod) {
543+
void printer::Printer::writeExternForSymbolicStubs(const Tests::MethodDescription &testMethod) {
545544
std::unordered_map<std::string, std::string> symbolicNamesToTypesMap;
546-
for (const auto& testCase: testMethod.testCases) {
545+
for (const auto &testCase: testMethod.testCases) {
547546
for (size_t i = 0; i < testCase.stubValues.size(); i++) {
548-
symbolicNamesToTypesMap[testCase.stubValues[i].name] = testCase.stubValuesTypes[i].type.usedType();
547+
std::string utype = testCase.stubValuesTypes[i].type.usedType();
548+
symbolicNamesToTypesMap[testCase.stubValues[i].name + "[]"] = utype.substr(0, utype.size() - 1);
549549
}
550550
}
551-
for (const auto& [name, type]: symbolicNamesToTypesMap) {
551+
for (const auto &[name, type]: symbolicNamesToTypesMap) {
552552
strDeclareVar("extern \"C\" " + type, name);
553553
}
554554
}
@@ -564,7 +564,7 @@ namespace printer {
564564
strTypedefFunctionPointer(*fInfo, typedefName);
565565
}
566566
strStubForMethod(tests::Tests::MethodDescription::fromFunctionInfo(*fInfo), *typesHandler,
567-
stubName, "stub", methodName, fInfo->name, makeStatic);
567+
stubName, "stub", methodName, makeStatic);
568568
}
569569

570570
void

server/src/printers/Printer.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,7 @@ namespace printer {
179179
const std::string &prefix,
180180
const std::string &suffix,
181181
const std::string &methodName,
182-
const std::string &nameForStub,
183-
bool makeStatic = false);
182+
bool makeStatic);
184183

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

server/src/printers/StubsPrinter.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,11 @@ Stubs printer::StubsPrinter::genStubFile(const tests::Tests &tests,
5353
}
5454
}
5555

56-
if (!typesHandler.omitMakeSymbolic(methodCopy.returnType)) {
57-
std::string stubSymbolicVarName = StubsUtils::getStubSymbolicVarName(method.name);
58-
strDeclareArrayVar(types::Type::createArray(method.returnType), stubSymbolicVarName,
59-
types::PointerUsage::PARAMETER);
60-
}
61-
62-
if (methodCopy.sourceBody) {
56+
if (methodCopy.sourceBody) {
6357
strFunctionDecl(methodCopy, " ");
6458
ss << methodCopy.sourceBody.value() << NL;
6559
} else {
66-
strStubForMethod(methodCopy, typesHandler, "", "", "", methodCopy.name);
60+
strStubForMethod(methodCopy, typesHandler, "", "", "", false);
6761
};
6862
ss << NL;
6963
}

server/src/stubs/StubsStorage.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
#include <utility>
44
#include "utils/StubsUtils.h"
55

6-
void StubsStorage::registerFunctionPointerStub(const std::string &methodName, std::shared_ptr<types::FunctionInfo> functionInfo) {
7-
_functionPointers[StubsUtils::getStubSymbolicVarName(methodName + "_" + functionInfo->name)] = std::move(functionInfo);
6+
void StubsStorage::registerFunctionPointerStub(const std::string &methodName,
7+
std::shared_ptr<types::FunctionInfo> functionInfo) {
8+
_functionPointers[StubsUtils::getStubSymbolicVarName(methodName + "_" + functionInfo->name)] = std::move(
9+
functionInfo);
810
}
911

1012
std::optional<std::shared_ptr<types::FunctionInfo>>

server/src/stubs/StubsStorage.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010
class StubsStorage {
1111
public:
1212
void registerFunctionPointerStub(const std::string &methodName, std::shared_ptr<types::FunctionInfo>);
13-
std::optional<std::shared_ptr<types::FunctionInfo>> getFunctionPointerByKTestObjectName(const std::string &objectName);
13+
14+
std::optional<std::shared_ptr<types::FunctionInfo>>
15+
getFunctionPointerByKTestObjectName(const std::string &objectName);
16+
1417
private:
15-
std::unordered_map<std::string, std::shared_ptr<types::FunctionInfo>> _functionPointers;
18+
std::unordered_map<std::string, std::shared_ptr<types::FunctionInfo>> _functionPointers{};
1619
};
1720

1821

server/src/testgens/BaseTestGen.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,22 @@ std::shared_ptr<TargetBuildDatabase> BaseTestGen::getTargetBuildDatabase() {
7777
return targetBuildDatabase;
7878
}
7979

80+
const CollectionUtils::FileSet &BaseTestGen::getTargetSourceFiles() const {
81+
return getTargetBuildDatabase()->compilationDatabase->getAllFiles();
82+
}
83+
84+
const CollectionUtils::FileSet &BaseTestGen::getProjectSourceFiles() const {
85+
return getProjectBuildDatabase()->compilationDatabase->getAllFiles();
86+
}
87+
8088
std::shared_ptr<const BuildDatabase::ObjectFileInfo>
8189
BaseTestGen::getClientCompilationUnitInfo(const fs::path &path, bool fullProject) const {
8290
std::shared_ptr<const BuildDatabase::ObjectFileInfo> objectFileInfo;
8391
if (targetBuildDatabase->hasUnitInfo(path) || !fullProject) {
8492
objectFileInfo = targetBuildDatabase->getClientCompilationUnitInfo(path);
8593
} else {
86-
objectFileInfo = projectBuildDatabase->getClientCompilationUnitInfo(path);
8794
LOG_S(WARNING) << "Can't find in target: " << path;
95+
objectFileInfo = projectBuildDatabase->getClientCompilationUnitInfo(path);
8896
}
8997
return objectFileInfo;
9098
}

server/src/testgens/BaseTestGen.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ class BaseTestGen {
5151

5252
std::shared_ptr<TargetBuildDatabase> getTargetBuildDatabase();
5353

54+
const CollectionUtils::FileSet &getTargetSourceFiles() const;
55+
56+
const CollectionUtils::FileSet &getProjectSourceFiles() const;
57+
5458
std::shared_ptr<const BuildDatabase::ObjectFileInfo>
5559
getClientCompilationUnitInfo(const fs::path &path, bool fullProject = false) const;
5660

server/src/utils/StubsUtils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace StubsUtils {
1717

1818
std::string getStubSymbolicVarName(const std::string &methodName) {
1919
std::string stubName = methodName + PrinterUtils::KLEE_SYMBOLIC_SUFFIX;
20-
StringUtils::replaceColon(stubName);
20+
StringUtils::replaceColon(stubName); //TODO has problem
2121
return stubName;
2222
}
2323

submodules/klee

Submodule klee updated 914 files

0 commit comments

Comments
 (0)