Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(493)

Side by Side Diff: third_party/protobuf/src/google/protobuf/descriptor_database.cc

Issue 2495533002: third_party/protobuf: Update to HEAD (83d681ee2c) (Closed)
Patch Set: Make chrome settings proto generated file a component Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Protocol Buffers - Google's data interchange format 1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved. 2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/ 3 // https://developers.google.com/protocol-buffers/
4 // 4 //
5 // Redistribution and use in source and binary forms, with or without 5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are 6 // modification, are permitted provided that the following conditions are
7 // met: 7 // met:
8 // 8 //
9 // * Redistributions of source code must retain the above copyright 9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer. 10 // notice, this list of conditions and the following disclaimer.
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 // If the symbol name is invalid it could break our lookup algorithm (which 90 // If the symbol name is invalid it could break our lookup algorithm (which
91 // relies on the fact that '.' sorts before all other characters that are 91 // relies on the fact that '.' sorts before all other characters that are
92 // valid in symbol names). 92 // valid in symbol names).
93 if (!ValidateSymbolName(name)) { 93 if (!ValidateSymbolName(name)) {
94 GOOGLE_LOG(ERROR) << "Invalid symbol name: " << name; 94 GOOGLE_LOG(ERROR) << "Invalid symbol name: " << name;
95 return false; 95 return false;
96 } 96 }
97 97
98 // Try to look up the symbol to make sure a super-symbol doesn't already 98 // Try to look up the symbol to make sure a super-symbol doesn't already
99 // exist. 99 // exist.
100 typename map<string, Value>::iterator iter = FindLastLessOrEqual(name); 100 typename std::map<string, Value>::iterator iter = FindLastLessOrEqual(name);
101 101
102 if (iter == by_symbol_.end()) { 102 if (iter == by_symbol_.end()) {
103 // Apparently the map is currently empty. Just insert and be done with it. 103 // Apparently the map is currently empty. Just insert and be done with it.
104 by_symbol_.insert(typename map<string, Value>::value_type(name, value)); 104 by_symbol_.insert(
105 typename std::map<string, Value>::value_type(name, value));
105 return true; 106 return true;
106 } 107 }
107 108
108 if (IsSubSymbol(iter->first, name)) { 109 if (IsSubSymbol(iter->first, name)) {
109 GOOGLE_LOG(ERROR) << "Symbol name \"" << name << "\" conflicts with the exis ting " 110 GOOGLE_LOG(ERROR) << "Symbol name \"" << name << "\" conflicts with the exis ting "
110 "symbol \"" << iter->first << "\"."; 111 "symbol \"" << iter->first << "\".";
111 return false; 112 return false;
112 } 113 }
113 114
114 // OK, that worked. Now we have to make sure that no symbol in the map is 115 // OK, that worked. Now we have to make sure that no symbol in the map is
115 // a sub-symbol of the one we are inserting. The only symbol which could 116 // a sub-symbol of the one we are inserting. The only symbol which could
116 // be so is the first symbol that is greater than the new symbol. Since 117 // be so is the first symbol that is greater than the new symbol. Since
117 // |iter| points at the last symbol that is less than or equal, we just have 118 // |iter| points at the last symbol that is less than or equal, we just have
118 // to increment it. 119 // to increment it.
119 ++iter; 120 ++iter;
120 121
121 if (iter != by_symbol_.end() && IsSubSymbol(name, iter->first)) { 122 if (iter != by_symbol_.end() && IsSubSymbol(name, iter->first)) {
122 GOOGLE_LOG(ERROR) << "Symbol name \"" << name << "\" conflicts with the exis ting " 123 GOOGLE_LOG(ERROR) << "Symbol name \"" << name << "\" conflicts with the exis ting "
123 "symbol \"" << iter->first << "\"."; 124 "symbol \"" << iter->first << "\".";
124 return false; 125 return false;
125 } 126 }
126 127
127 // OK, no conflicts. 128 // OK, no conflicts.
128 129
129 // Insert the new symbol using the iterator as a hint, the new entry will 130 // Insert the new symbol using the iterator as a hint, the new entry will
130 // appear immediately before the one the iterator is pointing at. 131 // appear immediately before the one the iterator is pointing at.
131 by_symbol_.insert(iter, typename map<string, Value>::value_type(name, value)); 132 by_symbol_.insert(iter,
133 typename std::map<string, Value>::value_type(name, value));
132 134
133 return true; 135 return true;
134 } 136 }
135 137
136 template <typename Value> 138 template <typename Value>
137 bool SimpleDescriptorDatabase::DescriptorIndex<Value>::AddNestedExtensions( 139 bool SimpleDescriptorDatabase::DescriptorIndex<Value>::AddNestedExtensions(
138 const DescriptorProto& message_type, 140 const DescriptorProto& message_type,
139 Value value) { 141 Value value) {
140 for (int i = 0; i < message_type.nested_type_size(); i++) { 142 for (int i = 0; i < message_type.nested_type_size(); i++) {
141 if (!AddNestedExtensions(message_type.nested_type(i), value)) return false; 143 if (!AddNestedExtensions(message_type.nested_type(i), value)) return false;
(...skipping 30 matching lines...) Expand all
172 174
173 template <typename Value> 175 template <typename Value>
174 Value SimpleDescriptorDatabase::DescriptorIndex<Value>::FindFile( 176 Value SimpleDescriptorDatabase::DescriptorIndex<Value>::FindFile(
175 const string& filename) { 177 const string& filename) {
176 return FindWithDefault(by_name_, filename, Value()); 178 return FindWithDefault(by_name_, filename, Value());
177 } 179 }
178 180
179 template <typename Value> 181 template <typename Value>
180 Value SimpleDescriptorDatabase::DescriptorIndex<Value>::FindSymbol( 182 Value SimpleDescriptorDatabase::DescriptorIndex<Value>::FindSymbol(
181 const string& name) { 183 const string& name) {
182 typename map<string, Value>::iterator iter = FindLastLessOrEqual(name); 184 typename std::map<string, Value>::iterator iter = FindLastLessOrEqual(name);
183 185
184 return (iter != by_symbol_.end() && IsSubSymbol(iter->first, name)) ? 186 return (iter != by_symbol_.end() && IsSubSymbol(iter->first, name)) ?
185 iter->second : Value(); 187 iter->second : Value();
186 } 188 }
187 189
188 template <typename Value> 190 template <typename Value>
189 Value SimpleDescriptorDatabase::DescriptorIndex<Value>::FindExtension( 191 Value SimpleDescriptorDatabase::DescriptorIndex<Value>::FindExtension(
190 const string& containing_type, 192 const string& containing_type,
191 int field_number) { 193 int field_number) {
192 return FindWithDefault( 194 return FindWithDefault(
193 by_extension_, std::make_pair(containing_type, field_number), Value()); 195 by_extension_, std::make_pair(containing_type, field_number), Value());
194 } 196 }
195 197
196 template <typename Value> 198 template <typename Value>
197 bool SimpleDescriptorDatabase::DescriptorIndex<Value>::FindAllExtensionNumbers( 199 bool SimpleDescriptorDatabase::DescriptorIndex<Value>::FindAllExtensionNumbers(
198 const string& containing_type, 200 const string& containing_type,
199 vector<int>* output) { 201 std::vector<int>* output) {
200 typename map<pair<string, int>, Value>::const_iterator it = 202 typename std::map<pair<string, int>, Value>::const_iterator it =
201 by_extension_.lower_bound(std::make_pair(containing_type, 0)); 203 by_extension_.lower_bound(std::make_pair(containing_type, 0));
202 bool success = false; 204 bool success = false;
203 205
204 for (; it != by_extension_.end() && it->first.first == containing_type; 206 for (; it != by_extension_.end() && it->first.first == containing_type;
205 ++it) { 207 ++it) {
206 output->push_back(it->first.second); 208 output->push_back(it->first.second);
207 success = true; 209 success = true;
208 } 210 }
209 211
210 return success; 212 return success;
211 } 213 }
212 214
213 template <typename Value> 215 template <typename Value>
214 typename map<string, Value>::iterator 216 typename map<string, Value>::iterator
215 SimpleDescriptorDatabase::DescriptorIndex<Value>::FindLastLessOrEqual( 217 SimpleDescriptorDatabase::DescriptorIndex<Value>::FindLastLessOrEqual(
216 const string& name) { 218 const string& name) {
217 // Find the last key in the map which sorts less than or equal to the 219 // Find the last key in the map which sorts less than or equal to the
218 // symbol name. Since upper_bound() returns the *first* key that sorts 220 // symbol name. Since upper_bound() returns the *first* key that sorts
219 // *greater* than the input, we want the element immediately before that. 221 // *greater* than the input, we want the element immediately before that.
220 typename map<string, Value>::iterator iter = by_symbol_.upper_bound(name); 222 typename std::map<string, Value>::iterator iter =
223 by_symbol_.upper_bound(name);
221 if (iter != by_symbol_.begin()) --iter; 224 if (iter != by_symbol_.begin()) --iter;
222 return iter; 225 return iter;
223 } 226 }
224 227
225 template <typename Value> 228 template <typename Value>
226 bool SimpleDescriptorDatabase::DescriptorIndex<Value>::IsSubSymbol( 229 bool SimpleDescriptorDatabase::DescriptorIndex<Value>::IsSubSymbol(
227 const string& sub_symbol, const string& super_symbol) { 230 const string& sub_symbol, const string& super_symbol) {
228 return sub_symbol == super_symbol || 231 return sub_symbol == super_symbol ||
229 (HasPrefixString(super_symbol, sub_symbol) && 232 (HasPrefixString(super_symbol, sub_symbol) &&
230 super_symbol[sub_symbol.size()] == '.'); 233 super_symbol[sub_symbol.size()] == '.');
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
277 280
278 bool SimpleDescriptorDatabase::FindFileContainingExtension( 281 bool SimpleDescriptorDatabase::FindFileContainingExtension(
279 const string& containing_type, 282 const string& containing_type,
280 int field_number, 283 int field_number,
281 FileDescriptorProto* output) { 284 FileDescriptorProto* output) {
282 return MaybeCopy(index_.FindExtension(containing_type, field_number), output); 285 return MaybeCopy(index_.FindExtension(containing_type, field_number), output);
283 } 286 }
284 287
285 bool SimpleDescriptorDatabase::FindAllExtensionNumbers( 288 bool SimpleDescriptorDatabase::FindAllExtensionNumbers(
286 const string& extendee_type, 289 const string& extendee_type,
287 vector<int>* output) { 290 std::vector<int>* output) {
288 return index_.FindAllExtensionNumbers(extendee_type, output); 291 return index_.FindAllExtensionNumbers(extendee_type, output);
289 } 292 }
290 293
291 294
292 bool SimpleDescriptorDatabase::MaybeCopy(const FileDescriptorProto* file, 295 bool SimpleDescriptorDatabase::MaybeCopy(const FileDescriptorProto* file,
293 FileDescriptorProto* output) { 296 FileDescriptorProto* output) {
294 if (file == NULL) return false; 297 if (file == NULL) return false;
295 output->CopyFrom(*file); 298 output->CopyFrom(*file);
296 return true; 299 return true;
297 } 300 }
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
333 336
334 bool EncodedDescriptorDatabase::FindFileContainingSymbol( 337 bool EncodedDescriptorDatabase::FindFileContainingSymbol(
335 const string& symbol_name, 338 const string& symbol_name,
336 FileDescriptorProto* output) { 339 FileDescriptorProto* output) {
337 return MaybeParse(index_.FindSymbol(symbol_name), output); 340 return MaybeParse(index_.FindSymbol(symbol_name), output);
338 } 341 }
339 342
340 bool EncodedDescriptorDatabase::FindNameOfFileContainingSymbol( 343 bool EncodedDescriptorDatabase::FindNameOfFileContainingSymbol(
341 const string& symbol_name, 344 const string& symbol_name,
342 string* output) { 345 string* output) {
343 pair<const void*, int> encoded_file = index_.FindSymbol(symbol_name); 346 std::pair<const void*, int> encoded_file = index_.FindSymbol(symbol_name);
344 if (encoded_file.first == NULL) return false; 347 if (encoded_file.first == NULL) return false;
345 348
346 // Optimization: The name should be the first field in the encoded message. 349 // Optimization: The name should be the first field in the encoded message.
347 // Try to just read it directly. 350 // Try to just read it directly.
348 io::CodedInputStream input(reinterpret_cast<const uint8*>(encoded_file.first), 351 io::CodedInputStream input(reinterpret_cast<const uint8*>(encoded_file.first),
349 encoded_file.second); 352 encoded_file.second);
350 353
351 const uint32 kNameTag = internal::WireFormatLite::MakeTag( 354 const uint32 kNameTag = internal::WireFormatLite::MakeTag(
352 FileDescriptorProto::kNameFieldNumber, 355 FileDescriptorProto::kNameFieldNumber,
353 internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED); 356 internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED);
354 357
355 if (input.ReadTag() == kNameTag) { 358 if (input.ReadTagNoLastTag() == kNameTag) {
356 // Success! 359 // Success!
357 return internal::WireFormatLite::ReadString(&input, output); 360 return internal::WireFormatLite::ReadString(&input, output);
358 } else { 361 } else {
359 // Slow path. Parse whole message. 362 // Slow path. Parse whole message.
360 FileDescriptorProto file_proto; 363 FileDescriptorProto file_proto;
361 if (!file_proto.ParseFromArray(encoded_file.first, encoded_file.second)) { 364 if (!file_proto.ParseFromArray(encoded_file.first, encoded_file.second)) {
362 return false; 365 return false;
363 } 366 }
364 *output = file_proto.name(); 367 *output = file_proto.name();
365 return true; 368 return true;
366 } 369 }
367 } 370 }
368 371
369 bool EncodedDescriptorDatabase::FindFileContainingExtension( 372 bool EncodedDescriptorDatabase::FindFileContainingExtension(
370 const string& containing_type, 373 const string& containing_type,
371 int field_number, 374 int field_number,
372 FileDescriptorProto* output) { 375 FileDescriptorProto* output) {
373 return MaybeParse(index_.FindExtension(containing_type, field_number), 376 return MaybeParse(index_.FindExtension(containing_type, field_number),
374 output); 377 output);
375 } 378 }
376 379
377 bool EncodedDescriptorDatabase::FindAllExtensionNumbers( 380 bool EncodedDescriptorDatabase::FindAllExtensionNumbers(
378 const string& extendee_type, 381 const string& extendee_type,
379 vector<int>* output) { 382 std::vector<int>* output) {
380 return index_.FindAllExtensionNumbers(extendee_type, output); 383 return index_.FindAllExtensionNumbers(extendee_type, output);
381 } 384 }
382 385
383 bool EncodedDescriptorDatabase::MaybeParse( 386 bool EncodedDescriptorDatabase::MaybeParse(
384 pair<const void*, int> encoded_file, 387 std::pair<const void*, int> encoded_file,
385 FileDescriptorProto* output) { 388 FileDescriptorProto* output) {
386 if (encoded_file.first == NULL) return false; 389 if (encoded_file.first == NULL) return false;
387 return output->ParseFromArray(encoded_file.first, encoded_file.second); 390 return output->ParseFromArray(encoded_file.first, encoded_file.second);
388 } 391 }
389 392
390 // =================================================================== 393 // ===================================================================
391 394
392 DescriptorPoolDatabase::DescriptorPoolDatabase(const DescriptorPool& pool) 395 DescriptorPoolDatabase::DescriptorPoolDatabase(const DescriptorPool& pool)
393 : pool_(pool) {} 396 : pool_(pool) {}
394 DescriptorPoolDatabase::~DescriptorPoolDatabase() {} 397 DescriptorPoolDatabase::~DescriptorPoolDatabase() {}
(...skipping 29 matching lines...) Expand all
424 pool_.FindExtensionByNumber(extendee, field_number); 427 pool_.FindExtensionByNumber(extendee, field_number);
425 if (extension == NULL) return false; 428 if (extension == NULL) return false;
426 429
427 output->Clear(); 430 output->Clear();
428 extension->file()->CopyTo(output); 431 extension->file()->CopyTo(output);
429 return true; 432 return true;
430 } 433 }
431 434
432 bool DescriptorPoolDatabase::FindAllExtensionNumbers( 435 bool DescriptorPoolDatabase::FindAllExtensionNumbers(
433 const string& extendee_type, 436 const string& extendee_type,
434 vector<int>* output) { 437 std::vector<int>* output) {
435 const Descriptor* extendee = pool_.FindMessageTypeByName(extendee_type); 438 const Descriptor* extendee = pool_.FindMessageTypeByName(extendee_type);
436 if (extendee == NULL) return false; 439 if (extendee == NULL) return false;
437 440
438 vector<const FieldDescriptor*> extensions; 441 std::vector<const FieldDescriptor*> extensions;
439 pool_.FindAllExtensions(extendee, &extensions); 442 pool_.FindAllExtensions(extendee, &extensions);
440 443
441 for (int i = 0; i < extensions.size(); ++i) { 444 for (int i = 0; i < extensions.size(); ++i) {
442 output->push_back(extensions[i]->number()); 445 output->push_back(extensions[i]->number());
443 } 446 }
444 447
445 return true; 448 return true;
446 } 449 }
447 450
448 // =================================================================== 451 // ===================================================================
449 452
450 MergedDescriptorDatabase::MergedDescriptorDatabase( 453 MergedDescriptorDatabase::MergedDescriptorDatabase(
451 DescriptorDatabase* source1, 454 DescriptorDatabase* source1,
452 DescriptorDatabase* source2) { 455 DescriptorDatabase* source2) {
453 sources_.push_back(source1); 456 sources_.push_back(source1);
454 sources_.push_back(source2); 457 sources_.push_back(source2);
455 } 458 }
456 MergedDescriptorDatabase::MergedDescriptorDatabase( 459 MergedDescriptorDatabase::MergedDescriptorDatabase(
457 const vector<DescriptorDatabase*>& sources) 460 const std::vector<DescriptorDatabase*>& sources)
458 : sources_(sources) {} 461 : sources_(sources) {}
459 MergedDescriptorDatabase::~MergedDescriptorDatabase() {} 462 MergedDescriptorDatabase::~MergedDescriptorDatabase() {}
460 463
461 bool MergedDescriptorDatabase::FindFileByName( 464 bool MergedDescriptorDatabase::FindFileByName(
462 const string& filename, 465 const string& filename,
463 FileDescriptorProto* output) { 466 FileDescriptorProto* output) {
464 for (int i = 0; i < sources_.size(); i++) { 467 for (int i = 0; i < sources_.size(); i++) {
465 if (sources_[i]->FindFileByName(filename, output)) { 468 if (sources_[i]->FindFileByName(filename, output)) {
466 return true; 469 return true;
467 } 470 }
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
510 } 513 }
511 } 514 }
512 return true; 515 return true;
513 } 516 }
514 } 517 }
515 return false; 518 return false;
516 } 519 }
517 520
518 bool MergedDescriptorDatabase::FindAllExtensionNumbers( 521 bool MergedDescriptorDatabase::FindAllExtensionNumbers(
519 const string& extendee_type, 522 const string& extendee_type,
520 vector<int>* output) { 523 std::vector<int>* output) {
521 set<int> merged_results; 524 std::set<int> merged_results;
522 vector<int> results; 525 std::vector<int> results;
523 bool success = false; 526 bool success = false;
524 527
525 for (int i = 0; i < sources_.size(); i++) { 528 for (int i = 0; i < sources_.size(); i++) {
526 if (sources_[i]->FindAllExtensionNumbers(extendee_type, &results)) { 529 if (sources_[i]->FindAllExtensionNumbers(extendee_type, &results)) {
527 std::copy( 530 std::copy(results.begin(), results.end(),
528 results.begin(), results.end(), 531 std::insert_iterator<std::set<int> >(merged_results,
529 insert_iterator<set<int> >(merged_results, merged_results.begin())); 532 merged_results.begin()));
530 success = true; 533 success = true;
531 } 534 }
532 results.clear(); 535 results.clear();
533 } 536 }
534 537
535 std::copy(merged_results.begin(), merged_results.end(), 538 std::copy(merged_results.begin(), merged_results.end(),
536 insert_iterator<vector<int> >(*output, output->end())); 539 std::insert_iterator<std::vector<int> >(*output, output->end()));
537 540
538 return success; 541 return success;
539 } 542 }
540 543
541 544
542 } // namespace protobuf 545 } // namespace protobuf
543 } // namespace google 546 } // namespace google
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698