mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-30 05:10:02 +00:00
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:
committed by
Wouter van Oortmerssen
parent
22743ca45a
commit
642254bee6
@@ -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,14 +72,15 @@ 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,12 +72,12 @@ 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() {
|
|||||||
|
|
||||||
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,
|
|||||||
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,
|
|||||||
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;
|
||||||
|
|||||||
Reference in New Issue
Block a user