Skip to content

Commit

Permalink
Add support of arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
IDKWNTCMF committed Jul 31, 2023
1 parent 9d47456 commit 53760c6
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 15 deletions.
26 changes: 12 additions & 14 deletions server/src/printers/Printer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ namespace printer {
types::PointerUsage usage,
std::optional<std::string_view> value,
std::optional<uint64_t> alignment,
bool complete) {
bool complete,
const types::TypesHandler* typesHandler,
bool namespaceNeeded) {
auto baseType = type.baseType();
std::string arrayName{ name.data(), name.length() };

Expand All @@ -133,6 +135,10 @@ namespace printer {
arrayName = NameDecorator::decorate(arrayName);
}

if (namespaceNeeded && typesHandler) {
PrinterUtils::decorateTypeNameWithNamespace(type.baseTypeObj().getId(), baseType, typesHandler);
}

ss << LINE_INDENT();
printAlignmentIfExists(alignment);
ss << baseType << " " << arrayName;
Expand Down Expand Up @@ -679,20 +685,12 @@ namespace printer {
for (const auto &var : vars) {
ss << "extern ";
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, typesHandler, namespaceNeeded);
} else {
std::string typeName = var.type.mTypeName();
if (namespaceNeeded) {
uint64_t id = var.type.baseTypeObj().getId();
if (typesHandler->isEnum(id)) {
std::string oldName = typesHandler->getEnumInfo(id).name;
std::string newName = StringUtils::stringFormat("%s::%s", PrinterUtils::TEST_NAMESPACE, oldName);
StringUtils::replaceFirst(typeName, oldName, newName);
} else if (typesHandler->isStructLike(id)) {
std::string oldName = typesHandler->getStructInfo(id).name;
std::string newName = StringUtils::stringFormat("%s::%s", PrinterUtils::TEST_NAMESPACE, oldName);
StringUtils::replaceFirst(typeName, oldName, newName);
}
types::TypeName typeName = var.type.mTypeName();
if (namespaceNeeded && typesHandler) {
PrinterUtils::decorateTypeNameWithNamespace(var.type.baseTypeObj().getId(), typeName, typesHandler);
}
strDeclareVar(typeName, var.varName);
}
Expand Down
4 changes: 3 additions & 1 deletion server/src/printers/Printer.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ namespace printer {
types::PointerUsage usage,
std::optional<std::string_view> value = std::nullopt,
std::optional<uint64_t> alignment = std::nullopt,
bool complete = true);
bool complete = true,
const types::TypesHandler* typesHandler = nullptr,
bool namespaceNeeded = false);

Stream strDeclareSetOfVars(const std::set<Tests::TypeAndVarName> &vars,
const types::TypesHandler* typesHandler,
Expand Down
1 change: 1 addition & 0 deletions server/src/printers/TestsPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ void TestsPrinter::joinToFinalCode(Tests &tests, const fs::path& generatedHeader
ss << NL;

strDeclareSetOfVars(tests.externVariables, typesHandler, true);
ss << NL;

ss << "namespace " << PrinterUtils::TEST_NAMESPACE << " {\n";

Expand Down
18 changes: 18 additions & 0 deletions server/src/utils/PrinterUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,24 @@ namespace PrinterUtils {
return "enum " + getReturnMangledTypeName(methodName);
}

void decorateBaseTypeNameWithNamespace(types::TypeName &typeName,
const types::TypeName &baseTypeName) {
types::TypeName newBaseTypeName =
StringUtils::stringFormat("%s::%s", PrinterUtils::TEST_NAMESPACE, baseTypeName);
StringUtils::replaceFirst(typeName, baseTypeName, newBaseTypeName);
}

void decorateTypeNameWithNamespace(uint64_t id, types::TypeName &typeName,
const types::TypesHandler* typesHandler) {
if (typesHandler->isEnum(id)) {
types::TypeName baseTypeName = typesHandler->getEnumInfo(id).name;
decorateBaseTypeNameWithNamespace(typeName, baseTypeName);
} else if (typesHandler->isStructLike(id)) {
types::TypeName baseTypeName = typesHandler->getStructInfo(id).name;
decorateBaseTypeNameWithNamespace(typeName, baseTypeName);
}
}

std::string getEqualString(const std::string& lhs, const std::string& rhs) {
return StringUtils::stringFormat("%s == %s", lhs, rhs);
}
Expand Down
5 changes: 5 additions & 0 deletions server/src/utils/PrinterUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ namespace PrinterUtils {
std::string getReturnMangledTypeName(const std::string& methodName);
std::string getEnumReturnMangledTypeName(const std::string& methodName);

void decorateBaseTypeNameWithNamespace(types::TypeName &typeName,
const types::TypeName &baseTypeName);
void decorateTypeNameWithNamespace(uint64_t id, types::TypeName &typeName,
const types::TypesHandler* typesHandler);

std::string getEqualString(const std::string &lhs, const std::string &rhs);
std::string getDereferencePointer(const std::string &name, const size_t depth);
std::string getExpectedVarName(const std::string &varName);
Expand Down

0 comments on commit 53760c6

Please sign in to comment.