Skip to content

Commit

Permalink
Add the file a symbol is declared in to Reflection (#6613)
Browse files Browse the repository at this point in the history
* Add the file a symbol is declared in to Reflection

If we move a code-generator to depend on Reflection,
it may need to know which file something was declared in
to properly name generated files.

* Doc comments in reflection, and more precise tests

* Add --project-root flag to flatc, normalize declaraion_file to this root

* fix --project-root stuff

* posixpath

* fix scripts

* format

* rename --project-root to --bfbs-filenames

Also, make it optional, rather than defaulting to `./`, if its not
specified, then don't serialize the filenames.

* bfbs generation

* fix some tests

* uncomment a thing

* add  to project root directory conditionally

* fix

* git clang format

* Added help description and removed != nullptr

* "

* Remove accidental change to docs

* Remove accidental change to docs

* Pool strings

Co-authored-by: Casper Neo <[email protected]>
  • Loading branch information
CasperN and Casper Neo authored Jun 17, 2021
1 parent 2cf7bb7 commit c58ae94
Show file tree
Hide file tree
Showing 20 changed files with 863 additions and 669 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ function(compile_flatbuffers_schema_to_binary SRC_FBS)
OUTPUT ${GEN_BINARY_SCHEMA}
COMMAND "${FLATBUFFERS_FLATC_EXECUTABLE}"
-b --schema --bfbs-comments --bfbs-builtins
--bfbs-filenames ${SRC_FBS_DIR}
-I "${CMAKE_CURRENT_SOURCE_DIR}/tests/include_test"
-o "${SRC_FBS_DIR}"
"${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}"
Expand All @@ -468,6 +469,7 @@ function(compile_flatbuffers_schema_to_embedded_binary SRC_FBS OPT)
--cpp-ptr-type flatbuffers::unique_ptr # Used to test with C++98 STLs
${OPT}
--bfbs-comments --bfbs-builtins --bfbs-gen-embed
--bfbs-filenames ${SRC_FBS_DIR}
-I "${CMAKE_CURRENT_SOURCE_DIR}/tests/include_test"
-o "${SRC_FBS_DIR}"
"${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}"
Expand Down
8 changes: 4 additions & 4 deletions grpc/examples/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ generator="--grpc $current_dir/greeter.fbs"
cd go

cd greeter
fbc --go ${generator}
fbc --bfbs-filenames ../.. --go ${generator}

cd ${current_dir}

Expand All @@ -50,22 +50,22 @@ cd python

cd greeter

fbc --python ${generator}
fbc --bfbs-filenames ../.. --python ${generator}

cd ${current_dir}

# Regenerate Swift code
cd swift

cd Greeter/Sources/Model
fbc --swift ${generator}
fbc --bfbs-filenames ../../../.. --swift ${generator}

cd ${current_dir}

# Regenerate Typescript code
cd ts

cd greeter/src
fbc --ts ${generator}
fbc --bfbs-filenames ../../.. --ts ${generator}

cd ${current_dir}
19 changes: 15 additions & 4 deletions include/flatbuffers/idl.h
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,8 @@ struct Definition {
defined_namespace(nullptr),
serialized_location(0),
index(-1),
refcount(1) {}
refcount(1),
declaration_file(nullptr) {}

flatbuffers::Offset<
flatbuffers::Vector<flatbuffers::Offset<reflection::KeyValue>>>
Expand All @@ -286,6 +287,7 @@ struct Definition {
uoffset_t serialized_location;
int index; // Inside the vector it is stored.
int refcount;
const std::string *declaration_file;
};

struct FieldDef : public Definition {
Expand Down Expand Up @@ -591,6 +593,7 @@ struct IDLOptions {
std::string filename_suffix;
std::string filename_extension;
bool no_warnings;
std::string project_root;

// Possible options for the more general generator below.
enum Language {
Expand Down Expand Up @@ -677,6 +680,7 @@ struct IDLOptions {
filename_suffix("_generated"),
filename_extension(),
no_warnings(false),
project_root(""),
lang(IDLOptions::kJava),
mini_reflect(IDLOptions::kNone),
require_explicit_ids(false),
Expand Down Expand Up @@ -941,14 +945,15 @@ class Parser : public ParserState {
StructDef *LookupCreateStruct(const std::string &name,
bool create_if_new = true,
bool definition = false);
FLATBUFFERS_CHECKED_ERROR ParseEnum(bool is_union, EnumDef **dest);
FLATBUFFERS_CHECKED_ERROR ParseEnum(bool is_union, EnumDef **dest,
const char *filename);
FLATBUFFERS_CHECKED_ERROR ParseNamespace();
FLATBUFFERS_CHECKED_ERROR StartStruct(const std::string &name,
StructDef **dest);
FLATBUFFERS_CHECKED_ERROR StartEnum(const std::string &name, bool is_union,
EnumDef **dest);
FLATBUFFERS_CHECKED_ERROR ParseDecl();
FLATBUFFERS_CHECKED_ERROR ParseService();
FLATBUFFERS_CHECKED_ERROR ParseDecl(const char *filename);
FLATBUFFERS_CHECKED_ERROR ParseService(const char *filename);
FLATBUFFERS_CHECKED_ERROR ParseProtoFields(StructDef *struct_def,
bool isextend, bool inside_oneof);
FLATBUFFERS_CHECKED_ERROR ParseProtoOption();
Expand Down Expand Up @@ -985,6 +990,8 @@ class Parser : public ParserState {
FLATBUFFERS_CHECKED_ERROR RecurseError();
template<typename F> CheckedError Recurse(F f);

const std::string &GetPooledString(const std::string &s) const;

public:
SymbolTable<Type> types_;
SymbolTable<StructDef> structs_;
Expand Down Expand Up @@ -1020,6 +1027,10 @@ class Parser : public ParserState {

std::vector<std::pair<Value, FieldDef *>> field_stack_;

// TODO(cneo): Refactor parser to use string_cache more often to save
// on memory usage.
mutable std::set<std::string> string_cache_;

int anonymous_counter_;
int parse_depth_counter_; // stack-overflow guard
};
Expand Down
Loading

0 comments on commit c58ae94

Please sign in to comment.