downgrade to a warning

pull/10586/head
Josh Humphries 3 years ago
parent e293b5cc43
commit 072fb8f58a
  1. 11
      src/google/protobuf/compiler/parser.cc
  2. 3
      src/google/protobuf/compiler/parser.h
  3. 36
      src/google/protobuf/compiler/parser_unittest.cc

@ -390,13 +390,16 @@ void Parser::AddError(const std::string& error) {
AddError(input_->current().line, input_->current().column, error);
}
void Parser::AddWarning(const std::string& warning) {
void Parser::AddWarning(int line, int column, const std::string& warning) {
if (error_collector_ != nullptr) {
error_collector_->AddWarning(input_->current().line,
input_->current().column, warning);
error_collector_->AddWarning(line, column, warning);
}
}
void Parser::AddWarning(const std::string& warning) {
AddWarning(input_->current().line, input_->current().column, warning);
}
// -------------------------------------------------------------------
Parser::LocationRecorder::LocationRecorder(Parser* parser)
@ -1736,7 +1739,7 @@ bool Parser::ParseReservedName(std::string* name, const char* error_message) {
int col = input_->current().column;
DO(ConsumeString(name, error_message));
if (!io::Tokenizer::IsIdentifier(*name)) {
AddError(line, col, absl::StrFormat("Reserved name \"%s\" is not a valid identifier.", *name));
AddWarning(line, col, absl::StrFormat("Reserved name \"%s\" is not a valid identifier.", *name));
return false;
}
return true;

@ -213,6 +213,9 @@ class PROTOBUF_EXPORT Parser {
// of the current token.
void AddError(const std::string& error);
// Invokes error_collector_->AddWarning(), if error_collector_ is not NULL.
void AddWarning(int line, int column, const std::string& warning);
// Invokes error_collector_->AddWarning() with the line and column number
// of the current token.
void AddWarning(const std::string& warning);

@ -147,6 +147,16 @@ class ParserTest : public testing::Test {
EXPECT_EQ(io::Tokenizer::TYPE_END, input_->current().type);
}
// Parse the text and expect that the given warnings are reported.
void ExpectHasWarnings(const char* text, const char* expected_warnings) {
SetupParser(text);
FileDescriptorProto file;
parser_->Parse(input_.get(), &file);
EXPECT_EQ(io::Tokenizer::TYPE_END, input_->current().type);
ASSERT_EQ("", error_collector_.text_);
EXPECT_EQ(expected_warnings, error_collector_.warning_);
}
// Same as above but does not expect that the parser parses the complete
// input.
void ExpectHasEarlyExitErrors(const char* text, const char* expected_errors) {
@ -1676,12 +1686,14 @@ TEST_F(ParseErrorTest, EnumReservedMissingQuotes) {
}
TEST_F(ParseErrorTest, EnumReservedInvalidIdentifier) {
ExpectHasErrors(
"enum TestEnum {\n"
" FOO = 1;\n"
" reserved \"foo bar\";\n"
"}\n",
"2:11: Reserved name \"foo bar\" is not a valid identifier.\n");
ExpectHasWarnings(
R"pb(
enum TestEnum {
FOO = 1;
reserved "foo bar";
}
)pb",
"3:17: Reserved name \"foo bar\" is not a valid identifier.\n");
}
// -------------------------------------------------------------------
@ -1712,11 +1724,13 @@ TEST_F(ParseErrorTest, ReservedMissingQuotes) {
}
TEST_F(ParseErrorTest, ReservedInvalidIdentifier) {
ExpectHasErrors(
"message Foo {\n"
" reserved \"foo bar\";\n"
"}\n",
"1:11: Reserved name \"foo bar\" is not a valid identifier.\n");
ExpectHasWarnings(
R"pb(
message Foo {
reserved "foo bar";
}
)pb",
"2:17: Reserved name \"foo bar\" is not a valid identifier.\n");
}
TEST_F(ParseErrorTest, ReservedNegativeNumber) {

Loading…
Cancel
Save