/thirdparty/breakpad/third_party/protobuf/protobuf/src/google/protobuf/compiler/plugin.cc

http://github.com/tomahawk-player/tomahawk · C++ · 163 lines · 105 code · 26 blank · 32 comment · 13 complexity · 851dd8b5a7e0cc56782d57dc9fc5afd9 MD5 · raw file

  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://code.google.com/p/protobuf/
  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. #include <google/protobuf/compiler/plugin.h>
  32. #include <iostream>
  33. #include <set>
  34. #ifdef _WIN32
  35. #include <io.h>
  36. #include <fcntl.h>
  37. #ifndef STDIN_FILENO
  38. #define STDIN_FILENO 0
  39. #endif
  40. #ifndef STDOUT_FILENO
  41. #define STDOUT_FILENO 1
  42. #endif
  43. #else
  44. #include <unistd.h>
  45. #endif
  46. #include <google/protobuf/stubs/common.h>
  47. #include <google/protobuf/compiler/plugin.pb.h>
  48. #include <google/protobuf/compiler/code_generator.h>
  49. #include <google/protobuf/descriptor.h>
  50. #include <google/protobuf/io/zero_copy_stream_impl.h>
  51. namespace google {
  52. namespace protobuf {
  53. namespace compiler {
  54. class GeneratorResponseContext : public GeneratorContext {
  55. public:
  56. GeneratorResponseContext(CodeGeneratorResponse* response,
  57. const vector<const FileDescriptor*>& parsed_files)
  58. : response_(response),
  59. parsed_files_(parsed_files) {}
  60. virtual ~GeneratorResponseContext() {}
  61. // implements GeneratorContext --------------------------------------
  62. virtual io::ZeroCopyOutputStream* Open(const string& filename) {
  63. CodeGeneratorResponse::File* file = response_->add_file();
  64. file->set_name(filename);
  65. return new io::StringOutputStream(file->mutable_content());
  66. }
  67. virtual io::ZeroCopyOutputStream* OpenForInsert(
  68. const string& filename, const string& insertion_point) {
  69. CodeGeneratorResponse::File* file = response_->add_file();
  70. file->set_name(filename);
  71. file->set_insertion_point(insertion_point);
  72. return new io::StringOutputStream(file->mutable_content());
  73. }
  74. void ListParsedFiles(vector<const FileDescriptor*>* output) {
  75. *output = parsed_files_;
  76. }
  77. private:
  78. CodeGeneratorResponse* response_;
  79. const vector<const FileDescriptor*>& parsed_files_;
  80. };
  81. int PluginMain(int argc, char* argv[], const CodeGenerator* generator) {
  82. if (argc > 1) {
  83. cerr << argv[0] << ": Unknown option: " << argv[1] << endl;
  84. return 1;
  85. }
  86. #ifdef _WIN32
  87. _setmode(STDIN_FILENO, _O_BINARY);
  88. _setmode(STDOUT_FILENO, _O_BINARY);
  89. #endif
  90. CodeGeneratorRequest request;
  91. if (!request.ParseFromFileDescriptor(STDIN_FILENO)) {
  92. cerr << argv[0] << ": protoc sent unparseable request to plugin." << endl;
  93. return 1;
  94. }
  95. DescriptorPool pool;
  96. for (int i = 0; i < request.proto_file_size(); i++) {
  97. const FileDescriptor* file = pool.BuildFile(request.proto_file(i));
  98. if (file == NULL) {
  99. // BuildFile() already wrote an error message.
  100. return 1;
  101. }
  102. }
  103. vector<const FileDescriptor*> parsed_files;
  104. for (int i = 0; i < request.file_to_generate_size(); i++) {
  105. parsed_files.push_back(pool.FindFileByName(request.file_to_generate(i)));
  106. if (parsed_files.back() == NULL) {
  107. cerr << argv[0] << ": protoc asked plugin to generate a file but "
  108. "did not provide a descriptor for the file: "
  109. << request.file_to_generate(i) << endl;
  110. return 1;
  111. }
  112. }
  113. CodeGeneratorResponse response;
  114. GeneratorResponseContext context(&response, parsed_files);
  115. for (int i = 0; i < parsed_files.size(); i++) {
  116. const FileDescriptor* file = parsed_files[i];
  117. string error;
  118. bool succeeded = generator->Generate(
  119. file, request.parameter(), &context, &error);
  120. if (!succeeded && error.empty()) {
  121. error = "Code generator returned false but provided no error "
  122. "description.";
  123. }
  124. if (!error.empty()) {
  125. response.set_error(file->name() + ": " + error);
  126. break;
  127. }
  128. }
  129. if (!response.SerializeToFileDescriptor(STDOUT_FILENO)) {
  130. cerr << argv[0] << ": Error writing to stdout." << endl;
  131. return 1;
  132. }
  133. return 0;
  134. }
  135. } // namespace compiler
  136. } // namespace protobuf
  137. } // namespace google