diff --git a/src/google/protobuf/descriptor.cc b/src/google/protobuf/descriptor.cc index f321a86ad7..a693489319 100644 --- a/src/google/protobuf/descriptor.cc +++ b/src/google/protobuf/descriptor.cc @@ -1821,6 +1821,35 @@ const SourceCodeInfo_Location* FileDescriptorTables::GetSourceLocation( DescriptorPool::ErrorCollector::~ErrorCollector() {} +absl::string_view DescriptorPool::ErrorCollector::ErrorLocationName( + ErrorLocation location) { + switch (location) { + case NAME: + return "NAME"; + case NUMBER: + return "NUMBER"; + case TYPE: + return "TYPE"; + case EXTENDEE: + return "EXTENDEE"; + case DEFAULT_VALUE: + return "DEFAULT_VALUE"; + case OPTION_NAME: + return "OPTION_NAME"; + case OPTION_VALUE: + return "OPTION_VALUE"; + case INPUT_TYPE: + return "INPUT_TYPE"; + case OUTPUT_TYPE: + return "OUTPUT_TYPE"; + case IMPORT: + return "IMPORT"; + case OTHER: + return "OTHER"; + } + return "UNKNOWN"; +} + DescriptorPool::DescriptorPool() : mutex_(nullptr), fallback_database_(nullptr), diff --git a/src/google/protobuf/descriptor.h b/src/google/protobuf/descriptor.h index 12b996ffdc..87ae9edb28 100644 --- a/src/google/protobuf/descriptor.h +++ b/src/google/protobuf/descriptor.h @@ -1949,8 +1949,9 @@ class PROTOBUF_EXPORT DescriptorPool { OPTION_NAME, // name in assignment OPTION_VALUE, // value in option assignment IMPORT, // import error - OTHER // some other problem + OTHER // some other problem }; + static absl::string_view ErrorLocationName(ErrorLocation location); // Reports an error in the FileDescriptorProto. Use this function if the // problem occurred should interrupt building the FileDescriptorProto. diff --git a/src/google/protobuf/descriptor_unittest.cc b/src/google/protobuf/descriptor_unittest.cc index b692b0ce13..21d356b146 100644 --- a/src/google/protobuf/descriptor_unittest.cc +++ b/src/google/protobuf/descriptor_unittest.cc @@ -216,90 +216,18 @@ class MockErrorCollector : public DescriptorPool::ErrorCollector { void RecordError(absl::string_view filename, absl::string_view element_name, const Message* descriptor, ErrorLocation location, absl::string_view message) override { - const char* location_name = nullptr; - switch (location) { - case NAME: - location_name = "NAME"; - break; - case NUMBER: - location_name = "NUMBER"; - break; - case TYPE: - location_name = "TYPE"; - break; - case EXTENDEE: - location_name = "EXTENDEE"; - break; - case DEFAULT_VALUE: - location_name = "DEFAULT_VALUE"; - break; - case OPTION_NAME: - location_name = "OPTION_NAME"; - break; - case OPTION_VALUE: - location_name = "OPTION_VALUE"; - break; - case INPUT_TYPE: - location_name = "INPUT_TYPE"; - break; - case OUTPUT_TYPE: - location_name = "OUTPUT_TYPE"; - break; - case IMPORT: - location_name = "IMPORT"; - break; - case OTHER: - location_name = "OTHER"; - break; - } - absl::SubstituteAndAppend(&text_, "$0: $1: $2: $3\n", filename, - element_name, location_name, message); + element_name, ErrorLocationName(location), + message); } // implements ErrorCollector --------------------------------------- void RecordWarning(absl::string_view filename, absl::string_view element_name, const Message* descriptor, ErrorLocation location, absl::string_view message) override { - const char* location_name = nullptr; - switch (location) { - case NAME: - location_name = "NAME"; - break; - case NUMBER: - location_name = "NUMBER"; - break; - case TYPE: - location_name = "TYPE"; - break; - case EXTENDEE: - location_name = "EXTENDEE"; - break; - case DEFAULT_VALUE: - location_name = "DEFAULT_VALUE"; - break; - case OPTION_NAME: - location_name = "OPTION_NAME"; - break; - case OPTION_VALUE: - location_name = "OPTION_VALUE"; - break; - case INPUT_TYPE: - location_name = "INPUT_TYPE"; - break; - case OUTPUT_TYPE: - location_name = "OUTPUT_TYPE"; - break; - case IMPORT: - location_name = "IMPORT"; - break; - case OTHER: - location_name = "OTHER"; - break; - } - absl::SubstituteAndAppend(&warning_text_, "$0: $1: $2: $3\n", filename, - element_name, location_name, message); + element_name, ErrorLocationName(location), + message); } };