From 072fb8f58a63f36b34b5cb05242903ae71245b2c Mon Sep 17 00:00:00 2001 From: Josh Humphries Date: Wed, 21 Sep 2022 18:24:59 -0400 Subject: [PATCH] downgrade to a warning --- src/google/protobuf/compiler/parser.cc | 11 +++--- src/google/protobuf/compiler/parser.h | 3 ++ .../protobuf/compiler/parser_unittest.cc | 36 +++++++++++++------ 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/google/protobuf/compiler/parser.cc b/src/google/protobuf/compiler/parser.cc index e053eca7e5..b937fa25b9 100644 --- a/src/google/protobuf/compiler/parser.cc +++ b/src/google/protobuf/compiler/parser.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; diff --git a/src/google/protobuf/compiler/parser.h b/src/google/protobuf/compiler/parser.h index b7dfec6e25..600fef6adb 100644 --- a/src/google/protobuf/compiler/parser.h +++ b/src/google/protobuf/compiler/parser.h @@ -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); diff --git a/src/google/protobuf/compiler/parser_unittest.cc b/src/google/protobuf/compiler/parser_unittest.cc index c6dae84494..9cd34f155c 100644 --- a/src/google/protobuf/compiler/parser_unittest.cc +++ b/src/google/protobuf/compiler/parser_unittest.cc @@ -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) {