From ca9ea6af3d3428cbd2395da6af24055e48a19ae0 Mon Sep 17 00:00:00 2001 From: Vladislav Kalugin Date: Wed, 25 Oct 2023 15:29:23 +0300 Subject: [PATCH] Fix write "extern" for stubs (#651) --- server/src/printers/Printer.cpp | 28 +++++++++++++++++++++------- server/src/printers/Printer.h | 9 ++++++++- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/server/src/printers/Printer.cpp b/server/src/printers/Printer.cpp index 12f7b794..a9df671d 100644 --- a/server/src/printers/Printer.cpp +++ b/server/src/printers/Printer.cpp @@ -125,7 +125,8 @@ namespace printer { types::PointerUsage usage, std::optional value, std::optional alignment, - bool complete) { + bool complete, + ExternType externType) { auto baseType = type.baseType(); std::string arrayName{ name.data(), name.length() }; @@ -135,6 +136,18 @@ namespace printer { } 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); ss << baseType << " " << arrayName; std::vector sizes = type.arraysSizes(usage); @@ -539,15 +552,16 @@ namespace printer { } } - void printer::Printer::writeExternForSymbolicStubs(const Tests::MethodDescription& testMethod) { - std::unordered_map symbolicNamesToTypesMap; - for (const auto& testCase: testMethod.testCases) { + void printer::Printer::writeExternForSymbolicStubs(const Tests::MethodDescription &testMethod) { + std::unordered_map 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(); + symbolicNamesToTypesMap[testCase.stubValues[i].name] = testCase.stubValuesTypes[i].type; } } - for (const auto& [name, type]: symbolicNamesToTypesMap) { - strDeclareVar("extern \"C\" " + type, name); + for (const auto &[name, type]: symbolicNamesToTypesMap) { + strDeclareArrayVar(type, name, types::PointerUsage::PARAMETER, std::nullopt, std::nullopt, true, + ExternType::C); } } diff --git a/server/src/printers/Printer.h b/server/src/printers/Printer.h index b9da7a14..f0b6f868 100644 --- a/server/src/printers/Printer.h +++ b/server/src/printers/Printer.h @@ -81,12 +81,19 @@ namespace printer { Stream strDeclareAbsError(SRef name); + enum ExternType { + NONE, + SAME_LANGUAGE, + C + }; + Stream strDeclareArrayVar(const types::Type& type, std::string_view name, types::PointerUsage usage, std::optional value = std::nullopt, std::optional alignment = std::nullopt, - bool complete = true); + bool complete = true, + ExternType externType = ExternType::NONE); Stream strDeclareSetOfVars(const std::set &vars);