mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-05 13:08:58 +00:00
Add options to print build rule dependencies
Tested: on Linux Bug: 16465909 Change-Id: I2f1a6def13e47716110426b00990c2c625c03251
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
#include <list>
|
||||
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
#include "flatbuffers/idl.h"
|
||||
@@ -333,6 +334,7 @@ FieldDef &Parser::AddField(StructDef &struct_def,
|
||||
field.value.offset =
|
||||
FieldIndexToOffset(static_cast<voffset_t>(struct_def.fields.vec.size()));
|
||||
field.name = name;
|
||||
field.file = struct_def.file;
|
||||
field.value.type = type;
|
||||
if (struct_def.fixed) { // statically compute the field offset
|
||||
auto size = InlineSize(type);
|
||||
@@ -732,6 +734,7 @@ void Parser::ParseEnum(bool is_union) {
|
||||
Expect(kTokenIdentifier);
|
||||
auto &enum_def = *new EnumDef();
|
||||
enum_def.name = name;
|
||||
if (!files_being_parsed_.empty()) enum_def.file = files_being_parsed_.top();
|
||||
enum_def.doc_comment = dc;
|
||||
enum_def.is_union = is_union;
|
||||
enum_def.defined_namespace = namespaces_.back();
|
||||
@@ -799,6 +802,7 @@ StructDef &Parser::StartStruct() {
|
||||
if (!struct_def.predecl) Error("datatype already exists: " + name);
|
||||
struct_def.predecl = false;
|
||||
struct_def.name = name;
|
||||
if (!files_being_parsed_.empty()) struct_def.file = files_being_parsed_.top();
|
||||
// Move this struct to the back of the vector just in case it was predeclared,
|
||||
// to preserve declaration order.
|
||||
remove(structs_.vec.begin(), structs_.vec.end(), &struct_def);
|
||||
@@ -1022,7 +1026,16 @@ Type Parser::ParseTypeFromProtoType() {
|
||||
|
||||
bool Parser::Parse(const char *source, const char **include_paths,
|
||||
const char *source_filename) {
|
||||
if (source_filename) included_files_[source_filename] = true;
|
||||
if (source_filename &&
|
||||
included_files_.find(source_filename) == included_files_.end()) {
|
||||
included_files_[source_filename] = true;
|
||||
files_included_per_file_[source_filename] = std::set<std::string>();
|
||||
files_being_parsed_.push(source_filename);
|
||||
}
|
||||
if (!include_paths) {
|
||||
const char *current_directory[] = { "", nullptr };
|
||||
include_paths = current_directory;
|
||||
}
|
||||
source_ = cursor_ = source;
|
||||
line_ = 1;
|
||||
error_.clear();
|
||||
@@ -1033,22 +1046,23 @@ bool Parser::Parse(const char *source, const char **include_paths,
|
||||
while (IsNext(kTokenInclude)) {
|
||||
auto name = attribute_;
|
||||
Expect(kTokenStringConstant);
|
||||
if (included_files_.find(name) == included_files_.end()) {
|
||||
// Look for the file in include_paths.
|
||||
std::string filepath;
|
||||
for (auto paths = include_paths; paths && *paths; paths++) {
|
||||
filepath = flatbuffers::ConCatPathFileName(*paths, name);
|
||||
if(FileExists(filepath.c_str())) break;
|
||||
}
|
||||
if (filepath.empty())
|
||||
Error("unable to locate include file: " + name);
|
||||
if (source_filename)
|
||||
files_included_per_file_[source_filename].insert(filepath);
|
||||
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;
|
||||
if (!include_paths) {
|
||||
const char *current_directory[] = { "", nullptr };
|
||||
include_paths = current_directory;
|
||||
}
|
||||
for (auto paths = include_paths; paths && *paths; paths++) {
|
||||
auto filepath = flatbuffers::ConCatPathFileName(*paths, name);
|
||||
if(LoadFile(filepath.c_str(), true, &contents)) break;
|
||||
}
|
||||
if (contents.empty())
|
||||
if (!LoadFile(filepath.c_str(), true, &contents))
|
||||
Error("unable to load include file: " + name);
|
||||
included_files_[name] = true;
|
||||
if (!Parse(contents.c_str(), include_paths)) {
|
||||
if (!Parse(contents.c_str(), include_paths, filepath.c_str())) {
|
||||
// Any errors, we're done.
|
||||
return false;
|
||||
}
|
||||
@@ -1143,10 +1157,35 @@ bool Parser::Parse(const char *source, const char **include_paths,
|
||||
error_ += NumToString(line_) + ":0"; // gcc alike
|
||||
#endif
|
||||
error_ += ": error: " + msg;
|
||||
if (source_filename) files_being_parsed_.pop();
|
||||
return false;
|
||||
}
|
||||
if (source_filename) files_being_parsed_.pop();
|
||||
assert(!struct_stack_.size());
|
||||
return true;
|
||||
}
|
||||
|
||||
std::set<std::string> Parser::GetIncludedFilesRecursive(
|
||||
const std::string &file_name) const {
|
||||
std::set<std::string> included_files;
|
||||
std::list<std::string> to_process;
|
||||
|
||||
if (file_name.empty()) return included_files;
|
||||
to_process.push_back(file_name);
|
||||
|
||||
while (!to_process.empty()) {
|
||||
std::string current = to_process.front();
|
||||
to_process.pop_front();
|
||||
included_files.insert(current);
|
||||
|
||||
auto new_files = files_included_per_file_.at(current);
|
||||
for (auto it = new_files.begin(); it != new_files.end(); ++it) {
|
||||
if (included_files.find(*it) == included_files.end())
|
||||
to_process.push_back(*it);
|
||||
}
|
||||
}
|
||||
|
||||
return included_files;
|
||||
}
|
||||
|
||||
} // namespace flatbuffers
|
||||
|
||||
Reference in New Issue
Block a user