Skip to content

Commit

Permalink
AVRO-3990 [C++] Fix invalid code generation for union with reserved n…
Browse files Browse the repository at this point in the history
…ame (#2930)
  • Loading branch information
Gerrit0 authored Jun 14, 2024
1 parent 1a348b2 commit 6aeb7b7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
5 changes: 3 additions & 2 deletions lang/c++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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})
Expand Down Expand Up @@ -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)

Expand Down
15 changes: 3 additions & 12 deletions lang/c++/impl/avrogencpp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -245,18 +245,15 @@ 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";
}
}
}
for (size_t i = 0; i < c; ++i) {
// 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";
}

Expand All @@ -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_ << ',';
Expand Down
13 changes: 13 additions & 0 deletions lang/c++/jsonschemas/cpp_reserved_words_union_typedef
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"type": "record",
"name": "Record",
"fields": [
{
"name": "void",
"type": [
"int",
"double"
]
}
]
}
1 change: 1 addition & 0 deletions lang/c++/test/AvrogencppTestReservedWords.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* limitations under the License.
*/
#include "cpp_reserved_words.hh"
#include "cpp_reserved_words_union_typedef.hh"

#include "Compiler.hh"

Expand Down

0 comments on commit 6aeb7b7

Please sign in to comment.