Skip to content

Fix stubs for cases with different targets #573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions server/src/Synchronizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,15 @@ void Synchronizer::synchronizeStubs(StubSet &outdatedStubs,
for (const StubOperator &outdatedStub : outdatedStubs) {
fs::path stubPath = outdatedStub.getStubPath(testGen->projectContext);
Tests const &methodDescription = stubFilesMap[stubPath];
tests::Tests tests = StubGen::mergeSourceFileIntoStub(
methodDescription, sourceFilesMap.at(outdatedStub.getSourceFilePath()));
if (outdatedStub.isHeader()) {
std::string code = sourceToHeaderRewriter.generateStubHeader(outdatedStub.getSourceFilePath());
std::string code = sourceToHeaderRewriter.generateStubHeader(tests, outdatedStub.getSourceFilePath());
testGen->synchronizedStubs.emplace_back(stubPath, code);
} else {
tests::Tests newStubFile = StubGen::mergeSourceFileIntoStub(
methodDescription, sourceFilesMap.at(outdatedStub.getSourceFilePath()));
printer::StubsPrinter stubsPrinter(Paths::getSourceLanguage(stubPath));
Stubs stubFile =
stubsPrinter.genStubFile(newStubFile, typesHandler, testGen->projectContext);
stubsPrinter.genStubFile(tests, typesHandler, testGen->projectContext);
testGen->synchronizedStubs.emplace_back(stubFile);
}
}
Expand Down
22 changes: 10 additions & 12 deletions server/src/Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Tests::MethodDescription::MethodDescription()
codeText{{ Tests::DEFAULT_SUITE_NAME, std::string() },
{ Tests::ERROR_SUITE_NAME, std::string() }},
modifiers{} {
stubsParamStorage = std::make_shared<StubsStorage>();
stubsStorage = std::make_shared<StubsStorage>();
}

Expand Down Expand Up @@ -681,7 +682,7 @@ void KTestObjectParser::assignTypeStubVar(Tests::MethodTestCase &testCase,
const Tests::MethodDescription &methodDescription) {
for (auto const &obj : testCase.objects) {
std::optional<std::shared_ptr<FunctionInfo>> maybeFunctionInfo =
methodDescription.stubsStorage->getFunctionPointerByKTestObjectName(obj.name);
methodDescription.stubsParamStorage->getFunctionInfoByKTestObjectName(obj.name);
if (maybeFunctionInfo.has_value()) {
types::Type stubType = types::Type::createArray(maybeFunctionInfo.value()->returnType);
std::shared_ptr<AbstractValueView> stubView = testParameterView({obj.name, obj.bytes}, {stubType, obj.name},
Expand Down Expand Up @@ -883,7 +884,7 @@ Tests::TestCaseDescription KTestObjectParser::parseTestCaseParams(
processSymbolicFiles(testCaseDescription, rawKleeParams);
}

processStubParamValue(testCaseDescription, methodNameToReturnTypeMap, rawKleeParams);
processStubParamValue(methodDescription, testCaseDescription, methodNameToReturnTypeMap, rawKleeParams);
if (!types::TypesHandler::skipTypeInReturn(methodDescription.returnType)) {
const auto kleeResParam = getKleeParamOrThrow(rawKleeParams, KleeUtils::RESULT_VARIABLE_NAME);
auto paramType = methodDescription.returnType.maybeReturnArray() ? methodDescription.returnType :
Expand Down Expand Up @@ -1040,22 +1041,19 @@ void KTestObjectParser::processParamPostValue(Tests::TestCaseDescription &testCa
testCaseDescription.paramPostValues.emplace_back( param.name, param.alignment, testParamView );
}

void KTestObjectParser::processStubParamValue(Tests::TestCaseDescription &testCaseDescription,
void KTestObjectParser::processStubParamValue(const Tests::MethodDescription &methodDescription,
Tests::TestCaseDescription &testCaseDescription,
const std::unordered_map<std::string, types::Type>& methodNameToReturnTypeMap,
std::vector<RawKleeParam> &rawKleeParams) {
for (const auto &kleeParam: rawKleeParams) {
if (StringUtils::endsWith(kleeParam.paramName, PrinterUtils::KLEE_SYMBOLIC_SUFFIX)) {
std::string methodName = kleeParam.paramName.substr(0, kleeParam.paramName.size() - PrinterUtils::KLEE_SYMBOLIC_SUFFIX.size());
if (!CollectionUtils::containsKey(methodNameToReturnTypeMap, methodName)) {
LOG_S(WARNING) << "Method name \"" << methodName << "\" was not fetched, skipping";
continue;
}
auto type = typesHandler.getReturnTypeToCheck(methodNameToReturnTypeMap.at(methodName));
Tests::TypeAndVarName typeAndVarName{ type, kleeParam.paramName };
auto maybeFunctionInfo = methodDescription.stubsStorage->getFunctionInfoByKTestObjectName(kleeParam.paramName);
if (maybeFunctionInfo.has_value()) {
types::Type stubType = types::Type::createArray(maybeFunctionInfo.value()->returnType);
Tests::TypeAndVarName typeAndVarName{ stubType, kleeParam.paramName };
auto testParamView = testParameterView(kleeParam, typeAndVarName, types::PointerUsage::PARAMETER,
testCaseDescription.lazyAddressToName, testCaseDescription.lazyReferences);
testCaseDescription.stubValues.emplace_back( kleeParam.paramName, 0, testParamView );
testCaseDescription.stubValuesTypes.emplace_back(type, kleeParam.paramName, 0);
testCaseDescription.stubValuesTypes.emplace_back(stubType, kleeParam.paramName, std::nullopt);
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions server/src/Tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ namespace tests {

typedef std::unordered_map<std::string, std::shared_ptr<types::FunctionInfo>> FPointerMap;
FPointerMap functionPointers;
std::shared_ptr<StubsStorage> stubsParamStorage;
std::shared_ptr<StubsStorage> stubsStorage;

std::vector<MethodTestCase> testCases;
Expand All @@ -521,7 +522,7 @@ namespace tests {
});
}

[[nodiscard]] types::FunctionInfo toFunctionInfo() {
[[nodiscard]] types::FunctionInfo toFunctionInfo() const {
types::FunctionInfo fInfo;
fInfo.isArray = false;
fInfo.name = name;
Expand Down Expand Up @@ -796,7 +797,8 @@ namespace tests {
const Tests::MethodParam &param,
std::vector<RawKleeParam> &rawKleeParams);

void processStubParamValue(Tests::TestCaseDescription &testCaseDescription,
void processStubParamValue(const Tests::MethodDescription &methodDescription,
Tests::TestCaseDescription &testCaseDescription,
const std::unordered_map<std::string, types::Type>& methodNameToReturnTypeMap,
std::vector<RawKleeParam> &rawKleeParams);

Expand Down
15 changes: 13 additions & 2 deletions server/src/clang-utils/SourceToHeaderRewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "SourceToHeaderMatchCallback.h"
#include "utils/Copyright.h"
#include "utils/LogUtils.h"
#include "utils/StubsUtils.h"

#include "loguru.h"

Expand Down Expand Up @@ -97,22 +98,32 @@ std::string SourceToHeaderRewriter::generateTestHeader(const fs::path &sourceFil
NameDecorator::UNDEF_WCHAR_T, NameDecorator::UNDEFS_CODE);
}

std::string SourceToHeaderRewriter::generateStubHeader(const fs::path &sourceFilePath) {
std::string SourceToHeaderRewriter::generateStubHeader(const tests::Tests &tests, const fs::path &sourceFilePath) {
MEASURE_FUNCTION_EXECUTION_TIME
LOG_IF_S(WARNING, Paths::isCXXFile(sourceFilePath))
<< "Stubs feature for C++ sources has not been tested thoroughly; some problems may occur";
auto sourceDeclarations = generateSourceDeclarations(sourceFilePath, true);
long long creationTime = TimeUtils::convertFileToSystemClock(fs::file_time_type::clock::now())
.time_since_epoch()
.count();
return StringUtils::stringFormat(
printer::StubsPrinter stubsPrinter(Paths::getSourceLanguage(sourceFilePath));
stubsPrinter.ss << StringUtils::stringFormat(
"//%s\n"
"//Please, do not change the line above\n"
"%s\n"
"#define _Alignas(x)\n"
"%s\n",
std::to_string(creationTime), Copyright::GENERATED_C_CPP_FILE_HEADER,
sourceDeclarations.externalDeclarations);
for (const auto &[methodName, methodDescription] : tests.methods) {
std::string stubSymbolicVarName = StubsUtils::getStubSymbolicVarName(methodName);
if (!types::TypesHandler::omitMakeSymbolic(methodDescription.returnType)) {
stubSymbolicVarName = StubsUtils::getStubSymbolicVarName("_" + methodName);
stubsPrinter.strDeclareArrayVar(types::Type::createArray(methodDescription.returnType), stubSymbolicVarName,
types::PointerUsage::PARAMETER);
}
}
return stubsPrinter.ss.str();
}

std::string SourceToHeaderRewriter::generateWrapper(const fs::path &sourceFilePath) {
Expand Down
3 changes: 2 additions & 1 deletion server/src/clang-utils/SourceToHeaderRewriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "fetchers/Fetcher.h"
#include "fetchers/FetcherUtils.h"
#include "utils/FileSystemUtils.h"
#include "printers/StubsPrinter.h"

#include <clang/AST/RecursiveASTVisitor.h>
#include <clang/Frontend/CompilerInstance.h>
Expand Down Expand Up @@ -53,7 +54,7 @@ class SourceToHeaderRewriter {

std::string generateTestHeader(const fs::path &sourceFilePath, const Tests &test);

std::string generateStubHeader(const fs::path &sourceFilePath);
std::string generateStubHeader(const tests::Tests &tests, const fs::path &sourceFilePath);

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

Expand Down
24 changes: 3 additions & 21 deletions server/src/printers/Printer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,11 +407,9 @@ namespace printer {
auto methodCopy = method;
methodCopy.name = method.name;

std::string stubSymbolicVarName = getStubSymbolicVarName(nameForStub);
std::string stubSymbolicVarName = StubsUtils::getStubSymbolicVarName(nameForStub);
if (!types::TypesHandler::omitMakeSymbolic(method.returnType)) {
stubSymbolicVarName = getStubSymbolicVarName(methodName + "_" + nameForStub);
strDeclareArrayVar(types::Type::createArray(method.returnType), stubSymbolicVarName,
types::PointerUsage::PARAMETER);
stubSymbolicVarName = StubsUtils::getStubSymbolicVarName(methodName + "_" + nameForStub);
}

if (!prefix.empty()) {
Expand Down Expand Up @@ -460,10 +458,6 @@ namespace printer {
return ss;
}

std::string Printer::getStubSymbolicVarName(const std::string &methodName) {
return methodName + PrinterUtils::KLEE_SYMBOLIC_SUFFIX;
}

Printer::Stream Printer::strKleeMakeSymbolic(const std::string &varName, bool needAmpersand, SRef pseudoName) {
auto pointer = (needAmpersand ? "&" : "") + varName;
auto size = "sizeof(" + varName + ")";
Expand Down Expand Up @@ -551,24 +545,12 @@ namespace printer {
std::string prefix = PrinterUtils::getKleePrefix(forKlee);
for (const auto &[name, pointerFunctionStub] : testMethod.functionPointers) {
std::string stubName = StubsUtils::getFunctionPointerStubName(scopeName, testMethod.name, name, true);
testMethod.stubsStorage->registerFunctionPointerStub(testMethod.name, pointerFunctionStub);
testMethod.stubsParamStorage->registerStub(testMethod.name, pointerFunctionStub, std::nullopt);
writeStubForParam(typesHandler, pointerFunctionStub, testMethod.name, stubName, true,
forKlee);
}
}

void printer::Printer::writeExternForSymbolicStubs(const Tests::MethodDescription& testMethod) {
std::unordered_map<std::string, std::string> symbolicNamesToTypesMap;
for (const auto& testCase: testMethod.testCases) {
for (size_t i = 0; i < testCase.stubValues.size(); i++) {
symbolicNamesToTypesMap[testCase.stubValues[i].name] = testCase.stubValuesTypes[i].type.usedType();
}
}
for (const auto& [name, type]: symbolicNamesToTypesMap) {
strDeclareVar("extern \"C\" " + type, name);
}
}

void printer::Printer::writeStubForParam(const types::TypesHandler *typesHandler,
const std::shared_ptr<types::FunctionInfo> &fInfo,
const std::string &methodName,
Expand Down
4 changes: 0 additions & 4 deletions server/src/printers/Printer.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,6 @@ namespace printer {
const std::string &nameForStub,
bool makeStatic = false);

static std::string getStubSymbolicVarName(const std::string &methodName);

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

static inline std::string getTypedefFunctionPointer(const std::string &parentFunctionName,
Expand All @@ -209,8 +207,6 @@ namespace printer {
const Tests::MethodDescription& testMethod,
bool forKlee);

void writeExternForSymbolicStubs(const Tests::MethodDescription& testMethod);

void writeStubsForStructureFields(const Tests &tests);

void writeStubsForParameters(const Tests &tests);
Expand Down
9 changes: 2 additions & 7 deletions server/src/printers/StubsPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "Paths.h"
#include "utils/TimeUtils.h"
#include "utils/StubsUtils.h"

using printer::StubsPrinter;

Expand Down Expand Up @@ -51,14 +52,8 @@ Stubs printer::StubsPrinter::genStubFile(const tests::Tests &tests,
param.type = copyParamType;
}
}

if (!typesHandler.omitMakeSymbolic(methodCopy.returnType)) {
std::string stubSymbolicVarName = getStubSymbolicVarName(method.name);
strDeclareArrayVar(types::Type::createArray(method.returnType), stubSymbolicVarName,
types::PointerUsage::PARAMETER);
}

if (methodCopy.sourceBody) {
if (methodCopy.sourceBody) {
strFunctionDecl(methodCopy, " ");
ss << methodCopy.sourceBody.value() << NL;
} else {
Expand Down
2 changes: 1 addition & 1 deletion server/src/printers/StubsPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "types/Types.h"

namespace printer {
class StubsPrinter : Printer {
class StubsPrinter : public Printer {
public:
StubsPrinter(utbot::Language srcLanguage);

Expand Down
7 changes: 6 additions & 1 deletion server/src/printers/TestsPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ void TestsPrinter::joinToFinalCode(Tests &tests, const fs::path& generatedHeader
genHeaders(tests, generatedHeaderPath);
ss << NL;

if (!tests.methods.empty()) {
for (const auto &stubsHeader : tests.methods.begin()->second.stubsStorage->getStubsHeaders()) {
strInclude(stubsHeader) << NL;
}
}

strDeclareSetOfVars(tests.externVariables);

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

writeStubsForFunctionParams(typesHandler, methodDescription, false);
writeExternForSymbolicStubs(methodDescription);

methodDescription.stubsText = ss.str();
resetStream();
Expand Down
10 changes: 9 additions & 1 deletion server/src/stubs/StubGen.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "StubGen.h"

#include <memory>

#include "FeaturesFilter.h"
#include "Paths.h"
#include "StubSourcesFinder.h"
Expand Down Expand Up @@ -42,7 +44,7 @@ StubGen::findStubFilesBySignatures(const std::vector<tests::Tests::MethodDescrip
for (const auto &file : stubFiles) {
stubFilesMap[file].sourceFilePath = file;
}
Fetcher::Options::Value options = Fetcher::Options::Value::FUNCTION_NAMES_ONLY;
Fetcher::Options::Value options = Fetcher::Options::Value::FUNCTION;
Fetcher fetcher(options, stubsCdb, stubFilesMap, nullptr, nullptr, ccJsonDirPath, true);
fetcher.fetchWithProgress(testGen.progressWriter, "Finding stub files", true);
CollectionUtils::FileSet stubFilesSet;
Expand All @@ -53,6 +55,12 @@ StubGen::findStubFilesBySignatures(const std::vector<tests::Tests::MethodDescrip
for (const auto &[methodName, methodDescription] : stub.methods) {
if (CollectionUtils::contains(signatureNamesSet, methodName)) {
stubFilesSet.insert(filePath);
auto stubInfo = std::make_shared<types::FunctionInfo>(methodDescription.toFunctionInfo());
for (auto &[_, tests] : testGen.tests) {
for (auto &[_, method] : tests.methods) {
method.stubsStorage->registerStub("", stubInfo, ((fs::path)filePath).replace_extension(".h"));
}
}
}
}
}
Expand Down
16 changes: 12 additions & 4 deletions server/src/stubs/StubsStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@
#include <utility>
#include "utils/StubsUtils.h"

void StubsStorage::registerFunctionPointerStub(const std::string &methodName, std::shared_ptr<types::FunctionInfo> functionInfo) {
_functionPointers[StubsUtils::getStubSymbolicVarName(methodName + "_" + functionInfo->name)] = std::move(functionInfo);
void StubsStorage::registerStub(const std::string &methodName, std::shared_ptr<types::FunctionInfo> functionInfo,
std::optional<fs::path> stubsHeaderPath) {
if (stubsHeaderPath.has_value()) {
_stubsHeaders.insert(stubsHeaderPath.value());
}
_functions[StubsUtils::getStubSymbolicVarName(methodName + "_" + functionInfo->name)] = std::move(functionInfo);
}

std::optional<std::shared_ptr<types::FunctionInfo>>
StubsStorage::getFunctionPointerByKTestObjectName(const std::string &objectName) {
return CollectionUtils::getOptionalValue(_functionPointers, objectName);
StubsStorage::getFunctionInfoByKTestObjectName(const std::string &objectName) const {
return CollectionUtils::getOptionalValue(_functions, objectName);
}

std::unordered_set<std::string> StubsStorage::getStubsHeaders() {
return _stubsHeaders;
}
12 changes: 9 additions & 3 deletions server/src/stubs/StubsStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@

class StubsStorage {
public:
void registerFunctionPointerStub(const std::string &methodName, std::shared_ptr<types::FunctionInfo>);
std::optional<std::shared_ptr<types::FunctionInfo>> getFunctionPointerByKTestObjectName(const std::string &objectName);
void registerStub(const std::string &methodName, std::shared_ptr<types::FunctionInfo>,
std::optional<fs::path> stubsHeaderPath = std::nullopt);
std::optional<std::shared_ptr<types::FunctionInfo>>
getFunctionInfoByKTestObjectName(const std::string &objectName) const;

std::unordered_set<std::string> getStubsHeaders();

private:
std::unordered_map<std::string, std::shared_ptr<types::FunctionInfo>> _functionPointers;
std::unordered_map<std::string, std::shared_ptr<types::FunctionInfo>> _functions;
std::unordered_set<std::string> _stubsHeaders;
};


Expand Down