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 | 7645fbd | 2019-03-12 18:49:28 -0700 | [diff] [blame] | 19 | #include <android-base/strings.h> |
Steven Moreland | 49bad8d | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 20 | #include <hidl-util/StringHelper.h> |
| 21 | |
| 22 | #include <cctype> |
Neel Mehta | 49e7b2d | 2019-07-03 11:49:59 -0700 | [diff] [blame] | 23 | #include <vector> |
Steven Moreland | 49bad8d | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 24 | |
Neel Mehta | 291d02e | 2019-06-06 17:51:07 -0700 | [diff] [blame] | 25 | #include "Location.h" |
Steven Moreland | 7645fbd | 2019-03-12 18:49:28 -0700 | [diff] [blame] | 26 | |
Steven Moreland | 49bad8d | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 27 | namespace android { |
| 28 | |
Neel Mehta | 291d02e | 2019-06-06 17:51:07 -0700 | [diff] [blame] | 29 | DocComment::DocComment(const std::string& comment, const Location& location) : mLocation(location) { |
Steven Moreland | 7645fbd | 2019-03-12 18:49:28 -0700 | [diff] [blame] | 30 | std::vector<std::string> lines = base::Split(base::Trim(comment), "\n"); |
Steven Moreland | 49bad8d | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 31 | |
| 32 | bool foundFirstLine = false; |
| 33 | |
Steven Moreland | 49bad8d | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 34 | for (size_t l = 0; l < lines.size(); l++) { |
| 35 | const std::string& line = lines[l]; |
| 36 | |
| 37 | // Delete prefixes like " * ", " *", or " ". |
| 38 | size_t idx = 0; |
| 39 | for (; idx < line.size() && isspace(line[idx]); idx++) |
| 40 | ; |
| 41 | if (idx < line.size() && line[idx] == '*') idx++; |
| 42 | if (idx < line.size() && line[idx] == ' ') idx++; |
| 43 | |
Neel Mehta | 49e7b2d | 2019-07-03 11:49:59 -0700 | [diff] [blame] | 44 | const std::string& sanitizedLine = line.substr(idx); |
| 45 | int i = sanitizedLine.size(); |
| 46 | for (; i > 0 && isspace(sanitizedLine[i - 1]); i--) |
| 47 | ; |
| 48 | |
| 49 | // Either the size is 0 or everything was whitespace. |
| 50 | bool isEmptyLine = i == 0; |
Steven Moreland | 49bad8d | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 51 | |
Steven Moreland | 7645fbd | 2019-03-12 18:49:28 -0700 | [diff] [blame] | 52 | foundFirstLine = foundFirstLine || !isEmptyLine; |
Steven Moreland | 49bad8d | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 53 | if (!foundFirstLine) continue; |
| 54 | |
Neel Mehta | 49e7b2d | 2019-07-03 11:49:59 -0700 | [diff] [blame] | 55 | // if isEmptyLine, i == 0 so substr == "" |
| 56 | mLines.push_back(sanitizedLine.substr(0, i)); |
Steven Moreland | 49bad8d | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 57 | } |
Steven Moreland | 49bad8d | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void DocComment::merge(const DocComment* comment) { |
Neel Mehta | 49e7b2d | 2019-07-03 11:49:59 -0700 | [diff] [blame] | 61 | mLines.insert(mLines.end(), 2, ""); |
| 62 | mLines.insert(mLines.end(), comment->mLines.begin(), comment->mLines.end()); |
Neel Mehta | 291d02e | 2019-06-06 17:51:07 -0700 | [diff] [blame] | 63 | mLocation.setLocation(mLocation.begin(), comment->mLocation.end()); |
Steven Moreland | 49bad8d | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Neel Mehta | 3b414a8 | 2019-07-02 15:47:48 -0700 | [diff] [blame] | 66 | void DocComment::emit(Formatter& out, CommentType type) const { |
| 67 | switch (type) { |
| 68 | case CommentType::DOC_MULTILINE: |
| 69 | out << "/**\n"; |
| 70 | break; |
| 71 | case CommentType::MULTILINE: |
| 72 | out << "/*\n"; |
| 73 | break; |
| 74 | } |
| 75 | |
Neel Mehta | 49e7b2d | 2019-07-03 11:49:59 -0700 | [diff] [blame] | 76 | out.setLinePrefix(" *"); |
| 77 | |
| 78 | for (const std::string& line : mLines) { |
| 79 | out << (line.empty() ? "" : " ") << line << "\n"; |
| 80 | } |
| 81 | |
Steven Moreland | 49bad8d | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 82 | out.unsetLinePrefix(); |
| 83 | out << " */\n"; |
| 84 | } |
| 85 | |
| 86 | } // namespace android |