java_doc_comment.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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/java/java_doc_comment.h>
  34. #include <vector>
  35. #include <google/protobuf/io/printer.h>
  36. #include <google/protobuf/stubs/strutil.h>
  37. namespace google {
  38. namespace protobuf {
  39. namespace compiler {
  40. namespace java {
  41. string EscapeJavadoc(const string& input) {
  42. string result;
  43. result.reserve(input.size() * 2);
  44. char prev = '*';
  45. for (string::size_type i = 0; i < input.size(); i++) {
  46. char c = input[i];
  47. switch (c) {
  48. case '*':
  49. // Avoid "/*".
  50. if (prev == '/') {
  51. result.append("&#42;");
  52. } else {
  53. result.push_back(c);
  54. }
  55. break;
  56. case '/':
  57. // Avoid "*/".
  58. if (prev == '*') {
  59. result.append("&#47;");
  60. } else {
  61. result.push_back(c);
  62. }
  63. break;
  64. case '@':
  65. // '@' starts javadoc tags including the @deprecated tag, which will
  66. // cause a compile-time error if inserted before a declaration that
  67. // does not have a corresponding @Deprecated annotation.
  68. result.append("&#64;");
  69. break;
  70. case '<':
  71. // Avoid interpretation as HTML.
  72. result.append("&lt;");
  73. break;
  74. case '>':
  75. // Avoid interpretation as HTML.
  76. result.append("&gt;");
  77. break;
  78. case '&':
  79. // Avoid interpretation as HTML.
  80. result.append("&amp;");
  81. break;
  82. case '\\':
  83. // Java interprets Unicode escape sequences anywhere!
  84. result.append("&#92;");
  85. break;
  86. default:
  87. result.push_back(c);
  88. break;
  89. }
  90. prev = c;
  91. }
  92. return result;
  93. }
  94. static void WriteDocCommentBodyForLocation(
  95. io::Printer* printer, const SourceLocation& location) {
  96. string comments = location.leading_comments.empty() ?
  97. location.trailing_comments : location.leading_comments;
  98. if (!comments.empty()) {
  99. // TODO(kenton): Ideally we should parse the comment text as Markdown and
  100. // write it back as HTML, but this requires a Markdown parser. For now
  101. // we just use <pre> to get fixed-width text formatting.
  102. // If the comment itself contains block comment start or end markers,
  103. // HTML-escape them so that they don't accidentally close the doc comment.
  104. comments = EscapeJavadoc(comments);
  105. std::vector<string> lines = Split(comments, "\n");
  106. while (!lines.empty() && lines.back().empty()) {
  107. lines.pop_back();
  108. }
  109. printer->Print(" * <pre>\n");
  110. for (int i = 0; i < lines.size(); i++) {
  111. // Most lines should start with a space. Watch out for lines that start
  112. // with a /, since putting that right after the leading asterisk will
  113. // close the comment.
  114. if (!lines[i].empty() && lines[i][0] == '/') {
  115. printer->Print(" * $line$\n", "line", lines[i]);
  116. } else {
  117. printer->Print(" *$line$\n", "line", lines[i]);
  118. }
  119. }
  120. printer->Print(
  121. " * </pre>\n"
  122. " *\n");
  123. }
  124. }
  125. template <typename DescriptorType>
  126. static void WriteDocCommentBody(
  127. io::Printer* printer, const DescriptorType* descriptor) {
  128. SourceLocation location;
  129. if (descriptor->GetSourceLocation(&location)) {
  130. WriteDocCommentBodyForLocation(printer, location);
  131. }
  132. }
  133. static string FirstLineOf(const string& value) {
  134. string result = value;
  135. string::size_type pos = result.find_first_of('\n');
  136. if (pos != string::npos) {
  137. result.erase(pos);
  138. }
  139. // If line ends in an opening brace, make it "{ ... }" so it looks nice.
  140. if (!result.empty() && result[result.size() - 1] == '{') {
  141. result.append(" ... }");
  142. }
  143. return result;
  144. }
  145. void WriteMessageDocComment(io::Printer* printer, const Descriptor* message) {
  146. printer->Print("/**\n");
  147. WriteDocCommentBody(printer, message);
  148. printer->Print(
  149. " * Protobuf type {@code $fullname$}\n"
  150. " */\n",
  151. "fullname", EscapeJavadoc(message->full_name()));
  152. }
  153. void WriteFieldDocComment(io::Printer* printer, const FieldDescriptor* field) {
  154. // In theory we should have slightly different comments for setters, getters,
  155. // etc., but in practice everyone already knows the difference between these
  156. // so it's redundant information.
  157. // We start the comment with the main body based on the comments from the
  158. // .proto file (if present). We then end with the field declaration, e.g.:
  159. // optional string foo = 5;
  160. // If the field is a group, the debug string might end with {.
  161. printer->Print("/**\n");
  162. WriteDocCommentBody(printer, field);
  163. printer->Print(
  164. " * <code>$def$</code>\n",
  165. "def", EscapeJavadoc(FirstLineOf(field->DebugString())));
  166. printer->Print(" */\n");
  167. }
  168. void WriteEnumDocComment(io::Printer* printer, const EnumDescriptor* enum_) {
  169. printer->Print("/**\n");
  170. WriteDocCommentBody(printer, enum_);
  171. printer->Print(
  172. " * Protobuf enum {@code $fullname$}\n"
  173. " */\n",
  174. "fullname", EscapeJavadoc(enum_->full_name()));
  175. }
  176. void WriteEnumValueDocComment(io::Printer* printer,
  177. const EnumValueDescriptor* value) {
  178. printer->Print("/**\n");
  179. WriteDocCommentBody(printer, value);
  180. printer->Print(
  181. " * <code>$def$</code>\n"
  182. " */\n",
  183. "def", EscapeJavadoc(FirstLineOf(value->DebugString())));
  184. }
  185. void WriteServiceDocComment(io::Printer* printer,
  186. const ServiceDescriptor* service) {
  187. printer->Print("/**\n");
  188. WriteDocCommentBody(printer, service);
  189. printer->Print(
  190. " * Protobuf service {@code $fullname$}\n"
  191. " */\n",
  192. "fullname", EscapeJavadoc(service->full_name()));
  193. }
  194. void WriteMethodDocComment(io::Printer* printer,
  195. const MethodDescriptor* method) {
  196. printer->Print("/**\n");
  197. WriteDocCommentBody(printer, method);
  198. printer->Print(
  199. " * <code>$def$</code>\n"
  200. " */\n",
  201. "def", EscapeJavadoc(FirstLineOf(method->DebugString())));
  202. }
  203. } // namespace java
  204. } // namespace compiler
  205. } // namespace protobuf
  206. } // namespace google