• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <google/protobuf/compiler/csharp/csharp_doc_comment.h>
35 #include <google/protobuf/descriptor.h>
36 #include <google/protobuf/io/printer.h>
37 #include <google/protobuf/stubs/strutil.h>
38 
39 namespace google {
40 namespace protobuf {
41 namespace compiler {
42 namespace csharp {
43 
44 // Functions to create C# XML documentation comments.
45 // Currently this only includes documentation comments containing text specified as comments
46 // in the .proto file; documentation comments generated just from field/message/enum/proto names
47 // is inlined in the relevant code. If more control is required, that code can be moved here.
48 
WriteDocCommentBodyImpl(io::Printer * printer,SourceLocation location)49 void WriteDocCommentBodyImpl(io::Printer* printer, SourceLocation location) {
50     string comments = location.leading_comments.empty() ?
51         location.trailing_comments : location.leading_comments;
52     if (comments.empty()) {
53         return;
54     }
55     // XML escaping... no need for apostrophes etc as the whole text is going to be a child
56     // node of a summary element, not part of an attribute.
57     comments = StringReplace(comments, "&", "&amp;", true);
58     comments = StringReplace(comments, "<", "&lt;", true);
59     vector<string> lines = Split(comments, "\n", false /* skip_empty */);
60     // TODO: We really should work out which part to put in the summary and which to put in the remarks...
61     // but that needs to be part of a bigger effort to understand the markdown better anyway.
62     printer->Print("/// <summary>\n");
63     bool last_was_empty = false;
64     // We squash multiple blank lines down to one, and remove any trailing blank lines. We need
65     // to preserve the blank lines themselves, as this is relevant in the markdown.
66     // Note that we can't remove leading or trailing whitespace as *that's* relevant in markdown too.
67     // (We don't skip "just whitespace" lines, either.)
68     for (std::vector<string>::iterator it = lines.begin(); it != lines.end(); ++it) {
69         string line = *it;
70         if (line.empty()) {
71             last_was_empty = true;
72         } else {
73             if (last_was_empty) {
74                 printer->Print("///\n");
75             }
76             last_was_empty = false;
77             printer->Print("/// $line$\n", "line", *it);
78         }
79     }
80     printer->Print("/// </summary>\n");
81 }
82 
83 template <typename DescriptorType>
WriteDocCommentBody(io::Printer * printer,const DescriptorType * descriptor)84 static void WriteDocCommentBody(
85     io::Printer* printer, const DescriptorType* descriptor) {
86     SourceLocation location;
87     if (descriptor->GetSourceLocation(&location)) {
88         WriteDocCommentBodyImpl(printer, location);
89     }
90 }
91 
WriteMessageDocComment(io::Printer * printer,const Descriptor * message)92 void WriteMessageDocComment(io::Printer* printer, const Descriptor* message) {
93     WriteDocCommentBody(printer, message);
94 }
95 
WritePropertyDocComment(io::Printer * printer,const FieldDescriptor * field)96 void WritePropertyDocComment(io::Printer* printer, const FieldDescriptor* field) {
97     WriteDocCommentBody(printer, field);
98 }
99 
WriteEnumDocComment(io::Printer * printer,const EnumDescriptor * enumDescriptor)100 void WriteEnumDocComment(io::Printer* printer, const EnumDescriptor* enumDescriptor) {
101     WriteDocCommentBody(printer, enumDescriptor);
102 }
WriteEnumValueDocComment(io::Printer * printer,const EnumValueDescriptor * value)103 void WriteEnumValueDocComment(io::Printer* printer, const EnumValueDescriptor* value) {
104     WriteDocCommentBody(printer, value);
105 }
106 
WriteMethodDocComment(io::Printer * printer,const MethodDescriptor * method)107 void WriteMethodDocComment(io::Printer* printer, const MethodDescriptor* method) {
108     WriteDocCommentBody(printer, method);
109 }
110 
111 }  // namespace csharp
112 }  // namespace compiler
113 }  // namespace protobuf
114 }  // namespace google
115