blob: afbdc8f82e9a4c637c24174f7bdc3fc15af3a1ae [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 = "/*";
43static const std::string_view kBlockCommentEnd = "*/";
Jooyung Hanb7a60ec2021-01-21 13:47:59 +090044static const std::string_view kDocCommentBegin = "/**";
Jooyung Han24effbf2021-01-16 10:24:03 +090045static const std::string kTagDeprecated = "@deprecated";
46static const std::regex kTagHideRegex{"@hide\\b"};
Jooyung Hand4fe00e2021-01-11 16:21:53 +090047
48std::string ConsumePrefix(const std::string& s, std::string_view prefix) {
Jooyung Han161bb0f2021-01-21 12:30:16 +090049 AIDL_FATAL_IF(!StartsWith(s, prefix), AIDL_LOCATION_HERE)
50 << "'" << s << "' has no prefix '" << prefix << "'";
Jooyung Hand4fe00e2021-01-11 16:21:53 +090051 return s.substr(prefix.size());
52}
53
54std::string ConsumeSuffix(const std::string& s, std::string_view suffix) {
55 AIDL_FATAL_IF(!EndsWith(s, suffix), AIDL_LOCATION_HERE);
56 return s.substr(0, s.size() - suffix.size());
57}
58
59struct BlockTag {
60 std::string name;
61 std::string description;
62};
63
Jooyung Han8451a202021-01-16 03:07:06 +090064// Removes comment markers: //, /*, */, optional leading "*" in block comments
Jooyung Hand4fe00e2021-01-11 16:21:53 +090065// - keeps leading spaces, but trims trailing spaces
66// - keeps empty lines
Jooyung Han8451a202021-01-16 03:07:06 +090067std::vector<std::string> TrimmedLines(const Comment& c) {
Jooyung Han161bb0f2021-01-21 12:30:16 +090068 if (c.type == Comment::Type::LINE) {
69 return std::vector{ConsumePrefix(c.body, kLineCommentBegin)};
70 }
Jooyung Hand4fe00e2021-01-11 16:21:53 +090071
Jooyung Han8451a202021-01-16 03:07:06 +090072 std::string stripped = ConsumeSuffix(ConsumePrefix(c.body, kBlockCommentBegin), kBlockCommentEnd);
Jooyung Hand4fe00e2021-01-11 16:21:53 +090073
74 std::vector<std::string> lines;
75 bool found_first_line = false;
76
77 for (auto& line : Split(stripped, "\n")) {
78 // Delete prefixes like " * ", " *", or " ".
79 size_t idx = 0;
80 for (; idx < line.size() && isspace(line[idx]); idx++)
81 ;
82 if (idx < line.size() && line[idx] == '*') idx++;
83 if (idx < line.size() && line[idx] == ' ') idx++;
84
85 const std::string& sanitized_line = line.substr(idx);
86 size_t i = sanitized_line.size();
87 for (; i > 0 && isspace(sanitized_line[i - 1]); i--)
88 ;
89
90 // Either the size is 0 or everything was whitespace.
91 bool is_empty_line = i == 0;
92
93 found_first_line = found_first_line || !is_empty_line;
94 if (!found_first_line) continue;
95
96 // if is_empty_line, i == 0 so substr == ""
97 lines.push_back(sanitized_line.substr(0, i));
98 }
99 // remove trailing empty lines
100 while (!lines.empty() && Trim(lines.back()).empty()) {
101 lines.pop_back();
102 }
103 return lines;
104}
105
Jooyung Han161bb0f2021-01-21 12:30:16 +0900106// Parses a block comment and returns block tags in the comment.
Jooyung Han8451a202021-01-16 03:07:06 +0900107std::vector<BlockTag> BlockTags(const Comment& c) {
Jooyung Han161bb0f2021-01-21 12:30:16 +0900108 AIDL_FATAL_IF(c.type != Comment::Type::BLOCK, AIDL_LOCATION_HERE);
109
Jooyung Hand4fe00e2021-01-11 16:21:53 +0900110 std::vector<BlockTag> tags;
111
112 // current tag and paragraph
113 std::string tag;
114 std::vector<std::string> paragraph;
115
116 auto end_paragraph = [&]() {
117 if (tag.empty()) {
118 paragraph.clear();
119 return;
120 }
121 // paragraph lines are trimed at both ends
122 tags.push_back({tag, Join(paragraph, " ")});
123 tag.clear();
124 paragraph.clear();
125 };
126
Jooyung Han8451a202021-01-16 03:07:06 +0900127 for (const auto& line : TrimmedLines(c)) {
Jooyung Hand4fe00e2021-01-11 16:21:53 +0900128 size_t idx = 0;
129 // skip leading spaces
130 for (; idx < line.size() && isspace(line[idx]); idx++)
131 ;
132
133 if (idx == line.size()) {
134 // skip empty lines
135 } else if (line[idx] == '@') {
136 // end the current paragraph before reading a new block tag (+ description paragraph)
137 end_paragraph();
138
139 size_t end_idx = idx + 1;
140 for (; end_idx < line.size() && isalpha(line[end_idx]); end_idx++)
141 ;
142
143 tag = line.substr(idx, end_idx - idx);
144
145 if (end_idx < line.size() && line[end_idx] == ' ') end_idx++;
146 // skip empty line
147 if (end_idx < line.size()) {
148 paragraph.push_back(line.substr(end_idx));
149 }
150 } else {
151 // gather paragraph lines with leading spaces trimmed
152 paragraph.push_back(line.substr(idx));
153 }
154 }
155
156 end_paragraph();
157
158 return tags;
159}
Jooyung Hand4fe00e2021-01-11 16:21:53 +0900160
Jooyung Han161bb0f2021-01-21 12:30:16 +0900161} // namespace
162
163Comment::Comment(const std::string& body) : body(body) {
164 if (StartsWith(body, kLineCommentBegin)) {
165 type = Type::LINE;
166 } else if (StartsWith(body, kBlockCommentBegin) && EndsWith(body, kBlockCommentEnd)) {
167 type = Type::BLOCK;
168 } else {
169 AIDL_FATAL(AIDL_LOCATION_HERE) << "invalid comments body:" << body;
Jooyung Han24effbf2021-01-16 10:24:03 +0900170 }
Jooyung Han24effbf2021-01-16 10:24:03 +0900171}
172
Jooyung Han161bb0f2021-01-21 12:30:16 +0900173// Returns the immediate block comment from the list of comments.
174// Only the last/block comment can have the tag.
175//
176// /* @hide */
177// int x;
178//
179// But tags in line or distant comments don't count. In the following,
180// the variable 'x' is not hidden.
181//
182// // @hide
183// int x;
184//
185// /* @hide */
186// /* this is the immemediate comment to 'x' */
187// int x;
188//
189static std::optional<Comment> GetValidComment(const Comments& comments) {
190 if (!comments.empty() && comments.back().type == Comment::Type::BLOCK) {
191 return comments.back();
192 }
193 return std::nullopt;
194}
195
196// Sees if comments have the @hide tag.
197// Example: /** @hide */
198bool HasHideInComments(const Comments& comments) {
199 const auto valid_comment = GetValidComment(comments);
200 return valid_comment && std::regex_search(valid_comment->body, kTagHideRegex);
201}
202
203// Finds the @deprecated tag in comments and returns it with optional note which
204// follows the tag.
205// Example: /** @deprecated reason */
Jooyung Han8451a202021-01-16 03:07:06 +0900206std::optional<Deprecated> FindDeprecated(const Comments& comments) {
Jooyung Han161bb0f2021-01-21 12:30:16 +0900207 if (const auto valid_comment = GetValidComment(comments); valid_comment) {
208 for (const auto& [name, description] : BlockTags(comments.back())) {
Jooyung Hand4fe00e2021-01-11 16:21:53 +0900209 // take the first @deprecated
210 if (kTagDeprecated == name) {
211 return Deprecated{description};
212 }
213 }
214 }
215 return std::nullopt;
216}
217
Jooyung Hanb7a60ec2021-01-21 13:47:59 +0900218// Formats comments for the Java backend.
219// The last/block comment is transformed into javadoc(/** */)
220// and others are used as they are.
221std::string FormatCommentsForJava(const Comments& comments) {
222 std::stringstream out;
223 for (auto it = begin(comments); it != end(comments); it++) {
224 const bool last = next(it) == end(comments);
225 // We only re-format the last/block comment which is not already a doc-style comment.
226 if (last && it->type == Comment::Type::BLOCK && !StartsWith(it->body, kDocCommentBegin)) {
227 out << kDocCommentBegin << ConsumePrefix(it->body, kBlockCommentBegin);
228 } else {
229 out << it->body;
230 }
231 }
232 return out.str();
233}
234
Jooyung Hand4fe00e2021-01-11 16:21:53 +0900235} // namespace aidl
236} // namespace android