Jooyung Han | d4fe00e | 2021-01-11 16:21:53 +0900 | [diff] [blame] | 1 | /* |
| 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 Han | 24effbf | 2021-01-16 10:24:03 +0900 | [diff] [blame] | 22 | #include <regex> |
Jooyung Han | d4fe00e | 2021-01-11 16:21:53 +0900 | [diff] [blame] | 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
| 26 | #include "logging.h" |
| 27 | |
| 28 | using android::base::EndsWith; |
| 29 | using android::base::Error; |
| 30 | using android::base::Join; |
| 31 | using android::base::Result; |
| 32 | using android::base::Split; |
| 33 | using android::base::StartsWith; |
| 34 | using android::base::Trim; |
| 35 | |
| 36 | namespace android { |
| 37 | namespace aidl { |
| 38 | |
| 39 | namespace { |
| 40 | |
| 41 | static const std::string_view kLineCommentBegin = "//"; |
| 42 | static const std::string_view kBlockCommentBegin = "/*"; |
| 43 | static const std::string_view kBlockCommentEnd = "*/"; |
Jooyung Han | 24effbf | 2021-01-16 10:24:03 +0900 | [diff] [blame] | 44 | static const std::string kTagDeprecated = "@deprecated"; |
| 45 | static const std::regex kTagHideRegex{"@hide\\b"}; |
Jooyung Han | d4fe00e | 2021-01-11 16:21:53 +0900 | [diff] [blame] | 46 | |
| 47 | std::string ConsumePrefix(const std::string& s, std::string_view prefix) { |
| 48 | AIDL_FATAL_IF(!StartsWith(s, prefix), AIDL_LOCATION_HERE); |
| 49 | return s.substr(prefix.size()); |
| 50 | } |
| 51 | |
| 52 | std::string ConsumeSuffix(const std::string& s, std::string_view suffix) { |
| 53 | AIDL_FATAL_IF(!EndsWith(s, suffix), AIDL_LOCATION_HERE); |
| 54 | return s.substr(0, s.size() - suffix.size()); |
| 55 | } |
| 56 | |
| 57 | struct BlockTag { |
| 58 | std::string name; |
| 59 | std::string description; |
| 60 | }; |
| 61 | |
Jooyung Han | 8451a20 | 2021-01-16 03:07:06 +0900 | [diff] [blame^] | 62 | // Removes comment markers: //, /*, */, optional leading "*" in block comments |
Jooyung Han | d4fe00e | 2021-01-11 16:21:53 +0900 | [diff] [blame] | 63 | // - keeps leading spaces, but trims trailing spaces |
| 64 | // - keeps empty lines |
Jooyung Han | 8451a20 | 2021-01-16 03:07:06 +0900 | [diff] [blame^] | 65 | std::vector<std::string> TrimmedLines(const Comment& c) { |
| 66 | if (c.type == Comment::Type::LINE) return std::vector{ConsumePrefix(c.body, kLineCommentBegin)}; |
Jooyung Han | d4fe00e | 2021-01-11 16:21:53 +0900 | [diff] [blame] | 67 | |
Jooyung Han | 8451a20 | 2021-01-16 03:07:06 +0900 | [diff] [blame^] | 68 | std::string stripped = ConsumeSuffix(ConsumePrefix(c.body, kBlockCommentBegin), kBlockCommentEnd); |
Jooyung Han | d4fe00e | 2021-01-11 16:21:53 +0900 | [diff] [blame] | 69 | |
| 70 | std::vector<std::string> lines; |
| 71 | bool found_first_line = false; |
| 72 | |
| 73 | for (auto& line : Split(stripped, "\n")) { |
| 74 | // Delete prefixes like " * ", " *", or " ". |
| 75 | size_t idx = 0; |
| 76 | for (; idx < line.size() && isspace(line[idx]); idx++) |
| 77 | ; |
| 78 | if (idx < line.size() && line[idx] == '*') idx++; |
| 79 | if (idx < line.size() && line[idx] == ' ') idx++; |
| 80 | |
| 81 | const std::string& sanitized_line = line.substr(idx); |
| 82 | size_t i = sanitized_line.size(); |
| 83 | for (; i > 0 && isspace(sanitized_line[i - 1]); i--) |
| 84 | ; |
| 85 | |
| 86 | // Either the size is 0 or everything was whitespace. |
| 87 | bool is_empty_line = i == 0; |
| 88 | |
| 89 | found_first_line = found_first_line || !is_empty_line; |
| 90 | if (!found_first_line) continue; |
| 91 | |
| 92 | // if is_empty_line, i == 0 so substr == "" |
| 93 | lines.push_back(sanitized_line.substr(0, i)); |
| 94 | } |
| 95 | // remove trailing empty lines |
| 96 | while (!lines.empty() && Trim(lines.back()).empty()) { |
| 97 | lines.pop_back(); |
| 98 | } |
| 99 | return lines; |
| 100 | } |
| 101 | |
Jooyung Han | 8451a20 | 2021-01-16 03:07:06 +0900 | [diff] [blame^] | 102 | std::vector<BlockTag> BlockTags(const Comment& c) { |
Jooyung Han | d4fe00e | 2021-01-11 16:21:53 +0900 | [diff] [blame] | 103 | std::vector<BlockTag> tags; |
| 104 | |
| 105 | // current tag and paragraph |
| 106 | std::string tag; |
| 107 | std::vector<std::string> paragraph; |
| 108 | |
| 109 | auto end_paragraph = [&]() { |
| 110 | if (tag.empty()) { |
| 111 | paragraph.clear(); |
| 112 | return; |
| 113 | } |
| 114 | // paragraph lines are trimed at both ends |
| 115 | tags.push_back({tag, Join(paragraph, " ")}); |
| 116 | tag.clear(); |
| 117 | paragraph.clear(); |
| 118 | }; |
| 119 | |
Jooyung Han | 8451a20 | 2021-01-16 03:07:06 +0900 | [diff] [blame^] | 120 | for (const auto& line : TrimmedLines(c)) { |
Jooyung Han | d4fe00e | 2021-01-11 16:21:53 +0900 | [diff] [blame] | 121 | size_t idx = 0; |
| 122 | // skip leading spaces |
| 123 | for (; idx < line.size() && isspace(line[idx]); idx++) |
| 124 | ; |
| 125 | |
| 126 | if (idx == line.size()) { |
| 127 | // skip empty lines |
| 128 | } else if (line[idx] == '@') { |
| 129 | // end the current paragraph before reading a new block tag (+ description paragraph) |
| 130 | end_paragraph(); |
| 131 | |
| 132 | size_t end_idx = idx + 1; |
| 133 | for (; end_idx < line.size() && isalpha(line[end_idx]); end_idx++) |
| 134 | ; |
| 135 | |
| 136 | tag = line.substr(idx, end_idx - idx); |
| 137 | |
| 138 | if (end_idx < line.size() && line[end_idx] == ' ') end_idx++; |
| 139 | // skip empty line |
| 140 | if (end_idx < line.size()) { |
| 141 | paragraph.push_back(line.substr(end_idx)); |
| 142 | } |
| 143 | } else { |
| 144 | // gather paragraph lines with leading spaces trimmed |
| 145 | paragraph.push_back(line.substr(idx)); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | end_paragraph(); |
| 150 | |
| 151 | return tags; |
| 152 | } |
Jooyung Han | d4fe00e | 2021-01-11 16:21:53 +0900 | [diff] [blame] | 153 | } |
| 154 | |
Jooyung Han | 8451a20 | 2021-01-16 03:07:06 +0900 | [diff] [blame^] | 155 | bool HasHideInComments(const Comments& comments) { |
| 156 | for (const auto& c : comments) { |
Jooyung Han | 24effbf | 2021-01-16 10:24:03 +0900 | [diff] [blame] | 157 | if (c.type == Comment::Type::LINE) continue; |
| 158 | if (std::regex_search(c.body, kTagHideRegex)) { |
| 159 | return true; |
| 160 | } |
| 161 | } |
| 162 | return false; |
| 163 | } |
| 164 | |
Jooyung Han | d4fe00e | 2021-01-11 16:21:53 +0900 | [diff] [blame] | 165 | // Finds @deprecated tag and returns it with optional note which follows the tag. |
Jooyung Han | 8451a20 | 2021-01-16 03:07:06 +0900 | [diff] [blame^] | 166 | std::optional<Deprecated> FindDeprecated(const Comments& comments) { |
| 167 | for (const auto& c : comments) { |
Jooyung Han | 24effbf | 2021-01-16 10:24:03 +0900 | [diff] [blame] | 168 | if (c.type == Comment::Type::LINE) continue; |
Jooyung Han | 8451a20 | 2021-01-16 03:07:06 +0900 | [diff] [blame^] | 169 | for (const auto& [name, description] : BlockTags(c)) { |
Jooyung Han | d4fe00e | 2021-01-11 16:21:53 +0900 | [diff] [blame] | 170 | // take the first @deprecated |
| 171 | if (kTagDeprecated == name) { |
| 172 | return Deprecated{description}; |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | return std::nullopt; |
| 177 | } |
| 178 | |
| 179 | } // namespace aidl |
| 180 | } // namespace android |