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.
This commit is contained in:
Jon Simantov
2022-01-25 17:01:16 -05:00
committed by GitHub
parent 9ef1524d3f
commit a2d38fbb98
4 changed files with 34 additions and 1 deletions

View File

@@ -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);

View File

@@ -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();
}