You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by mg...@apache.org on 2024/06/14 09:37:51 UTC

(avro) branch main updated: AVRO-3990 [C++] Fix invalid code generation for union with reserved name (#2930)

This is an automated email from the ASF dual-hosted git repository.

mgrigorov pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/avro.git


The following commit(s) were added to refs/heads/main by this push:
     new 6aeb7b7aa AVRO-3990 [C++] Fix invalid code generation for union with reserved name (#2930)
6aeb7b7aa is described below

commit 6aeb7b7aa10ef721f3a16158900205ecab7174b1
Author: Gerrit Birkeland <ge...@gerritbirkeland.com>
AuthorDate: Fri Jun 14 03:37:45 2024 -0600

    AVRO-3990 [C++] Fix invalid code generation for union with reserved name (#2930)
---
 lang/c++/CMakeLists.txt                               |  5 +++--
 lang/c++/impl/avrogencpp.cc                           | 15 +++------------
 lang/c++/jsonschemas/cpp_reserved_words_union_typedef | 13 +++++++++++++
 lang/c++/test/AvrogencppTestReservedWords.cc          |  1 +
 4 files changed, 20 insertions(+), 14 deletions(-)

diff --git a/lang/c++/CMakeLists.txt b/lang/c++/CMakeLists.txt
index 522f24843..fb7f951e2 100644
--- a/lang/c++/CMakeLists.txt
+++ b/lang/c++/CMakeLists.txt
@@ -158,7 +158,7 @@ macro (gen file ns)
         COMMAND avrogencpp
             -p -
             -i ${CMAKE_CURRENT_SOURCE_DIR}/jsonschemas/${file}
-            -o ${file}.hh -n ${ns} -U
+            -o ${file}.hh -n ${ns}
         DEPENDS avrogencpp ${CMAKE_CURRENT_SOURCE_DIR}/jsonschemas/${file})
     add_custom_target (${file}_hh DEPENDS ${file}.hh)
 endmacro (gen)
@@ -179,6 +179,7 @@ gen (tree2 tr2)
 gen (crossref cr)
 gen (primitivetypes pt)
 gen (cpp_reserved_words cppres)
+gen (cpp_reserved_words_union_typedef cppres_union)
 
 add_executable (avrogencpp impl/avrogencpp.cc)
 target_link_libraries (avrogencpp avrocpp_s ${Boost_LIBRARIES} ${SNAPPY_LIBRARIES})
@@ -212,7 +213,7 @@ add_dependencies (AvrogencppTests bigrecord_hh bigrecord_r_hh bigrecord2_hh
     tweet_hh
     union_array_union_hh union_map_union_hh union_conflict_hh
     recursive_hh reuse_hh circulardep_hh tree1_hh tree2_hh crossref_hh
-    primitivetypes_hh empty_record_hh)
+    primitivetypes_hh empty_record_hh cpp_reserved_words_union_typedef_hh)
 
 include (InstallRequiredSystemLibraries)
 
diff --git a/lang/c++/impl/avrogencpp.cc b/lang/c++/impl/avrogencpp.cc
index 19e38062c..88912fe42 100644
--- a/lang/c++/impl/avrogencpp.cc
+++ b/lang/c++/impl/avrogencpp.cc
@@ -245,6 +245,7 @@ string CodeGen::generateRecordType(const NodePtr &n) {
             if (n->leafAt(i)->type() == avro::AVRO_UNION) {
                 os_ << "    typedef " << types[i]
                     << ' ' << n->nameAt(i) << "_t;\n";
+                types[i] = n->nameAt(i) + "_t";
             }
         }
     }
@@ -252,11 +253,7 @@ string CodeGen::generateRecordType(const NodePtr &n) {
         // the nameAt(i) does not take c++ reserved words into account
         // so we need to call decorate on it
         std::string decoratedNameAt = decorate(n->nameAt(i));
-        if (!noUnion_ && n->leafAt(i)->type() == avro::AVRO_UNION) {
-            os_ << "    " << decoratedNameAt << "_t";
-        } else {
-            os_ << "    " << types[i];
-        }
+        os_ << "    " << types[i];
         os_ << ' ' << decoratedNameAt << ";\n";
     }
 
@@ -270,13 +267,7 @@ string CodeGen::generateRecordType(const NodePtr &n) {
         // so we need to call decorate on it
         std::string decoratedNameAt = decorate(n->nameAt(i));
         os_ << "        " << decoratedNameAt << "(";
-        if (!noUnion_ && n->leafAt(i)->type() == avro::AVRO_UNION) {
-            // the nameAt(i) does not take c++ reserved words into account
-            // so we need to call decorate on it
-            os_ << decoratedNameAt << "_t";
-        } else {
-            os_ << types[i];
-        }
+        os_ << types[i];
         os_ << "())";
         if (i != (c - 1)) {
             os_ << ',';
diff --git a/lang/c++/jsonschemas/cpp_reserved_words_union_typedef b/lang/c++/jsonschemas/cpp_reserved_words_union_typedef
new file mode 100644
index 000000000..215f2f4c0
--- /dev/null
+++ b/lang/c++/jsonschemas/cpp_reserved_words_union_typedef
@@ -0,0 +1,13 @@
+{
+  "type": "record",
+  "name": "Record",
+  "fields": [
+    {
+      "name": "void",
+      "type": [
+        "int",
+        "double"
+      ]
+    }
+  ]
+}
diff --git a/lang/c++/test/AvrogencppTestReservedWords.cc b/lang/c++/test/AvrogencppTestReservedWords.cc
index 2bee4ab25..e3b9838e5 100644
--- a/lang/c++/test/AvrogencppTestReservedWords.cc
+++ b/lang/c++/test/AvrogencppTestReservedWords.cc
@@ -16,6 +16,7 @@
  * limitations under the License.
  */
 #include "cpp_reserved_words.hh"
+#include "cpp_reserved_words_union_typedef.hh"
 
 #include "Compiler.hh"