From fcf557301d10a41d1f3912f9fb2660044fa2a345 Mon Sep 17 00:00:00 2001 From: Vladislav Kalugin Date: Fri, 17 May 2024 13:26:13 +0300 Subject: [PATCH] Fix extern --- server/src/Synchronizer.h | 2 +- server/src/coverage/LlvmCoverageTool.cpp | 3 +-- .../GlobalVariableUsageMatchCallback.cpp | 2 +- server/src/printers/KleePrinter.cpp | 2 +- server/src/printers/Printer.cpp | 25 ++++++++++++++++--- server/src/printers/Printer.h | 17 +++++++------ server/src/printers/TestsPrinter.cpp | 2 +- 7 files changed, 35 insertions(+), 18 deletions(-) diff --git a/server/src/Synchronizer.h b/server/src/Synchronizer.h index 2449bfdd7..398242f67 100644 --- a/server/src/Synchronizer.h +++ b/server/src/Synchronizer.h @@ -33,7 +33,7 @@ class Synchronizer { [[nodiscard]] bool isProbablyOutdatedWrappers(const fs::path &srcFilePath) const; - [[nodiscard]] bool removeStubIfSourceAbsent(const StubOperator &stub) const; + bool removeStubIfSourceAbsent(const StubOperator &stub) const; void synchronizeStubs(std::unordered_set &outdatedStubs, const types::TypesHandler &typesHandler); diff --git a/server/src/coverage/LlvmCoverageTool.cpp b/server/src/coverage/LlvmCoverageTool.cpp index d17adcdcc..ac0571ed4 100644 --- a/server/src/coverage/LlvmCoverageTool.cpp +++ b/server/src/coverage/LlvmCoverageTool.cpp @@ -112,8 +112,7 @@ LlvmCoverageTool::getCoverageCommands(const std::vector &testsToLaunch for (const std::string &objectFile : objectFiles) { if (firstBIN) { firstBIN = false; - } - else { + } else { exportArguments.emplace_back("-object"); } exportArguments.emplace_back(objectFile); diff --git a/server/src/fetchers/GlobalVariableUsageMatchCallback.cpp b/server/src/fetchers/GlobalVariableUsageMatchCallback.cpp index 34053b6c6..b02902ff5 100644 --- a/server/src/fetchers/GlobalVariableUsageMatchCallback.cpp +++ b/server/src/fetchers/GlobalVariableUsageMatchCallback.cpp @@ -67,7 +67,7 @@ void GlobalVariableUsageMatchCallback::handleUsage(const clang::FunctionDecl *fu const std::string usedParamTypeString = varDecl->getType().getAsString(); types::Type paramType = types::Type(realParamType, usedParamTypeString, sourceManager); method.globalParams.emplace_back(paramType, usage.variableName, AlignmentFetcher::fetch(varDecl)); - if (varDecl->isExternC() && !varDecl->isKnownToBeDefined()) { + if (varDecl->isExternC() && !varDecl->hasDefinition()) { tests.externVariables.insert({paramType, usage.variableName}); } } diff --git a/server/src/printers/KleePrinter.cpp b/server/src/printers/KleePrinter.cpp index 70c3b32e0..c2fd66f37 100644 --- a/server/src/printers/KleePrinter.cpp +++ b/server/src/printers/KleePrinter.cpp @@ -158,7 +158,7 @@ fs::path KleePrinter::writeTmpKleeFile( return filter && forThisFunction && forThisClass; }); - strDeclareSetOfVars(tests.externVariables); + strDeclareSetOfExternVars(tests.externVariables); ss << printer::NL; for (const auto &[methodName, testMethod]: tests.methods) { diff --git a/server/src/printers/Printer.cpp b/server/src/printers/Printer.cpp index e254e0599..719846e89 100644 --- a/server/src/printers/Printer.cpp +++ b/server/src/printers/Printer.cpp @@ -88,8 +88,23 @@ namespace printer { std::optional initValue, std::optional alignment, bool complete, - size_t additionalPointersCount) { + size_t additionalPointersCount, + ExternType externType) { ss << LINE_INDENT(); + + switch (externType) { + case ExternType::C : + if (getLanguage() == utbot::Language::CXX) { + ss << "extern \"C\" "; + break; + } + case ExternType::SAME_LANGUAGE : + ss << "extern "; + break; + case ExternType::NONE : + break; + } + printAlignmentIfExists(alignment); auto additionalPointers = StringUtils::repeat("*", additionalPointersCount); if (needDecorate()) { @@ -685,12 +700,14 @@ namespace printer { ss << Copyright::GENERATED_C_CPP_FILE_HEADER << printer::NL; } - Printer::Stream Printer::strDeclareSetOfVars(const std::set &vars) { + Printer::Stream Printer::strDeclareSetOfExternVars(const std::set &vars) { for (const auto &var: vars) { if (var.type.isArray()) { - strDeclareArrayVar(var.type, var.varName, types::PointerUsage::KNOWN_SIZE); + strDeclareArrayVar(var.type, var.varName, types::PointerUsage::KNOWN_SIZE, std::nullopt, + std::nullopt, true, ExternType::C); } else { - strDeclareVar(var.type.mTypeName(), var.varName); + strDeclareVar(var.type.mTypeName(), var.varName, std::nullopt, + std::nullopt, true, 0, ExternType::C); } } return ss; diff --git a/server/src/printers/Printer.h b/server/src/printers/Printer.h index 73795d860..b72fbbe18 100644 --- a/server/src/printers/Printer.h +++ b/server/src/printers/Printer.h @@ -73,21 +73,22 @@ namespace printer { Stream strIfBound(SRef condition); + enum ExternType { + NONE, + SAME_LANGUAGE, + C + }; + Stream strDeclareVar(std::string_view type, std::string_view name, std::optional initValue = std::nullopt, std::optional alignment = std::nullopt, bool complete = true, - size_t additionalPointersCount = 0); + size_t additionalPointersCount = 0, + ExternType externType = ExternType::NONE); Stream strDeclareAbsError(SRef name); - enum ExternType { - NONE, - SAME_LANGUAGE, - C - }; - Stream strDeclareArrayVar(const types::Type &type, std::string_view name, types::PointerUsage usage, @@ -96,7 +97,7 @@ namespace printer { bool complete = true, ExternType externType = ExternType::NONE); - Stream strDeclareSetOfVars(const std::set &vars); + Stream strDeclareSetOfExternVars(const std::set &vars); Stream strAssignVar(std::string_view name, std::string_view value); diff --git a/server/src/printers/TestsPrinter.cpp b/server/src/printers/TestsPrinter.cpp index 7248ee515..677002824 100644 --- a/server/src/printers/TestsPrinter.cpp +++ b/server/src/printers/TestsPrinter.cpp @@ -64,7 +64,7 @@ void TestsPrinter::joinToFinalCode(Tests &tests, const fs::path& generatedHeader genHeaders(tests, generatedHeaderPath); ss << printer::NL; - strDeclareSetOfVars(tests.externVariables); + strDeclareSetOfExternVars(tests.externVariables); ss << "namespace " << PrinterUtils::TEST_NAMESPACE << " {\n";