| Steven Moreland | 49bad8d | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 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 | |
| Steven Moreland | 04fff2b | 2019-10-24 16:14:48 -0700 | [diff] [blame] | 19 | #include <android-base/logging.h> |
| Steven Moreland | 7645fbd | 2019-03-12 18:49:28 -0700 | [diff] [blame] | 20 | #include <android-base/strings.h> |
| Steven Moreland | 49bad8d | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 21 | #include <hidl-util/StringHelper.h> |
| 22 | |
| 23 | #include <cctype> |
| Neel Mehta | 49e7b2d | 2019-07-03 11:49:59 -0700 | [diff] [blame] | 24 | #include <vector> |
| Steven Moreland | 49bad8d | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 25 | |
| Neel Mehta | 291d02e | 2019-06-06 17:51:07 -0700 | [diff] [blame] | 26 | #include "Location.h" |
| Steven Moreland | 7645fbd | 2019-03-12 18:49:28 -0700 | [diff] [blame] | 27 | |
| Steven Moreland | 49bad8d | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 28 | namespace android { |
| 29 | |
| Steven Moreland | 04fff2b | 2019-10-24 16:14:48 -0700 | [diff] [blame] | 30 | DocComment::DocComment(const std::string& comment, const Location& location, CommentType type) |
| Steven Moreland | 449d4f0 | 2019-10-24 17:43:39 -0700 | [diff] [blame^] | 31 | : DocComment(std::vector<std::string>(), location, type) { |
| Steven Moreland | 7645fbd | 2019-03-12 18:49:28 -0700 | [diff] [blame] | 32 | std::vector<std::string> lines = base::Split(base::Trim(comment), "\n"); |
| Steven Moreland | 49bad8d | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 33 | |
| 34 | bool foundFirstLine = false; |
| 35 | |
| Steven Moreland | 49bad8d | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 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 | |
| Neel Mehta | 49e7b2d | 2019-07-03 11:49:59 -0700 | [diff] [blame] | 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; |
| Steven Moreland | 49bad8d | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 53 | |
| Steven Moreland | 7645fbd | 2019-03-12 18:49:28 -0700 | [diff] [blame] | 54 | foundFirstLine = foundFirstLine || !isEmptyLine; |
| Steven Moreland | 49bad8d | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 55 | if (!foundFirstLine) continue; |
| 56 | |
| Neel Mehta | 49e7b2d | 2019-07-03 11:49:59 -0700 | [diff] [blame] | 57 | // if isEmptyLine, i == 0 so substr == "" |
| 58 | mLines.push_back(sanitizedLine.substr(0, i)); |
| Steven Moreland | 49bad8d | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 59 | } |
| Steven Moreland | 49bad8d | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| Steven Moreland | 449d4f0 | 2019-10-24 17:43:39 -0700 | [diff] [blame^] | 62 | DocComment::DocComment(const std::vector<std::string>& lines, const Location& location, |
| 63 | CommentType type) |
| 64 | : mLines(lines), mType(type), mLocation(location) {} |
| 65 | |
| Steven Moreland | 49bad8d | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 66 | void DocComment::merge(const DocComment* comment) { |
| Neel Mehta | 49e7b2d | 2019-07-03 11:49:59 -0700 | [diff] [blame] | 67 | mLines.insert(mLines.end(), 2, ""); |
| 68 | mLines.insert(mLines.end(), comment->mLines.begin(), comment->mLines.end()); |
| Neel Mehta | 291d02e | 2019-06-06 17:51:07 -0700 | [diff] [blame] | 69 | mLocation.setLocation(mLocation.begin(), comment->mLocation.end()); |
| Steven Moreland | 49bad8d | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| Neel Mehta | 3b414a8 | 2019-07-02 15:47:48 -0700 | [diff] [blame] | 72 | void DocComment::emit(Formatter& out, CommentType type) const { |
| Steven Moreland | 04fff2b | 2019-10-24 16:14:48 -0700 | [diff] [blame] | 73 | CommentType useType = type; |
| 74 | if (useType == CommentType::UNSPECIFIED) useType = mType; |
| 75 | if (useType == CommentType::UNSPECIFIED) useType = CommentType::DOC_MULTILINE; |
| Neel Mehta | 3b414a8 | 2019-07-02 15:47:48 -0700 | [diff] [blame] | 76 | |
| Steven Moreland | 04fff2b | 2019-10-24 16:14:48 -0700 | [diff] [blame] | 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 | } |
| Neel Mehta | 49e7b2d | 2019-07-03 11:49:59 -0700 | [diff] [blame] | 94 | |
| 95 | for (const std::string& line : mLines) { |
| Steven Moreland | 04fff2b | 2019-10-24 16:14:48 -0700 | [diff] [blame] | 96 | out << (line.empty() && isMultiline ? "" : " ") << line << "\n"; |
| Neel Mehta | 49e7b2d | 2019-07-03 11:49:59 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| Steven Moreland | 04fff2b | 2019-10-24 16:14:48 -0700 | [diff] [blame] | 99 | if (isMultiline) { |
| 100 | out.popLinePrefix(); |
| 101 | out << " */\n"; |
| 102 | } |
| Steven Moreland | 49bad8d | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | } // namespace android |