Only include direct included filed (#7348)

This commit is contained in:
Derek Bailey
2022-06-15 12:10:39 -07:00
committed by GitHub
parent 9a1913a87a
commit 5f01376027
3 changed files with 35 additions and 26 deletions

View File

@@ -911,6 +911,11 @@ class Parser : public ParserState {
// @param opts Options used to parce a schema and generate code. // @param opts Options used to parce a schema and generate code.
static bool SupportsOptionalScalars(const flatbuffers::IDLOptions &opts); static bool SupportsOptionalScalars(const flatbuffers::IDLOptions &opts);
// Get the set of included files that are directly referenced by the file
// being parsed. This does not include files that are transitively included by
// others includes.
std::vector<std::string> GetIncludedFiles() const;
private: private:
class ParseDepthGuard; class ParseDepthGuard;

View File

@@ -221,33 +221,34 @@ class CppGenerator : public BaseGenerator {
} }
void GenIncludeDependencies() { void GenIncludeDependencies() {
int num_includes = 0;
if (opts_.generate_object_based_api) { if (opts_.generate_object_based_api) {
for (auto it = parser_.native_included_files_.begin(); for (auto it = parser_.native_included_files_.begin();
it != parser_.native_included_files_.end(); ++it) { it != parser_.native_included_files_.end(); ++it) {
code_ += "#include \"" + *it + "\""; code_ += "#include \"" + *it + "\"";
num_includes++;
} }
} }
std::vector<std::string> include_files; // Get the directly included file of the file being parsed.
for (auto it = parser_.included_files_.begin(); std::vector<std::string> included_files(parser_.GetIncludedFiles());
it != parser_.included_files_.end(); ++it) {
if (it->second.empty()) continue;
include_files.push_back(it->second);
}
std::stable_sort(include_files.begin(), include_files.end());
for (auto it = include_files.begin(); it != include_files.end(); ++it) { // We are safe to sort them alphabetically, since there shouldn't be any
auto noext = flatbuffers::StripExtension(*it); // interdependence between them.
auto basename = flatbuffers::StripPath(noext); std::stable_sort(included_files.begin(), included_files.end());
auto includeName =
GeneratedFileName(opts_.include_prefix, for (const std::string &included_file : included_files) {
opts_.keep_include_path ? noext : basename, opts_); auto noext = flatbuffers::StripExtension(included_file);
code_ += "#include \"" + includeName + "\""; code_ +=
num_includes++; "#include \"" +
GeneratedFileName(
opts_.include_prefix,
opts_.keep_include_path ? noext : flatbuffers::StripPath(noext),
opts_) +
"\"";
}
if (!parser_.native_included_files_.empty() || !included_files.empty()) {
code_ += "";
} }
if (num_includes) code_ += "";
} }
void GenExtraIncludes() { void GenExtraIncludes() {

View File

@@ -2189,12 +2189,8 @@ template<typename T> void EnumDef::ChangeEnumValue(EnumVal *ev, T new_value) {
} }
namespace EnumHelper { namespace EnumHelper {
template<BaseType E> struct EnumValType { template<BaseType E> struct EnumValType { typedef int64_t type; };
typedef int64_t type; template<> struct EnumValType<BASE_TYPE_ULONG> { typedef uint64_t type; };
};
template<> struct EnumValType<BASE_TYPE_ULONG> {
typedef uint64_t type;
};
} // namespace EnumHelper } // namespace EnumHelper
struct EnumValBuilder { struct EnumValBuilder {
@@ -2464,6 +2460,13 @@ CheckedError Parser::CheckClash(std::vector<FieldDef *> &fields,
return NoError(); return NoError();
} }
std::vector<std::string> Parser::GetIncludedFiles() const {
const auto it = files_included_per_file_.find(file_being_parsed_);
if (it == files_included_per_file_.end()) { return {}; }
return { it->second.cbegin(), it->second.cend() };
}
bool Parser::SupportsOptionalScalars(const flatbuffers::IDLOptions &opts) { bool Parser::SupportsOptionalScalars(const flatbuffers::IDLOptions &opts) {
static FLATBUFFERS_CONSTEXPR unsigned long supported_langs = static FLATBUFFERS_CONSTEXPR unsigned long supported_langs =
IDLOptions::kRust | IDLOptions::kSwift | IDLOptions::kLobster | IDLOptions::kRust | IDLOptions::kSwift | IDLOptions::kLobster |
@@ -3332,8 +3335,8 @@ CheckedError Parser::CheckPrivatelyLeakedFields(const Definition &def,
const auto is_field_private = value_type.attributes.Lookup("private"); const auto is_field_private = value_type.attributes.Lookup("private");
if (!is_private && is_field_private) { if (!is_private && is_field_private) {
return Error( return Error(
"Leaking private implementation, verify all objects have similar " "Leaking private implementation, verify all objects have similar "
"annotations"); "annotations");
} }
return NoError(); return NoError();
} }