-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathDocComment.cpp
105 lines (83 loc) · 3.3 KB
/
DocComment.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "DocComment.h"
#include <android-base/logging.h>
#include <android-base/strings.h>
#include <hidl-util/StringHelper.h>
#include <cctype>
#include <vector>
#include "Location.h"
namespace android {
DocComment::DocComment(const std::string& comment, const Location& location, CommentType type)
: DocComment(std::vector<std::string>(), location, type) {
std::vector<std::string> lines = base::Split(base::Trim(comment), "\n");
bool foundFirstLine = false;
for (size_t l = 0; l < lines.size(); l++) {
const std::string& line = lines[l];
// Delete prefixes like " * ", " *", or " ".
size_t idx = 0;
for (; idx < line.size() && isspace(line[idx]); idx++)
;
if (idx < line.size() && line[idx] == '*') idx++;
if (idx < line.size() && line[idx] == ' ') idx++;
const std::string& sanitizedLine = line.substr(idx);
int i = sanitizedLine.size();
for (; i > 0 && isspace(sanitizedLine[i - 1]); i--)
;
// Either the size is 0 or everything was whitespace.
bool isEmptyLine = i == 0;
foundFirstLine = foundFirstLine || !isEmptyLine;
if (!foundFirstLine) continue;
// if isEmptyLine, i == 0 so substr == ""
mLines.push_back(sanitizedLine.substr(0, i));
}
}
DocComment::DocComment(const std::vector<std::string>& lines, const Location& location,
CommentType type)
: mLines(lines), mType(type), mLocation(location) {}
void DocComment::merge(const DocComment* comment) {
mLines.insert(mLines.end(), 2, "");
mLines.insert(mLines.end(), comment->mLines.begin(), comment->mLines.end());
mLocation.setLocation(mLocation.begin(), comment->mLocation.end());
}
void DocComment::emit(Formatter& out, CommentType type) const {
CommentType useType = type;
if (useType == CommentType::UNSPECIFIED) useType = mType;
if (useType == CommentType::UNSPECIFIED) useType = CommentType::DOC_MULTILINE;
bool isMultiline = useType != CommentType::SINGLELINE;
// singleline comments include '//' as part of line text
if (isMultiline) {
switch (useType) {
case CommentType::DOC_MULTILINE:
out << "/**\n";
break;
case CommentType::MULTILINE:
out << "/*\n";
break;
default:
LOG(FATAL) << "bad type: " << static_cast<int>(useType);
}
out.pushLinePrefix(" *");
}
for (const std::string& line : mLines) {
out << (line.empty() && isMultiline ? "" : " ") << line << "\n";
}
if (isMultiline) {
out.popLinePrefix();
out << " */\n";
}
}
} // namespace android