Skip to content
Merged
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
7 changes: 4 additions & 3 deletions server/src/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,6 @@ Status Server::TestsGenServiceImpl::ProcessBaseTestRequest(BaseTestGen &testGen,
testGen.compileCommandsJsonPath, false);
fetcher.fetchWithProgress(testGen.progressWriter, logMessage);
types::TypesHandler typesHandler{testGen.types, sizeContext};
SourceToHeaderRewriter(testGen.projectContext, testGen.getTargetBuildDatabase()->compilationDatabase,
fetcher.getStructsToDeclare(), testGen.serverBuildDir, typesHandler)
.generateTestHeaders(testGen.tests, testGen.progressWriter);
testGen.progressWriter->writeProgress("Generating stub files", 0.0);
StubGen stubGen(testGen);

Expand Down Expand Up @@ -265,6 +262,10 @@ Status Server::TestsGenServiceImpl::ProcessBaseTestRequest(BaseTestGen &testGen,
Linker linker{testGen, stubGen, lineInfo, generator};
linker.prepareArtifacts();
auto testMethods = linker.getTestMethods();
auto selectedTargets = linker.getSelectedTargets();
SourceToHeaderRewriter(testGen.projectContext, testGen.getTargetBuildDatabase()->compilationDatabase,
fetcher.getStructsToDeclare(), testGen.serverBuildDir, typesHandler)
.generateTestHeaders(testGen.tests, stubGen, selectedTargets, testGen.progressWriter);
KleeRunner kleeRunner{testGen.projectContext, testGen.settingsContext};
bool interactiveMode = (dynamic_cast<ProjectTestGen *>(&testGen) != nullptr);
auto generationStartTime = std::chrono::steady_clock::now();
Expand Down
6 changes: 6 additions & 0 deletions server/src/building/Linker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ void Linker::linkForOneFile(const fs::path &sourceFilePath) {
auto [targetBitcode, stubsSet, _] = result.getOpt().value();
addToGenerated({ objectFile }, targetBitcode);
auto&& targetUnitInfo = testGen.getTargetBuildDatabase()->getClientLinkUnitInfo(target);
selectedTargets[sourceFilePath] = target;
return;
} else {
LOG_S(DEBUG) << "Linkage for target " << target.filename() << " failed: " << result.getError()->c_str();
Expand Down Expand Up @@ -193,6 +194,7 @@ void Linker::linkForProject() {
return compilationUnitInfo->getOutputFile();
});
addToGenerated(objectFiles, linkres.bitcodeOutput);
selectedTargets[sourceFile] = target;
break;
} else {
std::stringstream ss;
Expand Down Expand Up @@ -311,6 +313,10 @@ std::vector<tests::TestMethod> Linker::getTestMethods() {
return testMethods;
}

CollectionUtils::MapFileTo<fs::path> Linker::getSelectedTargets() {
return selectedTargets;
}

Linker::Linker(BaseTestGen &testGen,
StubGen stubGen,
std::shared_ptr<LineInfo> lineInfo,
Expand Down
3 changes: 3 additions & 0 deletions server/src/building/Linker.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class Linker {

std::vector<tests::TestMethod> getTestMethods();

CollectionUtils::MapFileTo<fs::path> getSelectedTargets();

BuildResult
addLinkTargetRecursively(const fs::path &fileToBuild,
printer::DefaultMakefilePrinter &bitcodeLinkMakefilePrinter,
Expand All @@ -54,6 +56,7 @@ class Linker {

CollectionUtils::FileSet testedFiles;
CollectionUtils::MapFileTo<fs::path> bitcodeFileName;
CollectionUtils::MapFileTo<fs::path> selectedTargets;
CollectionUtils::FileSet brokenLinkFiles;

IRParser irParser;
Expand Down
20 changes: 13 additions & 7 deletions server/src/clang-utils/SourceToHeaderMatchCallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ SourceToHeaderMatchCallback::SourceToHeaderMatchCallback(utbot::ProjectContext p
raw_ostream *unnamedTypeDeclsStream,
raw_ostream *wrapperStream,
const types::TypesHandler &typesHandler,
bool forStubHeader)
bool forStubHeader,
bool externFromStub)
: projectContext(std::move(projectContext)),
sourceFilePath(std::move(sourceFilePath)), externalStream(externalStream),
internalStream(internalStream), unnamedTypeDeclsStream(unnamedTypeDeclsStream),
wrapperStream(wrapperStream), typesHandler(typesHandler), forStubHeader(forStubHeader) {
wrapperStream(wrapperStream), typesHandler(typesHandler), forStubHeader(forStubHeader),
externFromStub(externFromStub) {
}

void SourceToHeaderMatchCallback::run(const ast_matchers::MatchFinder::MatchResult &Result) {
Expand Down Expand Up @@ -222,14 +224,18 @@ void SourceToHeaderMatchCallback::generateInternal(const FunctionDecl *decl) con
renameAnonymousReturnTypeDecl(tagDecl, name);
}

*internalStream << "extern \"C\" " << wrapperDecl << ";\n";
*internalStream << "static " << curDecl << " {\n";
printReturn(decl, wrapperName, internalStream);
*internalStream << "}\n";
if (externFromStub) {
*internalStream << "extern \"C\" " << curDecl << ";\n";
} else {
*internalStream << "extern \"C\" " << wrapperDecl << ";\n";
*internalStream << "static " << curDecl << " {\n";
printReturn(decl, wrapperName, internalStream);
*internalStream << "}\n";
}
}

void SourceToHeaderMatchCallback::generateInternal(const VarDecl *decl) const {
if (internalStream == nullptr) {
if (internalStream == nullptr || externFromStub) {
return;
}
auto policy = getDefaultPrintingPolicy(decl, true);
Expand Down
4 changes: 3 additions & 1 deletion server/src/clang-utils/SourceToHeaderMatchCallback.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class SourceToHeaderMatchCallback : public clang::ast_matchers::MatchFinder::Mat
const types::TypesHandler &typesHandler;

bool forStubHeader;
bool externFromStub;
public:
SourceToHeaderMatchCallback(
utbot::ProjectContext projectContext,
Expand All @@ -38,7 +39,8 @@ class SourceToHeaderMatchCallback : public clang::ast_matchers::MatchFinder::Mat
llvm::raw_ostream *unnamedTypeDeclsStream,
llvm::raw_ostream *wrapperStream,
const types::TypesHandler &typesHandler,
bool forStubHeader);
bool forStubHeader,
bool externFromStub);

void run(const MatchFinder::MatchResult &Result) override;
private:
Expand Down
36 changes: 24 additions & 12 deletions server/src/clang-utils/SourceToHeaderRewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,30 @@ SourceToHeaderRewriter::createFactory(llvm::raw_ostream *externalStream,
llvm::raw_ostream *unnamedTypeDeclsStream,
llvm::raw_ostream *wrapperStream,
fs::path sourceFilePath,
bool forStubHeader) {
bool forStubHeader,
bool externFromStub) {
if (Paths::isCXXFile(sourceFilePath)) {
externalStream = nullptr;
internalStream = nullptr;
}
fetcherInstance = std::make_unique<SourceToHeaderMatchCallback>(
projectContext, sourceFilePath, externalStream, internalStream, unnamedTypeDeclsStream, wrapperStream, typesHandler, forStubHeader);
projectContext, sourceFilePath, externalStream, internalStream, unnamedTypeDeclsStream, wrapperStream,
typesHandler, forStubHeader, externFromStub);
finder = std::make_unique<clang::ast_matchers::MatchFinder>();
finder->addMatcher(Matchers::anyToplevelDeclarationMatcher, fetcherInstance.get());
return clang::tooling::newFrontendActionFactory(finder.get());
}

SourceToHeaderRewriter::SourceDeclarations
SourceToHeaderRewriter::generateSourceDeclarations(const fs::path &sourceFilePath, bool forStubHeader) {
SourceToHeaderRewriter::generateSourceDeclarations(const fs::path &sourceFilePath, bool forStubHeader, bool externFromStub) {
std::string externalDeclarations;
llvm::raw_string_ostream externalStream(externalDeclarations);
std::string internalDeclarations;
llvm::raw_string_ostream internalStream(internalDeclarations);
std::string unnamedTypeDeclarations;
llvm::raw_string_ostream unnamedTypeDeclsStream(unnamedTypeDeclarations);

auto factory = createFactory(&externalStream, &internalStream, &unnamedTypeDeclsStream, nullptr, sourceFilePath, forStubHeader);
auto factory = createFactory(&externalStream, &internalStream, &unnamedTypeDeclsStream, nullptr, sourceFilePath, forStubHeader, externFromStub);

if (CollectionUtils::containsKey(*structsToDeclare, sourceFilePath)) {
std::stringstream newContentStream;
Expand All @@ -72,9 +74,10 @@ SourceToHeaderRewriter::generateSourceDeclarations(const fs::path &sourceFilePat


std::string SourceToHeaderRewriter::generateTestHeader(const fs::path &sourceFilePath,
const Tests &test) {
const Tests &test,
bool externFromStub) {
MEASURE_FUNCTION_EXECUTION_TIME
auto sourceDeclarations = generateSourceDeclarations(sourceFilePath, false);
auto sourceDeclarations = generateSourceDeclarations(sourceFilePath, false, externFromStub);

if (Paths::isCXXFile(sourceFilePath)) {
auto sourceFileToInclude = sourceFilePath;
Expand Down Expand Up @@ -112,7 +115,7 @@ std::string SourceToHeaderRewriter::generateStubHeader(const fs::path &sourceFil
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);
auto sourceDeclarations = generateSourceDeclarations(sourceFilePath, true, false);
long long creationTime = TimeUtils::convertFileToSystemClock(fs::file_time_type::clock::now())
.time_since_epoch()
.count();
Expand All @@ -133,19 +136,28 @@ std::string SourceToHeaderRewriter::generateWrapper(const fs::path &sourceFilePa
}
std::string result;
llvm::raw_string_ostream wrapperStream(result);
auto factory = createFactory(nullptr, nullptr, nullptr, &wrapperStream, sourceFilePath, false);
auto factory = createFactory(
nullptr, nullptr, nullptr, &wrapperStream,
sourceFilePath, false, false);
clangToolRunner.run(sourceFilePath, factory.get());
wrapperStream.flush();
return result;
}

void SourceToHeaderRewriter::generateTestHeaders(tests::TestsMap &tests,
ProgressWriter const *progressWriter) {
const StubGen &stubGen,
const CollectionUtils::MapFileTo<fs::path> &selectedTargets,
ProgressWriter const *progressWriter) {
std::string logMessage = "Generating headers for tests";
LOG_S(DEBUG) << logMessage;
ExecUtils::doWorkWithProgress(tests, progressWriter, logMessage, [this](auto &it) {
ExecUtils::doWorkWithProgress(tests, progressWriter, logMessage,
[this, &stubGen, &selectedTargets](auto &it) {
fs::path const &sourceFilePath = it.first;
tests::Tests &test = const_cast<tests::Tests &>(it.second);
test.headerCode = generateTestHeader(sourceFilePath, test);
auto &test = const_cast<tests::Tests &>(it.second);
auto iterator = selectedTargets.find(sourceFilePath);
bool externFromStub =
iterator != selectedTargets.end() &&
CollectionUtils::contains(stubGen.getStubSources(iterator->second), sourceFilePath);
test.headerCode = generateTestHeader(sourceFilePath, test, externFromStub);
});
}
13 changes: 9 additions & 4 deletions server/src/clang-utils/SourceToHeaderRewriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "building/BuildDatabase.h"
#include "fetchers/Fetcher.h"
#include "fetchers/FetcherUtils.h"
#include "stubs/StubGen.h"
#include "utils/FileSystemUtils.h"

#include <clang/AST/RecursiveASTVisitor.h>
Expand Down Expand Up @@ -35,7 +36,8 @@ class SourceToHeaderRewriter {
llvm::raw_ostream *unnamedTypeDeclsStream,
llvm::raw_ostream *wrapperStream,
fs::path sourceFilePath,
bool forStubHeader);
bool forStubHeader,
bool externFromStub);

public:
struct SourceDeclarations {
Expand All @@ -53,15 +55,18 @@ class SourceToHeaderRewriter {
fs::path serverBuildDir,
const types::TypesHandler &typesHandler);

SourceDeclarations generateSourceDeclarations(const fs::path &sourceFilePath, bool forStubHeader);
SourceDeclarations generateSourceDeclarations(const fs::path &sourceFilePath, bool forStubHeader, bool externFromStub);

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

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

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

void generateTestHeaders(tests::TestsMap &tests, ProgressWriter const *progressWriter);
void generateTestHeaders(tests::TestsMap &tests,
const StubGen &stubGen,
const CollectionUtils::MapFileTo<fs::path> &selectedTargets,
ProgressWriter const *progressWriter);
};


Expand Down
4 changes: 4 additions & 0 deletions server/src/printers/SourceWrapperPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ namespace printer {

strInclude(Include(false, projectDirRelativeToWrapperFile / sourcePathRelativeToProjectDir));

ss << "#pragma GCC visibility push (default)" << NL;

ss << wrapperDefinitions;

ss << "#pragma GCC visibility pop" << NL;

FileSystemUtils::writeToFile(wrapperFilePath, ss.str());
}
}
2 changes: 2 additions & 0 deletions server/src/printers/StubsPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Stubs printer::StubsPrinter::genStubFile(const tests::Tests &tests,
ss << "#endif" << NL;
strInclude(Paths::sourcePathToHeaderInclude(tests.sourceFilePath));
ss << NL;
ss << "#pragma GCC visibility push (default)" << NL;
strDefine(PrinterUtils::C_NULL, "((void*)0)") << NL;
for (const auto &[_, method] : tests.methods) {
auto methodCopy = method;
Expand Down Expand Up @@ -66,6 +67,7 @@ Stubs printer::StubsPrinter::genStubFile(const tests::Tests &tests,
};
ss << NL;
}
ss << "#pragma GCC visibility pop" << NL;
stubFile.code = ss.str();
return stubFile;
}
2 changes: 1 addition & 1 deletion server/src/stubs/StubGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
StubGen::StubGen(BaseTestGen &testGen) : testGen(testGen) {
}

CollectionUtils::FileSet StubGen::getStubSources(const fs::path &target) {
CollectionUtils::FileSet StubGen::getStubSources(const fs::path &target) const {
if (!testGen.needToBeMocked() || !testGen.settingsContext.useStubs) {
return {};
}
Expand Down
2 changes: 1 addition & 1 deletion server/src/stubs/StubGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class StubGen {
public:
explicit StubGen(BaseTestGen &testGen);

CollectionUtils::FileSet getStubSources(const fs::path &target);
CollectionUtils::FileSet getStubSources(const fs::path &target) const;

CollectionUtils::FileSet
findStubFilesBySignatures(const std::vector<tests::Tests::MethodDescription> &signatures);
Expand Down