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


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:54