blob: 243e900dea5856d91067b11367dbcb86ef0a798b [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#ifndef DOC_COMMENT_H_
18
19#define DOC_COMMENT_H_
20
21#include <hidl-util/Formatter.h>
22
23#include <string>
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"
27
Steven Moreland49bad8d2018-05-17 15:45:26 -070028namespace android {
29
Neel Mehta3b414a82019-07-02 15:47:48 -070030enum class CommentType {
Steven Moreland04fff2b2019-10-24 16:14:48 -070031 // when no particular style is specified
32 UNSPECIFIED,
33
Neel Mehta3b414a82019-07-02 15:47:48 -070034 // multiline comment that begins with /**
35 DOC_MULTILINE,
36 // begins with /* (used for headers)
Steven Moreland04fff2b2019-10-24 16:14:48 -070037 MULTILINE,
38 // begins with '//'
39 SINGLELINE,
Neel Mehta3b414a82019-07-02 15:47:48 -070040};
41
Steven Moreland49bad8d2018-05-17 15:45:26 -070042struct DocComment {
Steven Moreland449d4f02019-10-24 17:43:39 -070043 // parse comment and remove leading comment characters
Steven Moreland04fff2b2019-10-24 16:14:48 -070044 DocComment(const std::string& comment, const Location& location,
45 CommentType type = CommentType::UNSPECIFIED);
Steven Moreland449d4f02019-10-24 17:43:39 -070046 // raw comment
47 DocComment(const std::vector<std::string>& lines, const Location& location,
48 CommentType type = CommentType::UNSPECIFIED);
Steven Moreland49bad8d2018-05-17 15:45:26 -070049
50 void merge(const DocComment* comment);
51
Steven Moreland04fff2b2019-10-24 16:14:48 -070052 void emit(Formatter& out, CommentType type = CommentType::UNSPECIFIED) const;
Steven Moreland49bad8d2018-05-17 15:45:26 -070053
Neel Mehta49e7b2d2019-07-03 11:49:59 -070054 const std::vector<std::string>& lines() const { return mLines; }
Neel Mehta291d02e2019-06-06 17:51:07 -070055
56 const Location& location() const { return mLocation; }
57
58 private:
Neel Mehta49e7b2d2019-07-03 11:49:59 -070059 std::vector<std::string> mLines;
Steven Moreland04fff2b2019-10-24 16:14:48 -070060 CommentType mType;
Neel Mehta291d02e2019-06-06 17:51:07 -070061 Location mLocation;
Steven Moreland49bad8d2018-05-17 15:45:26 -070062};
63
64struct DocCommentable {
65 void setDocComment(const DocComment* docComment) { mDocComment = docComment; }
66 void emitDocComment(Formatter& out) const {
67 if (mDocComment != nullptr) {
68 mDocComment->emit(out);
69 }
70 }
71
Steven Moreland7645fbd2019-03-12 18:49:28 -070072 const DocComment* getDocComment() const { return mDocComment; }
73
74 private:
Steven Moreland49bad8d2018-05-17 15:45:26 -070075 const DocComment* mDocComment = nullptr;
76};
77
78} // namespace android
79
80#endif // DOC_COMMENT_H_