code_generator.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. #include <google/protobuf/compiler/code_generator.h>
  34. #include <google/protobuf/stubs/logging.h>
  35. #include <google/protobuf/stubs/common.h>
  36. #include <google/protobuf/compiler/plugin.pb.h>
  37. #include <google/protobuf/descriptor.h>
  38. #include <google/protobuf/stubs/strutil.h>
  39. namespace google {
  40. namespace protobuf {
  41. namespace compiler {
  42. CodeGenerator::~CodeGenerator() {}
  43. bool CodeGenerator::GenerateAll(
  44. const std::vector<const FileDescriptor*>& files,
  45. const string& parameter,
  46. GeneratorContext* generator_context,
  47. string* error) const {
  48. // Default implemenation is just to call the per file method, and prefix any
  49. // error string with the file to provide context.
  50. bool succeeded = true;
  51. for (int i = 0; i < files.size(); i++) {
  52. const FileDescriptor* file = files[i];
  53. succeeded = Generate(file, parameter, generator_context, error);
  54. if (!succeeded && error && error->empty()) {
  55. *error = "Code generator returned false but provided no error "
  56. "description.";
  57. }
  58. if (error && !error->empty()) {
  59. *error = file->name() + ": " + *error;
  60. break;
  61. }
  62. if (!succeeded) {
  63. break;
  64. }
  65. }
  66. return succeeded;
  67. }
  68. GeneratorContext::~GeneratorContext() {}
  69. io::ZeroCopyOutputStream*
  70. GeneratorContext::OpenForAppend(const string& filename) {
  71. return NULL;
  72. }
  73. io::ZeroCopyOutputStream* GeneratorContext::OpenForInsert(
  74. const string& filename, const string& insertion_point) {
  75. GOOGLE_LOG(FATAL) << "This GeneratorContext does not support insertion.";
  76. return NULL; // make compiler happy
  77. }
  78. void GeneratorContext::ListParsedFiles(
  79. std::vector<const FileDescriptor*>* output) {
  80. GOOGLE_LOG(FATAL) << "This GeneratorContext does not support ListParsedFiles";
  81. }
  82. void GeneratorContext::GetCompilerVersion(Version* version) const {
  83. version->set_major(GOOGLE_PROTOBUF_VERSION / 1000000);
  84. version->set_minor(GOOGLE_PROTOBUF_VERSION / 1000 % 1000);
  85. version->set_patch(GOOGLE_PROTOBUF_VERSION % 1000);
  86. version->set_suffix(GOOGLE_PROTOBUF_VERSION_SUFFIX);
  87. }
  88. // Parses a set of comma-delimited name/value pairs.
  89. void ParseGeneratorParameter(const string& text,
  90. std::vector<std::pair<string, string> >* output) {
  91. std::vector<string> parts = Split(text, ",", true);
  92. for (int i = 0; i < parts.size(); i++) {
  93. string::size_type equals_pos = parts[i].find_first_of('=');
  94. std::pair<string, string> value;
  95. if (equals_pos == string::npos) {
  96. value.first = parts[i];
  97. value.second = "";
  98. } else {
  99. value.first = parts[i].substr(0, equals_pos);
  100. value.second = parts[i].substr(equals_pos + 1);
  101. }
  102. output->push_back(value);
  103. }
  104. }
  105. } // namespace compiler
  106. } // namespace protobuf
  107. } // namespace google