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); 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) { if (error_collector_ != nullptr) {
error_collector_->AddWarning(input_->current().line, error_collector_->AddWarning(line, column, warning);
input_->current().column, warning);
} }
} }
void Parser::AddWarning(const std::string& warning) {
AddWarning(input_->current().line, input_->current().column, warning);
}
// ------------------------------------------------------------------- // -------------------------------------------------------------------
Parser::LocationRecorder::LocationRecorder(Parser* parser) Parser::LocationRecorder::LocationRecorder(Parser* parser)
@ -1736,7 +1739,7 @@ bool Parser::ParseReservedName(std::string* name, const char* error_message) {
int col = input_->current().column; int col = input_->current().column;
DO(ConsumeString(name, error_message)); DO(ConsumeString(name, error_message));
if (!io::Tokenizer::IsIdentifier(*name)) { 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 false;
} }
return true; return true;

@ -213,6 +213,9 @@ class PROTOBUF_EXPORT Parser {
// of the current token. // of the current token.
void AddError(const std::string& error); 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 // Invokes error_collector_->AddWarning() with the line and column number
// of the current token. // of the current token.
void AddWarning(const std::string& warning); void AddWarning(const std::string& warning);

@ -147,6 +147,16 @@ class ParserTest : public testing::Test {
EXPECT_EQ(io::Tokenizer::TYPE_END, input_->current().type); 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 // Same as above but does not expect that the parser parses the complete
// input. // input.
void ExpectHasEarlyExitErrors(const char* text, const char* expected_errors) { void ExpectHasEarlyExitErrors(const char* text, const char* expected_errors) {
@ -1676,12 +1686,14 @@ TEST_F(ParseErrorTest, EnumReservedMissingQuotes) {
} }
TEST_F(ParseErrorTest, EnumReservedInvalidIdentifier) { TEST_F(ParseErrorTest, EnumReservedInvalidIdentifier) {
ExpectHasErrors( ExpectHasWarnings(
"enum TestEnum {\n" R"pb(
" FOO = 1;\n" enum TestEnum {
" reserved \"foo bar\";\n" FOO = 1;
"}\n", reserved "foo bar";
"2:11: Reserved name \"foo bar\" is not a valid identifier.\n"); }
)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) { TEST_F(ParseErrorTest, ReservedInvalidIdentifier) {
ExpectHasErrors( ExpectHasWarnings(
"message Foo {\n" R"pb(
" reserved \"foo bar\";\n" message Foo {
"}\n", reserved "foo bar";
"1:11: Reserved name \"foo bar\" is not a valid identifier.\n"); }
)pb",
"2:17: Reserved name \"foo bar\" is not a valid identifier.\n");
} }
TEST_F(ParseErrorTest, ReservedNegativeNumber) { TEST_F(ParseErrorTest, ReservedNegativeNumber) {

Loading…
Cancel
Save