blob: 2a1c9816e5c7ebd138bb7649f745cace2047dfcb [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 Moreland7645fbd2019-03-12 18:49:28 -070019#include <android-base/strings.h>
Steven Moreland49bad8d2018-05-17 15:45:26 -070020#include <hidl-util/StringHelper.h>
21
22#include <cctype>
Neel Mehta49e7b2d2019-07-03 11:49:59 -070023#include <vector>
Steven Moreland49bad8d2018-05-17 15:45:26 -070024
Neel Mehta291d02e2019-06-06 17:51:07 -070025#include "Location.h"
Steven Moreland7645fbd2019-03-12 18:49:28 -070026
Steven Moreland49bad8d2018-05-17 15:45:26 -070027namespace android {
28
Neel Mehta291d02e2019-06-06 17:51:07 -070029DocComment::DocComment(const std::string& comment, const Location& location) : mLocation(location) {
Steven Moreland7645fbd2019-03-12 18:49:28 -070030 std::vector<std::string> lines = base::Split(base::Trim(comment), "\n");
Steven Moreland49bad8d2018-05-17 15:45:26 -070031
32 bool foundFirstLine = false;
33
Steven Moreland49bad8d2018-05-17 15:45:26 -070034 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 Mehta49e7b2d2019-07-03 11:49:59 -070044 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 Moreland49bad8d2018-05-17 15:45:26 -070051
Steven Moreland7645fbd2019-03-12 18:49:28 -070052 foundFirstLine = foundFirstLine || !isEmptyLine;
Steven Moreland49bad8d2018-05-17 15:45:26 -070053 if (!foundFirstLine) continue;
54
Neel Mehta49e7b2d2019-07-03 11:49:59 -070055 // if isEmptyLine, i == 0 so substr == ""
56 mLines.push_back(sanitizedLine.substr(0, i));
Steven Moreland49bad8d2018-05-17 15:45:26 -070057 }
Steven Moreland49bad8d2018-05-17 15:45:26 -070058}
59
60void DocComment::merge(const DocComment* comment) {
Neel Mehta49e7b2d2019-07-03 11:49:59 -070061 mLines.insert(mLines.end(), 2, "");
62 mLines.insert(mLines.end(), comment->mLines.begin(), comment->mLines.end());
Neel Mehta291d02e2019-06-06 17:51:07 -070063 mLocation.setLocation(mLocation.begin(), comment->mLocation.end());
Steven Moreland49bad8d2018-05-17 15:45:26 -070064}
65
Neel Mehta3b414a82019-07-02 15:47:48 -070066void 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 Mehta49e7b2d2019-07-03 11:49:59 -070076 out.setLinePrefix(" *");
77
78 for (const std::string& line : mLines) {
79 out << (line.empty() ? "" : " ") << line << "\n";
80 }
81
Steven Moreland49bad8d2018-05-17 15:45:26 -070082 out.unsetLinePrefix();
83 out << " */\n";
84}
85
86} // namespace android