blob: 8feee285f98465cb7112669dd8601b3e780ec200 [file] [log] [blame]
Jooyung Hand4fe00e2021-01-11 16:21:53 +09001/*
2 * Copyright (C) 2021, 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#include "comments.h"
17
18#include <android-base/result.h>
19#include <android-base/strings.h>
20
21#include <optional>
Jooyung Han24effbf2021-01-16 10:24:03 +090022#include <regex>
Jooyung Hand4fe00e2021-01-11 16:21:53 +090023#include <string>
24#include <vector>
25
26#include "logging.h"
27
28using android::base::EndsWith;
29using android::base::Error;
30using android::base::Join;
31using android::base::Result;
32using android::base::Split;
33using android::base::StartsWith;
34using android::base::Trim;
35
36namespace android {
37namespace aidl {
38
39namespace {
40
41static const std::string_view kLineCommentBegin = "//";
42static const std::string_view kBlockCommentBegin = "/*";
Steven Morelandc29d7172021-10-12 15:25:11 -070043static const std::string_view kBlockCommentMid = " *";
Jooyung Hand4fe00e2021-01-11 16:21:53 +090044static const std::string_view kBlockCommentEnd = "*/";
Jooyung Hanb7a60ec2021-01-21 13:47:59 +090045static const std::string_view kDocCommentBegin = "/**";
Jooyung Han24effbf2021-01-16 10:24:03 +090046static const std::string kTagDeprecated = "@deprecated";
47static const std::regex kTagHideRegex{"@hide\\b"};
Jooyung Hand4fe00e2021-01-11 16:21:53 +090048
49std::string ConsumePrefix(const std::string& s, std::string_view prefix) {
Jooyung Han161bb0f2021-01-21 12:30:16 +090050 AIDL_FATAL_IF(!StartsWith(s, prefix), AIDL_LOCATION_HERE)
51 << "'" << s << "' has no prefix '" << prefix << "'";
Jooyung Hand4fe00e2021-01-11 16:21:53 +090052 return s.substr(prefix.size());
53}
54
55std::string ConsumeSuffix(const std::string& s, std::string_view suffix) {
56 AIDL_FATAL_IF(!EndsWith(s, suffix), AIDL_LOCATION_HERE);
57 return s.substr(0, s.size() - suffix.size());
58}
59
60struct BlockTag {
61 std::string name;
62 std::string description;
63};
64
Jooyung Han8451a202021-01-16 03:07:06 +090065// Removes comment markers: //, /*, */, optional leading "*" in block comments
Jooyung Hand4fe00e2021-01-11 16:21:53 +090066// - keeps leading spaces, but trims trailing spaces
67// - keeps empty lines
Jooyung Han8451a202021-01-16 03:07:06 +090068std::vector<std::string> TrimmedLines(const Comment& c) {
Jooyung Han161bb0f2021-01-21 12:30:16 +090069 if (c.type == Comment::Type::LINE) {
70 return std::vector{ConsumePrefix(c.body, kLineCommentBegin)};
71 }
Jooyung Hand4fe00e2021-01-11 16:21:53 +090072
Jooyung Han8451a202021-01-16 03:07:06 +090073 std::string stripped = ConsumeSuffix(ConsumePrefix(c.body, kBlockCommentBegin), kBlockCommentEnd);
Jooyung Hand4fe00e2021-01-11 16:21:53 +090074
75 std::vector<std::string> lines;
76 bool found_first_line = false;
77
78 for (auto& line : Split(stripped, "\n")) {
79 // Delete prefixes like " * ", " *", or " ".
80 size_t idx = 0;
81 for (; idx < line.size() && isspace(line[idx]); idx++)
82 ;
83 if (idx < line.size() && line[idx] == '*') idx++;
84 if (idx < line.size() && line[idx] == ' ') idx++;
85
86 const std::string& sanitized_line = line.substr(idx);
87 size_t i = sanitized_line.size();
88 for (; i > 0 && isspace(sanitized_line[i - 1]); i--)
89 ;
90
91 // Either the size is 0 or everything was whitespace.
92 bool is_empty_line = i == 0;
93
94 found_first_line = found_first_line || !is_empty_line;
95 if (!found_first_line) continue;
96
97 // if is_empty_line, i == 0 so substr == ""
98 lines.push_back(sanitized_line.substr(0, i));
99 }
100 // remove trailing empty lines
101 while (!lines.empty() && Trim(lines.back()).empty()) {
102 lines.pop_back();
103 }
104 return lines;
105}
106
Jooyung Han161bb0f2021-01-21 12:30:16 +0900107// Parses a block comment and returns block tags in the comment.
Jooyung Han8451a202021-01-16 03:07:06 +0900108std::vector<BlockTag> BlockTags(const Comment& c) {
Jooyung Han161bb0f2021-01-21 12:30:16 +0900109 AIDL_FATAL_IF(c.type != Comment::Type::BLOCK, AIDL_LOCATION_HERE);
110
Jooyung Hand4fe00e2021-01-11 16:21:53 +0900111 std::vector<BlockTag> tags;
112
113 // current tag and paragraph
114 std::string tag;
115 std::vector<std::string> paragraph;
116
117 auto end_paragraph = [&]() {
118 if (tag.empty()) {
119 paragraph.clear();
120 return;
121 }
122 // paragraph lines are trimed at both ends
123 tags.push_back({tag, Join(paragraph, " ")});
124 tag.clear();
125 paragraph.clear();
126 };
127
Jooyung Han8451a202021-01-16 03:07:06 +0900128 for (const auto& line : TrimmedLines(c)) {
Jooyung Hand4fe00e2021-01-11 16:21:53 +0900129 size_t idx = 0;
130 // skip leading spaces
131 for (; idx < line.size() && isspace(line[idx]); idx++)
132 ;
133
134 if (idx == line.size()) {
135 // skip empty lines
136 } else if (line[idx] == '@') {
137 // end the current paragraph before reading a new block tag (+ description paragraph)
138 end_paragraph();
139
140 size_t end_idx = idx + 1;
141 for (; end_idx < line.size() && isalpha(line[end_idx]); end_idx++)
142 ;
143
144 tag = line.substr(idx, end_idx - idx);
145
146 if (end_idx < line.size() && line[end_idx] == ' ') end_idx++;
147 // skip empty line
148 if (end_idx < line.size()) {
149 paragraph.push_back(line.substr(end_idx));
150 }
151 } else {
152 // gather paragraph lines with leading spaces trimmed
153 paragraph.push_back(line.substr(idx));
154 }
155 }
156
157 end_paragraph();
158
159 return tags;
160}
Jooyung Hand4fe00e2021-01-11 16:21:53 +0900161
Jooyung Han161bb0f2021-01-21 12:30:16 +0900162} // namespace
163
164Comment::Comment(const std::string& body) : body(body) {
165 if (StartsWith(body, kLineCommentBegin)) {
166 type = Type::LINE;
167 } else if (StartsWith(body, kBlockCommentBegin) && EndsWith(body, kBlockCommentEnd)) {
168 type = Type::BLOCK;
169 } else {
170 AIDL_FATAL(AIDL_LOCATION_HERE) << "invalid comments body:" << body;
Jooyung Han24effbf2021-01-16 10:24:03 +0900171 }
Jooyung Han24effbf2021-01-16 10:24:03 +0900172}
173
Jooyung Han161bb0f2021-01-21 12:30:16 +0900174// Returns the immediate block comment from the list of comments.
175// Only the last/block comment can have the tag.
176//
177// /* @hide */
178// int x;
179//
180// But tags in line or distant comments don't count. In the following,
181// the variable 'x' is not hidden.
182//
183// // @hide
184// int x;
185//
186// /* @hide */
187// /* this is the immemediate comment to 'x' */
188// int x;
189//
190static std::optional<Comment> GetValidComment(const Comments& comments) {
191 if (!comments.empty() && comments.back().type == Comment::Type::BLOCK) {
192 return comments.back();
193 }
194 return std::nullopt;
195}
196
197// Sees if comments have the @hide tag.
198// Example: /** @hide */
199bool HasHideInComments(const Comments& comments) {
200 const auto valid_comment = GetValidComment(comments);
201 return valid_comment && std::regex_search(valid_comment->body, kTagHideRegex);
202}
203
204// Finds the @deprecated tag in comments and returns it with optional note which
205// follows the tag.
206// Example: /** @deprecated reason */
Jooyung Han8451a202021-01-16 03:07:06 +0900207std::optional<Deprecated> FindDeprecated(const Comments& comments) {
Jooyung Han161bb0f2021-01-21 12:30:16 +0900208 if (const auto valid_comment = GetValidComment(comments); valid_comment) {
209 for (const auto& [name, description] : BlockTags(comments.back())) {
Jooyung Hand4fe00e2021-01-11 16:21:53 +0900210 // take the first @deprecated
211 if (kTagDeprecated == name) {
212 return Deprecated{description};
213 }
214 }
215 }
216 return std::nullopt;
217}
218
Jooyung Hanb7a60ec2021-01-21 13:47:59 +0900219// Formats comments for the Java backend.
220// The last/block comment is transformed into javadoc(/** */)
221// and others are used as they are.
222std::string FormatCommentsForJava(const Comments& comments) {
223 std::stringstream out;
224 for (auto it = begin(comments); it != end(comments); it++) {
225 const bool last = next(it) == end(comments);
Steven Morelandc29d7172021-10-12 15:25:11 -0700226 auto lines = TrimmedLines(*it);
227
228 if (it->type == Comment::Type::LINE) {
229 for (const auto& line : lines) {
230 out << kLineCommentBegin << line;
231 }
Jooyung Hanb7a60ec2021-01-21 13:47:59 +0900232 } else {
Steven Morelandc29d7172021-10-12 15:25:11 -0700233 if (last || StartsWith(it->body, kDocCommentBegin)) {
234 out << kDocCommentBegin;
235 } else {
236 out << kBlockCommentBegin;
237 }
238 bool multiline = lines.size() > 1;
239
240 if (multiline) out << "\n";
241 for (const auto& line : lines) {
242 if (multiline) out << kBlockCommentMid;
243 out << " " << line;
244 if (multiline) out << "\n";
245 }
246 out << " " << kBlockCommentEnd << "\n";
Jooyung Hanb7a60ec2021-01-21 13:47:59 +0900247 }
248 }
249 return out.str();
250}
251
Jooyung Hand4fe00e2021-01-11 16:21:53 +0900252} // namespace aidl
Steven Morelandc29d7172021-10-12 15:25:11 -0700253} // namespace android