From fb0a70a8317b92e2a77af147512bfbd528842874 Mon Sep 17 00:00:00 2001 From: ChristianS99 Date: Sat, 16 May 2020 22:57:37 +0200 Subject: [PATCH] Fix #101: sanitize names of namespaces/methods/signals/properties/arguments (#102) - add a list of c++ keywords and common defines - sanitize names so that there are no functions/args with names that are reserved in c++ Co-authored-by: Christian Schneider --- tools/xml2cpp-codegen/AdaptorGenerator.cpp | 15 +-- tools/xml2cpp-codegen/BaseGenerator.cpp | 10 +- tools/xml2cpp-codegen/ProxyGenerator.cpp | 8 +- tools/xml2cpp-codegen/generator_utils.cpp | 9 ++ tools/xml2cpp-codegen/generator_utils.h | 2 + tools/xml2cpp-codegen/reserved_names.h | 104 +++++++++++++++++++++ 6 files changed, 135 insertions(+), 13 deletions(-) create mode 100644 tools/xml2cpp-codegen/reserved_names.h diff --git a/tools/xml2cpp-codegen/AdaptorGenerator.cpp b/tools/xml2cpp-codegen/AdaptorGenerator.cpp index 0c94c3c..39b28d9 100644 --- a/tools/xml2cpp-codegen/AdaptorGenerator.cpp +++ b/tools/xml2cpp-codegen/AdaptorGenerator.cpp @@ -164,6 +164,7 @@ std::tuple AdaptorGenerator::processMethods(const Node for (const auto& method : methods) { auto methodName = method->get("name"); + auto methodNameSafe = mangle_name(methodName); auto annotations = getAnnotations(*method); bool async{false}; @@ -219,7 +220,7 @@ std::tuple AdaptorGenerator::processMethods(const Node << "[this](" << (async ? "sdbus::Result<" + outArgsToType(outArgs, true) + ">&& result" + (argTypeStr.empty() ? "" : ", ") : "") << argTypeStr - << "){ " << (async ? "" : "return ") << "this->" << methodName << "(" + << "){ " << (async ? "" : "return ") << "this->" << methodNameSafe << "(" << (async ? "std::move(result)"s + (argTypeStr.empty() ? "" : ", ") : "") << argStr << "); })" << annotationRegistration << ";" << endl; @@ -227,7 +228,7 @@ std::tuple AdaptorGenerator::processMethods(const Node declarationSS << tab << "virtual " << (async ? "void" : outArgsToType(outArgs)) - << " " << methodName + << " " << methodNameSafe << "(" << (async ? "sdbus::Result<" + outArgsToType(outArgs, true) + ">&& result" + (argTypeStr.empty() ? "" : ", ") : "") << argTypeStr @@ -279,6 +280,7 @@ std::tuple AdaptorGenerator::processSignals(const Node auto nameWithCapFirstLetter = name; nameWithCapFirstLetter[0] = std::toupper(nameWithCapFirstLetter[0]); + nameWithCapFirstLetter = mangle_name(nameWithCapFirstLetter); signalMethodSS << tab << "void emit" << nameWithCapFirstLetter << "(" << argTypeStr << ")" << endl << tab << "{" << endl @@ -305,6 +307,7 @@ std::tuple AdaptorGenerator::processProperties(const N for (const auto& property : properties) { auto propertyName = property->get("name"); + auto propertyNameSafe = mangle_name(propertyName); auto propertyAccess = property->get("access"); auto propertySignature = property->get("type"); @@ -336,23 +339,23 @@ std::tuple AdaptorGenerator::processProperties(const N if (propertyAccess == "read" || propertyAccess == "readwrite") { - registrationSS << ".withGetter([this](){ return this->" << propertyName << "(); })"; + registrationSS << ".withGetter([this](){ return this->" << propertyNameSafe << "(); })"; } if (propertyAccess == "readwrite" || propertyAccess == "write") { registrationSS << ".withSetter([this](" << propertyTypeArg << ")" - "{ this->" << propertyName << "(" << propertyArg << "); })"; + "{ this->" << propertyNameSafe << "(" << propertyArg << "); })"; } registrationSS << annotationRegistration; registrationSS << ";" << endl; if (propertyAccess == "read" || propertyAccess == "readwrite") - declarationSS << tab << "virtual " << propertyType << " " << propertyName << "() = 0;" << endl; + declarationSS << tab << "virtual " << propertyType << " " << propertyNameSafe << "() = 0;" << endl; if (propertyAccess == "readwrite" || propertyAccess == "write") - declarationSS << tab << "virtual void " << propertyName << "(" << propertyTypeArg << ") = 0;" << endl; + declarationSS << tab << "virtual void " << propertyNameSafe << "(" << propertyTypeArg << ") = 0;" << endl; } return std::make_tuple(registrationSS.str(), declarationSS.str()); diff --git a/tools/xml2cpp-codegen/BaseGenerator.cpp b/tools/xml2cpp-codegen/BaseGenerator.cpp index f9bf896..d95ec38 100644 --- a/tools/xml2cpp-codegen/BaseGenerator.cpp +++ b/tools/xml2cpp-codegen/BaseGenerator.cpp @@ -96,6 +96,7 @@ std::tuple BaseGenerator::generateNamespaces(const std::s { std::string nspace; getline(ss, nspace, '.'); + nspace = mangle_name(nspace); body << "namespace " << nspace << " {" << endl; ++count; } @@ -125,17 +126,18 @@ std::tuple BaseGenerator::ar { argName = "arg" + std::to_string(i); } + auto argNameSafe = mangle_name(argName); auto type = signature_to_type(arg->get("type")); argStringsSS << "\"" << argName << "\""; if (!async) { - argSS << argName; - argTypeSS << "const " << type << "& " << argName; + argSS << argNameSafe; + argTypeSS << "const " << type << "& " << argNameSafe; } else { - argSS << "std::move(" << argName << ")"; - argTypeSS << type << " " << argName; + argSS << "std::move(" << argNameSafe << ")"; + argTypeSS << type << " " << argNameSafe; } typeSS << type; } diff --git a/tools/xml2cpp-codegen/ProxyGenerator.cpp b/tools/xml2cpp-codegen/ProxyGenerator.cpp index 8cb2e2c..98b7007 100644 --- a/tools/xml2cpp-codegen/ProxyGenerator.cpp +++ b/tools/xml2cpp-codegen/ProxyGenerator.cpp @@ -135,6 +135,7 @@ std::tuple ProxyGenerator::processMethods(const Nodes& for (const auto& method : methods) { auto name = method->get("name"); + auto nameSafe = mangle_name(name); Nodes args = (*method)["arg"]; Nodes inArgs = args.select("direction" , "in"); Nodes outArgs = args.select("direction" , "out"); @@ -174,7 +175,7 @@ std::tuple ProxyGenerator::processMethods(const Nodes& std::tie(outArgStr, outArgTypeStr, std::ignore, std::ignore) = argsToNamesAndTypes(outArgs); const std::string realRetType = (async && !dontExpectReply ? "sdbus::PendingAsyncCall" : async ? "void" : retType); - definitionSS << tab << realRetType << " " << name << "(" << inArgTypeStr << ")" << endl + definitionSS << tab << realRetType << " " << nameSafe << "(" << inArgTypeStr << ")" << endl << tab << "{" << endl; if (!timeoutValue.empty()) @@ -260,6 +261,7 @@ std::string ProxyGenerator::processProperties(const Nodes& properties) const for (const auto& property : properties) { auto propertyName = property->get("name"); + auto propertyNameSafe = mangle_name(propertyName); auto propertyAccess = property->get("access"); auto propertySignature = property->get("type"); @@ -269,7 +271,7 @@ std::string ProxyGenerator::processProperties(const Nodes& properties) const if (propertyAccess == "read" || propertyAccess == "readwrite") { - propertySS << tab << propertyType << " " << propertyName << "()" << endl + propertySS << tab << propertyType << " " << propertyNameSafe << "()" << endl << tab << "{" << endl; propertySS << tab << tab << "return proxy_.getProperty(\"" << propertyName << "\")" ".onInterface(INTERFACE_NAME)"; @@ -278,7 +280,7 @@ std::string ProxyGenerator::processProperties(const Nodes& properties) const if (propertyAccess == "readwrite" || propertyAccess == "write") { - propertySS << tab << "void " << propertyName << "(" << propertyTypeArg << ")" << endl + propertySS << tab << "void " << propertyNameSafe << "(" << propertyTypeArg << ")" << endl << tab << "{" << endl; propertySS << tab << tab << "proxy_.setProperty(\"" << propertyName << "\")" ".onInterface(INTERFACE_NAME)" diff --git a/tools/xml2cpp-codegen/generator_utils.cpp b/tools/xml2cpp-codegen/generator_utils.cpp index cb8a403..0cb6e38 100644 --- a/tools/xml2cpp-codegen/generator_utils.cpp +++ b/tools/xml2cpp-codegen/generator_utils.cpp @@ -7,6 +7,7 @@ #include #include "generator_utils.h" +#include "reserved_names.h" std::string underscorize(const std::string& str) @@ -142,3 +143,11 @@ std::string signature_to_type(const std::string& signature) _parse_signature(signature, type, i); return type; } + +std::string mangle_name(const std::string& name) +{ + if (reserved_names.find(name) != reserved_names.end()) + return name + "_"; + else + return name; +} diff --git a/tools/xml2cpp-codegen/generator_utils.h b/tools/xml2cpp-codegen/generator_utils.h index 5b7b4ef..2c8162d 100644 --- a/tools/xml2cpp-codegen/generator_utils.h +++ b/tools/xml2cpp-codegen/generator_utils.h @@ -19,4 +19,6 @@ std::string underscorize(const std::string& str); constexpr const char* getHeaderComment() noexcept { return "\n/*\n * This file was automatically generated by sdbus-c++-xml2cpp; DO NOT EDIT!\n */\n\n"; } +std::string mangle_name (const std::string& name); + #endif //__SDBUSCPP_TOOLS_GENERATOR_UTILS_H diff --git a/tools/xml2cpp-codegen/reserved_names.h b/tools/xml2cpp-codegen/reserved_names.h new file mode 100644 index 0000000..d1c7b61 --- /dev/null +++ b/tools/xml2cpp-codegen/reserved_names.h @@ -0,0 +1,104 @@ +#include + +static const std::set reserved_names = { + // c++ keywords taken from https://en.cppreference.com/w/cpp/keyword + "alignas", + "alignof", + "and", + "and_eq", + "asm", + "atomic_cancel", + "atomic_commit", + "atomic_noexcept", + "auto", + "bitand", + "bitor", + "bool", + "break", + "case", + "catch", + "char", + "char8_t", + "char16_t", + "char32_t", + "class", + "compl", + "concept", + "const", + "consteval", + "constexpr", + "constinit", + "const_cast", + "continue", + "co_await", + "co_return", + "co_yield", + "decltype", + "default", + "delete", + "do", + "double", + "dynamic_cast", + "else", + "enum", + "explicit", + "export", + "extern", + "false", + "float", + "for", + "friend", + "goto", + "if", + "inline", + "int", + "long", + "mutable", + "namespace", + "new", + "noexcept", + "not", + "not_eq", + "nullptr", + "operator", + "or", + "or_eq", + "private", + "protected", + "public", + "reflexpr", + "register", + "reinterpret_cast", + "requires", + "return", + "short", + "signed", + "sizeof", + "static", + "static_assert", + "static_cast", + "struct", + "switch", + "synchronized", + "template", + "this", + "thread_local", + "throw", + "true", + "try", + "typedef", + "typeid", + "typename", + "union", + "unsigned", + "using", + "virtual", + "void", + "volatile", + "wchar_t", + "while", + "xor", + "xor_eq", + // common defines + "NULL" +};