浏览代码

Report a warning if the proto file doesn't have a syntax statement.

Feng Xiao 11 年之前
父节点
当前提交
3eb55df6a4
共有 2 个文件被更改,包括 13 次插入0 次删除
  1. 4 0
      src/google/protobuf/compiler/parser.cc
  2. 9 0
      src/google/protobuf/compiler/parser_unittest.cc

+ 4 - 0
src/google/protobuf/compiler/parser.cc

@@ -468,6 +468,10 @@ bool Parser::Parse(io::Tokenizer* input, FileDescriptorProto* file) {
       // Store the syntax into the file.
       // Store the syntax into the file.
       if (file != NULL) file->set_syntax(syntax_identifier_);
       if (file != NULL) file->set_syntax(syntax_identifier_);
     } else if (!stop_after_syntax_identifier_) {
     } else if (!stop_after_syntax_identifier_) {
+      GOOGLE_LOG(WARNING) << "No syntax specified for the proto file. "
+                          << "Please use 'syntax = \"proto2\";' or "
+                          << "'syntax = \"proto3\";' to specify a syntax "
+                          << "version. (Defaulted to proto2 syntax.)";
       syntax_identifier_ = "proto2";
       syntax_identifier_ = "proto2";
     }
     }
 
 

+ 9 - 0
src/google/protobuf/compiler/parser_unittest.cc

@@ -216,6 +216,15 @@ TEST_F(ParserTest, StopAfterSyntaxIdentifierWithErrors) {
   EXPECT_EQ("1:9: Expected syntax identifier.\n", error_collector_.text_);
   EXPECT_EQ("1:9: Expected syntax identifier.\n", error_collector_.text_);
 }
 }
 
 
+TEST_F(ParserTest, WarnIfSyntaxIdentifierOmmitted) {
+  SetupParser("message A {}");
+  FileDescriptorProto file;
+  CaptureTestStderr();
+  EXPECT_TRUE(parser_->Parse(input_.get(), &file));
+  EXPECT_TRUE(
+      GetCapturedTestStderr().find("No syntax specified") != string::npos);
+}
+
 // ===================================================================
 // ===================================================================
 
 
 typedef ParserTest ParseMessageTest;
 typedef ParserTest ParseMessageTest;