blob: b84ff3d24db44faff600d051c9044f49ebcc1f00 [file] [log] [blame]
Steven Moreland49bad8d2018-05-17 15:45:26 -07001/*
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 Moreland04fff2b2019-10-24 16:14:48 -070019#include <android-base/logging.h>
Steven Moreland7645fbd2019-03-12 18:49:28 -070020#include <android-base/strings.h>
Steven Moreland49bad8d2018-05-17 15:45:26 -070021#include <hidl-util/StringHelper.h>
22
23#include <cctype>
Neel Mehta49e7b2d2019-07-03 11:49:59 -070024#include <vector>
Steven Moreland49bad8d2018-05-17 15:45:26 -070025
Neel Mehta291d02e2019-06-06 17:51:07 -070026#include "Location.h"
Steven Moreland7645fbd2019-03-12 18:49:28 -070027
Steven Moreland49bad8d2018-05-17 15:45:26 -070028namespace android {
29
Steven Moreland04fff2b2019-10-24 16:14:48 -070030DocComment::DocComment(const std::string& comment, const Location& location, CommentType type)
Steven Moreland449d4f02019-10-24 17:43:39 -070031 : DocComment(std::vector<std::string>(), location, type) {
Steven Moreland7645fbd2019-03-12 18:49:28 -070032 std::vector<std::string> lines = base::Split(base::Trim(comment), "\n");
Steven Moreland49bad8d2018-05-17 15:45:26 -070033
34 bool foundFirstLine = false;
35
Steven Moreland49bad8d2018-05-17 15:45:26 -070036 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 Mehta49e7b2d2019-07-03 11:49:59 -070046 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 Moreland49bad8d2018-05-17 15:45:26 -070053
Steven Moreland7645fbd2019-03-12 18:49:28 -070054 foundFirstLine = foundFirstLine || !isEmptyLine;
Steven Moreland49bad8d2018-05-17 15:45:26 -070055 if (!foundFirstLine) continue;
56
Neel Mehta49e7b2d2019-07-03 11:49:59 -070057 // if isEmptyLine, i == 0 so substr == ""
58 mLines.push_back(sanitizedLine.substr(0, i));
Steven Moreland49bad8d2018-05-17 15:45:26 -070059 }
Steven Moreland49bad8d2018-05-17 15:45:26 -070060}
61
Steven Moreland449d4f02019-10-24 17:43:39 -070062DocComment::DocComment(const std::vector<std::string>& lines, const Location& location,
63 CommentType type)
64 : mLines(lines), mType(type), mLocation(location) {}
65
Steven Moreland49bad8d2018-05-17 15:45:26 -070066void DocComment::merge(const DocComment* comment) {
Neel Mehta49e7b2d2019-07-03 11:49:59 -070067 mLines.insert(mLines.end(), 2, "");
68 mLines.insert(mLines.end(), comment->mLines.begin(), comment->mLines.end());
Neel Mehta291d02e2019-06-06 17:51:07 -070069 mLocation.setLocation(mLocation.begin(), comment->mLocation.end());
Steven Moreland49bad8d2018-05-17 15:45:26 -070070}
71
Neel Mehta3b414a82019-07-02 15:47:48 -070072void DocComment::emit(Formatter& out, CommentType type) const {
Steven Moreland04fff2b2019-10-24 16:14:48 -070073 CommentType useType = type;
74 if (useType == CommentType::UNSPECIFIED) useType = mType;
75 if (useType == CommentType::UNSPECIFIED) useType = CommentType::DOC_MULTILINE;
Neel Mehta3b414a82019-07-02 15:47:48 -070076
Steven Moreland04fff2b2019-10-24 16:14:48 -070077 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 Mehta49e7b2d2019-07-03 11:49:59 -070094
95 for (const std::string& line : mLines) {
Steven Moreland04fff2b2019-10-24 16:14:48 -070096 out << (line.empty() && isMultiline ? "" : " ") << line << "\n";
Neel Mehta49e7b2d2019-07-03 11:49:59 -070097 }
98
Steven Moreland04fff2b2019-10-24 16:14:48 -070099 if (isMultiline) {
100 out.popLinePrefix();
101 out << " */\n";
102 }
Steven Moreland49bad8d2018-05-17 15:45:26 -0700103}
104
105} // namespace android