mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-10 23:17:27 +00:00
inject no long for FBS generation to remove logs in flattests (#7926)
* inject no long for FBS generation to remove logs in flattests * updated blaze rules
This commit is contained in:
@@ -5,20 +5,38 @@ package(
|
||||
default_visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "code_generators",
|
||||
srcs = ["code_generators.cpp"],
|
||||
hdrs = [
|
||||
"//:public_headers",
|
||||
],
|
||||
strip_include_prefix = "/include",
|
||||
visibility = ["//:__subpackages__"],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "generate_fbs",
|
||||
srcs = ["idl_gen_fbs.cpp"],
|
||||
hdrs = ["idl_gen_fbs.h"],
|
||||
strip_include_prefix = "/src",
|
||||
visibility = ["//:__subpackages__"],
|
||||
deps = [":code_generators"],
|
||||
)
|
||||
|
||||
# Public flatc library to compile flatbuffer files at runtime.
|
||||
cc_library(
|
||||
name = "flatbuffers",
|
||||
srcs = [
|
||||
"code_generators.cpp",
|
||||
"idl_gen_fbs.cpp",
|
||||
"idl_gen_fbs.h",
|
||||
"idl_gen_text.cpp",
|
||||
"idl_gen_text.h",
|
||||
"idl_parser.cpp",
|
||||
"reflection.cpp",
|
||||
"util.cpp",
|
||||
],
|
||||
hdrs = ["//:public_headers"],
|
||||
hdrs = [
|
||||
"//:public_headers",
|
||||
],
|
||||
linkopts = select({
|
||||
# TODO: Bazel uses `clang` instead of `clang++` to link
|
||||
# C++ code on BSD. Temporarily adding these linker flags while
|
||||
@@ -29,7 +47,11 @@ cc_library(
|
||||
"//conditions:default": [],
|
||||
}),
|
||||
strip_include_prefix = "/include",
|
||||
visibility = ["//:__pkg__"],
|
||||
visibility = ["//:__subpackages__"],
|
||||
deps = [
|
||||
":code_generators",
|
||||
":generate_fbs",
|
||||
],
|
||||
)
|
||||
|
||||
# Public flatc compiler library.
|
||||
|
||||
@@ -129,34 +129,43 @@ static bool HasGapInProtoId(const std::vector<FieldDef *> &fields) {
|
||||
}
|
||||
|
||||
static bool ProtobufIdSanityCheck(const StructDef &struct_def,
|
||||
IDLOptions::ProtoIdGapAction gap_action) {
|
||||
IDLOptions::ProtoIdGapAction gap_action,
|
||||
bool no_log = false) {
|
||||
const auto &fields = struct_def.fields.vec;
|
||||
if (HasNonPositiveFieldId(fields)) {
|
||||
// TODO: Use LogCompilerWarn
|
||||
fprintf(stderr, "Field id in struct %s has a non positive number value\n",
|
||||
struct_def.name.c_str());
|
||||
if (!no_log) {
|
||||
fprintf(stderr, "Field id in struct %s has a non positive number value\n",
|
||||
struct_def.name.c_str());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (HasTwiceUsedId(fields)) {
|
||||
// TODO: Use LogCompilerWarn
|
||||
fprintf(stderr, "Fields in struct %s have used an id twice\n",
|
||||
struct_def.name.c_str());
|
||||
if (!no_log) {
|
||||
fprintf(stderr, "Fields in struct %s have used an id twice\n",
|
||||
struct_def.name.c_str());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (HasFieldIdFromReservedIds(fields, struct_def.reserved_ids)) {
|
||||
// TODO: Use LogCompilerWarn
|
||||
fprintf(stderr, "Fields in struct %s use id from reserved ids\n",
|
||||
struct_def.name.c_str());
|
||||
if (!no_log) {
|
||||
fprintf(stderr, "Fields in struct %s use id from reserved ids\n",
|
||||
struct_def.name.c_str());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (gap_action != IDLOptions::ProtoIdGapAction::NO_OP) {
|
||||
if (HasGapInProtoId(fields)) {
|
||||
// TODO: Use LogCompilerWarn
|
||||
fprintf(stderr, "Fields in struct %s have gap between ids\n",
|
||||
struct_def.name.c_str());
|
||||
if (!no_log) {
|
||||
fprintf(stderr, "Fields in struct %s have gap between ids\n",
|
||||
struct_def.name.c_str());
|
||||
}
|
||||
if (gap_action == IDLOptions::ProtoIdGapAction::ERROR) { return false; }
|
||||
}
|
||||
}
|
||||
@@ -174,7 +183,8 @@ struct ProtobufToFbsIdMap {
|
||||
};
|
||||
|
||||
static ProtobufToFbsIdMap MapProtoIdsToFieldsId(
|
||||
const StructDef &struct_def, IDLOptions::ProtoIdGapAction gap_action) {
|
||||
const StructDef &struct_def, IDLOptions::ProtoIdGapAction gap_action,
|
||||
bool no_log) {
|
||||
const auto &fields = struct_def.fields.vec;
|
||||
|
||||
if (!HasFieldWithId(fields)) {
|
||||
@@ -183,7 +193,7 @@ static ProtobufToFbsIdMap MapProtoIdsToFieldsId(
|
||||
return result;
|
||||
}
|
||||
|
||||
if (!ProtobufIdSanityCheck(struct_def, gap_action)) { return {}; }
|
||||
if (!ProtobufIdSanityCheck(struct_def, gap_action, no_log)) { return {}; }
|
||||
|
||||
static constexpr int UNION_ID = -1;
|
||||
using ProtoIdFieldNamePair = std::pair<int, std::string>;
|
||||
@@ -203,8 +213,10 @@ static ProtobufToFbsIdMap MapProtoIdsToFieldsId(
|
||||
}
|
||||
} else {
|
||||
// TODO: Use LogCompilerWarn
|
||||
fprintf(stderr, "Fields id in struct %s is missing\n",
|
||||
struct_def.name.c_str());
|
||||
if (!no_log) {
|
||||
fprintf(stderr, "Fields id in struct %s is missing\n",
|
||||
struct_def.name.c_str());
|
||||
}
|
||||
return {};
|
||||
}
|
||||
}
|
||||
@@ -240,7 +252,8 @@ static void GenNameSpace(const Namespace &name_space, std::string *_schema,
|
||||
}
|
||||
|
||||
// Generate a flatbuffer schema from the Parser's internal representation.
|
||||
std::string GenerateFBS(const Parser &parser, const std::string &file_name) {
|
||||
std::string GenerateFBS(const Parser &parser, const std::string &file_name,
|
||||
bool no_log = false) {
|
||||
// Proto namespaces may clash with table names, escape the ones that were
|
||||
// generated from a table:
|
||||
for (auto it = parser.namespaces_.begin(); it != parser.namespaces_.end();
|
||||
@@ -315,8 +328,8 @@ std::string GenerateFBS(const Parser &parser, const std::string &file_name) {
|
||||
for (auto it = parser.structs_.vec.begin(); it != parser.structs_.vec.end();
|
||||
++it) {
|
||||
StructDef &struct_def = **it;
|
||||
const auto proto_fbs_ids =
|
||||
MapProtoIdsToFieldsId(struct_def, parser.opts.proto_id_gap_action);
|
||||
const auto proto_fbs_ids = MapProtoIdsToFieldsId(
|
||||
struct_def, parser.opts.proto_id_gap_action, no_log);
|
||||
if (!proto_fbs_ids.successful) { return {}; }
|
||||
|
||||
if (parser.opts.include_dependence_headers && struct_def.generated) {
|
||||
@@ -362,13 +375,15 @@ std::string GenerateFBS(const Parser &parser, const std::string &file_name) {
|
||||
}
|
||||
|
||||
bool GenerateFBS(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name) {
|
||||
const std::string fbs = GenerateFBS(parser, file_name);
|
||||
const std::string &file_name, bool no_log = false) {
|
||||
const std::string fbs = GenerateFBS(parser, file_name, no_log);
|
||||
if (fbs.empty()) { return false; }
|
||||
// TODO: Use LogCompilerWarn
|
||||
fprintf(stderr,
|
||||
"When you use --proto, that you should check for conformity "
|
||||
"yourself, using the existing --conform");
|
||||
if (!no_log) {
|
||||
fprintf(stderr,
|
||||
"When you use --proto, that you should check for conformity "
|
||||
"yourself, using the existing --conform");
|
||||
}
|
||||
return SaveFile((path + file_name + ".fbs").c_str(), fbs, false);
|
||||
}
|
||||
|
||||
@@ -376,9 +391,11 @@ namespace {
|
||||
|
||||
class FBSCodeGenerator : public CodeGenerator {
|
||||
public:
|
||||
explicit FBSCodeGenerator(const bool no_log) : no_log_(no_log) {}
|
||||
|
||||
Status GenerateCode(const Parser &parser, const std::string &path,
|
||||
const std::string &filename) override {
|
||||
if (!GenerateFBS(parser, path, filename)) { return Status::ERROR; }
|
||||
if (!GenerateFBS(parser, path, filename, no_log_)) { return Status::ERROR; }
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
@@ -424,12 +441,15 @@ class FBSCodeGenerator : public CodeGenerator {
|
||||
IDLOptions::Language Language() const override { return IDLOptions::kProto; }
|
||||
|
||||
std::string LanguageName() const override { return "proto"; }
|
||||
|
||||
protected:
|
||||
const bool no_log_;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
std::unique_ptr<CodeGenerator> NewFBSCodeGenerator() {
|
||||
return std::unique_ptr<FBSCodeGenerator>(new FBSCodeGenerator());
|
||||
std::unique_ptr<CodeGenerator> NewFBSCodeGenerator(const bool no_log) {
|
||||
return std::unique_ptr<FBSCodeGenerator>(new FBSCodeGenerator(no_log));
|
||||
}
|
||||
|
||||
} // namespace flatbuffers
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
|
||||
namespace flatbuffers {
|
||||
|
||||
std::unique_ptr<CodeGenerator> NewFBSCodeGenerator();
|
||||
std::unique_ptr<CodeGenerator> NewFBSCodeGenerator(bool no_log = false);
|
||||
|
||||
} // namespace flatbuffers
|
||||
|
||||
|
||||
Reference in New Issue
Block a user