Skip to content

Commit c5b8149

Browse files
author
Vladislav Kalugin
committed
refactoring after review 8
1 parent 65386a8 commit c5b8149

File tree

7 files changed

+49
-33
lines changed

7 files changed

+49
-33
lines changed

server/src/Server.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ Status Server::TestsGenServiceImpl::ProcessBaseTestRequest(BaseTestGen &testGen,
210210
testGen.progressWriter->writeProgress("Generating stub files", 0.0);
211211
StubGen stubGen(testGen);
212212

213-
Synchronizer synchronizer(&testGen, &stubGen, &sizeContext);
213+
Synchronizer synchronizer(&testGen, &sizeContext);
214214
synchronizer.synchronize(typesHandler);
215215

216216
std::shared_ptr<LineInfo> lineInfo = nullptr;
@@ -547,7 +547,6 @@ Status Server::TestsGenServiceImpl::ProcessProjectStubsRequest(BaseTestGen *test
547547
StubsWriter *stubsWriter) {
548548
types::TypesHandler::SizeContext sizeContext;
549549
types::TypesHandler typesHandler{testGen->types, sizeContext};
550-
StubGen stubGen(*testGen);
551550

552551
static std::string logMessage = "Traversing sources AST tree and fetching declarations.";
553552
LOG_S(DEBUG) << logMessage;
@@ -557,7 +556,7 @@ Status Server::TestsGenServiceImpl::ProcessProjectStubsRequest(BaseTestGen *test
557556
testGen->compileCommandsJsonPath, false);
558557

559558
fetcher.fetchWithProgress(testGen->progressWriter, logMessage);
560-
Synchronizer synchronizer(testGen, &stubGen, &sizeContext);
559+
Synchronizer synchronizer(testGen, &sizeContext);
561560
synchronizer.synchronize(typesHandler);
562561
stubsWriter->writeResponse(testGen->synchronizedStubs, testGen->projectContext.testDirPath);
563562
return Status::OK;

server/src/Synchronizer.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ bool StubOperator::isHeader() const {
4040
}
4141

4242
Synchronizer::Synchronizer(BaseTestGen *testGen,
43-
StubGen const *stubGen,
4443
types::TypesHandler::SizeContext *sizeContext)
45-
: testGen(testGen), stubGen(stubGen), sizeContext(sizeContext) {
44+
: testGen(testGen), sizeContext(sizeContext) {
4645
}
4746

4847
bool Synchronizer::isProbablyOutdated(const fs::path &srcFilePath) const {

server/src/Synchronizer.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ class StubOperator {
2121

2222
class Synchronizer {
2323
BaseTestGen *const testGen;
24-
StubGen const *const stubGen;
2524
types::TypesHandler::SizeContext *sizeContext;
2625

2726
[[nodiscard]] CollectionUtils::FileSet getOutdatedSourcePaths() const;
@@ -53,7 +52,7 @@ class Synchronizer {
5352

5453
static CollectionUtils::FileSet dropHeaders(const CollectionUtils::FileSet &files);
5554

56-
Synchronizer(BaseTestGen *testGen, StubGen const *stubGen, types::TypesHandler::SizeContext *sizeContext);
55+
Synchronizer(BaseTestGen *testGen, types::TypesHandler::SizeContext *sizeContext);
5756

5857
void synchronize(const types::TypesHandler &typesHandler);
5958

server/src/building/Linker.cpp

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -410,32 +410,12 @@ static const std::string STUB_BITCODE_FILES_NAME = "STUB_BITCODE_FILES";
410410
static const std::string STUB_BITCODE_FILES = "$(STUB_BITCODE_FILES)";
411411

412412
Result<CollectionUtils::FileSet> Linker::generateStubsMakefile(
413-
const fs::path &root, const fs::path &outputFile, const fs::path &stubsMakefile) const {
414-
ShellExecTask::ExecutionParameters nmCommand(
415-
Paths::getLLVMnm(),
416-
{ "--print-file-name", "--undefined-only", "--just-symbol-name", outputFile });
417-
auto [out, status, _] = ShellExecTask::runShellCommandTask(nmCommand, testGen.serverBuildDir);
418-
if (status != 0) {
419-
std::string errorMessage =
420-
StringUtils::stringFormat("llvm-nm on %s failed: %s", outputFile, out);
421-
LOG_S(ERROR) << errorMessage;
422-
return errorMessage;
413+
const fs::path &root, const fs::path &outputFile, const fs::path &stubsMakefile) const {
414+
auto result = StubGen(testGen).getStubSetForObject(outputFile);
415+
if (!result.isSuccess()) {
416+
return result;
423417
}
424-
auto symbols =
425-
CollectionUtils::transform(StringUtils::split(out, '\n'), [](std::string const &line) {
426-
return StringUtils::splitByWhitespaces(line).back();
427-
});
428-
CollectionUtils::erase_if(symbols, [](std::string const &symbol) {
429-
return StringUtils::startsWith(symbol, "__ubsan") ||
430-
StringUtils::startsWith(symbol, "klee_");
431-
});
432-
auto signatures = CollectionUtils::transform(symbols, [](std::string const &symbol) {
433-
Tests::MethodDescription methodDescription;
434-
methodDescription.name = symbol;
435-
return methodDescription;
436-
});
437-
auto rootLinkUnitInfo = testGen.getTargetBuildDatabase()->getClientLinkUnitInfo(root);
438-
auto stubsSet = StubGen(testGen).findStubFilesBySignatures(signatures);
418+
auto stubsSet = result.getOpt().value();
439419
printer::DefaultMakefilePrinter makefilePrinter;
440420
auto bitcodeStubFiles = CollectionUtils::transformTo<std::vector<fs::path>>(
441421
Synchronizer::dropHeaders(stubsSet), [this, &makefilePrinter](const fs::path &stubPath) {

server/src/stubs/StubGen.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
#include "clang-utils/SourceToHeaderRewriter.h"
99
#include "printers/CCJsonPrinter.h"
1010
#include "streams/stubs/StubsWriter.h"
11-
11+
#include "loguru.h"
12+
#include "environment/EnvironmentPaths.h"
1213

1314
StubGen::StubGen(BaseTestGen &testGen) : testGen(testGen) {
1415
}
@@ -99,3 +100,30 @@ bool StubGen::cmpMethodsDecl(const Tests::MethodDescription &decl1,
99100
}
100101
return true;
101102
}
103+
104+
Result<CollectionUtils::FileSet> StubGen::getStubSetForObject(const fs::path &objectFilePath) {
105+
ShellExecTask::ExecutionParameters nmCommand(
106+
Paths::getLLVMnm(),
107+
{ "--print-file-name", "--undefined-only", "--just-symbol-name", objectFilePath });
108+
auto [out, status, _] = ShellExecTask::runShellCommandTask(nmCommand, testGen.serverBuildDir);
109+
if (status != 0) {
110+
std::string errorMessage =
111+
StringUtils::stringFormat("llvm-nm on %s failed: %s", objectFilePath, out);
112+
LOG_S(ERROR) << errorMessage;
113+
return errorMessage;
114+
}
115+
auto symbols =
116+
CollectionUtils::transform(StringUtils::split(out, '\n'), [](std::string const &line) {
117+
return StringUtils::splitByWhitespaces(line).back();
118+
});
119+
CollectionUtils::erase_if(symbols, [](std::string const &symbol) {
120+
return StringUtils::startsWith(symbol, "__ubsan") ||
121+
StringUtils::startsWith(symbol, "klee_");
122+
});
123+
auto signatures = CollectionUtils::transform(symbols, [](std::string const &symbol) {
124+
Tests::MethodDescription methodDescription;
125+
methodDescription.name = symbol;
126+
return methodDescription;
127+
});
128+
return findStubFilesBySignatures(signatures);
129+
}

server/src/stubs/StubGen.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class StubGen {
2121
static tests::Tests mergeSourceFileIntoStub(const tests::Tests &methodDescription,
2222
const tests::Tests &srcFile);
2323

24+
Result<CollectionUtils::FileSet> getStubSetForObject(const fs::path &objectFilePath);
2425
private:
2526
BaseTestGen &testGen;
2627

server/test/framework/Stub_Tests.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,16 @@ namespace {
168168
Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get());
169169
ASSERT_TRUE(status.ok()) << status.error_message();
170170
EXPECT_EQ(testUtils::getNumberOfTests(testGen.tests), 2);
171+
172+
const fs::path objectFile = testGen.getClientCompilationUnitInfo(foreign_bar_c)->getOutputFile();
173+
auto result = StubGen(testGen).getStubSetForObject(objectFile);
174+
ASSERT_TRUE(result.isSuccess());
175+
auto stubCandidates = {calc_sum_c};
176+
auto expectedStubFiles = CollectionUtils::transformTo<CollectionUtils::FileSet>(
177+
stubCandidates, [&testGen](fs::path const &path) {
178+
return Paths::sourcePathToStubPath(testGen.projectContext, path);
179+
});
180+
EXPECT_EQ(expectedStubFiles, result.getOpt().value());
171181
}
172182

173183
TEST_F(Stub_Test, File_Tests_With_Stubs) {

0 commit comments

Comments
 (0)