1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "DocComment.h"
18
19 #include <android-base/logging.h>
20 #include <android-base/strings.h>
21 #include <hidl-util/StringHelper.h>
22
23 #include <cctype>
24 #include <vector>
25
26 #include "Location.h"
27
28 namespace android {
29
DocComment(const std::string & comment,const Location & location,CommentType type)30 DocComment::DocComment(const std::string& comment, const Location& location, CommentType type)
31 : DocComment(std::vector<std::string>(), location, type) {
32 std::vector<std::string> lines = base::Split(base::Trim(comment), "\n");
33
34 bool foundFirstLine = false;
35
36 for (size_t l = 0; l < lines.size(); l++) {
37 const std::string& line = lines[l];
38
39 // Delete prefixes like " * ", " *", or " ".
40 size_t idx = 0;
41 for (; idx < line.size() && isspace(line[idx]); idx++)
42 ;
43 if (idx < line.size() && line[idx] == '*') idx++;
44 if (idx < line.size() && line[idx] == ' ') idx++;
45
46 const std::string& sanitizedLine = line.substr(idx);
47 int i = sanitizedLine.size();
48 for (; i > 0 && isspace(sanitizedLine[i - 1]); i--)
49 ;
50
51 // Either the size is 0 or everything was whitespace.
52 bool isEmptyLine = i == 0;
53
54 foundFirstLine = foundFirstLine || !isEmptyLine;
55 if (!foundFirstLine) continue;
56
57 // if isEmptyLine, i == 0 so substr == ""
58 mLines.push_back(sanitizedLine.substr(0, i));
59 }
60 }
61
DocComment(const std::vector<std::string> & lines,const Location & location,CommentType type)62 DocComment::DocComment(const std::vector<std::string>& lines, const Location& location,
63 CommentType type)
64 : mLines(lines), mType(type), mLocation(location) {}
65
merge(const DocComment * comment)66 void DocComment::merge(const DocComment* comment) {
67 mLines.insert(mLines.end(), 2, "");
68 mLines.insert(mLines.end(), comment->mLines.begin(), comment->mLines.end());
69 mLocation.setLocation(mLocation.begin(), comment->mLocation.end());
70 }
71
emit(Formatter & out,CommentType type) const72 void DocComment::emit(Formatter& out, CommentType type) const {
73 CommentType useType = type;
74 if (useType == CommentType::UNSPECIFIED) useType = mType;
75 if (useType == CommentType::UNSPECIFIED) useType = CommentType::DOC_MULTILINE;
76
77 bool isMultiline = useType != CommentType::SINGLELINE;
78
79 // singleline comments include '//' as part of line text
80 if (isMultiline) {
81 switch (useType) {
82 case CommentType::DOC_MULTILINE:
83 out << "/**\n";
84 break;
85 case CommentType::MULTILINE:
86 out << "/*\n";
87 break;
88 default:
89 LOG(FATAL) << "bad type: " << static_cast<int>(useType);
90 }
91
92 out.pushLinePrefix(" *");
93 }
94
95 for (const std::string& line : mLines) {
96 out << (line.empty() && isMultiline ? "" : " ") << line << "\n";
97 }
98
99 if (isMultiline) {
100 out.popLinePrefix();
101 out << " */\n";
102 }
103 }
104
105 } // namespace android
106