summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Simantov <jsimantov@google.com>2022-01-25 17:01:16 -0500
committerGitHub <noreply@github.com>2022-01-25 14:01:16 -0800
commita2d38fbb9881ee64056fe2f6fe8aeb0b9eec9de9 (patch)
treea4cb1f9852b2137103686f4498566ad57f6382e4
parent9ef1524d3fe71351dfd99913e148ec588e45f7d4 (diff)
downloadflatbuffers-a2d38fbb9881ee64056fe2f6fe8aeb0b9eec9de9.tar.gz
flatbuffers-a2d38fbb9881ee64056fe2f6fe8aeb0b9eec9de9.tar.bz2
flatbuffers-a2d38fbb9881ee64056fe2f6fe8aeb0b9eec9de9.zip
Add --warnings-as-errors to flatc compiler. (#7034)
* Add --warnings-as-errors to flatc compiler. With this option set, flatc will return an error on parsing if any warnings occurred. * Add unit test for opts.warnings_as_errors. * Change explicit option setting to default.
-rw-r--r--include/flatbuffers/idl.h4
-rw-r--r--src/flatc.cpp3
-rw-r--r--src/idl_parser.cpp8
-rw-r--r--tests/test.cpp20
4 files changed, 34 insertions, 1 deletions
diff --git a/include/flatbuffers/idl.h b/include/flatbuffers/idl.h
index a67d713f..ef5bf122 100644
--- a/include/flatbuffers/idl.h
+++ b/include/flatbuffers/idl.h
@@ -591,6 +591,7 @@ struct IDLOptions {
std::string filename_suffix;
std::string filename_extension;
bool no_warnings;
+ bool warnings_as_errors;
std::string project_root;
bool cs_global_alias;
bool json_nested_flatbuffers;
@@ -681,6 +682,7 @@ struct IDLOptions {
filename_suffix("_generated"),
filename_extension(),
no_warnings(false),
+ warnings_as_errors(false),
project_root(""),
cs_global_alias(false),
json_nested_flatbuffers(true),
@@ -787,6 +789,7 @@ class Parser : public ParserState {
root_struct_def_(nullptr),
opts(options),
uses_flexbuffers_(false),
+ has_warning_(false),
advanced_features_(0),
source_(nullptr),
anonymous_counter_(0),
@@ -1021,6 +1024,7 @@ class Parser : public ParserState {
IDLOptions opts;
bool uses_flexbuffers_;
+ bool has_warning_;
uint64_t advanced_features_;
diff --git a/src/flatc.cpp b/src/flatc.cpp
index cee5f144..7c16abe2 100644
--- a/src/flatc.cpp
+++ b/src/flatc.cpp
@@ -199,6 +199,7 @@ const static FlatCOption options[] = {
"Used with \"binary\" and \"json\" options, it generates data using "
"schema-less FlexBuffers." },
{ "", "no-warnings", "", "Inhibit all warnings messages." },
+ { "", "warning-as-errors", "", "Treat all warnings as errors." },
{ "", "cs-global-alias", "",
"Prepend \"global::\" to all user generated csharp classes and "
"structs." },
@@ -496,6 +497,8 @@ int FlatCompiler::Compile(int argc, const char **argv) {
opts.gen_jvmstatic = true;
} else if (arg == "--no-warnings") {
opts.no_warnings = true;
+ } else if (arg == "--warnings-as-errors") {
+ opts.warnings_as_errors = true;
} else if (arg == "--cpp-std") {
if (++argi >= argc)
Error("missing C++ standard specification" + arg, true);
diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp
index fefc46d4..4ebf99b3 100644
--- a/src/idl_parser.cpp
+++ b/src/idl_parser.cpp
@@ -144,7 +144,10 @@ void Parser::Message(const std::string &msg) {
}
void Parser::Warning(const std::string &msg) {
- if (!opts.no_warnings) Message("warning: " + msg);
+ if (!opts.no_warnings) {
+ Message("warning: " + msg);
+ has_warning_ = true; // for opts.warnings_as_errors
+ }
}
CheckedError Parser::Error(const std::string &msg) {
@@ -3444,6 +3447,9 @@ CheckedError Parser::DoParse(const char *source, const char **include_paths,
ECHECK(ParseDecl(source_filename));
}
}
+ if (opts.warnings_as_errors && has_warning_) {
+ return Error("treating warnings as errors, failed due to above warnings");
+ }
return NoError();
}
diff --git a/tests/test.cpp b/tests/test.cpp
index 2c490a82..1fa75fe7 100644
--- a/tests/test.cpp
+++ b/tests/test.cpp
@@ -2943,6 +2943,25 @@ void StructUnionTest() {
gadget.Set(fan);
}
+void WarningsAsErrorsTest() {
+ {
+ flatbuffers::IDLOptions opts;
+ // opts.warnings_as_errors should default to false
+ flatbuffers::Parser parser(opts);
+ TEST_EQ(parser.Parse("table T { THIS_NAME_CAUSES_A_WARNING:string;}\n"
+ "root_type T;"),
+ true);
+ }
+ {
+ flatbuffers::IDLOptions opts;
+ opts.warnings_as_errors = true;
+ flatbuffers::Parser parser(opts);
+ TEST_EQ(parser.Parse("table T { THIS_NAME_CAUSES_A_WARNING:string;}\n"
+ "root_type T;"),
+ false);
+ }
+}
+
void ConformTest() {
flatbuffers::Parser parser;
TEST_EQ(parser.Parse("table T { A:int; } enum E:byte { A }"), true);
@@ -4209,6 +4228,7 @@ int FlatBufferTests() {
FlatbuffersIteratorsTest();
FixedLengthArraySpanTest();
StructUnionTest();
+ WarningsAsErrorsTest();
return 0;
}