google / flatbuffers

FlatBuffers: Memory Efficient Serialization Library

Home Page:https://flatbuffers.dev/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Invalid code generated

AustinSchuh opened this issue · comments

I'm generating the object API for the following code for C++.

table VectorOfStrings {
  str:[string] (id: 0); 
}

table VectorOfVectorOfString {
  v:[VectorOfStrings] (id: 0); 
}

The generated code doesn't compile.

ERROR: /home/austin/local/971-Robot-Code/aos/BUILD:385:8: Compiling aos/flatbuffer_merge_test.cc failed: (Exit 1): cc_wrapper.sh failed: error executing command external/llvm_toolchain/bin/cc_wrapper.sh '--target=x86_64-unknown-linux-gnu' -U_FORTIFY_SOURCE '-D_FORTIFY_SOURCE=2' -ggdb3 -fstack-protector -fno-omit-frame-pointer -fcolor-diagnostics -Wall ... (remaining 115 arguments skipped)

Use --sandbox_debug to see verbose messages from the sandbox
In file included from aos/flatbuffer_merge_test.cc:9:
bazel-out/k8-opt/bin/aos/json_to_flatbuffer_generated.h:2092:33: error: no
      member named 'emplace_back' in 'std::unique_ptr<aos::testing::VectorOfStringsT>'
  for (const auto &v : o.v) { v.emplace_back((v) ? new aos::testing::VectorOfStringsT(*v) : ...
                              ~ ^
1 error generated.

The generated code in question is:

inline VectorOfVectorOfStringT::VectorOfVectorOfStringT(const VectorOfVectorOfStringT &o) {
  v.reserve(o.v.size());
  for (const auto &v : o.v) { v.emplace_back((v) ? new aos::testing::VectorOfStringsT(*v) : nullptr); }
}

Looks like the code generator is using v as the temporary type which is also the field type, and exploding at compilation.

I put up a fix in #7133. Not the nicest in readability, but does prevent any clashes in the future.