Skip to content

Commit 368e00d

Browse files
committed
[Temp] fix stubs 2
1 parent 3b68f66 commit 368e00d

File tree

12 files changed

+118
-76
lines changed

12 files changed

+118
-76
lines changed

server/src/Synchronizer.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,15 +200,14 @@ void Synchronizer::synchronizeStubs(StubSet &outdatedStubs,
200200
for (const StubOperator &outdatedStub : outdatedStubs) {
201201
fs::path stubPath = outdatedStub.getStubPath(testGen->projectContext);
202202
Tests const &methodDescription = stubFilesMap[stubPath];
203+
tests::Tests tests = StubGen::mergeSourceFileIntoStub(
204+
methodDescription, sourceFilesMap.at(outdatedStub.getSourceFilePath()));
203205
if (outdatedStub.isHeader()) {
204-
std::string code = sourceToHeaderRewriter.generateStubHeader(outdatedStub.getSourceFilePath());
206+
std::string code = sourceToHeaderRewriter.generateStubHeader(tests, outdatedStub.getSourceFilePath());
205207
testGen->synchronizedStubs.emplace_back(stubPath, code);
206208
} else {
207-
tests::Tests newStubFile = StubGen::mergeSourceFileIntoStub(
208-
methodDescription, sourceFilesMap.at(outdatedStub.getSourceFilePath()));
209209
printer::StubsPrinter stubsPrinter(Paths::getSourceLanguage(stubPath));
210-
Stubs stubFile =
211-
stubsPrinter.genStubFile(newStubFile, typesHandler, testGen->projectContext);
210+
Stubs stubFile = stubsPrinter.genStubFile(tests, typesHandler, testGen->projectContext);
212211
testGen->synchronizedStubs.emplace_back(stubFile);
213212
}
214213
}

server/src/Tests.cpp

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Tests::MethodDescription::MethodDescription()
3333
codeText{{ Tests::DEFAULT_SUITE_NAME, std::string() },
3434
{ Tests::ERROR_SUITE_NAME, std::string() }},
3535
modifiers{} {
36+
stubsParamStorage = std::make_shared<StubsStorage>();
3637
stubsStorage = std::make_shared<StubsStorage>();
3738
}
3839

@@ -747,8 +748,9 @@ size_t KTestObjectParser::getOffsetInStruct(Tests::TypeAndVarName &objTypeAndNam
747748
void KTestObjectParser::assignTypeStubVar(Tests::MethodTestCase &testCase,
748749
const Tests::MethodDescription &methodDescription) {
749750
for (auto const &obj : testCase.objects) {
750-
std::optional<std::shared_ptr<FunctionInfo>> maybeFunctionInfo =
751-
methodDescription.stubsStorage->getFunctionPointerByKTestObjectName(obj.name);
751+
std::optional<std::shared_ptr<FunctionInfo>>
752+
maybeFunctionInfo = methodDescription.stubsParamStorage->getFunctionInfoByKTestObjectName(obj.name);
753+
// maybeFunctionInfo = methodDescription.stubsStorage->getFunctionPointerByKTestObjectName(obj.name);
752754
if (maybeFunctionInfo.has_value()) {
753755
types::Type stubType = types::Type::createArray(maybeFunctionInfo.value()->returnType);
754756
std::shared_ptr<AbstractValueView> stubView =
@@ -983,10 +985,10 @@ Tests::TestCaseDescription KTestObjectParser::parseTestCaseParams(
983985
processSymbolicFiles(testCaseDescription, rawKleeParams);
984986
}
985987

986-
processStubParamValue(testCaseDescription, methodNameToReturnTypeMap, rawKleeParams);
988+
processStubParamValue(methodDescription, testCaseDescription, methodNameToReturnTypeMap, rawKleeParams);
987989
if (!types::TypesHandler::skipTypeInReturn(methodDescription.returnType)) {
988990
const auto kleeResParam =
989-
getKleeParamOrThrow(rawKleeParams, KleeUtils::RESULT_VARIABLE_NAME);
991+
getKleeParamOrThrow(rawKleeParams, KleeUtils::RESULT_VARIABLE_NAME);
990992
auto paramType = methodDescription.returnType.maybeReturnArray()
991993
? methodDescription.returnType
992994
: methodDescription.returnType.baseTypeObj();
@@ -1162,29 +1164,25 @@ void KTestObjectParser::processParamPostValue(Tests::TestCaseDescription &testCa
11621164
}
11631165

11641166
void KTestObjectParser::processStubParamValue(
1165-
Tests::TestCaseDescription &testCaseDescription,
1166-
const std::unordered_map<std::string, types::Type> &methodNameToReturnTypeMap,
1167-
std::vector<RawKleeParam> &rawKleeParams) {
1168-
for (const auto &kleeParam : rawKleeParams) {
1169-
std::string methodName = StubsUtils::tryGetMethodNameFromStubSymbolic(kleeParam.paramName);
1170-
if (!methodName.empty()) {
1171-
if (!CollectionUtils::containsKey(methodNameToReturnTypeMap, methodName)) {
1172-
LOG_S(WARNING) << "Method name \"" << methodName << "\" was not fetched, skipping";
1173-
continue;
1174-
}
1175-
auto type = types::Type::createArray(
1176-
typesHandler.getReturnTypeToCheck(methodNameToReturnTypeMap.at(methodName)));
1177-
Tests::TypeAndVarName typeAndVarName{type, kleeParam.paramName};
1167+
const Tests::MethodDescription &methodDescription,
1168+
Tests::TestCaseDescription &testCaseDescription,
1169+
const std::unordered_map<std::string, types::Type> &methodNameToReturnTypeMap,
1170+
std::vector<RawKleeParam> &rawKleeParams) {
1171+
for (const auto &kleeParam: rawKleeParams) {
1172+
auto maybeFunctionInfo = methodDescription.stubsStorage->getFunctionInfoByKTestObjectName(kleeParam.paramName);
1173+
if (maybeFunctionInfo.has_value()) {
1174+
types::Type stubType = types::Type::createArray(maybeFunctionInfo.value()->returnType);
1175+
Tests::TypeAndVarName typeAndVarName{stubType, kleeParam.paramName};
11781176
auto testParamView =
11791177
testParameterView(kleeParam, typeAndVarName, types::PointerUsage::PARAMETER,
11801178
testCaseDescription.objects, testCaseDescription.lazyReferences);
11811179
testCaseDescription.stubValues.emplace_back(kleeParam.paramName, 0, testParamView);
1182-
testCaseDescription.stubValuesTypes.emplace_back(type, kleeParam.paramName, std::nullopt);
1180+
testCaseDescription.stubValuesTypes.emplace_back(stubType, kleeParam.paramName, std::nullopt);
11831181
}
11841182
}
11851183
}
11861184

1187-
std::shared_ptr<AbstractValueView> KTestObjectParser::testParameterView(
1185+
std::shared_ptr<AbstractValueView> KTestObjectParser::testParameterView(
11881186
const KTestObjectParser::RawKleeParam &kleeParam,
11891187
const Tests::TypeAndVarName &param,
11901188
PointerUsage usage,

server/src/Tests.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ namespace tests {
516516

517517
typedef std::unordered_map<std::string, std::shared_ptr<types::FunctionInfo>> FPointerMap;
518518
FPointerMap functionPointers;
519+
std::shared_ptr<StubsStorage> stubsParamStorage;
519520
std::shared_ptr<StubsStorage> stubsStorage;
520521

521522
std::vector<MethodTestCase> testCases;
@@ -541,7 +542,7 @@ namespace tests {
541542
});
542543
}
543544

544-
[[nodiscard]] types::FunctionInfo toFunctionInfo() {
545+
[[nodiscard]] types::FunctionInfo toFunctionInfo() const {
545546
types::FunctionInfo fInfo;
546547
fInfo.isArray = false;
547548
fInfo.name = name;
@@ -822,8 +823,9 @@ namespace tests {
822823
const Tests::MethodParam &param,
823824
std::vector<RawKleeParam> &rawKleeParams);
824825

825-
void processStubParamValue(Tests::TestCaseDescription &testCaseDescription,
826-
const std::unordered_map<std::string, types::Type>& methodNameToReturnTypeMap,
826+
void processStubParamValue(const Tests::MethodDescription &methodDescription,
827+
Tests::TestCaseDescription &testCaseDescription,
828+
const std::unordered_map<std::string, types::Type> &methodNameToReturnTypeMap,
827829
std::vector<RawKleeParam> &rawKleeParams);
828830

829831
static void addToOrder(const std::vector<UTBotKTestObject> &objects,

server/src/clang-utils/SourceToHeaderRewriter.cpp

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@
1010

1111
#include <utility>
1212
#include <fstream>
13+
#include <printers/StubsPrinter.h>
1314

1415
SourceToHeaderRewriter::SourceToHeaderRewriter(
15-
utbot::ProjectContext projectContext,
16-
const std::shared_ptr<CompilationDatabase> &compilationDatabase,
17-
std::shared_ptr<Fetcher::FileToStringSet> structsToDeclare,
18-
fs::path serverBuildDir,
19-
const types::TypesHandler &typesHandler)
16+
utbot::ProjectContext projectContext,
17+
const std::shared_ptr<CompilationDatabase> &compilationDatabase,
18+
std::shared_ptr<Fetcher::FileToStringSet> structsToDeclare,
19+
fs::path serverBuildDir,
20+
const types::TypesHandler &typesHandler)
2021
: projectContext(std::move(projectContext)),
2122
clangToolRunner(compilationDatabase), structsToDeclare(structsToDeclare),
2223
serverBuildDir(std::move(serverBuildDir)), typesHandler(typesHandler) {
@@ -111,22 +112,31 @@ std::string SourceToHeaderRewriter::generateTestHeader(const fs::path &sourceFil
111112
NameDecorator::UNDEF_WCHAR_T, NameDecorator::UNDEFS_CODE, sourceDeclarations.unnamedTypeDeclarations);
112113
}
113114

114-
std::string SourceToHeaderRewriter::generateStubHeader(const fs::path &sourceFilePath) {
115+
std::string SourceToHeaderRewriter::generateStubHeader(const tests::Tests &tests, const fs::path &sourceFilePath) {
115116
MEASURE_FUNCTION_EXECUTION_TIME
116117
LOG_IF_S(WARNING, Paths::isCXXFile(sourceFilePath))
117-
<< "Stubs feature for C++ sources has not been tested thoroughly; some problems may occur";
118+
<< "Stubs feature for C++ sources has not been tested thoroughly; some problems may occur";
118119
auto sourceDeclarations = generateSourceDeclarations(sourceFilePath, true, false);
119120
long long creationTime = TimeUtils::convertFileToSystemClock(fs::file_time_type::clock::now())
120-
.time_since_epoch()
121-
.count();
122-
return StringUtils::stringFormat(
123-
"//%s\n"
124-
"//Please, do not change the line above\n"
125-
"%s\n"
126-
"#define _Alignas(x)\n"
127-
"%s\n",
128-
std::to_string(creationTime), Copyright::GENERATED_C_CPP_FILE_HEADER,
129-
sourceDeclarations.externalDeclarations);
121+
.time_since_epoch()
122+
.count();
123+
printer::StubsPrinter stubsPrinter(Paths::getSourceLanguage(sourceFilePath));
124+
stubsPrinter.ss << StringUtils::stringFormat(
125+
"//%s\n"
126+
"//Please, do not change the line above\n"
127+
"%s\n"
128+
"#define _Alignas(x)\n"
129+
"%s\n",
130+
std::to_string(creationTime), Copyright::GENERATED_C_CPP_FILE_HEADER,
131+
sourceDeclarations.externalDeclarations);
132+
for (const auto &[methodName, methodDescription] : tests.methods) {
133+
std::string stubSymbolicVarName = StubsUtils::getStubSymbolicVarName(methodName, "");
134+
if (!types::TypesHandler::omitMakeSymbolic(methodDescription.returnType)) {
135+
stubsPrinter.strDeclareArrayVar(types::Type::createArray(methodDescription.returnType), stubSymbolicVarName,
136+
types::PointerUsage::PARAMETER);
137+
}
138+
}
139+
return stubsPrinter.ss.str();
130140
}
131141

132142
std::string SourceToHeaderRewriter::generateWrapper(const fs::path &sourceFilePath) {

server/src/clang-utils/SourceToHeaderRewriter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class SourceToHeaderRewriter {
5959

6060
std::string generateTestHeader(const fs::path &sourceFilePath, const Tests &test, bool externFromStub);
6161

62-
std::string generateStubHeader(const fs::path &sourceFilePath);
62+
std::string generateStubHeader(const tests::Tests &tests, const fs::path &sourceFilePath);
6363

6464
std::string generateWrapper(const fs::path &sourceFilePath);
6565

server/src/printers/Printer.cpp

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -394,10 +394,11 @@ namespace printer {
394394
methodCopy.name = method.name;
395395

396396
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-
}
397+
// if (methodCopy.name != StubsUtils::tryGetMethodNameFromStubSymbolic(stubSymbolicVarName)) {
398+
// LOG_S(WARNING) << "Can't generate symbolic value for \"" << stubSymbolicVarName << "\" function";
399+
// return ss;
400+
// }
401+
401402
if (!types::TypesHandler::omitMakeSymbolic(method.returnType)) {
402403
strDeclareArrayVar(types::Type::createArray(method.returnType), stubSymbolicVarName,
403404
types::PointerUsage::PARAMETER);
@@ -537,24 +538,24 @@ namespace printer {
537538
std::string prefix = PrinterUtils::getKleePrefix(forKlee);
538539
for (const auto &[name, pointerFunctionStub] : testMethod.functionPointers) {
539540
std::string stubName = StubsUtils::getFunctionPointerStubName(scopeName, testMethod.name, name, true);
540-
testMethod.stubsStorage->registerFunctionPointerStub(testMethod.name, pointerFunctionStub);
541+
testMethod.stubsParamStorage->registerStub(testMethod.name, pointerFunctionStub, std::nullopt);
541542
writeStubForParam(typesHandler, pointerFunctionStub, testMethod.name, stubName, true,
542543
forKlee);
543544
}
544545
}
545546

546-
void printer::Printer::writeExternForSymbolicStubs(const Tests::MethodDescription &testMethod) {
547-
std::unordered_map<std::string, std::string> symbolicNamesToTypesMap;
548-
for (const auto &testCase: testMethod.testCases) {
549-
for (size_t i = 0; i < testCase.stubValues.size(); i++) {
550-
std::string utype = testCase.stubValuesTypes[i].type.usedType();
551-
symbolicNamesToTypesMap[testCase.stubValues[i].name + "[]"] = utype.substr(0, utype.size() - 1);
552-
}
553-
}
554-
for (const auto &[name, type]: symbolicNamesToTypesMap) {
555-
strDeclareVar("extern \"C\" " + type, name);
556-
}
557-
}
547+
// void printer::Printer::writeExternForSymbolicStubs(const Tests::MethodDescription &testMethod) {
548+
// std::unordered_map<std::string, std::string> symbolicNamesToTypesMap;
549+
// for (const auto &testCase: testMethod.testCases) {
550+
// for (size_t i = 0; i < testCase.stubValues.size(); i++) {
551+
// std::string utype = testCase.stubValuesTypes[i].type.usedType();
552+
// symbolicNamesToTypesMap[testCase.stubValues[i].name + "[]"] = utype.substr(0, utype.size() - 1);
553+
// }
554+
// }
555+
// for (const auto &[name, type]: symbolicNamesToTypesMap) {
556+
// strDeclareVar("extern \"C\" " + type, name);
557+
// }
558+
// }
558559

559560
void printer::Printer::writeStubForParam(const types::TypesHandler *typesHandler,
560561
const std::shared_ptr<types::FunctionInfo> &fInfo,

server/src/printers/Printer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ namespace printer {
202202
const Tests::MethodDescription& testMethod,
203203
bool forKlee);
204204

205-
void writeExternForSymbolicStubs(const Tests::MethodDescription& testMethod);
205+
// void writeExternForSymbolicStubs(const Tests::MethodDescription& testMethod);
206206

207207
void writeStubsForStructureFields(const Tests &tests);
208208

server/src/printers/StubsPrinter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "utils/StubsUtils.h"
99

1010
namespace printer {
11-
class StubsPrinter : Printer {
11+
class StubsPrinter : public Printer {
1212
public:
1313
StubsPrinter(utbot::Language srcLanguage);
1414

server/src/printers/TestsPrinter.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ void TestsPrinter::joinToFinalCode(Tests &tests, const fs::path& generatedHeader
6464
genHeaders(tests, generatedHeaderPath);
6565
ss << NL;
6666

67+
if (!tests.methods.empty()) {
68+
for (const auto &stubsHeader : tests.methods.begin()->second.stubsStorage->getStubsHeaders()) {
69+
strInclude(stubsHeader) << NL;
70+
}
71+
}
72+
6773
strDeclareSetOfVars(tests.externVariables);
6874

6975
ss << "namespace " << PrinterUtils::TEST_NAMESPACE << " {\n";
@@ -171,7 +177,7 @@ void TestsPrinter::genCode(Tests::MethodDescription &methodDescription,
171177
int testNum = 0;
172178

173179
writeStubsForFunctionParams(typesHandler, methodDescription, false);
174-
writeExternForSymbolicStubs(methodDescription);
180+
// writeExternForSymbolicStubs(methodDescription);
175181

176182
methodDescription.stubsText = ss.str();
177183
resetStream();

server/src/stubs/StubGen.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ StubGen::findStubFilesBySignatures(const std::vector<tests::Tests::MethodDescrip
4242
for (const auto &file : stubFiles) {
4343
stubFilesMap[file].sourceFilePath = file;
4444
}
45-
Fetcher::Options::Value options = Fetcher::Options::Value::FUNCTION_NAMES_ONLY;
45+
Fetcher::Options::Value options = Fetcher::Options::Value::RETURN_TYPE_NAMES_ONLY;
4646
Fetcher fetcher(options, stubsCdb, stubFilesMap, nullptr, nullptr, ccJsonDirPath, true);
4747
fetcher.fetchWithProgress(testGen.progressWriter, "Finding stub files", true);
4848
CollectionUtils::FileSet stubFilesSet;
@@ -53,6 +53,12 @@ StubGen::findStubFilesBySignatures(const std::vector<tests::Tests::MethodDescrip
5353
for (const auto &[methodName, methodDescription] : stub.methods) {
5454
if (CollectionUtils::contains(signatureNamesSet, methodName)) {
5555
stubFilesSet.insert(filePath);
56+
auto stubInfo = std::make_shared<types::FunctionInfo>(methodDescription.toFunctionInfo());
57+
for (auto &[_, tests] : testGen.tests) {
58+
for (auto &[_, method] : tests.methods) {
59+
method.stubsStorage->registerStub("", stubInfo, ((fs::path)filePath).replace_extension(".h"));
60+
}
61+
}
5662
}
5763
}
5864
}

server/src/stubs/StubsStorage.cpp

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,30 @@
33
#include <utility>
44
#include "utils/StubsUtils.h"
55

6-
void StubsStorage::registerFunctionPointerStub(const std::string &methodName,
7-
std::shared_ptr<types::FunctionInfo> functionInfo) {
8-
_functionPointers[StubsUtils::getStubSymbolicVarName(functionInfo->name, methodName)] = std::move(
9-
functionInfo);
6+
//void StubsStorage::registerFunctionPointerStub(const std::string &methodName,
7+
// std::shared_ptr<types::FunctionInfo> functionInfo) {
8+
// _functionPointers[StubsUtils::getStubSymbolicVarName(functionInfo->name, methodName)] = std::move(
9+
// functionInfo);
10+
//}
11+
//
12+
//std::optional<std::shared_ptr<types::FunctionInfo>>
13+
//StubsStorage::getFunctionPointerByKTestObjectName(const std::string &objectName) {
14+
// return CollectionUtils::getOptionalValue(_functionPointers, objectName);
15+
//}
16+
17+
void StubsStorage::registerStub(const std::string &methodName, std::shared_ptr<types::FunctionInfo> functionInfo,
18+
std::optional<fs::path> stubsHeaderPath) {
19+
if (stubsHeaderPath.has_value()) {
20+
_stubsHeaders.insert(stubsHeaderPath.value());
21+
}
22+
_functions[StubsUtils::getStubSymbolicVarName(functionInfo->name, methodName)] = std::move(functionInfo);
1023
}
1124

1225
std::optional<std::shared_ptr<types::FunctionInfo>>
13-
StubsStorage::getFunctionPointerByKTestObjectName(const std::string &objectName) {
14-
return CollectionUtils::getOptionalValue(_functionPointers, objectName);
26+
StubsStorage::getFunctionInfoByKTestObjectName(const std::string &objectName) const {
27+
return CollectionUtils::getOptionalValue(_functions, objectName);
28+
}
29+
30+
std::unordered_set<std::string> StubsStorage::getStubsHeaders() {
31+
return _stubsHeaders;
1532
}

server/src/stubs/StubsStorage.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@
99

1010
class StubsStorage {
1111
public:
12-
void registerFunctionPointerStub(const std::string &methodName, std::shared_ptr<types::FunctionInfo>);
13-
12+
void registerStub(const std::string &methodName, std::shared_ptr<types::FunctionInfo>,
13+
std::optional<fs::path> stubsHeaderPath = std::nullopt);
1414
std::optional<std::shared_ptr<types::FunctionInfo>>
15-
getFunctionPointerByKTestObjectName(const std::string &objectName);
15+
getFunctionInfoByKTestObjectName(const std::string &objectName) const;
16+
17+
std::unordered_set<std::string> getStubsHeaders();
1618

1719
private:
18-
std::unordered_map<std::string, std::shared_ptr<types::FunctionInfo>> _functionPointers{};
20+
std::unordered_map<std::string, std::shared_ptr<types::FunctionInfo>> _functions;
21+
std::unordered_set<std::string> _stubsHeaders;
1922
};
2023

2124

0 commit comments

Comments
 (0)