~ubuntu-branches/debian/stretch/protobuf/stretch

« back to all changes in this revision

Viewing changes to src/google/protobuf/compiler/cpp/cpp_helpers.cc

  • Committer: Package Import Robot
  • Author(s): Robert S. Edmonds
  • Date: 2014-09-11 22:50:10 UTC
  • mfrom: (10.1.9 experimental)
  • Revision ID: package-import@ubuntu.com-20140911225010-wt4yo9dpc1fzuq5g
Tags: 2.6.0-3
Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
82
82
 
83
83
hash_set<string> kKeywords = MakeKeywordsMap();
84
84
 
 
85
// Returns whether the provided descriptor has an extension. This includes its
 
86
// nested types.
 
87
bool HasExtension(const Descriptor* descriptor) {
 
88
  if (descriptor->extension_count() > 0) {
 
89
    return true;
 
90
  }
 
91
  for (int i = 0; i < descriptor->nested_type_count(); ++i) {
 
92
    if (HasExtension(descriptor->nested_type(i))) {
 
93
      return true;
 
94
    }
 
95
  }
 
96
  return false;
 
97
}
 
98
 
 
99
}  // namespace
 
100
 
85
101
string UnderscoresToCamelCase(const string& input, bool cap_next_letter) {
86
102
  string result;
87
103
  // Note:  I distrust ctype.h due to locales.
107
123
  return result;
108
124
}
109
125
 
110
 
// Returns whether the provided descriptor has an extension. This includes its
111
 
// nested types.
112
 
bool HasExtension(const Descriptor* descriptor) {
113
 
  if (descriptor->extension_count() > 0) {
114
 
    return true;
115
 
  }
116
 
  for (int i = 0; i < descriptor->nested_type_count(); ++i) {
117
 
    if (HasExtension(descriptor->nested_type(i))) {
118
 
      return true;
119
 
    }
120
 
  }
121
 
  return false;
122
 
}
123
 
 
124
 
}  // namespace
125
 
 
126
126
const char kThickSeparator[] =
127
127
  "// ===================================================================\n";
128
128
const char kThinSeparator[] =
256
256
  return "";
257
257
}
258
258
 
 
259
string Int32ToString(int number) {
 
260
  // gcc rejects the decimal form of kint32min.
 
261
  if (number == kint32min) {
 
262
    GOOGLE_COMPILE_ASSERT(kint32min == (~0x7fffffff), kint32min_value_error);
 
263
    return "(~0x7fffffff)";
 
264
  } else {
 
265
    return SimpleItoa(number);
 
266
  }
 
267
}
 
268
 
 
269
string Int64ToString(int64 number) {
 
270
  // gcc rejects the decimal form of kint64min
 
271
  if (number == kint64min) {
 
272
    // Make sure we are in a 2's complement system.
 
273
    GOOGLE_COMPILE_ASSERT(kint64min == GOOGLE_LONGLONG(~0x7fffffffffffffff),
 
274
                   kint64min_value_error);
 
275
    return "GOOGLE_LONGLONG(~0x7fffffffffffffff)";
 
276
  }
 
277
  return "GOOGLE_LONGLONG(" + SimpleItoa(number) + ")";
 
278
}
 
279
 
259
280
string DefaultValue(const FieldDescriptor* field) {
260
281
  switch (field->cpp_type()) {
261
282
    case FieldDescriptor::CPPTYPE_INT32:
262
 
      // gcc rejects the decimal form of kint32min and kint64min.
263
 
      if (field->default_value_int32() == kint32min) {
264
 
        // Make sure we are in a 2's complement system.
265
 
        GOOGLE_COMPILE_ASSERT(kint32min == -0x80000000, kint32min_value_error);
266
 
        return "-0x80000000";
267
 
      }
268
 
      return SimpleItoa(field->default_value_int32());
 
283
      return Int32ToString(field->default_value_int32());
269
284
    case FieldDescriptor::CPPTYPE_UINT32:
270
285
      return SimpleItoa(field->default_value_uint32()) + "u";
271
286
    case FieldDescriptor::CPPTYPE_INT64:
272
 
      // See the comments for CPPTYPE_INT32.
273
 
      if (field->default_value_int64() == kint64min) {
274
 
        // Make sure we are in a 2's complement system.
275
 
        GOOGLE_COMPILE_ASSERT(kint64min == GOOGLE_LONGLONG(-0x8000000000000000),
276
 
                       kint64min_value_error);
277
 
        return "GOOGLE_LONGLONG(-0x8000000000000000)";
278
 
      }
279
 
      return "GOOGLE_LONGLONG(" + SimpleItoa(field->default_value_int64()) + ")";
 
287
      return Int64ToString(field->default_value_int64());
280
288
    case FieldDescriptor::CPPTYPE_UINT64:
281
289
      return "GOOGLE_ULONGLONG(" + SimpleItoa(field->default_value_uint64())+ ")";
282
290
    case FieldDescriptor::CPPTYPE_DOUBLE: {
319
327
      return strings::Substitute(
320
328
          "static_cast< $0 >($1)",
321
329
          ClassName(field->enum_type(), true),
322
 
          field->default_value_enum()->number());
 
330
          Int32ToString(field->default_value_enum()->number()));
323
331
    case FieldDescriptor::CPPTYPE_STRING:
324
332
      return "\"" + EscapeTrigraphs(
325
333
        CEscape(field->default_value_string())) +
366
374
  return "protobuf_ShutdownFile_" + FilenameIdentifier(filename);
367
375
}
368
376
 
 
377
// Return the qualified C++ name for a file level symbol.
 
378
string QualifiedFileLevelSymbol(const string& package, const string& name) {
 
379
  if (package.empty()) {
 
380
    return StrCat("::", name);
 
381
  }
 
382
  return StrCat("::", DotsToColons(package), "::", name);
 
383
}
 
384
 
369
385
// Escape C++ trigraphs by escaping question marks to \?
370
386
string EscapeTrigraphs(const string& to_escape) {
371
387
  return StringReplace(to_escape, "?", "\\?", true);
372
388
}
373
389
 
 
390
// Escaped function name to eliminate naming conflict.
 
391
string SafeFunctionName(const Descriptor* descriptor,
 
392
                        const FieldDescriptor* field,
 
393
                        const string& prefix) {
 
394
  // Do not use FieldName() since it will escape keywords.
 
395
  string name = field->name();
 
396
  LowerString(&name);
 
397
  string function_name = prefix + name;
 
398
  if (descriptor->FindFieldByName(function_name)) {
 
399
    // Single underscore will also make it conflicting with the private data
 
400
    // member. We use double underscore to escape function names.
 
401
    function_name.append("__");
 
402
  } else if (kKeywords.count(name) > 0) {
 
403
    // If the field name is a keyword, we append the underscore back to keep it
 
404
    // consistent with other function names.
 
405
    function_name.append("_");
 
406
  }
 
407
  return function_name;
 
408
}
 
409
 
374
410
bool StaticInitializersForced(const FileDescriptor* file) {
375
411
  if (HasDescriptorMethods(file) || file->extension_count() > 0) {
376
412
    return true;
432
468
  return false;
433
469
}
434
470
 
 
471
bool IsStringOrMessage(const FieldDescriptor* field) {
 
472
  switch (field->cpp_type()) {
 
473
    case FieldDescriptor::CPPTYPE_INT32:
 
474
    case FieldDescriptor::CPPTYPE_INT64:
 
475
    case FieldDescriptor::CPPTYPE_UINT32:
 
476
    case FieldDescriptor::CPPTYPE_UINT64:
 
477
    case FieldDescriptor::CPPTYPE_DOUBLE:
 
478
    case FieldDescriptor::CPPTYPE_FLOAT:
 
479
    case FieldDescriptor::CPPTYPE_BOOL:
 
480
    case FieldDescriptor::CPPTYPE_ENUM:
 
481
      return false;
 
482
    case FieldDescriptor::CPPTYPE_STRING:
 
483
    case FieldDescriptor::CPPTYPE_MESSAGE:
 
484
      return true;
 
485
  }
 
486
 
 
487
  GOOGLE_LOG(FATAL) << "Can't get here.";
 
488
  return false;
 
489
}
 
490
 
435
491
}  // namespace cpp
436
492
}  // namespace compiler
437
493
}  // namespace protobuf