Commit 642254be authored by Pavel Kalinnikov's avatar Pavel Kalinnikov Committed by Wouter van Oortmerssen

Track included files in PATH-agnostic way. (#4329)

* Track included files in PATH-agnostic way.

Use full paths as keys in the map of included files. Store logical
include path as a value, in order to put it to the generated file.

* Fix tests by accepting null |include_filename|.

* Fix self-includes code generators.
parent 22743ca4
...@@ -610,7 +610,7 @@ private: ...@@ -610,7 +610,7 @@ private:
std::string file_identifier_; std::string file_identifier_;
std::string file_extension_; std::string file_extension_;
std::map<std::string, bool> included_files_; std::map<std::string, std::string> included_files_;
std::map<std::string, std::set<std::string>> files_included_per_file_; std::map<std::string, std::set<std::string>> files_included_per_file_;
std::vector<std::string> native_included_files_; std::vector<std::string> native_included_files_;
......
...@@ -72,15 +72,16 @@ class CppGenerator : public BaseGenerator { ...@@ -72,15 +72,16 @@ class CppGenerator : public BaseGenerator {
} }
for (auto it = parser_.included_files_.begin(); for (auto it = parser_.included_files_.begin();
it != parser_.included_files_.end(); ++it) { 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); auto basename = flatbuffers::StripPath(noext);
if (basename != file_name_) {
code_ += "#include \"" + parser_.opts.include_prefix + code_ += "#include \"" + parser_.opts.include_prefix +
(parser_.opts.keep_include_path ? noext : basename) + (parser_.opts.keep_include_path ? noext : basename) +
"_generated.h\""; "_generated.h\"";
num_includes++; num_includes++;
} }
}
if (num_includes) code_ += ""; if (num_includes) code_ += "";
} }
......
...@@ -72,13 +72,13 @@ std::string GenerateFBS(const Parser &parser, const std::string &file_name) { ...@@ -72,13 +72,13 @@ std::string GenerateFBS(const Parser &parser, const std::string &file_name) {
int num_includes = 0; int num_includes = 0;
for (auto it = parser.included_files_.begin(); for (auto it = parser.included_files_.begin();
it != parser.included_files_.end(); ++it) { it != parser.included_files_.end(); ++it) {
if (it->second.empty())
continue;
auto basename = flatbuffers::StripPath( auto basename = flatbuffers::StripPath(
flatbuffers::StripExtension(it->first)); flatbuffers::StripExtension(it->second));
if (basename != file_name) {
schema += "include \"" + basename + ".fbs\";\n"; schema += "include \"" + basename + ".fbs\";\n";
num_includes++; num_includes++;
} }
}
if (num_includes) schema += "\n"; if (num_includes) schema += "\n";
#endif #endif
} }
......
...@@ -1917,8 +1917,7 @@ CheckedError Parser::SkipJsonString() { ...@@ -1917,8 +1917,7 @@ CheckedError Parser::SkipJsonString() {
bool Parser::Parse(const char *source, const char **include_paths, bool Parser::Parse(const char *source, const char **include_paths,
const char *source_filename) { const char *source_filename) {
return !DoParse(source, include_paths, source_filename, return !DoParse(source, include_paths, source_filename, nullptr).Check();
source_filename).Check();
} }
CheckedError Parser::DoParse(const char *source, const char **include_paths, CheckedError Parser::DoParse(const char *source, const char **include_paths,
...@@ -1926,8 +1925,8 @@ 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) { const char *include_filename) {
file_being_parsed_ = source_filename ? source_filename : ""; file_being_parsed_ = source_filename ? source_filename : "";
if (source_filename && if (source_filename &&
included_files_.find(include_filename) == included_files_.end()) { included_files_.find(source_filename) == included_files_.end()) {
included_files_[include_filename] = true; included_files_[source_filename] = include_filename ? include_filename : "";
files_included_per_file_[source_filename] = std::set<std::string>(); files_included_per_file_[source_filename] = std::set<std::string>();
} }
if (!include_paths) { if (!include_paths) {
...@@ -1976,7 +1975,7 @@ CheckedError Parser::DoParse(const char *source, const char **include_paths, ...@@ -1976,7 +1975,7 @@ CheckedError Parser::DoParse(const char *source, const char **include_paths,
return Error("unable to locate include file: " + name); return Error("unable to locate include file: " + name);
if (source_filename) if (source_filename)
files_included_per_file_[source_filename].insert(filepath); 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. // We found an include file that we have not parsed yet.
// Load it and parse it. // Load it and parse it.
std::string contents; std::string contents;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment