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.
This commit is contained in:
Pavel Kalinnikov 2017-06-02 17:50:18 +02:00 committed by Wouter van Oortmerssen
parent 22743ca45a
commit 642254bee6
4 changed files with 18 additions and 18 deletions

View File

@ -610,7 +610,7 @@ private:
std::string file_identifier_;
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::vector<std::string> native_included_files_;

View File

@ -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_ += "";
}

View File

@ -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

View File

@ -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<std::string>();
}
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;