diff --git a/include/flatbuffers/idl.h b/include/flatbuffers/idl.h index 2781852cd..00cec0306 100644 --- a/include/flatbuffers/idl.h +++ b/include/flatbuffers/idl.h @@ -610,7 +610,7 @@ private: std::string file_identifier_; std::string file_extension_; - std::map included_files_; + std::map included_files_; std::map> files_included_per_file_; std::vector native_included_files_; diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 6bd958f5c..cbbd1ffcb 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -72,14 +72,15 @@ class CppGenerator : public BaseGenerator { } for (auto it = parser_.included_files_.begin(); it != parser_.included_files_.end(); ++it) { - auto noext = flatbuffers::StripExtension(it->first); + if (it->second.empty()) + continue; + auto noext = flatbuffers::StripExtension(it->second); auto basename = flatbuffers::StripPath(noext); - if (basename != file_name_) { - code_ += "#include \"" + parser_.opts.include_prefix + - (parser_.opts.keep_include_path ? noext : basename) + - "_generated.h\""; - num_includes++; - } + + code_ += "#include \"" + parser_.opts.include_prefix + + (parser_.opts.keep_include_path ? noext : basename) + + "_generated.h\""; + num_includes++; } if (num_includes) code_ += ""; } diff --git a/src/idl_gen_fbs.cpp b/src/idl_gen_fbs.cpp index 7752369ca..c7762c12f 100644 --- a/src/idl_gen_fbs.cpp +++ b/src/idl_gen_fbs.cpp @@ -72,12 +72,12 @@ std::string GenerateFBS(const Parser &parser, const std::string &file_name) { int num_includes = 0; for (auto it = parser.included_files_.begin(); it != parser.included_files_.end(); ++it) { + if (it->second.empty()) + continue; auto basename = flatbuffers::StripPath( - flatbuffers::StripExtension(it->first)); - if (basename != file_name) { - schema += "include \"" + basename + ".fbs\";\n"; - num_includes++; - } + flatbuffers::StripExtension(it->second)); + schema += "include \"" + basename + ".fbs\";\n"; + num_includes++; } if (num_includes) schema += "\n"; #endif diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp index f03ad9a88..a3a72f6e9 100644 --- a/src/idl_parser.cpp +++ b/src/idl_parser.cpp @@ -1917,8 +1917,7 @@ CheckedError Parser::SkipJsonString() { bool Parser::Parse(const char *source, const char **include_paths, const char *source_filename) { - return !DoParse(source, include_paths, source_filename, - source_filename).Check(); + return !DoParse(source, include_paths, source_filename, nullptr).Check(); } CheckedError Parser::DoParse(const char *source, const char **include_paths, @@ -1926,8 +1925,8 @@ CheckedError Parser::DoParse(const char *source, const char **include_paths, const char *include_filename) { file_being_parsed_ = source_filename ? source_filename : ""; if (source_filename && - included_files_.find(include_filename) == included_files_.end()) { - included_files_[include_filename] = true; + included_files_.find(source_filename) == included_files_.end()) { + included_files_[source_filename] = include_filename ? include_filename : ""; files_included_per_file_[source_filename] = std::set(); } if (!include_paths) { @@ -1976,7 +1975,7 @@ CheckedError Parser::DoParse(const char *source, const char **include_paths, return Error("unable to locate include file: " + name); if (source_filename) files_included_per_file_[source_filename].insert(filepath); - if (included_files_.find(name) == included_files_.end()) { + if (included_files_.find(filepath) == included_files_.end()) { // We found an include file that we have not parsed yet. // Load it and parse it. std::string contents;