Dmitri Gribenko | 5188c4b | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 1 | //===--- CommentBriefParser.cpp - Dumb comment parser ---------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Dmitri Gribenko | 5188c4b | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "clang/AST/CommentBriefParser.h" |
Dmitri Gribenko | ca7f80a | 2012-08-09 00:03:17 +0000 | [diff] [blame] | 10 | #include "clang/AST/CommentCommandTraits.h" |
Dmitri Gribenko | 5188c4b | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 11 | |
| 12 | namespace clang { |
| 13 | namespace comments { |
| 14 | |
Dmitri Gribenko | 0743f94 | 2012-06-28 01:38:21 +0000 | [diff] [blame] | 15 | namespace { |
Dmitri Gribenko | 75eea89 | 2012-08-21 21:15:34 +0000 | [diff] [blame] | 16 | inline bool isWhitespace(char C) { |
| 17 | return C == ' ' || C == '\n' || C == '\r' || |
| 18 | C == '\t' || C == '\f' || C == '\v'; |
| 19 | } |
| 20 | |
Dmitri Gribenko | 0743f94 | 2012-06-28 01:38:21 +0000 | [diff] [blame] | 21 | /// Convert all whitespace into spaces, remove leading and trailing spaces, |
| 22 | /// compress multiple spaces into one. |
| 23 | void cleanupBrief(std::string &S) { |
| 24 | bool PrevWasSpace = true; |
| 25 | std::string::iterator O = S.begin(); |
| 26 | for (std::string::iterator I = S.begin(), E = S.end(); |
| 27 | I != E; ++I) { |
| 28 | const char C = *I; |
Dmitri Gribenko | 75eea89 | 2012-08-21 21:15:34 +0000 | [diff] [blame] | 29 | if (isWhitespace(C)) { |
Dmitri Gribenko | 0743f94 | 2012-06-28 01:38:21 +0000 | [diff] [blame] | 30 | if (!PrevWasSpace) { |
| 31 | *O++ = ' '; |
| 32 | PrevWasSpace = true; |
| 33 | } |
| 34 | continue; |
| 35 | } else { |
| 36 | *O++ = C; |
| 37 | PrevWasSpace = false; |
| 38 | } |
| 39 | } |
| 40 | if (O != S.begin() && *(O - 1) == ' ') |
| 41 | --O; |
| 42 | |
| 43 | S.resize(O - S.begin()); |
| 44 | } |
Dmitri Gribenko | 75eea89 | 2012-08-21 21:15:34 +0000 | [diff] [blame] | 45 | |
| 46 | bool isWhitespace(StringRef Text) { |
| 47 | for (StringRef::const_iterator I = Text.begin(), E = Text.end(); |
| 48 | I != E; ++I) { |
| 49 | if (!isWhitespace(*I)) |
| 50 | return false; |
| 51 | } |
| 52 | return true; |
| 53 | } |
Dmitri Gribenko | 0743f94 | 2012-06-28 01:38:21 +0000 | [diff] [blame] | 54 | } // unnamed namespace |
| 55 | |
Dmitri Gribenko | ca7f80a | 2012-08-09 00:03:17 +0000 | [diff] [blame] | 56 | BriefParser::BriefParser(Lexer &L, const CommandTraits &Traits) : |
| 57 | L(L), Traits(Traits) { |
| 58 | // Get lookahead token. |
| 59 | ConsumeToken(); |
| 60 | } |
| 61 | |
Dmitri Gribenko | 5188c4b | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 62 | std::string BriefParser::Parse() { |
Dmitri Gribenko | 77369ee | 2012-07-20 17:01:34 +0000 | [diff] [blame] | 63 | std::string FirstParagraphOrBrief; |
| 64 | std::string ReturnsParagraph; |
Dmitri Gribenko | 5188c4b | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 65 | bool InFirstParagraph = true; |
| 66 | bool InBrief = false; |
Dmitri Gribenko | 77369ee | 2012-07-20 17:01:34 +0000 | [diff] [blame] | 67 | bool InReturns = false; |
Dmitri Gribenko | 5188c4b | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 68 | |
| 69 | while (Tok.isNot(tok::eof)) { |
| 70 | if (Tok.is(tok::text)) { |
Dmitri Gribenko | 99e0942 | 2012-06-27 01:17:34 +0000 | [diff] [blame] | 71 | if (InFirstParagraph || InBrief) |
Dmitri Gribenko | 77369ee | 2012-07-20 17:01:34 +0000 | [diff] [blame] | 72 | FirstParagraphOrBrief += Tok.getText(); |
| 73 | else if (InReturns) |
| 74 | ReturnsParagraph += Tok.getText(); |
Dmitri Gribenko | 5188c4b | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 75 | ConsumeToken(); |
| 76 | continue; |
| 77 | } |
| 78 | |
Fariborz Jahanian | e400cb7 | 2013-03-02 02:39:57 +0000 | [diff] [blame] | 79 | if (Tok.is(tok::backslash_command) || Tok.is(tok::at_command)) { |
Dmitri Gribenko | 7acbf00 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 80 | const CommandInfo *Info = Traits.getCommandInfo(Tok.getCommandID()); |
| 81 | if (Info->IsBriefCommand) { |
Dmitri Gribenko | 77369ee | 2012-07-20 17:01:34 +0000 | [diff] [blame] | 82 | FirstParagraphOrBrief.clear(); |
Dmitri Gribenko | a1e9c8e | 2012-06-28 00:01:41 +0000 | [diff] [blame] | 83 | InBrief = true; |
| 84 | ConsumeToken(); |
| 85 | continue; |
| 86 | } |
Dmitri Gribenko | 7acbf00 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 87 | if (Info->IsReturnsCommand) { |
Dmitri Gribenko | 77369ee | 2012-07-20 17:01:34 +0000 | [diff] [blame] | 88 | InReturns = true; |
Dmitri Gribenko | 75eea89 | 2012-08-21 21:15:34 +0000 | [diff] [blame] | 89 | InBrief = false; |
| 90 | InFirstParagraph = false; |
Dmitri Gribenko | 77369ee | 2012-07-20 17:01:34 +0000 | [diff] [blame] | 91 | ReturnsParagraph += "Returns "; |
Dmitri Gribenko | 75eea89 | 2012-08-21 21:15:34 +0000 | [diff] [blame] | 92 | ConsumeToken(); |
| 93 | continue; |
Dmitri Gribenko | 77369ee | 2012-07-20 17:01:34 +0000 | [diff] [blame] | 94 | } |
Dmitri Gribenko | 025d518 | 2012-06-29 18:19:20 +0000 | [diff] [blame] | 95 | // Block commands implicitly start a new paragraph. |
Dmitri Gribenko | 7acbf00 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 96 | if (Info->IsBlockCommand) { |
Dmitri Gribenko | a1e9c8e | 2012-06-28 00:01:41 +0000 | [diff] [blame] | 97 | // We found an implicit paragraph end. |
| 98 | InFirstParagraph = false; |
Dmitri Gribenko | 767ea0f | 2012-07-03 18:10:20 +0000 | [diff] [blame] | 99 | if (InBrief) |
Dmitri Gribenko | a1e9c8e | 2012-06-28 00:01:41 +0000 | [diff] [blame] | 100 | break; |
Dmitri Gribenko | a1e9c8e | 2012-06-28 00:01:41 +0000 | [diff] [blame] | 101 | } |
Dmitri Gribenko | 5188c4b | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | if (Tok.is(tok::newline)) { |
Dmitri Gribenko | 99e0942 | 2012-06-27 01:17:34 +0000 | [diff] [blame] | 105 | if (InFirstParagraph || InBrief) |
Dmitri Gribenko | 77369ee | 2012-07-20 17:01:34 +0000 | [diff] [blame] | 106 | FirstParagraphOrBrief += ' '; |
| 107 | else if (InReturns) |
| 108 | ReturnsParagraph += ' '; |
Dmitri Gribenko | 5188c4b | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 109 | ConsumeToken(); |
| 110 | |
Dmitri Gribenko | 75eea89 | 2012-08-21 21:15:34 +0000 | [diff] [blame] | 111 | // If the next token is a whitespace only text, ignore it. Thus we allow |
| 112 | // two paragraphs to be separated by line that has only whitespace in it. |
| 113 | // |
| 114 | // We don't need to add a space to the parsed text because we just added |
| 115 | // a space for the newline. |
| 116 | if (Tok.is(tok::text)) { |
| 117 | if (isWhitespace(Tok.getText())) |
| 118 | ConsumeToken(); |
| 119 | } |
| 120 | |
Dmitri Gribenko | 5188c4b | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 121 | if (Tok.is(tok::newline)) { |
| 122 | ConsumeToken(); |
Dmitri Gribenko | 75eea89 | 2012-08-21 21:15:34 +0000 | [diff] [blame] | 123 | // We found a paragraph end. This ends the brief description if |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 124 | // \command or its equivalent was explicitly used. |
| 125 | // Stop scanning text because an explicit \paragraph is the |
Dmitri Gribenko | 75eea89 | 2012-08-21 21:15:34 +0000 | [diff] [blame] | 126 | // preffered one. |
Dmitri Gribenko | 767ea0f | 2012-07-03 18:10:20 +0000 | [diff] [blame] | 127 | if (InBrief) |
Dmitri Gribenko | a1e9c8e | 2012-06-28 00:01:41 +0000 | [diff] [blame] | 128 | break; |
Dmitri Gribenko | 75eea89 | 2012-08-21 21:15:34 +0000 | [diff] [blame] | 129 | // End first paragraph if we found some non-whitespace text. |
| 130 | if (InFirstParagraph && !isWhitespace(FirstParagraphOrBrief)) |
| 131 | InFirstParagraph = false; |
| 132 | // End the \\returns paragraph because we found the paragraph end. |
| 133 | InReturns = false; |
Dmitri Gribenko | 5188c4b | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 134 | } |
| 135 | continue; |
| 136 | } |
| 137 | |
| 138 | // We didn't handle this token, so just drop it. |
| 139 | ConsumeToken(); |
| 140 | } |
| 141 | |
Dmitri Gribenko | 77369ee | 2012-07-20 17:01:34 +0000 | [diff] [blame] | 142 | cleanupBrief(FirstParagraphOrBrief); |
| 143 | if (!FirstParagraphOrBrief.empty()) |
| 144 | return FirstParagraphOrBrief; |
| 145 | |
| 146 | cleanupBrief(ReturnsParagraph); |
| 147 | return ReturnsParagraph; |
Dmitri Gribenko | 5188c4b | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Dmitri Gribenko | 5188c4b | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 150 | } // end namespace comments |
| 151 | } // end namespace clang |
| 152 | |
| 153 | |