aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/google/protobuf/descriptor.cc
diff options
context:
space:
mode:
authorGravatar Jisi Liu <jisi.liu@gmail.com>2015-02-28 14:51:22 -0800
committerGravatar Jisi Liu <jisi.liu@gmail.com>2015-02-28 17:06:49 -0800
commit885b612f74f133678bf82808c589331e4c59dad9 (patch)
treee5f3f65b41af477c52810053b8694896c8bcd1f7 /src/google/protobuf/descriptor.cc
parent1939efed2db35020b7830a4927f10feac47b6757 (diff)
Down integrate from Google internal branch for C++ and Java.
- Maps for C++ lite - C++ Arena optimizations. - Java Lite runtime code size optimization. Change-Id: I7537a4357c1cb385d23f9e8aa7ffdfeefe079f13
Diffstat (limited to 'src/google/protobuf/descriptor.cc')
-rw-r--r--src/google/protobuf/descriptor.cc88
1 files changed, 60 insertions, 28 deletions
diff --git a/src/google/protobuf/descriptor.cc b/src/google/protobuf/descriptor.cc
index b8dd198d..bfdacd95 100644
--- a/src/google/protobuf/descriptor.cc
+++ b/src/google/protobuf/descriptor.cc
@@ -783,7 +783,7 @@ inline const FileDescriptor* DescriptorPool::Tables::FindFile(
inline const FieldDescriptor* FileDescriptorTables::FindFieldByNumber(
const Descriptor* parent, int number) const {
- return FindPtrOrNull(fields_by_number_, make_pair(parent, number));
+ return FindPtrOrNull(fields_by_number_, std::make_pair(parent, number));
}
inline const FieldDescriptor* FileDescriptorTables::FindFieldByLowercaseName(
@@ -800,7 +800,7 @@ inline const FieldDescriptor* FileDescriptorTables::FindFieldByCamelcaseName(
inline const EnumValueDescriptor* FileDescriptorTables::FindEnumValueByNumber(
const EnumDescriptor* parent, int number) const {
- return FindPtrOrNull(enum_values_by_number_, make_pair(parent, number));
+ return FindPtrOrNull(enum_values_by_number_, std::make_pair(parent, number));
}
inline const EnumValueDescriptor*
@@ -808,8 +808,8 @@ FileDescriptorTables::FindEnumValueByNumberCreatingIfUnknown(
const EnumDescriptor* parent, int number) const {
// First try, with map of compiled-in values.
{
- const EnumValueDescriptor* desc = FindPtrOrNull(
- enum_values_by_number_, make_pair(parent, number));
+ const EnumValueDescriptor* desc =
+ FindPtrOrNull(enum_values_by_number_, std::make_pair(parent, number));
if (desc != NULL) {
return desc;
}
@@ -818,7 +818,7 @@ FileDescriptorTables::FindEnumValueByNumberCreatingIfUnknown(
{
ReaderMutexLock l(&unknown_enum_values_mu_);
const EnumValueDescriptor* desc = FindPtrOrNull(
- unknown_enum_values_by_number_, make_pair(parent, number));
+ unknown_enum_values_by_number_, std::make_pair(parent, number));
if (desc != NULL) {
return desc;
}
@@ -828,7 +828,7 @@ FileDescriptorTables::FindEnumValueByNumberCreatingIfUnknown(
{
WriterMutexLock l(&unknown_enum_values_mu_);
const EnumValueDescriptor* desc = FindPtrOrNull(
- unknown_enum_values_by_number_, make_pair(parent, number));
+ unknown_enum_values_by_number_, std::make_pair(parent, number));
if (desc != NULL) {
return desc;
}
@@ -850,8 +850,7 @@ FileDescriptorTables::FindEnumValueByNumberCreatingIfUnknown(
result->type_ = parent;
result->options_ = &EnumValueOptions::default_instance();
InsertIfNotPresent(&unknown_enum_values_by_number_,
- make_pair(parent, number),
- result);
+ std::make_pair(parent, number), result);
return result;
}
}
@@ -859,13 +858,13 @@ FileDescriptorTables::FindEnumValueByNumberCreatingIfUnknown(
inline const FieldDescriptor* DescriptorPool::Tables::FindExtension(
const Descriptor* extendee, int number) {
- return FindPtrOrNull(extensions_, make_pair(extendee, number));
+ return FindPtrOrNull(extensions_, std::make_pair(extendee, number));
}
inline void DescriptorPool::Tables::FindAllExtensions(
const Descriptor* extendee, vector<const FieldDescriptor*>* out) const {
ExtensionsGroupedByDescriptorMap::const_iterator it =
- extensions_.lower_bound(make_pair(extendee, 0));
+ extensions_.lower_bound(std::make_pair(extendee, 0));
for (; it != extensions_.end() && it->first.first == extendee; ++it) {
out->push_back(it->second);
}
@@ -993,7 +992,7 @@ void FileDescriptorTables::BuildLocationsByPath(
const SourceCodeInfo_Location* FileDescriptorTables::GetSourceLocation(
const vector<int>& path, const SourceCodeInfo* info) const {
pair<const FileDescriptorTables*, const SourceCodeInfo*> p(
- make_pair(this, info));
+ std::make_pair(this, info));
locations_by_path_once_.Init(&FileDescriptorTables::BuildLocationsByPath, &p);
return FindPtrOrNull(locations_by_path_, Join(path, ","));
}
@@ -1929,9 +1928,9 @@ bool FormatLineOptions(int depth, const Message &options, string *output) {
return !all_options.empty();
}
-template<typename DescType>
class SourceLocationCommentPrinter {
public:
+ template<typename DescType>
SourceLocationCommentPrinter(const DescType* desc,
const string& prefix,
const DebugStringOptions& options)
@@ -1941,9 +1940,27 @@ class SourceLocationCommentPrinter {
have_source_loc_ = options.include_comments &&
desc->GetSourceLocation(&source_loc_);
}
+ SourceLocationCommentPrinter(const FileDescriptor* file,
+ const vector<int>& path,
+ const string& prefix,
+ const DebugStringOptions& options)
+ : options_(options), prefix_(prefix) {
+ // Perform the SourceLocation lookup only if we're including user comments,
+ // because the lookup is fairly expensive.
+ have_source_loc_ = options.include_comments &&
+ file->GetSourceLocation(path, &source_loc_);
+ }
void AddPreComment(string* output) {
- if (have_source_loc_ && source_loc_.leading_comments.size() > 0) {
- *output += FormatComment(source_loc_.leading_comments);
+ if (have_source_loc_) {
+ // Detached leading comments.
+ for (int i = 0 ; i < source_loc_.leading_detached_comments.size(); ++i) {
+ *output += FormatComment(source_loc_.leading_detached_comments[i]);
+ *output += "\n";
+ }
+ // Attached leading comments.
+ if (!source_loc_.leading_comments.empty()) {
+ *output += FormatComment(source_loc_.leading_comments);
+ }
}
}
void AddPostComment(string* output) {
@@ -1967,7 +1984,6 @@ class SourceLocationCommentPrinter {
}
private:
- const DescType* desc_;
bool have_source_loc_;
SourceLocation source_loc_;
DebugStringOptions options_;
@@ -1984,10 +2000,18 @@ string FileDescriptor::DebugString() const {
string FileDescriptor::DebugStringWithOptions(
const DebugStringOptions& debug_string_options) const {
string contents;
- strings::SubstituteAndAppend(&contents, "syntax = \"$0\";\n\n",
- SyntaxName(syntax()));
+ {
+ vector<int> path;
+ path.push_back(FileDescriptorProto::kSyntaxFieldNumber);
+ SourceLocationCommentPrinter syntax_comment(
+ this, path, "", debug_string_options);
+ syntax_comment.AddPreComment(&contents);
+ strings::SubstituteAndAppend(&contents, "syntax = \"$0\";\n\n",
+ SyntaxName(syntax()));
+ syntax_comment.AddPostComment(&contents);
+ }
- SourceLocationCommentPrinter<FileDescriptor>
+ SourceLocationCommentPrinter
comment_printer(this, "", debug_string_options);
comment_printer.AddPreComment(&contents);
@@ -2012,7 +2036,13 @@ string FileDescriptor::DebugStringWithOptions(
}
if (!package().empty()) {
+ vector<int> path;
+ path.push_back(FileDescriptorProto::kPackageFieldNumber);
+ SourceLocationCommentPrinter package_comment(
+ this, path, "", debug_string_options);
+ package_comment.AddPreComment(&contents);
strings::SubstituteAndAppend(&contents, "package $0;\n\n", package());
+ package_comment.AddPostComment(&contents);
}
if (FormatLineOptions(0, options(), &contents)) {
@@ -2087,7 +2117,7 @@ void Descriptor::DebugString(int depth, string *contents,
string prefix(depth * 2, ' ');
++depth;
- SourceLocationCommentPrinter<Descriptor>
+ SourceLocationCommentPrinter
comment_printer(this, prefix, debug_string_options);
comment_printer.AddPreComment(contents);
@@ -2217,7 +2247,7 @@ void FieldDescriptor::DebugString(int depth,
label.push_back(' ');
}
- SourceLocationCommentPrinter<FieldDescriptor>
+ SourceLocationCommentPrinter
comment_printer(this, prefix, debug_string_options);
comment_printer.AddPreComment(contents);
@@ -2274,7 +2304,7 @@ void OneofDescriptor::DebugString(int depth, string* contents,
debug_string_options) const {
string prefix(depth * 2, ' ');
++depth;
- SourceLocationCommentPrinter<OneofDescriptor>
+ SourceLocationCommentPrinter
comment_printer(this, prefix, debug_string_options);
comment_printer.AddPreComment(contents);
strings::SubstituteAndAppend(
@@ -2305,7 +2335,7 @@ void EnumDescriptor::DebugString(int depth, string *contents,
string prefix(depth * 2, ' ');
++depth;
- SourceLocationCommentPrinter<EnumDescriptor>
+ SourceLocationCommentPrinter
comment_printer(this, prefix, debug_string_options);
comment_printer.AddPreComment(contents);
@@ -2339,7 +2369,7 @@ void EnumValueDescriptor::DebugString(int depth, string *contents,
debug_string_options) const {
string prefix(depth * 2, ' ');
- SourceLocationCommentPrinter<EnumValueDescriptor>
+ SourceLocationCommentPrinter
comment_printer(this, prefix, debug_string_options);
comment_printer.AddPreComment(contents);
@@ -2370,7 +2400,7 @@ string ServiceDescriptor::DebugStringWithOptions(
void ServiceDescriptor::DebugString(string *contents,
const DebugStringOptions&
debug_string_options) const {
- SourceLocationCommentPrinter<ServiceDescriptor>
+ SourceLocationCommentPrinter
comment_printer(this, /* prefix */ "", debug_string_options);
comment_printer.AddPreComment(contents);
@@ -2405,7 +2435,7 @@ void MethodDescriptor::DebugString(int depth, string *contents,
string prefix(depth * 2, ' ');
++depth;
- SourceLocationCommentPrinter<MethodDescriptor>
+ SourceLocationCommentPrinter
comment_printer(this, prefix, debug_string_options);
comment_printer.AddPreComment(contents);
@@ -2445,6 +2475,9 @@ bool FileDescriptor::GetSourceLocation(const vector<int>& path,
out_location->leading_comments = loc->leading_comments();
out_location->trailing_comments = loc->trailing_comments();
+ out_location->leading_detached_comments.assign(
+ loc->leading_detached_comments().begin(),
+ loc->leading_detached_comments().end());
return true;
}
}
@@ -3697,7 +3730,6 @@ const FileDescriptor* DescriptorBuilder::BuildFile(
"Unrecognized syntax: " + proto.syntax());
}
-
result->name_ = tables_->AllocateString(proto.name());
if (proto.has_package()) {
result->package_ = tables_->AllocateString(proto.package());
@@ -5110,7 +5142,7 @@ bool DescriptorBuilder::ValidateMapEntry(FieldDescriptor* field,
case FieldDescriptor::TYPE_SFIXED64:
// Legal cases
break;
- // Do not add a default, so that the compiler will complian when new types
+ // Do not add a default, so that the compiler will complain when new types
// are added.
}
@@ -5123,7 +5155,7 @@ void DescriptorBuilder::DetectMapConflicts(const Descriptor* message,
for (int i = 0; i < message->nested_type_count(); ++i) {
const Descriptor* nested = message->nested_type(i);
pair<map<string, const Descriptor*>::iterator, bool> result =
- seen_types.insert(make_pair(nested->name(), nested));
+ seen_types.insert(std::make_pair(nested->name(), nested));
if (!result.second) {
if (result.first->second->options().map_entry() ||
nested->options().map_entry()) {