From fe132ff7135824da58b1f566ead1bdb6fb991d94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A4fele=2C=20Philipp?= Date: Thu, 4 Apr 2024 16:08:05 +0200 Subject: [PATCH] erpcgen/C++: Add namespace and remove same name typedefs - Only typdefs for same name structs and unions are removed - Constant variable declarations are not namespaced --- erpcgen/src/CGenerator.cpp | 3 +++ erpcgen/src/templates/c_common_header.template | 9 ++++++++- test/test_annotations/external.h | 8 ++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/erpcgen/src/CGenerator.cpp b/erpcgen/src/CGenerator.cpp index 16b86311..e64a004b 100644 --- a/erpcgen/src/CGenerator.cpp +++ b/erpcgen/src/CGenerator.cpp @@ -765,6 +765,8 @@ void CGenerator::makeAliasesTemplateData() DataType *elementDataType = aliasType->getElementType(); setTemplateComments(aliasType, aliasInfo); + aliasInfo["isSameNameTypedef"] = false; + if (elementDataType->getName() != "") { string realType; @@ -785,6 +787,7 @@ void CGenerator::makeAliasesTemplateData() if (elementDataType->getName() == aliasType->getName() || getOutputName(elementDataType, false) == aliasType->getName()) { + aliasInfo["isSameNameTypedef"] = true; if (elementDataType->isStruct()) { realType = "struct " + realType; diff --git a/erpcgen/src/templates/c_common_header.template b/erpcgen/src/templates/c_common_header.template index 9e203662..7c50568f 100644 --- a/erpcgen/src/templates/c_common_header.template +++ b/erpcgen/src/templates/c_common_header.template @@ -40,6 +40,9 @@ extern "C" #if !defined(ERPC_TYPE_DEFINITIONS{$scopeNamePrefix}{$scopeNameC}) #define ERPC_TYPE_DEFINITIONS{$scopeNamePrefix}{$scopeNameC} +{% if not cCommonHeaderFile %} +{$fillNamespaceBegin()} +{% endif -- not cCommonHeaderFile %} {% if not empty(enums) %} // Enumerators data types declarations @@ -67,7 +70,9 @@ typedef {$alias.unnamedType} {% endfor -- alias.unnamed.members %} } {$alias.unnamedName}; {% else -- alias.typenameName %} +{% if (cCommonHeaderFile) || (not alias.isSameNameTypedef) %} typedef {$alias.typenameName};{$alias.ilComment} +{% endif -- (cCommonHeaderFile) || (not alias.isSameNameTypedef) %} {% endif -- alias.typenameName %} {% endfor -- aliases %} {% endif -- aliases %} @@ -95,8 +100,10 @@ union {$us.name} {% endif -- us.type == "union/struct" %} {% endif -- !us.isExternal %} {% endfor -- symbols %} - {% endif -- nonExternalStruct || nonExternalUnion %} +{% if not cCommonHeaderFile %} +{$fillNamespaceEnd()} +{% endif -- not cCommonHeaderFile %} {% if not empty(consts) %} // Constant variable declarations diff --git a/test/test_annotations/external.h b/test/test_annotations/external.h index f6d7bef0..aaaa8c80 100644 --- a/test/test_annotations/external.h +++ b/test/test_annotations/external.h @@ -16,6 +16,10 @@ #include +#ifdef __cplusplus +namespace erpcShim { +#endif + // Enumerators data types declarations typedef enum myEnum { @@ -36,6 +40,10 @@ struct fooStruct float y; }; +#ifdef __cplusplus +} // namespace erpcShim +#endif + // Constant variable declarations const int32_t i = 4;