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 | #ifndef DOC_COMMENT_H_ |
| 18 | |
| 19 | #define DOC_COMMENT_H_ |
| 20 | |
| 21 | #include <hidl-util/Formatter.h> |
| 22 | |
| 23 | #include <string> |
Neel Mehta | 49e7b2d | 2019-07-03 11:49:59 -0700 | [diff] [blame] | 24 | #include <vector> |
Steven Moreland | 49bad8d | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 25 | |
Neel Mehta | 291d02e | 2019-06-06 17:51:07 -0700 | [diff] [blame] | 26 | #include "Location.h" |
| 27 | |
Steven Moreland | 49bad8d | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 28 | namespace android { |
| 29 | |
Neel Mehta | 3b414a8 | 2019-07-02 15:47:48 -0700 | [diff] [blame] | 30 | enum class CommentType { |
Steven Moreland | 04fff2b | 2019-10-24 16:14:48 -0700 | [diff] [blame] | 31 | // when no particular style is specified |
| 32 | UNSPECIFIED, |
| 33 | |
Neel Mehta | 3b414a8 | 2019-07-02 15:47:48 -0700 | [diff] [blame] | 34 | // multiline comment that begins with /** |
| 35 | DOC_MULTILINE, |
| 36 | // begins with /* (used for headers) |
Steven Moreland | 04fff2b | 2019-10-24 16:14:48 -0700 | [diff] [blame] | 37 | MULTILINE, |
| 38 | // begins with '//' |
| 39 | SINGLELINE, |
Neel Mehta | 3b414a8 | 2019-07-02 15:47:48 -0700 | [diff] [blame] | 40 | }; |
| 41 | |
Steven Moreland | 49bad8d | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 42 | struct DocComment { |
Steven Moreland | 449d4f0 | 2019-10-24 17:43:39 -0700 | [diff] [blame] | 43 | // parse comment and remove leading comment characters |
Steven Moreland | 04fff2b | 2019-10-24 16:14:48 -0700 | [diff] [blame] | 44 | DocComment(const std::string& comment, const Location& location, |
| 45 | CommentType type = CommentType::UNSPECIFIED); |
Steven Moreland | 449d4f0 | 2019-10-24 17:43:39 -0700 | [diff] [blame] | 46 | // raw comment |
| 47 | DocComment(const std::vector<std::string>& lines, const Location& location, |
| 48 | CommentType type = CommentType::UNSPECIFIED); |
Steven Moreland | 49bad8d | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 49 | |
| 50 | void merge(const DocComment* comment); |
| 51 | |
Steven Moreland | 04fff2b | 2019-10-24 16:14:48 -0700 | [diff] [blame] | 52 | void emit(Formatter& out, CommentType type = CommentType::UNSPECIFIED) const; |
Steven Moreland | 49bad8d | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 53 | |
Neel Mehta | 49e7b2d | 2019-07-03 11:49:59 -0700 | [diff] [blame] | 54 | const std::vector<std::string>& lines() const { return mLines; } |
Neel Mehta | 291d02e | 2019-06-06 17:51:07 -0700 | [diff] [blame] | 55 | |
| 56 | const Location& location() const { return mLocation; } |
| 57 | |
| 58 | private: |
Neel Mehta | 49e7b2d | 2019-07-03 11:49:59 -0700 | [diff] [blame] | 59 | std::vector<std::string> mLines; |
Steven Moreland | 04fff2b | 2019-10-24 16:14:48 -0700 | [diff] [blame] | 60 | CommentType mType; |
Neel Mehta | 291d02e | 2019-06-06 17:51:07 -0700 | [diff] [blame] | 61 | Location mLocation; |
Steven Moreland | 49bad8d | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | struct 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 Moreland | 7645fbd | 2019-03-12 18:49:28 -0700 | [diff] [blame] | 72 | const DocComment* getDocComment() const { return mDocComment; } |
| 73 | |
| 74 | private: |
Steven Moreland | 49bad8d | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 75 | const DocComment* mDocComment = nullptr; |
| 76 | }; |
| 77 | |
| 78 | } // namespace android |
| 79 | |
| 80 | #endif // DOC_COMMENT_H_ |