Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1 | //===--- UnwrappedLineParser.cpp - Format C++ code ------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// |
| 10 | /// \file |
| 11 | /// \brief This file contains the implementation of the UnwrappedLineParser, |
| 12 | /// which turns a stream of tokens into UnwrappedLines. |
| 13 | /// |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Chandler Carruth | 4b41745 | 2013-01-19 08:09:44 +0000 | [diff] [blame] | 16 | #include "UnwrappedLineParser.h" |
Benjamin Kramer | 33335df | 2015-03-01 21:36:40 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/STLExtras.h" |
Manuel Klimek | ab3dc00 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | 53f5e89 | 2015-03-23 18:05:43 +0000 | [diff] [blame] | 19 | #include "llvm/Support/raw_ostream.h" |
Manuel Klimek | ab3dc00 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 20 | |
Martin Probst | 7e0f25b | 2017-11-25 09:19:42 +0000 | [diff] [blame] | 21 | #include <algorithm> |
| 22 | |
Chandler Carruth | 1034666 | 2014-04-22 03:17:02 +0000 | [diff] [blame] | 23 | #define DEBUG_TYPE "format-parser" |
| 24 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 25 | namespace clang { |
| 26 | namespace format { |
| 27 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 28 | class FormatTokenSource { |
| 29 | public: |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 30 | virtual ~FormatTokenSource() {} |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 31 | virtual FormatToken *getNextToken() = 0; |
| 32 | |
| 33 | virtual unsigned getPosition() = 0; |
| 34 | virtual FormatToken *setPosition(unsigned Position) = 0; |
| 35 | }; |
| 36 | |
Craig Topper | 69665e1 | 2013-07-01 04:21:54 +0000 | [diff] [blame] | 37 | namespace { |
| 38 | |
Manuel Klimek | 0a3a3c9 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 39 | class ScopedDeclarationState { |
| 40 | public: |
| 41 | ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack, |
| 42 | bool MustBeDeclaration) |
| 43 | : Line(Line), Stack(Stack) { |
Manuel Klimek | 0a3a3c9 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 44 | Line.MustBeDeclaration = MustBeDeclaration; |
Manuel Klimek | 3908057 | 2013-01-23 11:03:04 +0000 | [diff] [blame] | 45 | Stack.push_back(MustBeDeclaration); |
Manuel Klimek | 0a3a3c9 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 46 | } |
| 47 | ~ScopedDeclarationState() { |
Manuel Klimek | 0a3a3c9 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 48 | Stack.pop_back(); |
Manuel Klimek | c1237a8 | 2013-01-23 14:08:21 +0000 | [diff] [blame] | 49 | if (!Stack.empty()) |
| 50 | Line.MustBeDeclaration = Stack.back(); |
| 51 | else |
| 52 | Line.MustBeDeclaration = true; |
Manuel Klimek | 0a3a3c9 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 53 | } |
Daniel Jasper | 393564f | 2013-05-31 14:56:29 +0000 | [diff] [blame] | 54 | |
Manuel Klimek | 0a3a3c9 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 55 | private: |
| 56 | UnwrappedLine &Line; |
| 57 | std::vector<bool> &Stack; |
| 58 | }; |
| 59 | |
Krasimir Georgiev | a1c3093 | 2017-05-19 10:34:57 +0000 | [diff] [blame] | 60 | static bool isLineComment(const FormatToken &FormatTok) { |
Krasimir Georgiev | 410ed24 | 2017-11-10 12:50:09 +0000 | [diff] [blame] | 61 | return FormatTok.is(tok::comment) && !FormatTok.TokenText.startswith("/*"); |
Krasimir Georgiev | a1c3093 | 2017-05-19 10:34:57 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Krasimir Georgiev | ea222a7 | 2017-05-22 10:07:56 +0000 | [diff] [blame] | 64 | // Checks if \p FormatTok is a line comment that continues the line comment |
| 65 | // \p Previous. The original column of \p MinColumnToken is used to determine |
| 66 | // whether \p FormatTok is indented enough to the right to continue \p Previous. |
| 67 | static bool continuesLineComment(const FormatToken &FormatTok, |
| 68 | const FormatToken *Previous, |
| 69 | const FormatToken *MinColumnToken) { |
| 70 | if (!Previous || !MinColumnToken) |
| 71 | return false; |
| 72 | unsigned MinContinueColumn = |
| 73 | MinColumnToken->OriginalColumn + (isLineComment(*MinColumnToken) ? 0 : 1); |
| 74 | return isLineComment(FormatTok) && FormatTok.NewlinesBefore == 1 && |
| 75 | isLineComment(*Previous) && |
| 76 | FormatTok.OriginalColumn >= MinContinueColumn; |
| 77 | } |
| 78 | |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 79 | class ScopedMacroState : public FormatTokenSource { |
| 80 | public: |
| 81 | ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource, |
Manuel Klimek | 20e0af6 | 2015-05-06 11:56:29 +0000 | [diff] [blame] | 82 | FormatToken *&ResetToken) |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 83 | : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken), |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 84 | PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource), |
Krasimir Georgiev | a1c3093 | 2017-05-19 10:34:57 +0000 | [diff] [blame] | 85 | Token(nullptr), PreviousToken(nullptr) { |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 86 | TokenSource = this; |
Manuel Klimek | ef2cfb1 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 87 | Line.Level = 0; |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 88 | Line.InPPDirective = true; |
| 89 | } |
| 90 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 91 | ~ScopedMacroState() override { |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 92 | TokenSource = PreviousTokenSource; |
| 93 | ResetToken = Token; |
| 94 | Line.InPPDirective = false; |
Manuel Klimek | ef2cfb1 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 95 | Line.Level = PreviousLineLevel; |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 98 | FormatToken *getNextToken() override { |
Manuel Klimek | 7872571 | 2013-01-07 10:03:37 +0000 | [diff] [blame] | 99 | // The \c UnwrappedLineParser guards against this by never calling |
| 100 | // \c getNextToken() after it has encountered the first eof token. |
| 101 | assert(!eof()); |
Krasimir Georgiev | a1c3093 | 2017-05-19 10:34:57 +0000 | [diff] [blame] | 102 | PreviousToken = Token; |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 103 | Token = PreviousTokenSource->getNextToken(); |
| 104 | if (eof()) |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 105 | return getFakeEOF(); |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 106 | return Token; |
| 107 | } |
| 108 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 109 | unsigned getPosition() override { return PreviousTokenSource->getPosition(); } |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 110 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 111 | FormatToken *setPosition(unsigned Position) override { |
Krasimir Georgiev | a1c3093 | 2017-05-19 10:34:57 +0000 | [diff] [blame] | 112 | PreviousToken = nullptr; |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 113 | Token = PreviousTokenSource->setPosition(Position); |
| 114 | return Token; |
| 115 | } |
| 116 | |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 117 | private: |
Krasimir Georgiev | a1c3093 | 2017-05-19 10:34:57 +0000 | [diff] [blame] | 118 | bool eof() { |
| 119 | return Token && Token->HasUnescapedNewline && |
Krasimir Georgiev | ea222a7 | 2017-05-22 10:07:56 +0000 | [diff] [blame] | 120 | !continuesLineComment(*Token, PreviousToken, |
| 121 | /*MinColumnToken=*/PreviousToken); |
Krasimir Georgiev | a1c3093 | 2017-05-19 10:34:57 +0000 | [diff] [blame] | 122 | } |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 123 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 124 | FormatToken *getFakeEOF() { |
| 125 | static bool EOFInitialized = false; |
| 126 | static FormatToken FormatTok; |
| 127 | if (!EOFInitialized) { |
| 128 | FormatTok.Tok.startToken(); |
| 129 | FormatTok.Tok.setKind(tok::eof); |
| 130 | EOFInitialized = true; |
| 131 | } |
| 132 | return &FormatTok; |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | UnwrappedLine &Line; |
| 136 | FormatTokenSource *&TokenSource; |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 137 | FormatToken *&ResetToken; |
Manuel Klimek | ef2cfb1 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 138 | unsigned PreviousLineLevel; |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 139 | FormatTokenSource *PreviousTokenSource; |
| 140 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 141 | FormatToken *Token; |
Krasimir Georgiev | a1c3093 | 2017-05-19 10:34:57 +0000 | [diff] [blame] | 142 | FormatToken *PreviousToken; |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 143 | }; |
| 144 | |
Craig Topper | 69665e1 | 2013-07-01 04:21:54 +0000 | [diff] [blame] | 145 | } // end anonymous namespace |
| 146 | |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 147 | class ScopedLineState { |
| 148 | public: |
Manuel Klimek | d3b92fa | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 149 | ScopedLineState(UnwrappedLineParser &Parser, |
| 150 | bool SwitchToPreprocessorLines = false) |
David Blaikie | efb6eb2 | 2014-08-09 20:02:07 +0000 | [diff] [blame] | 151 | : Parser(Parser), OriginalLines(Parser.CurrentLines) { |
Manuel Klimek | d3b92fa | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 152 | if (SwitchToPreprocessorLines) |
| 153 | Parser.CurrentLines = &Parser.PreprocessorDirectives; |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 154 | else if (!Parser.Line->Tokens.empty()) |
| 155 | Parser.CurrentLines = &Parser.Line->Tokens.back().Children; |
David Blaikie | efb6eb2 | 2014-08-09 20:02:07 +0000 | [diff] [blame] | 156 | PreBlockLine = std::move(Parser.Line); |
| 157 | Parser.Line = llvm::make_unique<UnwrappedLine>(); |
Daniel Jasper | daffc0d | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 158 | Parser.Line->Level = PreBlockLine->Level; |
| 159 | Parser.Line->InPPDirective = PreBlockLine->InPPDirective; |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | ~ScopedLineState() { |
Daniel Jasper | daffc0d | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 163 | if (!Parser.Line->Tokens.empty()) { |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 164 | Parser.addUnwrappedLine(); |
| 165 | } |
Daniel Jasper | daffc0d | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 166 | assert(Parser.Line->Tokens.empty()); |
David Blaikie | efb6eb2 | 2014-08-09 20:02:07 +0000 | [diff] [blame] | 167 | Parser.Line = std::move(PreBlockLine); |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 168 | if (Parser.CurrentLines == &Parser.PreprocessorDirectives) |
| 169 | Parser.MustBreakBeforeNextToken = true; |
| 170 | Parser.CurrentLines = OriginalLines; |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | private: |
| 174 | UnwrappedLineParser &Parser; |
| 175 | |
David Blaikie | efb6eb2 | 2014-08-09 20:02:07 +0000 | [diff] [blame] | 176 | std::unique_ptr<UnwrappedLine> PreBlockLine; |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 177 | SmallVectorImpl<UnwrappedLine> *OriginalLines; |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 178 | }; |
| 179 | |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 180 | class CompoundStatementIndenter { |
| 181 | public: |
| 182 | CompoundStatementIndenter(UnwrappedLineParser *Parser, |
| 183 | const FormatStyle &Style, unsigned &LineLevel) |
| 184 | : LineLevel(LineLevel), OldLineLevel(LineLevel) { |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 185 | if (Style.BraceWrapping.AfterControlStatement) |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 186 | Parser->addUnwrappedLine(); |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 187 | if (Style.BraceWrapping.IndentBraces) |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 188 | ++LineLevel; |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 189 | } |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 190 | ~CompoundStatementIndenter() { LineLevel = OldLineLevel; } |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 191 | |
| 192 | private: |
| 193 | unsigned &LineLevel; |
| 194 | unsigned OldLineLevel; |
| 195 | }; |
| 196 | |
Craig Topper | 69665e1 | 2013-07-01 04:21:54 +0000 | [diff] [blame] | 197 | namespace { |
| 198 | |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 199 | class IndexedTokenSource : public FormatTokenSource { |
| 200 | public: |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 201 | IndexedTokenSource(ArrayRef<FormatToken *> Tokens) |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 202 | : Tokens(Tokens), Position(-1) {} |
| 203 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 204 | FormatToken *getNextToken() override { |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 205 | ++Position; |
| 206 | return Tokens[Position]; |
| 207 | } |
| 208 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 209 | unsigned getPosition() override { |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 210 | assert(Position >= 0); |
| 211 | return Position; |
| 212 | } |
| 213 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 214 | FormatToken *setPosition(unsigned P) override { |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 215 | Position = P; |
| 216 | return Tokens[Position]; |
| 217 | } |
| 218 | |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 219 | void reset() { Position = -1; } |
| 220 | |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 221 | private: |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 222 | ArrayRef<FormatToken *> Tokens; |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 223 | int Position; |
| 224 | }; |
| 225 | |
Craig Topper | 69665e1 | 2013-07-01 04:21:54 +0000 | [diff] [blame] | 226 | } // end anonymous namespace |
| 227 | |
Daniel Jasper | d2ae41a | 2013-05-15 08:14:19 +0000 | [diff] [blame] | 228 | UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style, |
Daniel Jasper | d0ec0d6 | 2014-11-04 12:41:02 +0000 | [diff] [blame] | 229 | const AdditionalKeywords &Keywords, |
Krasimir Georgiev | 9ad83fe | 2017-10-30 14:01:50 +0000 | [diff] [blame] | 230 | unsigned FirstStartColumn, |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 231 | ArrayRef<FormatToken *> Tokens, |
Daniel Jasper | d2ae41a | 2013-05-15 08:14:19 +0000 | [diff] [blame] | 232 | UnwrappedLineConsumer &Callback) |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 233 | : Line(new UnwrappedLine), MustBreakBeforeNextToken(false), |
Krasimir Georgiev | 00c5c72 | 2017-02-02 15:32:19 +0000 | [diff] [blame] | 234 | CurrentLines(&Lines), Style(Style), Keywords(Keywords), |
| 235 | CommentPragmasRegex(Style.CommentPragmas), Tokens(nullptr), |
Krasimir Georgiev | ad47c90 | 2017-08-30 14:34:57 +0000 | [diff] [blame] | 236 | Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1), |
Mark Zeren | 1c3afaf | 2018-02-05 15:59:00 +0000 | [diff] [blame] | 237 | IncludeGuard(Style.IndentPPDirectives == FormatStyle::PPDIS_None |
| 238 | ? IG_Rejected |
| 239 | : IG_Inited), |
| 240 | IncludeGuardToken(nullptr), FirstStartColumn(FirstStartColumn) {} |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 241 | |
| 242 | void UnwrappedLineParser::reset() { |
| 243 | PPBranchLevel = -1; |
Mark Zeren | 1c3afaf | 2018-02-05 15:59:00 +0000 | [diff] [blame] | 244 | IncludeGuard = Style.IndentPPDirectives == FormatStyle::PPDIS_None |
| 245 | ? IG_Rejected |
| 246 | : IG_Inited; |
| 247 | IncludeGuardToken = nullptr; |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 248 | Line.reset(new UnwrappedLine); |
| 249 | CommentsBeforeNextToken.clear(); |
Craig Topper | 2145bc0 | 2014-05-09 08:15:10 +0000 | [diff] [blame] | 250 | FormatTok = nullptr; |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 251 | MustBreakBeforeNextToken = false; |
| 252 | PreprocessorDirectives.clear(); |
| 253 | CurrentLines = &Lines; |
| 254 | DeclarationScopeStack.clear(); |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 255 | PPStack.clear(); |
Krasimir Georgiev | 9ad83fe | 2017-10-30 14:01:50 +0000 | [diff] [blame] | 256 | Line->FirstStartColumn = FirstStartColumn; |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 257 | } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 258 | |
Manuel Klimek | 20e0af6 | 2015-05-06 11:56:29 +0000 | [diff] [blame] | 259 | void UnwrappedLineParser::parse() { |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 260 | IndexedTokenSource TokenSource(AllTokens); |
Krasimir Georgiev | 9ad83fe | 2017-10-30 14:01:50 +0000 | [diff] [blame] | 261 | Line->FirstStartColumn = FirstStartColumn; |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 262 | do { |
| 263 | DEBUG(llvm::dbgs() << "----\n"); |
| 264 | reset(); |
| 265 | Tokens = &TokenSource; |
| 266 | TokenSource.reset(); |
Daniel Jasper | a79064a | 2013-03-01 18:11:39 +0000 | [diff] [blame] | 267 | |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 268 | readToken(); |
| 269 | parseFile(); |
Mark Zeren | 1c3afaf | 2018-02-05 15:59:00 +0000 | [diff] [blame] | 270 | |
| 271 | // If we found an include guard then all preprocessor directives (other than |
| 272 | // the guard) are over-indented by one. |
| 273 | if (IncludeGuard == IG_Found) |
| 274 | for (auto &Line : Lines) |
| 275 | if (Line.InPPDirective && Line.Level > 0) |
| 276 | --Line.Level; |
| 277 | |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 278 | // Create line with eof token. |
| 279 | pushToken(FormatTok); |
| 280 | addUnwrappedLine(); |
| 281 | |
| 282 | for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(), |
| 283 | E = Lines.end(); |
| 284 | I != E; ++I) { |
| 285 | Callback.consumeUnwrappedLine(*I); |
| 286 | } |
| 287 | Callback.finishRun(); |
| 288 | Lines.clear(); |
| 289 | while (!PPLevelBranchIndex.empty() && |
Daniel Jasper | 53bd167 | 2013-10-12 13:32:56 +0000 | [diff] [blame] | 290 | PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) { |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 291 | PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1); |
| 292 | PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1); |
| 293 | } |
| 294 | if (!PPLevelBranchIndex.empty()) { |
| 295 | ++PPLevelBranchIndex.back(); |
| 296 | assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size()); |
| 297 | assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back()); |
| 298 | } |
| 299 | } while (!PPLevelBranchIndex.empty()); |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 302 | void UnwrappedLineParser::parseFile() { |
Daniel Jasper | 9326f91 | 2015-05-05 08:40:32 +0000 | [diff] [blame] | 303 | // The top-level context in a file always has declarations, except for pre- |
| 304 | // processor directives and JavaScript files. |
| 305 | bool MustBeDeclaration = |
| 306 | !Line->InPPDirective && Style.Language != FormatStyle::LK_JavaScript; |
| 307 | ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, |
| 308 | MustBeDeclaration); |
Krasimir Georgiev | 26b144c | 2017-07-03 15:05:14 +0000 | [diff] [blame] | 309 | if (Style.Language == FormatStyle::LK_TextProto) |
| 310 | parseBracedList(); |
| 311 | else |
| 312 | parseLevel(/*HasOpeningBrace=*/false); |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 313 | // Make sure to format the remaining tokens. |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 314 | flushComments(true); |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 315 | addUnwrappedLine(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 318 | void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) { |
Daniel Jasper | 516d797 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 319 | bool SwitchLabelEncountered = false; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 320 | do { |
Birunthan Mohanathas | b001a0b | 2015-07-03 17:25:16 +0000 | [diff] [blame] | 321 | tok::TokenKind kind = FormatTok->Tok.getKind(); |
| 322 | if (FormatTok->Type == TT_MacroBlockBegin) { |
| 323 | kind = tok::l_brace; |
| 324 | } else if (FormatTok->Type == TT_MacroBlockEnd) { |
| 325 | kind = tok::r_brace; |
| 326 | } |
| 327 | |
| 328 | switch (kind) { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 329 | case tok::comment: |
Daniel Jasper | e25509f | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 330 | nextToken(); |
| 331 | addUnwrappedLine(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 332 | break; |
| 333 | case tok::l_brace: |
Manuel Klimek | 0a3a3c9 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 334 | // FIXME: Add parameter whether this can happen - if this happens, we must |
| 335 | // be in a non-declaration context. |
Daniel Jasper | b86e272 | 2015-08-24 13:23:37 +0000 | [diff] [blame] | 336 | if (!FormatTok->is(TT_MacroBlockBegin) && tryToParseBracedList()) |
| 337 | continue; |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 338 | parseBlock(/*MustBeDeclaration=*/false); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 339 | addUnwrappedLine(); |
| 340 | break; |
| 341 | case tok::r_brace: |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 342 | if (HasOpeningBrace) |
| 343 | return; |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 344 | nextToken(); |
| 345 | addUnwrappedLine(); |
Manuel Klimek | 1058d98 | 2013-01-06 20:07:31 +0000 | [diff] [blame] | 346 | break; |
Nico Weber | c29f83b | 2018-01-23 16:30:56 +0000 | [diff] [blame] | 347 | case tok::kw_default: { |
| 348 | unsigned StoredPosition = Tokens->getPosition(); |
| 349 | FormatToken *Next = Tokens->getNextToken(); |
| 350 | FormatTok = Tokens->setPosition(StoredPosition); |
| 351 | if (Next && Next->isNot(tok::colon)) { |
| 352 | // default not followed by ':' is not a case label; treat it like |
| 353 | // an identifier. |
| 354 | parseStructuralElement(); |
| 355 | break; |
| 356 | } |
| 357 | // Else, if it is 'default:', fall through to the case handling. |
Nico Weber | f1add5e | 2018-01-24 01:47:22 +0000 | [diff] [blame] | 358 | LLVM_FALLTHROUGH; |
Nico Weber | c29f83b | 2018-01-23 16:30:56 +0000 | [diff] [blame] | 359 | } |
Daniel Jasper | 516d797 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 360 | case tok::kw_case: |
Manuel Klimek | 89628f6 | 2017-09-20 09:51:03 +0000 | [diff] [blame] | 361 | if (Style.Language == FormatStyle::LK_JavaScript && |
| 362 | Line->MustBeDeclaration) { |
Martin Probst | f785fd9 | 2017-08-04 17:07:15 +0000 | [diff] [blame] | 363 | // A 'case: string' style field declaration. |
| 364 | parseStructuralElement(); |
| 365 | break; |
| 366 | } |
Daniel Jasper | 7240762 | 2013-09-02 08:26:29 +0000 | [diff] [blame] | 367 | if (!SwitchLabelEncountered && |
| 368 | (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1))) |
| 369 | ++Line->Level; |
Daniel Jasper | 516d797 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 370 | SwitchLabelEncountered = true; |
| 371 | parseStructuralElement(); |
| 372 | break; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 373 | default: |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 374 | parseStructuralElement(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 375 | break; |
| 376 | } |
| 377 | } while (!eof()); |
| 378 | } |
| 379 | |
Daniel Jasper | adba2aa | 2015-05-18 12:52:00 +0000 | [diff] [blame] | 380 | void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) { |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 381 | // We'll parse forward through the tokens until we hit |
| 382 | // a closing brace or eof - note that getNextToken() will |
| 383 | // parse macros, so this will magically work inside macro |
| 384 | // definitions, too. |
| 385 | unsigned StoredPosition = Tokens->getPosition(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 386 | FormatToken *Tok = FormatTok; |
Manuel Klimek | 89628f6 | 2017-09-20 09:51:03 +0000 | [diff] [blame] | 387 | const FormatToken *PrevTok = Tok->Previous; |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 388 | // Keep a stack of positions of lbrace tokens. We will |
| 389 | // update information about whether an lbrace starts a |
| 390 | // braced init list or a different block during the loop. |
Daniel Jasper | b1f74a8 | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 391 | SmallVector<FormatToken *, 8> LBraceStack; |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 392 | assert(Tok->Tok.is(tok::l_brace)); |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 393 | do { |
Daniel Jasper | eb65e91 | 2015-12-21 18:31:15 +0000 | [diff] [blame] | 394 | // Get next non-comment token. |
Daniel Jasper | 7f5d53e | 2013-07-01 09:15:46 +0000 | [diff] [blame] | 395 | FormatToken *NextTok; |
Daniel Jasper | ca7bd72 | 2013-07-01 16:43:38 +0000 | [diff] [blame] | 396 | unsigned ReadTokens = 0; |
Daniel Jasper | 7f5d53e | 2013-07-01 09:15:46 +0000 | [diff] [blame] | 397 | do { |
| 398 | NextTok = Tokens->getNextToken(); |
Daniel Jasper | ca7bd72 | 2013-07-01 16:43:38 +0000 | [diff] [blame] | 399 | ++ReadTokens; |
Daniel Jasper | 7f5d53e | 2013-07-01 09:15:46 +0000 | [diff] [blame] | 400 | } while (NextTok->is(tok::comment)); |
| 401 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 402 | switch (Tok->Tok.getKind()) { |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 403 | case tok::l_brace: |
Martin Probst | 95ed8e7 | 2017-05-31 09:29:40 +0000 | [diff] [blame] | 404 | if (Style.Language == FormatStyle::LK_JavaScript && PrevTok) { |
Martin Probst | e8e27ca | 2017-11-25 09:33:47 +0000 | [diff] [blame] | 405 | if (PrevTok->isOneOf(tok::colon, tok::less)) |
| 406 | // A ':' indicates this code is in a type, or a braced list |
| 407 | // following a label in an object literal ({a: {b: 1}}). |
| 408 | // A '<' could be an object used in a comparison, but that is nonsense |
| 409 | // code (can never return true), so more likely it is a generic type |
| 410 | // argument (`X<{a: string; b: number}>`). |
| 411 | // The code below could be confused by semicolons between the |
| 412 | // individual members in a type member list, which would normally |
| 413 | // trigger BK_Block. In both cases, this must be parsed as an inline |
| 414 | // braced init. |
Martin Probst | 95ed8e7 | 2017-05-31 09:29:40 +0000 | [diff] [blame] | 415 | Tok->BlockKind = BK_BracedInit; |
| 416 | else if (PrevTok->is(tok::r_paren)) |
| 417 | // `) { }` can only occur in function or method declarations in JS. |
| 418 | Tok->BlockKind = BK_Block; |
| 419 | } else { |
Daniel Jasper | b9a4990 | 2016-01-09 15:56:28 +0000 | [diff] [blame] | 420 | Tok->BlockKind = BK_Unknown; |
Martin Probst | 95ed8e7 | 2017-05-31 09:29:40 +0000 | [diff] [blame] | 421 | } |
Daniel Jasper | b1f74a8 | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 422 | LBraceStack.push_back(Tok); |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 423 | break; |
| 424 | case tok::r_brace: |
Daniel Jasper | b9a4990 | 2016-01-09 15:56:28 +0000 | [diff] [blame] | 425 | if (LBraceStack.empty()) |
| 426 | break; |
| 427 | if (LBraceStack.back()->BlockKind == BK_Unknown) { |
| 428 | bool ProbablyBracedList = false; |
| 429 | if (Style.Language == FormatStyle::LK_Proto) { |
| 430 | ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square); |
| 431 | } else { |
| 432 | // Using OriginalColumn to distinguish between ObjC methods and |
| 433 | // binary operators is a bit hacky. |
| 434 | bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) && |
| 435 | NextTok->OriginalColumn == 0; |
Daniel Jasper | 91b032a | 2014-05-22 12:46:38 +0000 | [diff] [blame] | 436 | |
Daniel Jasper | b9a4990 | 2016-01-09 15:56:28 +0000 | [diff] [blame] | 437 | // If there is a comma, semicolon or right paren after the closing |
| 438 | // brace, we assume this is a braced initializer list. Note that |
| 439 | // regardless how we mark inner braces here, we will overwrite the |
| 440 | // BlockKind later if we parse a braced list (where all blocks |
| 441 | // inside are by default braced lists), or when we explicitly detect |
| 442 | // blocks (for example while parsing lambdas). |
Martin Probst | 95ed8e7 | 2017-05-31 09:29:40 +0000 | [diff] [blame] | 443 | // FIXME: Some of these do not apply to JS, e.g. "} {" can never be a |
| 444 | // braced list in JS. |
Daniel Jasper | b9a4990 | 2016-01-09 15:56:28 +0000 | [diff] [blame] | 445 | ProbablyBracedList = |
Daniel Jasper | acffeb8 | 2016-03-05 18:34:26 +0000 | [diff] [blame] | 446 | (Style.Language == FormatStyle::LK_JavaScript && |
Martin Probst | e1e12a7 | 2016-08-19 14:35:01 +0000 | [diff] [blame] | 447 | NextTok->isOneOf(Keywords.kw_of, Keywords.kw_in, |
| 448 | Keywords.kw_as)) || |
Martin Probst | b7fb267 | 2017-05-10 13:53:29 +0000 | [diff] [blame] | 449 | (Style.isCpp() && NextTok->is(tok::l_paren)) || |
Daniel Jasper | b9a4990 | 2016-01-09 15:56:28 +0000 | [diff] [blame] | 450 | NextTok->isOneOf(tok::comma, tok::period, tok::colon, |
| 451 | tok::r_paren, tok::r_square, tok::l_brace, |
Martin Probst | b7fb267 | 2017-05-10 13:53:29 +0000 | [diff] [blame] | 452 | tok::l_square, tok::ellipsis) || |
Daniel Jasper | e4ada02 | 2016-12-13 10:05:03 +0000 | [diff] [blame] | 453 | (NextTok->is(tok::identifier) && |
| 454 | !PrevTok->isOneOf(tok::semi, tok::r_brace, tok::l_brace)) || |
Daniel Jasper | b9a4990 | 2016-01-09 15:56:28 +0000 | [diff] [blame] | 455 | (NextTok->is(tok::semi) && |
| 456 | (!ExpectClassBody || LBraceStack.size() != 1)) || |
| 457 | (NextTok->isBinaryOperator() && !NextIsObjCMethod); |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 458 | } |
Daniel Jasper | b9a4990 | 2016-01-09 15:56:28 +0000 | [diff] [blame] | 459 | if (ProbablyBracedList) { |
| 460 | Tok->BlockKind = BK_BracedInit; |
| 461 | LBraceStack.back()->BlockKind = BK_BracedInit; |
| 462 | } else { |
| 463 | Tok->BlockKind = BK_Block; |
| 464 | LBraceStack.back()->BlockKind = BK_Block; |
| 465 | } |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 466 | } |
Daniel Jasper | b9a4990 | 2016-01-09 15:56:28 +0000 | [diff] [blame] | 467 | LBraceStack.pop_back(); |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 468 | break; |
Daniel Jasper | ac7e34e | 2014-03-13 10:11:17 +0000 | [diff] [blame] | 469 | case tok::at: |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 470 | case tok::semi: |
| 471 | case tok::kw_if: |
| 472 | case tok::kw_while: |
| 473 | case tok::kw_for: |
| 474 | case tok::kw_switch: |
| 475 | case tok::kw_try: |
Nico Weber | fac2371 | 2015-02-04 15:26:27 +0000 | [diff] [blame] | 476 | case tok::kw___try: |
Daniel Jasper | b9a4990 | 2016-01-09 15:56:28 +0000 | [diff] [blame] | 477 | if (!LBraceStack.empty() && LBraceStack.back()->BlockKind == BK_Unknown) |
Daniel Jasper | b1f74a8 | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 478 | LBraceStack.back()->BlockKind = BK_Block; |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 479 | break; |
| 480 | default: |
| 481 | break; |
| 482 | } |
Daniel Jasper | b9a4990 | 2016-01-09 15:56:28 +0000 | [diff] [blame] | 483 | PrevTok = Tok; |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 484 | Tok = NextTok; |
Manuel Klimek | bab25fd | 2013-09-04 08:20:47 +0000 | [diff] [blame] | 485 | } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty()); |
Daniel Jasper | b9a4990 | 2016-01-09 15:56:28 +0000 | [diff] [blame] | 486 | |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 487 | // Assume other blocks for all unclosed opening braces. |
| 488 | for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) { |
Daniel Jasper | b1f74a8 | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 489 | if (LBraceStack[i]->BlockKind == BK_Unknown) |
| 490 | LBraceStack[i]->BlockKind = BK_Block; |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 491 | } |
Manuel Klimek | bab25fd | 2013-09-04 08:20:47 +0000 | [diff] [blame] | 492 | |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 493 | FormatTok = Tokens->setPosition(StoredPosition); |
| 494 | } |
| 495 | |
Francois Ferrand | a98a95c | 2017-07-28 07:56:14 +0000 | [diff] [blame] | 496 | template <class T> |
| 497 | static inline void hash_combine(std::size_t &seed, const T &v) { |
| 498 | std::hash<T> hasher; |
| 499 | seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); |
| 500 | } |
| 501 | |
| 502 | size_t UnwrappedLineParser::computePPHash() const { |
| 503 | size_t h = 0; |
| 504 | for (const auto &i : PPStack) { |
| 505 | hash_combine(h, size_t(i.Kind)); |
| 506 | hash_combine(h, i.Line); |
| 507 | } |
| 508 | return h; |
| 509 | } |
| 510 | |
Manuel Klimek | b212f3b | 2013-10-12 22:46:56 +0000 | [diff] [blame] | 511 | void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel, |
| 512 | bool MunchSemi) { |
Birunthan Mohanathas | b001a0b | 2015-07-03 17:25:16 +0000 | [diff] [blame] | 513 | assert(FormatTok->isOneOf(tok::l_brace, TT_MacroBlockBegin) && |
| 514 | "'{' or macro block token expected"); |
| 515 | const bool MacroBlock = FormatTok->is(TT_MacroBlockBegin); |
Daniel Jasper | eb65e91 | 2015-12-21 18:31:15 +0000 | [diff] [blame] | 516 | FormatTok->BlockKind = BK_Block; |
Birunthan Mohanathas | b001a0b | 2015-07-03 17:25:16 +0000 | [diff] [blame] | 517 | |
Francois Ferrand | a98a95c | 2017-07-28 07:56:14 +0000 | [diff] [blame] | 518 | size_t PPStartHash = computePPHash(); |
| 519 | |
Daniel Jasper | 516d797 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 520 | unsigned InitialLevel = Line->Level; |
Krasimir Georgiev | 3e05105 | 2017-07-24 14:51:59 +0000 | [diff] [blame] | 521 | nextToken(/*LevelDifference=*/AddLevel ? 1 : 0); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 522 | |
Birunthan Mohanathas | b001a0b | 2015-07-03 17:25:16 +0000 | [diff] [blame] | 523 | if (MacroBlock && FormatTok->is(tok::l_paren)) |
| 524 | parseParens(); |
| 525 | |
Francois Ferrand | a98a95c | 2017-07-28 07:56:14 +0000 | [diff] [blame] | 526 | size_t NbPreprocessorDirectives = |
| 527 | CurrentLines == &Lines ? PreprocessorDirectives.size() : 0; |
Manuel Klimek | a4fe1c1 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 528 | addUnwrappedLine(); |
Francois Ferrand | a98a95c | 2017-07-28 07:56:14 +0000 | [diff] [blame] | 529 | size_t OpeningLineIndex = |
| 530 | CurrentLines->empty() |
| 531 | ? (UnwrappedLine::kInvalidIndex) |
| 532 | : (CurrentLines->size() - 1 - NbPreprocessorDirectives); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 533 | |
Manuel Klimek | 0a3a3c9 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 534 | ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, |
| 535 | MustBeDeclaration); |
Daniel Jasper | 65ee347 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 536 | if (AddLevel) |
| 537 | ++Line->Level; |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 538 | parseLevel(/*HasOpeningBrace=*/true); |
Alexander Kornienko | 578fdd8 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 539 | |
Marianne Mailhot-Sarrasin | 03137c6 | 2016-04-14 14:56:49 +0000 | [diff] [blame] | 540 | if (eof()) |
| 541 | return; |
| 542 | |
Birunthan Mohanathas | b001a0b | 2015-07-03 17:25:16 +0000 | [diff] [blame] | 543 | if (MacroBlock ? !FormatTok->is(TT_MacroBlockEnd) |
| 544 | : !FormatTok->is(tok::r_brace)) { |
Daniel Jasper | 516d797 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 545 | Line->Level = InitialLevel; |
Daniel Jasper | eb65e91 | 2015-12-21 18:31:15 +0000 | [diff] [blame] | 546 | FormatTok->BlockKind = BK_Block; |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 547 | return; |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 548 | } |
Alexander Kornienko | 0ea8e10 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 549 | |
Francois Ferrand | a98a95c | 2017-07-28 07:56:14 +0000 | [diff] [blame] | 550 | size_t PPEndHash = computePPHash(); |
| 551 | |
Krasimir Georgiev | 3e05105 | 2017-07-24 14:51:59 +0000 | [diff] [blame] | 552 | // Munch the closing brace. |
| 553 | nextToken(/*LevelDifference=*/AddLevel ? -1 : 0); |
Birunthan Mohanathas | b001a0b | 2015-07-03 17:25:16 +0000 | [diff] [blame] | 554 | |
| 555 | if (MacroBlock && FormatTok->is(tok::l_paren)) |
| 556 | parseParens(); |
| 557 | |
Manuel Klimek | b212f3b | 2013-10-12 22:46:56 +0000 | [diff] [blame] | 558 | if (MunchSemi && FormatTok->Tok.is(tok::semi)) |
| 559 | nextToken(); |
Krasimir Georgiev | 3e05105 | 2017-07-24 14:51:59 +0000 | [diff] [blame] | 560 | Line->Level = InitialLevel; |
Francois Ferrand | a98a95c | 2017-07-28 07:56:14 +0000 | [diff] [blame] | 561 | |
| 562 | if (PPStartHash == PPEndHash) { |
| 563 | Line->MatchingOpeningBlockLineIndex = OpeningLineIndex; |
| 564 | if (OpeningLineIndex != UnwrappedLine::kInvalidIndex) { |
| 565 | // Update the opening line to add the forward reference as well |
| 566 | (*CurrentLines)[OpeningLineIndex].MatchingOpeningBlockLineIndex = |
| 567 | CurrentLines->size() - 1; |
| 568 | } |
Francois Ferrand | e56a829 | 2017-06-14 12:29:47 +0000 | [diff] [blame] | 569 | } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Daniel Jasper | 02c7bca | 2015-03-30 09:56:50 +0000 | [diff] [blame] | 572 | static bool isGoogScope(const UnwrappedLine &Line) { |
Daniel Jasper | 616de864 | 2014-11-23 16:46:28 +0000 | [diff] [blame] | 573 | // FIXME: Closure-library specific stuff should not be hard-coded but be |
| 574 | // configurable. |
Daniel Jasper | 4a39c84 | 2014-05-06 13:54:10 +0000 | [diff] [blame] | 575 | if (Line.Tokens.size() < 4) |
| 576 | return false; |
| 577 | auto I = Line.Tokens.begin(); |
| 578 | if (I->Tok->TokenText != "goog") |
| 579 | return false; |
| 580 | ++I; |
| 581 | if (I->Tok->isNot(tok::period)) |
| 582 | return false; |
| 583 | ++I; |
| 584 | if (I->Tok->TokenText != "scope") |
| 585 | return false; |
| 586 | ++I; |
| 587 | return I->Tok->is(tok::l_paren); |
| 588 | } |
| 589 | |
Martin Probst | 101ec89 | 2017-05-09 20:04:09 +0000 | [diff] [blame] | 590 | static bool isIIFE(const UnwrappedLine &Line, |
| 591 | const AdditionalKeywords &Keywords) { |
| 592 | // Look for the start of an immediately invoked anonymous function. |
| 593 | // https://en.wikipedia.org/wiki/Immediately-invoked_function_expression |
| 594 | // This is commonly done in JavaScript to create a new, anonymous scope. |
| 595 | // Example: (function() { ... })() |
| 596 | if (Line.Tokens.size() < 3) |
| 597 | return false; |
| 598 | auto I = Line.Tokens.begin(); |
| 599 | if (I->Tok->isNot(tok::l_paren)) |
| 600 | return false; |
| 601 | ++I; |
| 602 | if (I->Tok->isNot(Keywords.kw_function)) |
| 603 | return false; |
| 604 | ++I; |
| 605 | return I->Tok->is(tok::l_paren); |
| 606 | } |
| 607 | |
Roman Kashitsyn | a043ced | 2014-08-11 12:18:01 +0000 | [diff] [blame] | 608 | static bool ShouldBreakBeforeBrace(const FormatStyle &Style, |
| 609 | const FormatToken &InitialToken) { |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 610 | if (InitialToken.is(tok::kw_namespace)) |
| 611 | return Style.BraceWrapping.AfterNamespace; |
| 612 | if (InitialToken.is(tok::kw_class)) |
| 613 | return Style.BraceWrapping.AfterClass; |
| 614 | if (InitialToken.is(tok::kw_union)) |
| 615 | return Style.BraceWrapping.AfterUnion; |
| 616 | if (InitialToken.is(tok::kw_struct)) |
| 617 | return Style.BraceWrapping.AfterStruct; |
| 618 | return false; |
Roman Kashitsyn | a043ced | 2014-08-11 12:18:01 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 621 | void UnwrappedLineParser::parseChildBlock() { |
| 622 | FormatTok->BlockKind = BK_Block; |
| 623 | nextToken(); |
| 624 | { |
Manuel Klimek | 89628f6 | 2017-09-20 09:51:03 +0000 | [diff] [blame] | 625 | bool SkipIndent = (Style.Language == FormatStyle::LK_JavaScript && |
| 626 | (isGoogScope(*Line) || isIIFE(*Line, Keywords))); |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 627 | ScopedLineState LineState(*this); |
| 628 | ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, |
| 629 | /*MustBeDeclaration=*/false); |
Martin Probst | 101ec89 | 2017-05-09 20:04:09 +0000 | [diff] [blame] | 630 | Line->Level += SkipIndent ? 0 : 1; |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 631 | parseLevel(/*HasOpeningBrace=*/true); |
Daniel Jasper | 02c7bca | 2015-03-30 09:56:50 +0000 | [diff] [blame] | 632 | flushComments(isOnNewLine(*FormatTok)); |
Martin Probst | 101ec89 | 2017-05-09 20:04:09 +0000 | [diff] [blame] | 633 | Line->Level -= SkipIndent ? 0 : 1; |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 634 | } |
| 635 | nextToken(); |
| 636 | } |
| 637 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 638 | void UnwrappedLineParser::parsePPDirective() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 639 | assert(FormatTok->Tok.is(tok::hash) && "'#' expected"); |
Manuel Klimek | 20e0af6 | 2015-05-06 11:56:29 +0000 | [diff] [blame] | 640 | ScopedMacroState MacroState(*Line, Tokens, FormatTok); |
Manuel Klimek | a71e5d8 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 641 | nextToken(); |
| 642 | |
Craig Topper | 2145bc0 | 2014-05-09 08:15:10 +0000 | [diff] [blame] | 643 | if (!FormatTok->Tok.getIdentifierInfo()) { |
Manuel Klimek | 591b580 | 2013-01-31 15:58:48 +0000 | [diff] [blame] | 644 | parsePPUnknown(); |
Manuel Klimek | a71e5d8 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 645 | return; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 646 | } |
Manuel Klimek | a71e5d8 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 647 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 648 | switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) { |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 649 | case tok::pp_define: |
| 650 | parsePPDefine(); |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 651 | return; |
| 652 | case tok::pp_if: |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 653 | parsePPIf(/*IfDef=*/false); |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 654 | break; |
| 655 | case tok::pp_ifdef: |
| 656 | case tok::pp_ifndef: |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 657 | parsePPIf(/*IfDef=*/true); |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 658 | break; |
| 659 | case tok::pp_else: |
| 660 | parsePPElse(); |
| 661 | break; |
| 662 | case tok::pp_elif: |
| 663 | parsePPElIf(); |
| 664 | break; |
| 665 | case tok::pp_endif: |
| 666 | parsePPEndIf(); |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 667 | break; |
| 668 | default: |
| 669 | parsePPUnknown(); |
| 670 | break; |
| 671 | } |
| 672 | } |
| 673 | |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 674 | void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) { |
Francois Ferrand | a98a95c | 2017-07-28 07:56:14 +0000 | [diff] [blame] | 675 | size_t Line = CurrentLines->size(); |
| 676 | if (CurrentLines == &PreprocessorDirectives) |
| 677 | Line += Lines.size(); |
| 678 | |
| 679 | if (Unreachable || |
| 680 | (!PPStack.empty() && PPStack.back().Kind == PP_Unreachable)) |
| 681 | PPStack.push_back({PP_Unreachable, Line}); |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 682 | else |
Francois Ferrand | a98a95c | 2017-07-28 07:56:14 +0000 | [diff] [blame] | 683 | PPStack.push_back({PP_Conditional, Line}); |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 686 | void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) { |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 687 | ++PPBranchLevel; |
| 688 | assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size()); |
| 689 | if (PPBranchLevel == (int)PPLevelBranchIndex.size()) { |
| 690 | PPLevelBranchIndex.push_back(0); |
| 691 | PPLevelBranchCount.push_back(0); |
| 692 | } |
| 693 | PPChainBranchIndex.push(0); |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 694 | bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0; |
| 695 | conditionalCompilationCondition(Unreachable || Skip); |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 698 | void UnwrappedLineParser::conditionalCompilationAlternative() { |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 699 | if (!PPStack.empty()) |
| 700 | PPStack.pop_back(); |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 701 | assert(PPBranchLevel < (int)PPLevelBranchIndex.size()); |
| 702 | if (!PPChainBranchIndex.empty()) |
| 703 | ++PPChainBranchIndex.top(); |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 704 | conditionalCompilationCondition( |
| 705 | PPBranchLevel >= 0 && !PPChainBranchIndex.empty() && |
| 706 | PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top()); |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 707 | } |
| 708 | |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 709 | void UnwrappedLineParser::conditionalCompilationEnd() { |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 710 | assert(PPBranchLevel < (int)PPLevelBranchIndex.size()); |
| 711 | if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) { |
| 712 | if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) { |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 713 | PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1; |
| 714 | } |
| 715 | } |
Manuel Klimek | 14bd917 | 2014-01-29 08:49:02 +0000 | [diff] [blame] | 716 | // Guard against #endif's without #if. |
Krasimir Georgiev | ad47c90 | 2017-08-30 14:34:57 +0000 | [diff] [blame] | 717 | if (PPBranchLevel > -1) |
Manuel Klimek | 14bd917 | 2014-01-29 08:49:02 +0000 | [diff] [blame] | 718 | --PPBranchLevel; |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 719 | if (!PPChainBranchIndex.empty()) |
| 720 | PPChainBranchIndex.pop(); |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 721 | if (!PPStack.empty()) |
| 722 | PPStack.pop_back(); |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | void UnwrappedLineParser::parsePPIf(bool IfDef) { |
Daniel Jasper | 62703eb | 2017-03-01 11:10:11 +0000 | [diff] [blame] | 726 | bool IfNDef = FormatTok->is(tok::pp_ifndef); |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 727 | nextToken(); |
Daniel Jasper | eab6cd4 | 2017-03-01 10:47:52 +0000 | [diff] [blame] | 728 | bool Unreachable = false; |
| 729 | if (!IfDef && (FormatTok->is(tok::kw_false) || FormatTok->TokenText == "0")) |
| 730 | Unreachable = true; |
Daniel Jasper | 62703eb | 2017-03-01 11:10:11 +0000 | [diff] [blame] | 731 | if (IfDef && !IfNDef && FormatTok->TokenText == "SWIG") |
Daniel Jasper | eab6cd4 | 2017-03-01 10:47:52 +0000 | [diff] [blame] | 732 | Unreachable = true; |
| 733 | conditionalCompilationStart(Unreachable); |
Krasimir Georgiev | ad47c90 | 2017-08-30 14:34:57 +0000 | [diff] [blame] | 734 | FormatToken *IfCondition = FormatTok; |
| 735 | // If there's a #ifndef on the first line, and the only lines before it are |
| 736 | // comments, it could be an include guard. |
| 737 | bool MaybeIncludeGuard = IfNDef; |
Mark Zeren | 1c3afaf | 2018-02-05 15:59:00 +0000 | [diff] [blame] | 738 | if (IncludeGuard == IG_Inited && MaybeIncludeGuard) |
Krasimir Georgiev | ad47c90 | 2017-08-30 14:34:57 +0000 | [diff] [blame] | 739 | for (auto &Line : Lines) { |
| 740 | if (!Line.Tokens.front().Tok->is(tok::comment)) { |
| 741 | MaybeIncludeGuard = false; |
Mark Zeren | 1c3afaf | 2018-02-05 15:59:00 +0000 | [diff] [blame] | 742 | IncludeGuard = IG_Rejected; |
Krasimir Georgiev | ad47c90 | 2017-08-30 14:34:57 +0000 | [diff] [blame] | 743 | break; |
| 744 | } |
| 745 | } |
Krasimir Georgiev | ad47c90 | 2017-08-30 14:34:57 +0000 | [diff] [blame] | 746 | --PPBranchLevel; |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 747 | parsePPUnknown(); |
Krasimir Georgiev | ad47c90 | 2017-08-30 14:34:57 +0000 | [diff] [blame] | 748 | ++PPBranchLevel; |
Mark Zeren | 1c3afaf | 2018-02-05 15:59:00 +0000 | [diff] [blame] | 749 | if (IncludeGuard == IG_Inited && MaybeIncludeGuard) { |
| 750 | IncludeGuard = IG_IfNdefed; |
| 751 | IncludeGuardToken = IfCondition; |
| 752 | } |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | void UnwrappedLineParser::parsePPElse() { |
Krasimir Georgiev | ad47c90 | 2017-08-30 14:34:57 +0000 | [diff] [blame] | 756 | // If a potential include guard has an #else, it's not an include guard. |
Mark Zeren | 1c3afaf | 2018-02-05 15:59:00 +0000 | [diff] [blame] | 757 | if (IncludeGuard == IG_Defined && PPBranchLevel == 0) |
| 758 | IncludeGuard = IG_Rejected; |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 759 | conditionalCompilationAlternative(); |
Krasimir Georgiev | ad47c90 | 2017-08-30 14:34:57 +0000 | [diff] [blame] | 760 | if (PPBranchLevel > -1) |
| 761 | --PPBranchLevel; |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 762 | parsePPUnknown(); |
Krasimir Georgiev | ad47c90 | 2017-08-30 14:34:57 +0000 | [diff] [blame] | 763 | ++PPBranchLevel; |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | void UnwrappedLineParser::parsePPElIf() { parsePPElse(); } |
| 767 | |
| 768 | void UnwrappedLineParser::parsePPEndIf() { |
| 769 | conditionalCompilationEnd(); |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 770 | parsePPUnknown(); |
Krasimir Georgiev | ad47c90 | 2017-08-30 14:34:57 +0000 | [diff] [blame] | 771 | // If the #endif of a potential include guard is the last thing in the file, |
Mark Zeren | 1c3afaf | 2018-02-05 15:59:00 +0000 | [diff] [blame] | 772 | // then we found an include guard. |
Krasimir Georgiev | ad47c90 | 2017-08-30 14:34:57 +0000 | [diff] [blame] | 773 | unsigned TokenPosition = Tokens->getPosition(); |
| 774 | FormatToken *PeekNext = AllTokens[TokenPosition]; |
Mark Zeren | 1c3afaf | 2018-02-05 15:59:00 +0000 | [diff] [blame] | 775 | if (IncludeGuard == IG_Defined && PPBranchLevel == -1 && |
| 776 | PeekNext->is(tok::eof) && |
Daniel Jasper | 4df130f | 2017-09-04 13:33:52 +0000 | [diff] [blame] | 777 | Style.IndentPPDirectives != FormatStyle::PPDIS_None) |
Mark Zeren | 1c3afaf | 2018-02-05 15:59:00 +0000 | [diff] [blame] | 778 | IncludeGuard = IG_Found; |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 779 | } |
| 780 | |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 781 | void UnwrappedLineParser::parsePPDefine() { |
| 782 | nextToken(); |
| 783 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 784 | if (FormatTok->Tok.getKind() != tok::identifier) { |
Mark Zeren | 1c3afaf | 2018-02-05 15:59:00 +0000 | [diff] [blame] | 785 | IncludeGuard = IG_Rejected; |
| 786 | IncludeGuardToken = nullptr; |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 787 | parsePPUnknown(); |
| 788 | return; |
| 789 | } |
Mark Zeren | 1c3afaf | 2018-02-05 15:59:00 +0000 | [diff] [blame] | 790 | |
| 791 | if (IncludeGuard == IG_IfNdefed && |
| 792 | IncludeGuardToken->TokenText == FormatTok->TokenText) { |
| 793 | IncludeGuard = IG_Defined; |
| 794 | IncludeGuardToken = nullptr; |
Krasimir Georgiev | ad47c90 | 2017-08-30 14:34:57 +0000 | [diff] [blame] | 795 | for (auto &Line : Lines) { |
| 796 | if (!Line.Tokens.front().Tok->isOneOf(tok::comment, tok::hash)) { |
Mark Zeren | 1c3afaf | 2018-02-05 15:59:00 +0000 | [diff] [blame] | 797 | IncludeGuard = IG_Rejected; |
Krasimir Georgiev | ad47c90 | 2017-08-30 14:34:57 +0000 | [diff] [blame] | 798 | break; |
| 799 | } |
| 800 | } |
| 801 | } |
Mark Zeren | 1c3afaf | 2018-02-05 15:59:00 +0000 | [diff] [blame] | 802 | |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 803 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 804 | if (FormatTok->Tok.getKind() == tok::l_paren && |
| 805 | FormatTok->WhitespaceRange.getBegin() == |
| 806 | FormatTok->WhitespaceRange.getEnd()) { |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 807 | parseParens(); |
| 808 | } |
Krasimir Georgiev | ad47c90 | 2017-08-30 14:34:57 +0000 | [diff] [blame] | 809 | if (Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash) |
| 810 | Line->Level += PPBranchLevel + 1; |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 811 | addUnwrappedLine(); |
Krasimir Georgiev | ad47c90 | 2017-08-30 14:34:57 +0000 | [diff] [blame] | 812 | ++Line->Level; |
Manuel Klimek | 1b89629 | 2013-01-07 09:34:28 +0000 | [diff] [blame] | 813 | |
| 814 | // Errors during a preprocessor directive can only affect the layout of the |
| 815 | // preprocessor directive, and thus we ignore them. An alternative approach |
| 816 | // would be to use the same approach we use on the file level (no |
| 817 | // re-indentation if there was a structural error) within the macro |
| 818 | // definition. |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 819 | parseFile(); |
| 820 | } |
| 821 | |
| 822 | void UnwrappedLineParser::parsePPUnknown() { |
Manuel Klimek | a71e5d8 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 823 | do { |
Manuel Klimek | a71e5d8 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 824 | nextToken(); |
| 825 | } while (!eof()); |
Krasimir Georgiev | ad47c90 | 2017-08-30 14:34:57 +0000 | [diff] [blame] | 826 | if (Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash) |
| 827 | Line->Level += PPBranchLevel + 1; |
Manuel Klimek | a71e5d8 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 828 | addUnwrappedLine(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 829 | } |
| 830 | |
Alexander Kornienko | a04e5e2 | 2013-04-09 16:15:19 +0000 | [diff] [blame] | 831 | // Here we blacklist certain tokens that are not usually the first token in an |
| 832 | // unwrapped line. This is used in attempt to distinguish macro calls without |
| 833 | // trailing semicolons from other constructs split to several lines. |
Benjamin Kramer | 8407df7 | 2015-03-09 16:47:52 +0000 | [diff] [blame] | 834 | static bool tokenCanStartNewLine(const clang::Token &Tok) { |
Alexander Kornienko | a04e5e2 | 2013-04-09 16:15:19 +0000 | [diff] [blame] | 835 | // Semicolon can be a null-statement, l_square can be a start of a macro or |
| 836 | // a C++11 attribute, but this doesn't seem to be common. |
| 837 | return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) && |
| 838 | Tok.isNot(tok::l_square) && |
| 839 | // Tokens that can only be used as binary operators and a part of |
| 840 | // overloaded operator names. |
| 841 | Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) && |
| 842 | Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) && |
| 843 | Tok.isNot(tok::less) && Tok.isNot(tok::greater) && |
| 844 | Tok.isNot(tok::slash) && Tok.isNot(tok::percent) && |
| 845 | Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) && |
| 846 | Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) && |
| 847 | Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) && |
| 848 | Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) && |
| 849 | Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) && |
| 850 | Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) && |
| 851 | Tok.isNot(tok::lesslessequal) && |
| 852 | // Colon is used in labels, base class lists, initializer lists, |
| 853 | // range-based for loops, ternary operator, but should never be the |
| 854 | // first token in an unwrapped line. |
Daniel Jasper | 5ebb2f3 | 2014-05-21 13:08:17 +0000 | [diff] [blame] | 855 | Tok.isNot(tok::colon) && |
| 856 | // 'noexcept' is a trailing annotation. |
| 857 | Tok.isNot(tok::kw_noexcept); |
Alexander Kornienko | a04e5e2 | 2013-04-09 16:15:19 +0000 | [diff] [blame] | 858 | } |
| 859 | |
Martin Probst | 533965c | 2016-04-19 18:19:06 +0000 | [diff] [blame] | 860 | static bool mustBeJSIdent(const AdditionalKeywords &Keywords, |
| 861 | const FormatToken *FormatTok) { |
Daniel Jasper | 1dcbbcfc | 2016-03-14 19:21:36 +0000 | [diff] [blame] | 862 | // FIXME: This returns true for C/C++ keywords like 'struct'. |
| 863 | return FormatTok->is(tok::identifier) && |
| 864 | (FormatTok->Tok.getIdentifierInfo() == nullptr || |
Martin Probst | 3dbbefa | 2016-11-10 16:21:02 +0000 | [diff] [blame] | 865 | !FormatTok->isOneOf( |
| 866 | Keywords.kw_in, Keywords.kw_of, Keywords.kw_as, Keywords.kw_async, |
| 867 | Keywords.kw_await, Keywords.kw_yield, Keywords.kw_finally, |
| 868 | Keywords.kw_function, Keywords.kw_import, Keywords.kw_is, |
| 869 | Keywords.kw_let, Keywords.kw_var, tok::kw_const, |
| 870 | Keywords.kw_abstract, Keywords.kw_extends, Keywords.kw_implements, |
Manuel Klimek | 89628f6 | 2017-09-20 09:51:03 +0000 | [diff] [blame] | 871 | Keywords.kw_instanceof, Keywords.kw_interface, Keywords.kw_throws, |
| 872 | Keywords.kw_from)); |
Daniel Jasper | 1dcbbcfc | 2016-03-14 19:21:36 +0000 | [diff] [blame] | 873 | } |
| 874 | |
Martin Probst | 533965c | 2016-04-19 18:19:06 +0000 | [diff] [blame] | 875 | static bool mustBeJSIdentOrValue(const AdditionalKeywords &Keywords, |
| 876 | const FormatToken *FormatTok) { |
Martin Probst | b9316ff | 2016-09-18 17:21:52 +0000 | [diff] [blame] | 877 | return FormatTok->Tok.isLiteral() || |
| 878 | FormatTok->isOneOf(tok::kw_true, tok::kw_false) || |
| 879 | mustBeJSIdent(Keywords, FormatTok); |
Martin Probst | 533965c | 2016-04-19 18:19:06 +0000 | [diff] [blame] | 880 | } |
| 881 | |
Daniel Jasper | 1dcbbcfc | 2016-03-14 19:21:36 +0000 | [diff] [blame] | 882 | // isJSDeclOrStmt returns true if |FormatTok| starts a declaration or statement |
| 883 | // when encountered after a value (see mustBeJSIdentOrValue). |
| 884 | static bool isJSDeclOrStmt(const AdditionalKeywords &Keywords, |
| 885 | const FormatToken *FormatTok) { |
| 886 | return FormatTok->isOneOf( |
Martin Probst | 5f8445b | 2016-04-24 22:05:09 +0000 | [diff] [blame] | 887 | tok::kw_return, Keywords.kw_yield, |
Daniel Jasper | 1dcbbcfc | 2016-03-14 19:21:36 +0000 | [diff] [blame] | 888 | // conditionals |
| 889 | tok::kw_if, tok::kw_else, |
| 890 | // loops |
| 891 | tok::kw_for, tok::kw_while, tok::kw_do, tok::kw_continue, tok::kw_break, |
| 892 | // switch/case |
| 893 | tok::kw_switch, tok::kw_case, |
| 894 | // exceptions |
| 895 | tok::kw_throw, tok::kw_try, tok::kw_catch, Keywords.kw_finally, |
| 896 | // declaration |
| 897 | tok::kw_const, tok::kw_class, Keywords.kw_var, Keywords.kw_let, |
Martin Probst | 5f8445b | 2016-04-24 22:05:09 +0000 | [diff] [blame] | 898 | Keywords.kw_async, Keywords.kw_function, |
| 899 | // import/export |
| 900 | Keywords.kw_import, tok::kw_export); |
Daniel Jasper | 1dcbbcfc | 2016-03-14 19:21:36 +0000 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | // readTokenWithJavaScriptASI reads the next token and terminates the current |
| 904 | // line if JavaScript Automatic Semicolon Insertion must |
| 905 | // happen between the current token and the next token. |
| 906 | // |
| 907 | // This method is conservative - it cannot cover all edge cases of JavaScript, |
| 908 | // but only aims to correctly handle certain well known cases. It *must not* |
| 909 | // return true in speculative cases. |
| 910 | void UnwrappedLineParser::readTokenWithJavaScriptASI() { |
| 911 | FormatToken *Previous = FormatTok; |
| 912 | readToken(); |
| 913 | FormatToken *Next = FormatTok; |
| 914 | |
| 915 | bool IsOnSameLine = |
| 916 | CommentsBeforeNextToken.empty() |
| 917 | ? Next->NewlinesBefore == 0 |
| 918 | : CommentsBeforeNextToken.front()->NewlinesBefore == 0; |
| 919 | if (IsOnSameLine) |
| 920 | return; |
| 921 | |
| 922 | bool PreviousMustBeValue = mustBeJSIdentOrValue(Keywords, Previous); |
Martin Probst | 717f6dc | 2016-10-21 05:11:38 +0000 | [diff] [blame] | 923 | bool PreviousStartsTemplateExpr = |
| 924 | Previous->is(TT_TemplateString) && Previous->TokenText.endswith("${"); |
Martin Probst | 7e0f25b | 2017-11-25 09:19:42 +0000 | [diff] [blame] | 925 | if (PreviousMustBeValue || Previous->is(tok::r_paren)) { |
| 926 | // If the line contains an '@' sign, the previous token might be an |
| 927 | // annotation, which can precede another identifier/value. |
| 928 | bool HasAt = std::find_if(Line->Tokens.begin(), Line->Tokens.end(), |
| 929 | [](UnwrappedLineNode &LineNode) { |
| 930 | return LineNode.Tok->is(tok::at); |
| 931 | }) != Line->Tokens.end(); |
| 932 | if (HasAt) |
Martin Probst | bbffeac | 2016-04-11 07:35:57 +0000 | [diff] [blame] | 933 | return; |
| 934 | } |
Daniel Jasper | 1dcbbcfc | 2016-03-14 19:21:36 +0000 | [diff] [blame] | 935 | if (Next->is(tok::exclaim) && PreviousMustBeValue) |
Martin Probst | d40bca4 | 2017-01-09 08:56:36 +0000 | [diff] [blame] | 936 | return addUnwrappedLine(); |
Daniel Jasper | 1dcbbcfc | 2016-03-14 19:21:36 +0000 | [diff] [blame] | 937 | bool NextMustBeValue = mustBeJSIdentOrValue(Keywords, Next); |
Martin Probst | 717f6dc | 2016-10-21 05:11:38 +0000 | [diff] [blame] | 938 | bool NextEndsTemplateExpr = |
| 939 | Next->is(TT_TemplateString) && Next->TokenText.startswith("}"); |
| 940 | if (NextMustBeValue && !NextEndsTemplateExpr && !PreviousStartsTemplateExpr && |
| 941 | (PreviousMustBeValue || |
| 942 | Previous->isOneOf(tok::r_square, tok::r_paren, tok::plusplus, |
| 943 | tok::minusminus))) |
Martin Probst | d40bca4 | 2017-01-09 08:56:36 +0000 | [diff] [blame] | 944 | return addUnwrappedLine(); |
Martin Probst | 0a19d43 | 2017-08-09 15:19:16 +0000 | [diff] [blame] | 945 | if ((PreviousMustBeValue || Previous->is(tok::r_paren)) && |
| 946 | isJSDeclOrStmt(Keywords, Next)) |
Martin Probst | d40bca4 | 2017-01-09 08:56:36 +0000 | [diff] [blame] | 947 | return addUnwrappedLine(); |
Daniel Jasper | 1dcbbcfc | 2016-03-14 19:21:36 +0000 | [diff] [blame] | 948 | } |
| 949 | |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 950 | void UnwrappedLineParser::parseStructuralElement() { |
Daniel Jasper | 498f558 | 2015-12-25 08:53:31 +0000 | [diff] [blame] | 951 | assert(!FormatTok->is(tok::l_brace)); |
| 952 | if (Style.Language == FormatStyle::LK_TableGen && |
| 953 | FormatTok->is(tok::pp_include)) { |
| 954 | nextToken(); |
| 955 | if (FormatTok->is(tok::string_literal)) |
| 956 | nextToken(); |
| 957 | addUnwrappedLine(); |
| 958 | return; |
| 959 | } |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 960 | switch (FormatTok->Tok.getKind()) { |
Daniel Jasper | 8f46365 | 2014-08-26 23:15:12 +0000 | [diff] [blame] | 961 | case tok::kw_asm: |
Daniel Jasper | 8f46365 | 2014-08-26 23:15:12 +0000 | [diff] [blame] | 962 | nextToken(); |
| 963 | if (FormatTok->is(tok::l_brace)) { |
Daniel Jasper | c636607 | 2015-05-10 08:42:04 +0000 | [diff] [blame] | 964 | FormatTok->Type = TT_InlineASMBrace; |
Daniel Jasper | 2337f28 | 2015-01-12 10:14:56 +0000 | [diff] [blame] | 965 | nextToken(); |
Daniel Jasper | 4429f14 | 2014-08-27 17:16:46 +0000 | [diff] [blame] | 966 | while (FormatTok && FormatTok->isNot(tok::eof)) { |
Daniel Jasper | 8f46365 | 2014-08-26 23:15:12 +0000 | [diff] [blame] | 967 | if (FormatTok->is(tok::r_brace)) { |
Daniel Jasper | c636607 | 2015-05-10 08:42:04 +0000 | [diff] [blame] | 968 | FormatTok->Type = TT_InlineASMBrace; |
Daniel Jasper | 8f46365 | 2014-08-26 23:15:12 +0000 | [diff] [blame] | 969 | nextToken(); |
Daniel Jasper | 790d4f9 | 2015-05-11 11:59:46 +0000 | [diff] [blame] | 970 | addUnwrappedLine(); |
Daniel Jasper | 8f46365 | 2014-08-26 23:15:12 +0000 | [diff] [blame] | 971 | break; |
| 972 | } |
Daniel Jasper | 2337f28 | 2015-01-12 10:14:56 +0000 | [diff] [blame] | 973 | FormatTok->Finalized = true; |
Daniel Jasper | 8f46365 | 2014-08-26 23:15:12 +0000 | [diff] [blame] | 974 | nextToken(); |
| 975 | } |
| 976 | } |
| 977 | break; |
Alexander Kornienko | 578fdd8 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 978 | case tok::kw_namespace: |
| 979 | parseNamespace(); |
| 980 | return; |
Dmitri Gribenko | 58d64e2 | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 981 | case tok::kw_inline: |
| 982 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 983 | if (FormatTok->Tok.is(tok::kw_namespace)) { |
Dmitri Gribenko | 58d64e2 | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 984 | parseNamespace(); |
| 985 | return; |
| 986 | } |
| 987 | break; |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 988 | case tok::kw_public: |
| 989 | case tok::kw_protected: |
| 990 | case tok::kw_private: |
Daniel Jasper | 8370908 | 2015-02-18 17:14:05 +0000 | [diff] [blame] | 991 | if (Style.Language == FormatStyle::LK_Java || |
| 992 | Style.Language == FormatStyle::LK_JavaScript) |
Daniel Jasper | c58c70e | 2014-09-15 11:21:46 +0000 | [diff] [blame] | 993 | nextToken(); |
| 994 | else |
| 995 | parseAccessSpecifier(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 996 | return; |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 997 | case tok::kw_if: |
| 998 | parseIfThenElse(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 999 | return; |
Alexander Kornienko | 37d6c94 | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 1000 | case tok::kw_for: |
| 1001 | case tok::kw_while: |
| 1002 | parseForOrWhileLoop(); |
| 1003 | return; |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 1004 | case tok::kw_do: |
| 1005 | parseDoWhile(); |
| 1006 | return; |
| 1007 | case tok::kw_switch: |
Martin Probst | f785fd9 | 2017-08-04 17:07:15 +0000 | [diff] [blame] | 1008 | if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration) |
| 1009 | // 'switch: string' field declaration. |
| 1010 | break; |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 1011 | parseSwitch(); |
| 1012 | return; |
| 1013 | case tok::kw_default: |
Martin Probst | f785fd9 | 2017-08-04 17:07:15 +0000 | [diff] [blame] | 1014 | if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration) |
| 1015 | // 'default: string' field declaration. |
| 1016 | break; |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 1017 | nextToken(); |
Nico Weber | c29f83b | 2018-01-23 16:30:56 +0000 | [diff] [blame] | 1018 | if (FormatTok->is(tok::colon)) { |
| 1019 | parseLabel(); |
| 1020 | return; |
| 1021 | } |
| 1022 | // e.g. "default void f() {}" in a Java interface. |
| 1023 | break; |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 1024 | case tok::kw_case: |
Martin Probst | f785fd9 | 2017-08-04 17:07:15 +0000 | [diff] [blame] | 1025 | if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration) |
| 1026 | // 'case: string' field declaration. |
| 1027 | break; |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 1028 | parseCaseLabel(); |
| 1029 | return; |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 1030 | case tok::kw_try: |
Nico Weber | fac2371 | 2015-02-04 15:26:27 +0000 | [diff] [blame] | 1031 | case tok::kw___try: |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 1032 | parseTryCatch(); |
| 1033 | return; |
Manuel Klimek | ae610d1 | 2013-01-21 14:32:05 +0000 | [diff] [blame] | 1034 | case tok::kw_extern: |
| 1035 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1036 | if (FormatTok->Tok.is(tok::string_literal)) { |
Manuel Klimek | ae610d1 | 2013-01-21 14:32:05 +0000 | [diff] [blame] | 1037 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1038 | if (FormatTok->Tok.is(tok::l_brace)) { |
Krasimir Georgiev | d6ce937 | 2017-09-15 11:23:50 +0000 | [diff] [blame] | 1039 | if (Style.BraceWrapping.AfterExternBlock) { |
| 1040 | addUnwrappedLine(); |
| 1041 | parseBlock(/*MustBeDeclaration=*/true); |
| 1042 | } else { |
| 1043 | parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false); |
| 1044 | } |
Manuel Klimek | ae610d1 | 2013-01-21 14:32:05 +0000 | [diff] [blame] | 1045 | addUnwrappedLine(); |
| 1046 | return; |
| 1047 | } |
| 1048 | } |
Daniel Jasper | e1e4319 | 2014-04-01 12:55:11 +0000 | [diff] [blame] | 1049 | break; |
Daniel Jasper | fca735c | 2015-02-19 16:14:18 +0000 | [diff] [blame] | 1050 | case tok::kw_export: |
| 1051 | if (Style.Language == FormatStyle::LK_JavaScript) { |
| 1052 | parseJavaScriptEs6ImportExport(); |
| 1053 | return; |
| 1054 | } |
| 1055 | break; |
Daniel Jasper | e1e4319 | 2014-04-01 12:55:11 +0000 | [diff] [blame] | 1056 | case tok::identifier: |
Daniel Jasper | 66cb8c5 | 2015-05-04 09:22:29 +0000 | [diff] [blame] | 1057 | if (FormatTok->is(TT_ForEachMacro)) { |
Daniel Jasper | e1e4319 | 2014-04-01 12:55:11 +0000 | [diff] [blame] | 1058 | parseForOrWhileLoop(); |
| 1059 | return; |
| 1060 | } |
Birunthan Mohanathas | b001a0b | 2015-07-03 17:25:16 +0000 | [diff] [blame] | 1061 | if (FormatTok->is(TT_MacroBlockBegin)) { |
| 1062 | parseBlock(/*MustBeDeclaration=*/false, /*AddLevel=*/true, |
| 1063 | /*MunchSemi=*/false); |
| 1064 | return; |
| 1065 | } |
Daniel Jasper | 3d5a7d6 | 2016-06-20 18:20:38 +0000 | [diff] [blame] | 1066 | if (FormatTok->is(Keywords.kw_import)) { |
| 1067 | if (Style.Language == FormatStyle::LK_JavaScript) { |
| 1068 | parseJavaScriptEs6ImportExport(); |
| 1069 | return; |
| 1070 | } |
| 1071 | if (Style.Language == FormatStyle::LK_Proto) { |
| 1072 | nextToken(); |
Daniel Jasper | 8b61d14 | 2016-06-20 20:39:53 +0000 | [diff] [blame] | 1073 | if (FormatTok->is(tok::kw_public)) |
| 1074 | nextToken(); |
Daniel Jasper | 3d5a7d6 | 2016-06-20 18:20:38 +0000 | [diff] [blame] | 1075 | if (!FormatTok->is(tok::string_literal)) |
| 1076 | return; |
| 1077 | nextToken(); |
| 1078 | if (FormatTok->is(tok::semi)) |
| 1079 | nextToken(); |
| 1080 | addUnwrappedLine(); |
| 1081 | return; |
| 1082 | } |
Daniel Jasper | 354aa51 | 2015-02-19 16:07:32 +0000 | [diff] [blame] | 1083 | } |
Daniel Jasper | 1dbc210 | 2017-03-31 13:30:24 +0000 | [diff] [blame] | 1084 | if (Style.isCpp() && |
Daniel Jasper | 72b3357 | 2017-03-31 12:04:37 +0000 | [diff] [blame] | 1085 | FormatTok->isOneOf(Keywords.kw_signals, Keywords.kw_qsignals, |
Daniel Jasper | a00de63 | 2015-12-01 12:05:04 +0000 | [diff] [blame] | 1086 | Keywords.kw_slots, Keywords.kw_qslots)) { |
Daniel Jasper | de0d1f3 | 2015-04-24 07:50:34 +0000 | [diff] [blame] | 1087 | nextToken(); |
| 1088 | if (FormatTok->is(tok::colon)) { |
| 1089 | nextToken(); |
| 1090 | addUnwrappedLine(); |
Daniel Jasper | 3134383 | 2016-07-27 10:13:24 +0000 | [diff] [blame] | 1091 | return; |
Daniel Jasper | de0d1f3 | 2015-04-24 07:50:34 +0000 | [diff] [blame] | 1092 | } |
Daniel Jasper | 5339540 | 2015-04-07 15:04:40 +0000 | [diff] [blame] | 1093 | } |
Manuel Klimek | ae610d1 | 2013-01-21 14:32:05 +0000 | [diff] [blame] | 1094 | // In all other cases, parse the declaration. |
| 1095 | break; |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 1096 | default: |
| 1097 | break; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1098 | } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1099 | do { |
Manuel Klimek | e411aa8 | 2017-09-20 09:29:37 +0000 | [diff] [blame] | 1100 | const FormatToken *Previous = FormatTok->Previous; |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1101 | switch (FormatTok->Tok.getKind()) { |
Nico Weber | 372d8dc | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 1102 | case tok::at: |
| 1103 | nextToken(); |
Krasimir Georgiev | 26b144c | 2017-07-03 15:05:14 +0000 | [diff] [blame] | 1104 | if (FormatTok->Tok.is(tok::l_brace)) { |
| 1105 | nextToken(); |
Nico Weber | 372d8dc | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 1106 | parseBracedList(); |
Nico Weber | c068ff7 | 2018-01-23 17:10:25 +0000 | [diff] [blame] | 1107 | break; |
| 1108 | } |
| 1109 | switch (FormatTok->Tok.getObjCKeywordID()) { |
| 1110 | case tok::objc_public: |
| 1111 | case tok::objc_protected: |
| 1112 | case tok::objc_package: |
| 1113 | case tok::objc_private: |
| 1114 | return parseAccessSpecifier(); |
| 1115 | case tok::objc_interface: |
| 1116 | case tok::objc_implementation: |
| 1117 | return parseObjCInterfaceOrImplementation(); |
| 1118 | case tok::objc_protocol: |
| 1119 | if (parseObjCProtocol()) |
| 1120 | return; |
| 1121 | break; |
| 1122 | case tok::objc_end: |
| 1123 | return; // Handled by the caller. |
| 1124 | case tok::objc_optional: |
| 1125 | case tok::objc_required: |
| 1126 | nextToken(); |
| 1127 | addUnwrappedLine(); |
| 1128 | return; |
| 1129 | case tok::objc_autoreleasepool: |
| 1130 | nextToken(); |
| 1131 | if (FormatTok->Tok.is(tok::l_brace)) { |
Francois Ferrand | a2484b2 | 2018-02-27 13:48:27 +0000 | [diff] [blame] | 1132 | if (Style.BraceWrapping.AfterControlStatement) |
Nico Weber | c068ff7 | 2018-01-23 17:10:25 +0000 | [diff] [blame] | 1133 | addUnwrappedLine(); |
| 1134 | parseBlock(/*MustBeDeclaration=*/false); |
| 1135 | } |
| 1136 | addUnwrappedLine(); |
| 1137 | return; |
Francois Ferrand | ba91c3d | 2018-02-27 13:48:21 +0000 | [diff] [blame] | 1138 | case tok::objc_synchronized: |
| 1139 | nextToken(); |
| 1140 | if (FormatTok->Tok.is(tok::l_paren)) |
| 1141 | // Skip synchronization object |
| 1142 | parseParens(); |
| 1143 | if (FormatTok->Tok.is(tok::l_brace)) { |
Francois Ferrand | a2484b2 | 2018-02-27 13:48:27 +0000 | [diff] [blame] | 1144 | if (Style.BraceWrapping.AfterControlStatement) |
Francois Ferrand | ba91c3d | 2018-02-27 13:48:21 +0000 | [diff] [blame] | 1145 | addUnwrappedLine(); |
| 1146 | parseBlock(/*MustBeDeclaration=*/false); |
| 1147 | } |
| 1148 | addUnwrappedLine(); |
| 1149 | return; |
Nico Weber | c068ff7 | 2018-01-23 17:10:25 +0000 | [diff] [blame] | 1150 | case tok::objc_try: |
| 1151 | // This branch isn't strictly necessary (the kw_try case below would |
| 1152 | // do this too after the tok::at is parsed above). But be explicit. |
| 1153 | parseTryCatch(); |
| 1154 | return; |
| 1155 | default: |
| 1156 | break; |
Krasimir Georgiev | 26b144c | 2017-07-03 15:05:14 +0000 | [diff] [blame] | 1157 | } |
Nico Weber | 372d8dc | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 1158 | break; |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 1159 | case tok::kw_enum: |
Daniel Jasper | a7900ad | 2016-05-08 18:12:22 +0000 | [diff] [blame] | 1160 | // Ignore if this is part of "template <enum ...". |
| 1161 | if (Previous && Previous->is(tok::less)) { |
| 1162 | nextToken(); |
| 1163 | break; |
| 1164 | } |
| 1165 | |
Daniel Jasper | 90cf380 | 2015-06-17 09:44:02 +0000 | [diff] [blame] | 1166 | // parseEnum falls through and does not yet add an unwrapped line as an |
| 1167 | // enum definition can start a structural element. |
Daniel Jasper | 6f5a193 | 2015-12-29 08:54:23 +0000 | [diff] [blame] | 1168 | if (!parseEnum()) |
| 1169 | break; |
Daniel Jasper | c6dd273 | 2015-07-16 14:25:43 +0000 | [diff] [blame] | 1170 | // This only applies for C++. |
Daniel Jasper | 1dbc210 | 2017-03-31 13:30:24 +0000 | [diff] [blame] | 1171 | if (!Style.isCpp()) { |
Daniel Jasper | 90cf380 | 2015-06-17 09:44:02 +0000 | [diff] [blame] | 1172 | addUnwrappedLine(); |
| 1173 | return; |
| 1174 | } |
Manuel Klimek | 2cec019 | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 1175 | break; |
Daniel Jasper | a88f80a | 2014-01-30 14:38:37 +0000 | [diff] [blame] | 1176 | case tok::kw_typedef: |
| 1177 | nextToken(); |
Daniel Jasper | 31f6c54 | 2014-12-05 10:42:21 +0000 | [diff] [blame] | 1178 | if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS, |
| 1179 | Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS)) |
Daniel Jasper | a88f80a | 2014-01-30 14:38:37 +0000 | [diff] [blame] | 1180 | parseEnum(); |
| 1181 | break; |
Alexander Kornienko | 1231e06 | 2013-01-16 11:43:46 +0000 | [diff] [blame] | 1182 | case tok::kw_struct: |
| 1183 | case tok::kw_union: |
Manuel Klimek | 28cacc7 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 1184 | case tok::kw_class: |
Daniel Jasper | 910807d | 2015-06-12 04:52:02 +0000 | [diff] [blame] | 1185 | // parseRecord falls through and does not yet add an unwrapped line as a |
| 1186 | // record declaration or definition can start a structural element. |
Manuel Klimek | e01bab5 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 1187 | parseRecord(); |
Daniel Jasper | 910807d | 2015-06-12 04:52:02 +0000 | [diff] [blame] | 1188 | // This does not apply for Java and JavaScript. |
| 1189 | if (Style.Language == FormatStyle::LK_Java || |
| 1190 | Style.Language == FormatStyle::LK_JavaScript) { |
Daniel Jasper | d5ec65b | 2016-01-08 07:06:07 +0000 | [diff] [blame] | 1191 | if (FormatTok->is(tok::semi)) |
| 1192 | nextToken(); |
Daniel Jasper | 910807d | 2015-06-12 04:52:02 +0000 | [diff] [blame] | 1193 | addUnwrappedLine(); |
| 1194 | return; |
| 1195 | } |
Manuel Klimek | e01bab5 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 1196 | break; |
Daniel Jasper | e5d7486 | 2014-11-26 08:17:08 +0000 | [diff] [blame] | 1197 | case tok::period: |
| 1198 | nextToken(); |
| 1199 | // In Java, classes have an implicit static member "class". |
| 1200 | if (Style.Language == FormatStyle::LK_Java && FormatTok && |
| 1201 | FormatTok->is(tok::kw_class)) |
| 1202 | nextToken(); |
Daniel Jasper | ba52fcb | 2015-09-28 14:29:45 +0000 | [diff] [blame] | 1203 | if (Style.Language == FormatStyle::LK_JavaScript && FormatTok && |
| 1204 | FormatTok->Tok.getIdentifierInfo()) |
| 1205 | // JavaScript only has pseudo keywords, all keywords are allowed to |
| 1206 | // appear in "IdentifierName" positions. See http://es5.github.io/#x7.6 |
| 1207 | nextToken(); |
Daniel Jasper | e5d7486 | 2014-11-26 08:17:08 +0000 | [diff] [blame] | 1208 | break; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1209 | case tok::semi: |
| 1210 | nextToken(); |
| 1211 | addUnwrappedLine(); |
| 1212 | return; |
Alexander Kornienko | 1231e06 | 2013-01-16 11:43:46 +0000 | [diff] [blame] | 1213 | case tok::r_brace: |
| 1214 | addUnwrappedLine(); |
| 1215 | return; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1216 | case tok::l_paren: |
| 1217 | parseParens(); |
| 1218 | break; |
Daniel Jasper | 5af04a4 | 2015-10-07 03:43:10 +0000 | [diff] [blame] | 1219 | case tok::kw_operator: |
| 1220 | nextToken(); |
| 1221 | if (FormatTok->isBinaryOperator()) |
| 1222 | nextToken(); |
| 1223 | break; |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 1224 | case tok::caret: |
| 1225 | nextToken(); |
Daniel Jasper | 395193c | 2014-03-28 07:48:59 +0000 | [diff] [blame] | 1226 | if (FormatTok->Tok.isAnyIdentifier() || |
| 1227 | FormatTok->isSimpleTypeSpecifier()) |
| 1228 | nextToken(); |
| 1229 | if (FormatTok->is(tok::l_paren)) |
| 1230 | parseParens(); |
| 1231 | if (FormatTok->is(tok::l_brace)) |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 1232 | parseChildBlock(); |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 1233 | break; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1234 | case tok::l_brace: |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 1235 | if (!tryToParseBracedList()) { |
| 1236 | // A block outside of parentheses must be the last part of a |
| 1237 | // structural element. |
| 1238 | // FIXME: Figure out cases where this is not true, and add projections |
| 1239 | // for them (the one we know is missing are lambdas). |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 1240 | if (Style.BraceWrapping.AfterFunction) |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 1241 | addUnwrappedLine(); |
Alexander Kornienko | 3cfa973 | 2013-11-20 16:33:05 +0000 | [diff] [blame] | 1242 | FormatTok->Type = TT_FunctionLBrace; |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 1243 | parseBlock(/*MustBeDeclaration=*/false); |
Manuel Klimek | a8eb914 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 1244 | addUnwrappedLine(); |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 1245 | return; |
| 1246 | } |
| 1247 | // Otherwise this was a braced init list, and the structural |
| 1248 | // element continues. |
| 1249 | break; |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 1250 | case tok::kw_try: |
| 1251 | // We arrive here when parsing function-try blocks. |
| 1252 | parseTryCatch(); |
| 1253 | return; |
Daniel Jasper | 40e1921 | 2013-05-29 13:16:10 +0000 | [diff] [blame] | 1254 | case tok::identifier: { |
Birunthan Mohanathas | b001a0b | 2015-07-03 17:25:16 +0000 | [diff] [blame] | 1255 | if (FormatTok->is(TT_MacroBlockEnd)) { |
| 1256 | addUnwrappedLine(); |
| 1257 | return; |
| 1258 | } |
| 1259 | |
Martin Probst | 973ff79 | 2017-04-27 13:07:24 +0000 | [diff] [blame] | 1260 | // Function declarations (as opposed to function expressions) are parsed |
| 1261 | // on their own unwrapped line by continuing this loop. Function |
| 1262 | // expressions (functions that are not on their own line) must not create |
| 1263 | // a new unwrapped line, so they are special cased below. |
| 1264 | size_t TokenCount = Line->Tokens.size(); |
Daniel Jasper | 9326f91 | 2015-05-05 08:40:32 +0000 | [diff] [blame] | 1265 | if (Style.Language == FormatStyle::LK_JavaScript && |
Martin Probst | 973ff79 | 2017-04-27 13:07:24 +0000 | [diff] [blame] | 1266 | FormatTok->is(Keywords.kw_function) && |
| 1267 | (TokenCount > 1 || (TokenCount == 1 && !Line->Tokens.front().Tok->is( |
| 1268 | Keywords.kw_async)))) { |
Daniel Jasper | 069e5f4 | 2014-05-20 11:14:57 +0000 | [diff] [blame] | 1269 | tryToParseJSFunction(); |
| 1270 | break; |
| 1271 | } |
Daniel Jasper | 9326f91 | 2015-05-05 08:40:32 +0000 | [diff] [blame] | 1272 | if ((Style.Language == FormatStyle::LK_JavaScript || |
| 1273 | Style.Language == FormatStyle::LK_Java) && |
| 1274 | FormatTok->is(Keywords.kw_interface)) { |
Martin Probst | 1e8261e | 2016-04-19 18:18:59 +0000 | [diff] [blame] | 1275 | if (Style.Language == FormatStyle::LK_JavaScript) { |
| 1276 | // In JavaScript/TypeScript, "interface" can be used as a standalone |
| 1277 | // identifier, e.g. in `var interface = 1;`. If "interface" is |
| 1278 | // followed by another identifier, it is very like to be an actual |
| 1279 | // interface declaration. |
| 1280 | unsigned StoredPosition = Tokens->getPosition(); |
| 1281 | FormatToken *Next = Tokens->getNextToken(); |
| 1282 | FormatTok = Tokens->setPosition(StoredPosition); |
Martin Probst | 533965c | 2016-04-19 18:19:06 +0000 | [diff] [blame] | 1283 | if (Next && !mustBeJSIdent(Keywords, Next)) { |
Martin Probst | 1e8261e | 2016-04-19 18:18:59 +0000 | [diff] [blame] | 1284 | nextToken(); |
| 1285 | break; |
| 1286 | } |
| 1287 | } |
Daniel Jasper | 9326f91 | 2015-05-05 08:40:32 +0000 | [diff] [blame] | 1288 | parseRecord(); |
Daniel Jasper | 259188b | 2015-06-12 04:56:34 +0000 | [diff] [blame] | 1289 | addUnwrappedLine(); |
Daniel Jasper | 5c235c0 | 2015-07-06 14:26:04 +0000 | [diff] [blame] | 1290 | return; |
Daniel Jasper | 9326f91 | 2015-05-05 08:40:32 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
Daniel Jasper | 1dcbbcfc | 2016-03-14 19:21:36 +0000 | [diff] [blame] | 1293 | // See if the following token should start a new unwrapped line. |
Daniel Jasper | 9326f91 | 2015-05-05 08:40:32 +0000 | [diff] [blame] | 1294 | StringRef Text = FormatTok->TokenText; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1295 | nextToken(); |
Daniel Jasper | 8370908 | 2015-02-18 17:14:05 +0000 | [diff] [blame] | 1296 | if (Line->Tokens.size() == 1 && |
| 1297 | // JS doesn't have macros, and within classes colons indicate fields, |
| 1298 | // not labels. |
Daniel Jasper | 676e516 | 2015-04-07 14:36:33 +0000 | [diff] [blame] | 1299 | Style.Language != FormatStyle::LK_JavaScript) { |
| 1300 | if (FormatTok->Tok.is(tok::colon) && !Line->MustBeDeclaration) { |
Daniel Jasper | 4060947 | 2016-04-06 15:02:46 +0000 | [diff] [blame] | 1301 | Line->Tokens.begin()->Tok->MustBreakBefore = true; |
Alexander Kornienko | de64427 | 2013-04-08 22:16:06 +0000 | [diff] [blame] | 1302 | parseLabel(); |
| 1303 | return; |
| 1304 | } |
Daniel Jasper | 680b09b | 2014-11-05 10:48:04 +0000 | [diff] [blame] | 1305 | // Recognize function-like macro usages without trailing semicolon as |
Daniel Jasper | 8370908 | 2015-02-18 17:14:05 +0000 | [diff] [blame] | 1306 | // well as free-standing macros like Q_OBJECT. |
Daniel Jasper | 680b09b | 2014-11-05 10:48:04 +0000 | [diff] [blame] | 1307 | bool FunctionLike = FormatTok->is(tok::l_paren); |
| 1308 | if (FunctionLike) |
Alexander Kornienko | de64427 | 2013-04-08 22:16:06 +0000 | [diff] [blame] | 1309 | parseParens(); |
Daniel Jasper | e60cba1 | 2015-05-13 11:35:53 +0000 | [diff] [blame] | 1310 | |
| 1311 | bool FollowedByNewline = |
| 1312 | CommentsBeforeNextToken.empty() |
| 1313 | ? FormatTok->NewlinesBefore > 0 |
| 1314 | : CommentsBeforeNextToken.front()->NewlinesBefore > 0; |
| 1315 | |
Daniel Jasper | e6fcf7d | 2015-06-17 13:08:06 +0000 | [diff] [blame] | 1316 | if (FollowedByNewline && (Text.size() >= 5 || FunctionLike) && |
Daniel Jasper | 680b09b | 2014-11-05 10:48:04 +0000 | [diff] [blame] | 1317 | tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) { |
Daniel Jasper | 40e1921 | 2013-05-29 13:16:10 +0000 | [diff] [blame] | 1318 | addUnwrappedLine(); |
Daniel Jasper | 41a0f78 | 2013-05-29 14:09:17 +0000 | [diff] [blame] | 1319 | return; |
Alexander Kornienko | de64427 | 2013-04-08 22:16:06 +0000 | [diff] [blame] | 1320 | } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1321 | } |
| 1322 | break; |
Daniel Jasper | 40e1921 | 2013-05-29 13:16:10 +0000 | [diff] [blame] | 1323 | } |
Daniel Jasper | e25509f | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 1324 | case tok::equal: |
Manuel Klimek | 79e0608 | 2015-05-21 12:23:34 +0000 | [diff] [blame] | 1325 | // Fat arrows (=>) have tok::TokenKind tok::equal but TokenType |
| 1326 | // TT_JsFatArrow. The always start an expression or a child block if |
| 1327 | // followed by a curly. |
| 1328 | if (FormatTok->is(TT_JsFatArrow)) { |
| 1329 | nextToken(); |
Daniel Jasper | be520bd | 2015-05-31 08:51:54 +0000 | [diff] [blame] | 1330 | if (FormatTok->is(tok::l_brace)) |
Manuel Klimek | 79e0608 | 2015-05-21 12:23:34 +0000 | [diff] [blame] | 1331 | parseChildBlock(); |
Manuel Klimek | 79e0608 | 2015-05-21 12:23:34 +0000 | [diff] [blame] | 1332 | break; |
| 1333 | } |
| 1334 | |
Daniel Jasper | e25509f | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 1335 | nextToken(); |
Krasimir Georgiev | 26b144c | 2017-07-03 15:05:14 +0000 | [diff] [blame] | 1336 | if (FormatTok->Tok.is(tok::l_brace)) { |
| 1337 | nextToken(); |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 1338 | parseBracedList(); |
Krasimir Georgiev | 26b144c | 2017-07-03 15:05:14 +0000 | [diff] [blame] | 1339 | } else if (Style.Language == FormatStyle::LK_Proto && |
Manuel Klimek | 89628f6 | 2017-09-20 09:51:03 +0000 | [diff] [blame] | 1340 | FormatTok->Tok.is(tok::less)) { |
Krasimir Georgiev | 26b144c | 2017-07-03 15:05:14 +0000 | [diff] [blame] | 1341 | nextToken(); |
Krasimir Georgiev | 0b41fcb | 2017-06-27 13:58:41 +0000 | [diff] [blame] | 1342 | parseBracedList(/*ContinueOnSemicolons=*/false, |
| 1343 | /*ClosingBraceKind=*/tok::greater); |
Krasimir Georgiev | 26b144c | 2017-07-03 15:05:14 +0000 | [diff] [blame] | 1344 | } |
Daniel Jasper | e25509f | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 1345 | break; |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 1346 | case tok::l_square: |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 1347 | parseSquare(); |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 1348 | break; |
Daniel Jasper | 6acf513 | 2015-03-12 14:44:29 +0000 | [diff] [blame] | 1349 | case tok::kw_new: |
| 1350 | parseNew(); |
| 1351 | break; |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 1352 | default: |
| 1353 | nextToken(); |
| 1354 | break; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1355 | } |
| 1356 | } while (!eof()); |
| 1357 | } |
| 1358 | |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 1359 | bool UnwrappedLineParser::tryToParseLambda() { |
Daniel Jasper | 1dbc210 | 2017-03-31 13:30:24 +0000 | [diff] [blame] | 1360 | if (!Style.isCpp()) { |
Daniel Jasper | 1feab0f | 2015-06-02 15:31:37 +0000 | [diff] [blame] | 1361 | nextToken(); |
| 1362 | return false; |
| 1363 | } |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1364 | assert(FormatTok->is(tok::l_square)); |
| 1365 | FormatToken &LSquare = *FormatTok; |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 1366 | if (!tryToParseLambdaIntroducer()) |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 1367 | return false; |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 1368 | |
Alexander Kornienko | c2ee9cf | 2014-03-13 13:59:48 +0000 | [diff] [blame] | 1369 | while (FormatTok->isNot(tok::l_brace)) { |
Daniel Jasper | cb51cf4 | 2014-01-16 09:11:55 +0000 | [diff] [blame] | 1370 | if (FormatTok->isSimpleTypeSpecifier()) { |
| 1371 | nextToken(); |
| 1372 | continue; |
| 1373 | } |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 1374 | switch (FormatTok->Tok.getKind()) { |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 1375 | case tok::l_brace: |
| 1376 | break; |
| 1377 | case tok::l_paren: |
| 1378 | parseParens(); |
| 1379 | break; |
Daniel Jasper | bcb55ee | 2014-11-21 14:08:38 +0000 | [diff] [blame] | 1380 | case tok::amp: |
| 1381 | case tok::star: |
| 1382 | case tok::kw_const: |
Daniel Jasper | 3431b75 | 2014-12-08 13:22:37 +0000 | [diff] [blame] | 1383 | case tok::comma: |
Daniel Jasper | cb51cf4 | 2014-01-16 09:11:55 +0000 | [diff] [blame] | 1384 | case tok::less: |
| 1385 | case tok::greater: |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 1386 | case tok::identifier: |
Daniel Jasper | 5eaa009 | 2015-08-13 13:37:08 +0000 | [diff] [blame] | 1387 | case tok::numeric_constant: |
Daniel Jasper | 1067ab0 | 2014-02-11 10:16:55 +0000 | [diff] [blame] | 1388 | case tok::coloncolon: |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 1389 | case tok::kw_mutable: |
Daniel Jasper | 81a2078 | 2014-03-10 10:02:02 +0000 | [diff] [blame] | 1390 | nextToken(); |
| 1391 | break; |
Daniel Jasper | cb51cf4 | 2014-01-16 09:11:55 +0000 | [diff] [blame] | 1392 | case tok::arrow: |
Daniel Jasper | 6f2b88a | 2015-06-05 13:18:09 +0000 | [diff] [blame] | 1393 | FormatTok->Type = TT_LambdaArrow; |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 1394 | nextToken(); |
| 1395 | break; |
| 1396 | default: |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 1397 | return true; |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 1398 | } |
| 1399 | } |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1400 | LSquare.Type = TT_LambdaLSquare; |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 1401 | parseChildBlock(); |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 1402 | return true; |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
| 1405 | bool UnwrappedLineParser::tryToParseLambdaIntroducer() { |
Manuel Klimek | 89628f6 | 2017-09-20 09:51:03 +0000 | [diff] [blame] | 1406 | const FormatToken *Previous = FormatTok->Previous; |
Manuel Klimek | 9f0a4e5 | 2017-09-19 09:59:30 +0000 | [diff] [blame] | 1407 | if (Previous && |
| 1408 | (Previous->isOneOf(tok::identifier, tok::kw_operator, tok::kw_new, |
| 1409 | tok::kw_delete) || |
Manuel Klimek | 89628f6 | 2017-09-20 09:51:03 +0000 | [diff] [blame] | 1410 | FormatTok->isCppStructuredBinding(Style) || Previous->closesScope() || |
| 1411 | Previous->isSimpleTypeSpecifier())) { |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 1412 | nextToken(); |
Manuel Klimek | 9f0a4e5 | 2017-09-19 09:59:30 +0000 | [diff] [blame] | 1413 | return false; |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 1414 | } |
Manuel Klimek | 9f0a4e5 | 2017-09-19 09:59:30 +0000 | [diff] [blame] | 1415 | nextToken(); |
| 1416 | parseSquare(/*LambdaIntroducer=*/true); |
| 1417 | return true; |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 1418 | } |
| 1419 | |
Daniel Jasper | c03e16a | 2014-05-08 09:25:39 +0000 | [diff] [blame] | 1420 | void UnwrappedLineParser::tryToParseJSFunction() { |
Martin Probst | 409697e | 2016-05-29 14:41:07 +0000 | [diff] [blame] | 1421 | assert(FormatTok->is(Keywords.kw_function) || |
| 1422 | FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)); |
Martin Probst | 5f8445b | 2016-04-24 22:05:09 +0000 | [diff] [blame] | 1423 | if (FormatTok->is(Keywords.kw_async)) |
| 1424 | nextToken(); |
| 1425 | // Consume "function". |
Daniel Jasper | c03e16a | 2014-05-08 09:25:39 +0000 | [diff] [blame] | 1426 | nextToken(); |
Daniel Jasper | 5217a8b | 2014-06-13 07:02:04 +0000 | [diff] [blame] | 1427 | |
Daniel Jasper | 71e50af | 2016-11-01 06:22:59 +0000 | [diff] [blame] | 1428 | // Consume * (generator function). Treat it like C++'s overloaded operators. |
| 1429 | if (FormatTok->is(tok::star)) { |
| 1430 | FormatTok->Type = TT_OverloadedOperator; |
Martin Probst | 5f8445b | 2016-04-24 22:05:09 +0000 | [diff] [blame] | 1431 | nextToken(); |
Daniel Jasper | 71e50af | 2016-11-01 06:22:59 +0000 | [diff] [blame] | 1432 | } |
Martin Probst | 5f8445b | 2016-04-24 22:05:09 +0000 | [diff] [blame] | 1433 | |
Daniel Jasper | 5217a8b | 2014-06-13 07:02:04 +0000 | [diff] [blame] | 1434 | // Consume function name. |
| 1435 | if (FormatTok->is(tok::identifier)) |
Daniel Jasper | fca735c | 2015-02-19 16:14:18 +0000 | [diff] [blame] | 1436 | nextToken(); |
Daniel Jasper | 5217a8b | 2014-06-13 07:02:04 +0000 | [diff] [blame] | 1437 | |
Daniel Jasper | c03e16a | 2014-05-08 09:25:39 +0000 | [diff] [blame] | 1438 | if (FormatTok->isNot(tok::l_paren)) |
| 1439 | return; |
Manuel Klimek | 79e0608 | 2015-05-21 12:23:34 +0000 | [diff] [blame] | 1440 | |
| 1441 | // Parse formal parameter list. |
Daniel Jasper | be520bd | 2015-05-31 08:51:54 +0000 | [diff] [blame] | 1442 | parseParens(); |
Manuel Klimek | 79e0608 | 2015-05-21 12:23:34 +0000 | [diff] [blame] | 1443 | |
| 1444 | if (FormatTok->is(tok::colon)) { |
| 1445 | // Parse a type definition. |
| 1446 | nextToken(); |
| 1447 | |
| 1448 | // Eat the type declaration. For braced inline object types, balance braces, |
| 1449 | // otherwise just parse until finding an l_brace for the function body. |
Daniel Jasper | be520bd | 2015-05-31 08:51:54 +0000 | [diff] [blame] | 1450 | if (FormatTok->is(tok::l_brace)) |
| 1451 | tryToParseBracedList(); |
| 1452 | else |
Martin Probst | af16c50 | 2017-01-04 13:36:43 +0000 | [diff] [blame] | 1453 | while (!FormatTok->isOneOf(tok::l_brace, tok::semi) && !eof()) |
Manuel Klimek | 79e0608 | 2015-05-21 12:23:34 +0000 | [diff] [blame] | 1454 | nextToken(); |
Manuel Klimek | 79e0608 | 2015-05-21 12:23:34 +0000 | [diff] [blame] | 1455 | } |
| 1456 | |
Martin Probst | af16c50 | 2017-01-04 13:36:43 +0000 | [diff] [blame] | 1457 | if (FormatTok->is(tok::semi)) |
| 1458 | return; |
| 1459 | |
Manuel Klimek | 79e0608 | 2015-05-21 12:23:34 +0000 | [diff] [blame] | 1460 | parseChildBlock(); |
| 1461 | } |
| 1462 | |
Daniel Jasper | 3c883d1 | 2015-05-18 14:49:19 +0000 | [diff] [blame] | 1463 | bool UnwrappedLineParser::tryToParseBracedList() { |
Daniel Jasper | b1f74a8 | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 1464 | if (FormatTok->BlockKind == BK_Unknown) |
Daniel Jasper | 3c883d1 | 2015-05-18 14:49:19 +0000 | [diff] [blame] | 1465 | calculateBraceTypes(); |
Daniel Jasper | b1f74a8 | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 1466 | assert(FormatTok->BlockKind != BK_Unknown); |
| 1467 | if (FormatTok->BlockKind == BK_Block) |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 1468 | return false; |
Krasimir Georgiev | 26b144c | 2017-07-03 15:05:14 +0000 | [diff] [blame] | 1469 | nextToken(); |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 1470 | parseBracedList(); |
| 1471 | return true; |
| 1472 | } |
| 1473 | |
Krasimir Georgiev | ff747be | 2017-06-27 13:43:07 +0000 | [diff] [blame] | 1474 | bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons, |
| 1475 | tok::TokenKind ClosingBraceKind) { |
Daniel Jasper | 015ed02 | 2013-09-13 09:20:45 +0000 | [diff] [blame] | 1476 | bool HasError = false; |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 1477 | |
Manuel Klimek | a3ff45e | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 1478 | // FIXME: Once we have an expression parser in the UnwrappedLineParser, |
| 1479 | // replace this by using parseAssigmentExpression() inside. |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 1480 | do { |
Manuel Klimek | 79e0608 | 2015-05-21 12:23:34 +0000 | [diff] [blame] | 1481 | if (Style.Language == FormatStyle::LK_JavaScript) { |
Martin Probst | 409697e | 2016-05-29 14:41:07 +0000 | [diff] [blame] | 1482 | if (FormatTok->is(Keywords.kw_function) || |
| 1483 | FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)) { |
Manuel Klimek | 79e0608 | 2015-05-21 12:23:34 +0000 | [diff] [blame] | 1484 | tryToParseJSFunction(); |
| 1485 | continue; |
Daniel Jasper | be520bd | 2015-05-31 08:51:54 +0000 | [diff] [blame] | 1486 | } |
| 1487 | if (FormatTok->is(TT_JsFatArrow)) { |
Manuel Klimek | 79e0608 | 2015-05-21 12:23:34 +0000 | [diff] [blame] | 1488 | nextToken(); |
| 1489 | // Fat arrows can be followed by simple expressions or by child blocks |
| 1490 | // in curly braces. |
Daniel Jasper | e6fcf7d | 2015-06-17 13:08:06 +0000 | [diff] [blame] | 1491 | if (FormatTok->is(tok::l_brace)) { |
Manuel Klimek | 79e0608 | 2015-05-21 12:23:34 +0000 | [diff] [blame] | 1492 | parseChildBlock(); |
| 1493 | continue; |
| 1494 | } |
| 1495 | } |
Martin Probst | 8e3eba0 | 2017-02-07 16:33:13 +0000 | [diff] [blame] | 1496 | if (FormatTok->is(tok::l_brace)) { |
| 1497 | // Could be a method inside of a braced list `{a() { return 1; }}`. |
| 1498 | if (tryToParseBracedList()) |
| 1499 | continue; |
| 1500 | parseChildBlock(); |
| 1501 | } |
Daniel Jasper | c03e16a | 2014-05-08 09:25:39 +0000 | [diff] [blame] | 1502 | } |
Krasimir Georgiev | ff747be | 2017-06-27 13:43:07 +0000 | [diff] [blame] | 1503 | if (FormatTok->Tok.getKind() == ClosingBraceKind) { |
| 1504 | nextToken(); |
| 1505 | return !HasError; |
| 1506 | } |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1507 | switch (FormatTok->Tok.getKind()) { |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 1508 | case tok::caret: |
| 1509 | nextToken(); |
| 1510 | if (FormatTok->is(tok::l_brace)) { |
| 1511 | parseChildBlock(); |
| 1512 | } |
| 1513 | break; |
| 1514 | case tok::l_square: |
| 1515 | tryToParseLambda(); |
| 1516 | break; |
Daniel Jasper | a87af7a | 2015-06-30 11:32:22 +0000 | [diff] [blame] | 1517 | case tok::l_paren: |
| 1518 | parseParens(); |
Daniel Jasper | f46dec8 | 2015-03-31 14:34:15 +0000 | [diff] [blame] | 1519 | // JavaScript can just have free standing methods and getters/setters in |
| 1520 | // object literals. Detect them by a "{" following ")". |
| 1521 | if (Style.Language == FormatStyle::LK_JavaScript) { |
Daniel Jasper | f46dec8 | 2015-03-31 14:34:15 +0000 | [diff] [blame] | 1522 | if (FormatTok->is(tok::l_brace)) |
| 1523 | parseChildBlock(); |
| 1524 | break; |
| 1525 | } |
Daniel Jasper | f46dec8 | 2015-03-31 14:34:15 +0000 | [diff] [blame] | 1526 | break; |
Martin Probst | 8e3eba0 | 2017-02-07 16:33:13 +0000 | [diff] [blame] | 1527 | case tok::l_brace: |
| 1528 | // Assume there are no blocks inside a braced init list apart |
| 1529 | // from the ones we explicitly parse out (like lambdas). |
| 1530 | FormatTok->BlockKind = BK_BracedInit; |
Krasimir Georgiev | 26b144c | 2017-07-03 15:05:14 +0000 | [diff] [blame] | 1531 | nextToken(); |
Martin Probst | 8e3eba0 | 2017-02-07 16:33:13 +0000 | [diff] [blame] | 1532 | parseBracedList(); |
| 1533 | break; |
Krasimir Georgiev | fa4dbb6 | 2017-08-03 13:43:45 +0000 | [diff] [blame] | 1534 | case tok::less: |
| 1535 | if (Style.Language == FormatStyle::LK_Proto) { |
| 1536 | nextToken(); |
| 1537 | parseBracedList(/*ContinueOnSemicolons=*/false, |
| 1538 | /*ClosingBraceKind=*/tok::greater); |
| 1539 | } else { |
| 1540 | nextToken(); |
| 1541 | } |
| 1542 | break; |
Manuel Klimek | a3ff45e | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 1543 | case tok::semi: |
Daniel Jasper | b9a4990 | 2016-01-09 15:56:28 +0000 | [diff] [blame] | 1544 | // JavaScript (or more precisely TypeScript) can have semicolons in braced |
| 1545 | // lists (in so-called TypeMemberLists). Thus, the semicolon cannot be |
| 1546 | // used for error recovery if we have otherwise determined that this is |
| 1547 | // a braced list. |
| 1548 | if (Style.Language == FormatStyle::LK_JavaScript) { |
| 1549 | nextToken(); |
| 1550 | break; |
| 1551 | } |
Daniel Jasper | 015ed02 | 2013-09-13 09:20:45 +0000 | [diff] [blame] | 1552 | HasError = true; |
| 1553 | if (!ContinueOnSemicolons) |
| 1554 | return !HasError; |
| 1555 | nextToken(); |
| 1556 | break; |
Manuel Klimek | a3ff45e | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 1557 | case tok::comma: |
| 1558 | nextToken(); |
Manuel Klimek | a3ff45e | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 1559 | break; |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 1560 | default: |
| 1561 | nextToken(); |
| 1562 | break; |
| 1563 | } |
| 1564 | } while (!eof()); |
Daniel Jasper | 015ed02 | 2013-09-13 09:20:45 +0000 | [diff] [blame] | 1565 | return false; |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1568 | void UnwrappedLineParser::parseParens() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1569 | assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected."); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1570 | nextToken(); |
| 1571 | do { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1572 | switch (FormatTok->Tok.getKind()) { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1573 | case tok::l_paren: |
| 1574 | parseParens(); |
Daniel Jasper | 5f1fa85 | 2015-01-04 20:40:51 +0000 | [diff] [blame] | 1575 | if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace)) |
| 1576 | parseChildBlock(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1577 | break; |
| 1578 | case tok::r_paren: |
| 1579 | nextToken(); |
| 1580 | return; |
Daniel Jasper | 393564f | 2013-05-31 14:56:29 +0000 | [diff] [blame] | 1581 | case tok::r_brace: |
| 1582 | // A "}" inside parenthesis is an error if there wasn't a matching "{". |
| 1583 | return; |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 1584 | case tok::l_square: |
| 1585 | tryToParseLambda(); |
| 1586 | break; |
Daniel Jasper | 5f1fa85 | 2015-01-04 20:40:51 +0000 | [diff] [blame] | 1587 | case tok::l_brace: |
Daniel Jasper | adba2aa | 2015-05-18 12:52:00 +0000 | [diff] [blame] | 1588 | if (!tryToParseBracedList()) |
Manuel Klimek | f017dc0 | 2013-09-04 13:34:14 +0000 | [diff] [blame] | 1589 | parseChildBlock(); |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 1590 | break; |
Nico Weber | 372d8dc | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 1591 | case tok::at: |
| 1592 | nextToken(); |
Krasimir Georgiev | 26b144c | 2017-07-03 15:05:14 +0000 | [diff] [blame] | 1593 | if (FormatTok->Tok.is(tok::l_brace)) { |
| 1594 | nextToken(); |
Nico Weber | 372d8dc | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 1595 | parseBracedList(); |
Krasimir Georgiev | 26b144c | 2017-07-03 15:05:14 +0000 | [diff] [blame] | 1596 | } |
Nico Weber | 372d8dc | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 1597 | break; |
Martin Probst | 1027fb8 | 2017-02-07 14:05:30 +0000 | [diff] [blame] | 1598 | case tok::kw_class: |
| 1599 | if (Style.Language == FormatStyle::LK_JavaScript) |
| 1600 | parseRecord(/*ParseAsExpr=*/true); |
| 1601 | else |
| 1602 | nextToken(); |
| 1603 | break; |
Daniel Jasper | 3f69ba1 | 2014-09-05 08:42:27 +0000 | [diff] [blame] | 1604 | case tok::identifier: |
| 1605 | if (Style.Language == FormatStyle::LK_JavaScript && |
Martin Probst | 409697e | 2016-05-29 14:41:07 +0000 | [diff] [blame] | 1606 | (FormatTok->is(Keywords.kw_function) || |
| 1607 | FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function))) |
Daniel Jasper | 3f69ba1 | 2014-09-05 08:42:27 +0000 | [diff] [blame] | 1608 | tryToParseJSFunction(); |
| 1609 | else |
| 1610 | nextToken(); |
| 1611 | break; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1612 | default: |
| 1613 | nextToken(); |
| 1614 | break; |
| 1615 | } |
| 1616 | } while (!eof()); |
| 1617 | } |
| 1618 | |
Manuel Klimek | 9f0a4e5 | 2017-09-19 09:59:30 +0000 | [diff] [blame] | 1619 | void UnwrappedLineParser::parseSquare(bool LambdaIntroducer) { |
| 1620 | if (!LambdaIntroducer) { |
| 1621 | assert(FormatTok->Tok.is(tok::l_square) && "'[' expected."); |
| 1622 | if (tryToParseLambda()) |
| 1623 | return; |
| 1624 | } |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 1625 | do { |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 1626 | switch (FormatTok->Tok.getKind()) { |
| 1627 | case tok::l_paren: |
| 1628 | parseParens(); |
| 1629 | break; |
| 1630 | case tok::r_square: |
| 1631 | nextToken(); |
| 1632 | return; |
| 1633 | case tok::r_brace: |
| 1634 | // A "}" inside parenthesis is an error if there wasn't a matching "{". |
| 1635 | return; |
| 1636 | case tok::l_square: |
| 1637 | parseSquare(); |
| 1638 | break; |
| 1639 | case tok::l_brace: { |
Daniel Jasper | adba2aa | 2015-05-18 12:52:00 +0000 | [diff] [blame] | 1640 | if (!tryToParseBracedList()) |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 1641 | parseChildBlock(); |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 1642 | break; |
| 1643 | } |
| 1644 | case tok::at: |
| 1645 | nextToken(); |
Krasimir Georgiev | 26b144c | 2017-07-03 15:05:14 +0000 | [diff] [blame] | 1646 | if (FormatTok->Tok.is(tok::l_brace)) { |
| 1647 | nextToken(); |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 1648 | parseBracedList(); |
Krasimir Georgiev | 26b144c | 2017-07-03 15:05:14 +0000 | [diff] [blame] | 1649 | } |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 1650 | break; |
| 1651 | default: |
| 1652 | nextToken(); |
| 1653 | break; |
| 1654 | } |
| 1655 | } while (!eof()); |
| 1656 | } |
| 1657 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1658 | void UnwrappedLineParser::parseIfThenElse() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1659 | assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected"); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1660 | nextToken(); |
Daniel Jasper | 6a7d5a7 | 2017-06-19 07:40:49 +0000 | [diff] [blame] | 1661 | if (FormatTok->Tok.is(tok::kw_constexpr)) |
| 1662 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1663 | if (FormatTok->Tok.is(tok::l_paren)) |
Manuel Klimek | adededf | 2013-01-11 18:28:36 +0000 | [diff] [blame] | 1664 | parseParens(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1665 | bool NeedsUnwrappedLine = false; |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1666 | if (FormatTok->Tok.is(tok::l_brace)) { |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1667 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 1668 | parseBlock(/*MustBeDeclaration=*/false); |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 1669 | if (Style.BraceWrapping.BeforeElse) |
Manuel Klimek | d3ed59a | 2013-08-02 21:31:59 +0000 | [diff] [blame] | 1670 | addUnwrappedLine(); |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 1671 | else |
Manuel Klimek | d3ed59a | 2013-08-02 21:31:59 +0000 | [diff] [blame] | 1672 | NeedsUnwrappedLine = true; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1673 | } else { |
| 1674 | addUnwrappedLine(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1675 | ++Line->Level; |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 1676 | parseStructuralElement(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1677 | --Line->Level; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1678 | } |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1679 | if (FormatTok->Tok.is(tok::kw_else)) { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1680 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1681 | if (FormatTok->Tok.is(tok::l_brace)) { |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1682 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 1683 | parseBlock(/*MustBeDeclaration=*/false); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1684 | addUnwrappedLine(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1685 | } else if (FormatTok->Tok.is(tok::kw_if)) { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1686 | parseIfThenElse(); |
| 1687 | } else { |
| 1688 | addUnwrappedLine(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1689 | ++Line->Level; |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 1690 | parseStructuralElement(); |
Daniel Jasper | 451544a | 2016-05-19 06:30:48 +0000 | [diff] [blame] | 1691 | if (FormatTok->is(tok::eof)) |
| 1692 | addUnwrappedLine(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1693 | --Line->Level; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1694 | } |
| 1695 | } else if (NeedsUnwrappedLine) { |
| 1696 | addUnwrappedLine(); |
| 1697 | } |
| 1698 | } |
| 1699 | |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 1700 | void UnwrappedLineParser::parseTryCatch() { |
Nico Weber | fac2371 | 2015-02-04 15:26:27 +0000 | [diff] [blame] | 1701 | assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected"); |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 1702 | nextToken(); |
| 1703 | bool NeedsUnwrappedLine = false; |
| 1704 | if (FormatTok->is(tok::colon)) { |
| 1705 | // We are in a function try block, what comes is an initializer list. |
| 1706 | nextToken(); |
| 1707 | while (FormatTok->is(tok::identifier)) { |
| 1708 | nextToken(); |
| 1709 | if (FormatTok->is(tok::l_paren)) |
| 1710 | parseParens(); |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 1711 | if (FormatTok->is(tok::comma)) |
| 1712 | nextToken(); |
| 1713 | } |
| 1714 | } |
Daniel Jasper | e189d46 | 2015-01-14 10:48:41 +0000 | [diff] [blame] | 1715 | // Parse try with resource. |
| 1716 | if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) { |
| 1717 | parseParens(); |
| 1718 | } |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 1719 | if (FormatTok->is(tok::l_brace)) { |
| 1720 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
| 1721 | parseBlock(/*MustBeDeclaration=*/false); |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 1722 | if (Style.BraceWrapping.BeforeCatch) { |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 1723 | addUnwrappedLine(); |
| 1724 | } else { |
| 1725 | NeedsUnwrappedLine = true; |
| 1726 | } |
| 1727 | } else if (!FormatTok->is(tok::kw_catch)) { |
| 1728 | // The C++ standard requires a compound-statement after a try. |
| 1729 | // If there's none, we try to assume there's a structuralElement |
| 1730 | // and try to continue. |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 1731 | addUnwrappedLine(); |
| 1732 | ++Line->Level; |
| 1733 | parseStructuralElement(); |
| 1734 | --Line->Level; |
| 1735 | } |
Nico Weber | 33381f5 | 2015-02-07 01:57:32 +0000 | [diff] [blame] | 1736 | while (1) { |
| 1737 | if (FormatTok->is(tok::at)) |
| 1738 | nextToken(); |
| 1739 | if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except, |
| 1740 | tok::kw___finally) || |
| 1741 | ((Style.Language == FormatStyle::LK_Java || |
| 1742 | Style.Language == FormatStyle::LK_JavaScript) && |
| 1743 | FormatTok->is(Keywords.kw_finally)) || |
| 1744 | (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) || |
| 1745 | FormatTok->Tok.isObjCAtKeyword(tok::objc_finally)))) |
| 1746 | break; |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 1747 | nextToken(); |
| 1748 | while (FormatTok->isNot(tok::l_brace)) { |
| 1749 | if (FormatTok->is(tok::l_paren)) { |
| 1750 | parseParens(); |
| 1751 | continue; |
| 1752 | } |
Daniel Jasper | 2bd7a64 | 2015-01-19 10:50:51 +0000 | [diff] [blame] | 1753 | if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof)) |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 1754 | return; |
| 1755 | nextToken(); |
| 1756 | } |
| 1757 | NeedsUnwrappedLine = false; |
| 1758 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
| 1759 | parseBlock(/*MustBeDeclaration=*/false); |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 1760 | if (Style.BraceWrapping.BeforeCatch) |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 1761 | addUnwrappedLine(); |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 1762 | else |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 1763 | NeedsUnwrappedLine = true; |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 1764 | } |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 1765 | if (NeedsUnwrappedLine) |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 1766 | addUnwrappedLine(); |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 1767 | } |
| 1768 | |
Alexander Kornienko | 578fdd8 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1769 | void UnwrappedLineParser::parseNamespace() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1770 | assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected"); |
Roman Kashitsyn | a043ced | 2014-08-11 12:18:01 +0000 | [diff] [blame] | 1771 | |
| 1772 | const FormatToken &InitialToken = *FormatTok; |
Alexander Kornienko | 578fdd8 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1773 | nextToken(); |
Saleem Abdulrasool | 328085f | 2015-10-30 05:07:56 +0000 | [diff] [blame] | 1774 | while (FormatTok->isOneOf(tok::identifier, tok::coloncolon)) |
Alexander Kornienko | 578fdd8 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1775 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1776 | if (FormatTok->Tok.is(tok::l_brace)) { |
Roman Kashitsyn | a043ced | 2014-08-11 12:18:01 +0000 | [diff] [blame] | 1777 | if (ShouldBreakBeforeBrace(Style, InitialToken)) |
Manuel Klimek | a8eb914 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 1778 | addUnwrappedLine(); |
| 1779 | |
Daniel Jasper | 65ee347 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 1780 | bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All || |
| 1781 | (Style.NamespaceIndentation == FormatStyle::NI_Inner && |
| 1782 | DeclarationScopeStack.size() > 1); |
| 1783 | parseBlock(/*MustBeDeclaration=*/true, AddLevel); |
Manuel Klimek | 046b930 | 2013-02-06 16:08:09 +0000 | [diff] [blame] | 1784 | // Munch the semicolon after a namespace. This is more common than one would |
| 1785 | // think. Puttin the semicolon into its own line is very ugly. |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1786 | if (FormatTok->Tok.is(tok::semi)) |
Manuel Klimek | 046b930 | 2013-02-06 16:08:09 +0000 | [diff] [blame] | 1787 | nextToken(); |
Alexander Kornienko | 578fdd8 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1788 | addUnwrappedLine(); |
| 1789 | } |
| 1790 | // FIXME: Add error handling. |
| 1791 | } |
| 1792 | |
Daniel Jasper | 6acf513 | 2015-03-12 14:44:29 +0000 | [diff] [blame] | 1793 | void UnwrappedLineParser::parseNew() { |
| 1794 | assert(FormatTok->is(tok::kw_new) && "'new' expected"); |
| 1795 | nextToken(); |
| 1796 | if (Style.Language != FormatStyle::LK_Java) |
| 1797 | return; |
| 1798 | |
| 1799 | // In Java, we can parse everything up to the parens, which aren't optional. |
| 1800 | do { |
| 1801 | // There should not be a ;, { or } before the new's open paren. |
| 1802 | if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace)) |
| 1803 | return; |
| 1804 | |
| 1805 | // Consume the parens. |
| 1806 | if (FormatTok->is(tok::l_paren)) { |
| 1807 | parseParens(); |
| 1808 | |
| 1809 | // If there is a class body of an anonymous class, consume that as child. |
| 1810 | if (FormatTok->is(tok::l_brace)) |
| 1811 | parseChildBlock(); |
| 1812 | return; |
| 1813 | } |
| 1814 | nextToken(); |
| 1815 | } while (!eof()); |
| 1816 | } |
| 1817 | |
Alexander Kornienko | 37d6c94 | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 1818 | void UnwrappedLineParser::parseForOrWhileLoop() { |
Daniel Jasper | 66cb8c5 | 2015-05-04 09:22:29 +0000 | [diff] [blame] | 1819 | assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) && |
Daniel Jasper | e1e4319 | 2014-04-01 12:55:11 +0000 | [diff] [blame] | 1820 | "'for', 'while' or foreach macro expected"); |
Alexander Kornienko | 37d6c94 | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 1821 | nextToken(); |
Martin Probst | a050f41 | 2017-05-18 21:19:29 +0000 | [diff] [blame] | 1822 | // JS' for await ( ... |
Martin Probst | bd49e32 | 2017-05-15 19:33:20 +0000 | [diff] [blame] | 1823 | if (Style.Language == FormatStyle::LK_JavaScript && |
Martin Probst | a050f41 | 2017-05-18 21:19:29 +0000 | [diff] [blame] | 1824 | FormatTok->is(Keywords.kw_await)) |
Martin Probst | bd49e32 | 2017-05-15 19:33:20 +0000 | [diff] [blame] | 1825 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1826 | if (FormatTok->Tok.is(tok::l_paren)) |
Manuel Klimek | 9fa8d55 | 2013-01-11 19:23:05 +0000 | [diff] [blame] | 1827 | parseParens(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1828 | if (FormatTok->Tok.is(tok::l_brace)) { |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1829 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 1830 | parseBlock(/*MustBeDeclaration=*/false); |
Alexander Kornienko | 37d6c94 | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 1831 | addUnwrappedLine(); |
| 1832 | } else { |
| 1833 | addUnwrappedLine(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1834 | ++Line->Level; |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 1835 | parseStructuralElement(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1836 | --Line->Level; |
Alexander Kornienko | 37d6c94 | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 1837 | } |
| 1838 | } |
| 1839 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1840 | void UnwrappedLineParser::parseDoWhile() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1841 | assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected"); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1842 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1843 | if (FormatTok->Tok.is(tok::l_brace)) { |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1844 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 1845 | parseBlock(/*MustBeDeclaration=*/false); |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 1846 | if (Style.BraceWrapping.IndentBraces) |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1847 | addUnwrappedLine(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1848 | } else { |
| 1849 | addUnwrappedLine(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1850 | ++Line->Level; |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 1851 | parseStructuralElement(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1852 | --Line->Level; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1853 | } |
| 1854 | |
Alexander Kornienko | 0ea8e10 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 1855 | // FIXME: Add error handling. |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1856 | if (!FormatTok->Tok.is(tok::kw_while)) { |
Alexander Kornienko | 0ea8e10 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 1857 | addUnwrappedLine(); |
| 1858 | return; |
| 1859 | } |
| 1860 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1861 | nextToken(); |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 1862 | parseStructuralElement(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
| 1865 | void UnwrappedLineParser::parseLabel() { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1866 | nextToken(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1867 | unsigned OldLineLevel = Line->Level; |
Daniel Jasper | a127512 | 2013-03-20 10:23:53 +0000 | [diff] [blame] | 1868 | if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0)) |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1869 | --Line->Level; |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1870 | if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) { |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1871 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 1872 | parseBlock(/*MustBeDeclaration=*/false); |
Manuel Klimek | d3ed59a | 2013-08-02 21:31:59 +0000 | [diff] [blame] | 1873 | if (FormatTok->Tok.is(tok::kw_break)) { |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 1874 | if (Style.BraceWrapping.AfterControlStatement) |
Manuel Klimek | d3ed59a | 2013-08-02 21:31:59 +0000 | [diff] [blame] | 1875 | addUnwrappedLine(); |
| 1876 | parseStructuralElement(); |
| 1877 | } |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1878 | addUnwrappedLine(); |
| 1879 | } else { |
Daniel Jasper | 1fe0d5c | 2015-05-06 15:19:47 +0000 | [diff] [blame] | 1880 | if (FormatTok->is(tok::semi)) |
| 1881 | nextToken(); |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1882 | addUnwrappedLine(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1883 | } |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1884 | Line->Level = OldLineLevel; |
Daniel Jasper | 2cce7b7 | 2016-04-06 16:41:39 +0000 | [diff] [blame] | 1885 | if (FormatTok->isNot(tok::l_brace)) { |
Daniel Jasper | 4060947 | 2016-04-06 15:02:46 +0000 | [diff] [blame] | 1886 | parseStructuralElement(); |
Daniel Jasper | 2cce7b7 | 2016-04-06 16:41:39 +0000 | [diff] [blame] | 1887 | addUnwrappedLine(); |
| 1888 | } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1889 | } |
| 1890 | |
| 1891 | void UnwrappedLineParser::parseCaseLabel() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1892 | assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected"); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1893 | // FIXME: fix handling of complex expressions here. |
| 1894 | do { |
| 1895 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1896 | } while (!eof() && !FormatTok->Tok.is(tok::colon)); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1897 | parseLabel(); |
| 1898 | } |
| 1899 | |
| 1900 | void UnwrappedLineParser::parseSwitch() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1901 | assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected"); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1902 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1903 | if (FormatTok->Tok.is(tok::l_paren)) |
Manuel Klimek | 9fa8d55 | 2013-01-11 19:23:05 +0000 | [diff] [blame] | 1904 | parseParens(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1905 | if (FormatTok->Tok.is(tok::l_brace)) { |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1906 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
Daniel Jasper | 65ee347 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 1907 | parseBlock(/*MustBeDeclaration=*/false); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1908 | addUnwrappedLine(); |
| 1909 | } else { |
| 1910 | addUnwrappedLine(); |
Daniel Jasper | 516d797 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 1911 | ++Line->Level; |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 1912 | parseStructuralElement(); |
Daniel Jasper | 516d797 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 1913 | --Line->Level; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1914 | } |
| 1915 | } |
| 1916 | |
| 1917 | void UnwrappedLineParser::parseAccessSpecifier() { |
| 1918 | nextToken(); |
Daniel Jasper | 84c47a1 | 2013-11-23 17:53:41 +0000 | [diff] [blame] | 1919 | // Understand Qt's slots. |
Daniel Jasper | 5339540 | 2015-04-07 15:04:40 +0000 | [diff] [blame] | 1920 | if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots)) |
Daniel Jasper | 84c47a1 | 2013-11-23 17:53:41 +0000 | [diff] [blame] | 1921 | nextToken(); |
Alexander Kornienko | 2ca766f | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1922 | // Otherwise, we don't know what it is, and we'd better keep the next token. |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1923 | if (FormatTok->Tok.is(tok::colon)) |
Alexander Kornienko | 2ca766f | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1924 | nextToken(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1925 | addUnwrappedLine(); |
| 1926 | } |
| 1927 | |
Daniel Jasper | 6f5a193 | 2015-12-29 08:54:23 +0000 | [diff] [blame] | 1928 | bool UnwrappedLineParser::parseEnum() { |
Daniel Jasper | 6be0f55 | 2014-11-13 15:56:28 +0000 | [diff] [blame] | 1929 | // Won't be 'enum' for NS_ENUMs. |
| 1930 | if (FormatTok->Tok.is(tok::kw_enum)) |
Daniel Jasper | ccb68b4 | 2014-11-19 22:38:18 +0000 | [diff] [blame] | 1931 | nextToken(); |
Daniel Jasper | 6be0f55 | 2014-11-13 15:56:28 +0000 | [diff] [blame] | 1932 | |
Daniel Jasper | 6f5a193 | 2015-12-29 08:54:23 +0000 | [diff] [blame] | 1933 | // In TypeScript, "enum" can also be used as property name, e.g. in interface |
| 1934 | // declarations. An "enum" keyword followed by a colon would be a syntax |
| 1935 | // error and thus assume it is just an identifier. |
Daniel Jasper | 8737930 | 2016-02-03 05:33:44 +0000 | [diff] [blame] | 1936 | if (Style.Language == FormatStyle::LK_JavaScript && |
| 1937 | FormatTok->isOneOf(tok::colon, tok::question)) |
Daniel Jasper | 6f5a193 | 2015-12-29 08:54:23 +0000 | [diff] [blame] | 1938 | return false; |
| 1939 | |
Daniel Jasper | 2b41a82 | 2013-08-20 12:42:50 +0000 | [diff] [blame] | 1940 | // Eat up enum class ... |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 1941 | if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct)) |
| 1942 | nextToken(); |
Daniel Jasper | b5a0b85 | 2015-06-19 08:17:32 +0000 | [diff] [blame] | 1943 | |
Daniel Jasper | 786a550 | 2013-09-06 21:32:35 +0000 | [diff] [blame] | 1944 | while (FormatTok->Tok.getIdentifierInfo() || |
Daniel Jasper | ccb68b4 | 2014-11-19 22:38:18 +0000 | [diff] [blame] | 1945 | FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less, |
| 1946 | tok::greater, tok::comma, tok::question)) { |
Manuel Klimek | 2cec019 | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 1947 | nextToken(); |
| 1948 | // We can have macros or attributes in between 'enum' and the enum name. |
Daniel Jasper | ccb68b4 | 2014-11-19 22:38:18 +0000 | [diff] [blame] | 1949 | if (FormatTok->is(tok::l_paren)) |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 1950 | parseParens(); |
Daniel Jasper | b5a0b85 | 2015-06-19 08:17:32 +0000 | [diff] [blame] | 1951 | if (FormatTok->is(tok::identifier)) { |
Manuel Klimek | 2cec019 | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 1952 | nextToken(); |
Daniel Jasper | b5a0b85 | 2015-06-19 08:17:32 +0000 | [diff] [blame] | 1953 | // If there are two identifiers in a row, this is likely an elaborate |
| 1954 | // return type. In Java, this can be "implements", etc. |
Daniel Jasper | 1dbc210 | 2017-03-31 13:30:24 +0000 | [diff] [blame] | 1955 | if (Style.isCpp() && FormatTok->is(tok::identifier)) |
Daniel Jasper | 6f5a193 | 2015-12-29 08:54:23 +0000 | [diff] [blame] | 1956 | return false; |
Daniel Jasper | b5a0b85 | 2015-06-19 08:17:32 +0000 | [diff] [blame] | 1957 | } |
Manuel Klimek | 2cec019 | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 1958 | } |
Daniel Jasper | 6be0f55 | 2014-11-13 15:56:28 +0000 | [diff] [blame] | 1959 | |
| 1960 | // Just a declaration or something is wrong. |
Daniel Jasper | ccb68b4 | 2014-11-19 22:38:18 +0000 | [diff] [blame] | 1961 | if (FormatTok->isNot(tok::l_brace)) |
Daniel Jasper | 6f5a193 | 2015-12-29 08:54:23 +0000 | [diff] [blame] | 1962 | return true; |
Daniel Jasper | 6be0f55 | 2014-11-13 15:56:28 +0000 | [diff] [blame] | 1963 | FormatTok->BlockKind = BK_Block; |
| 1964 | |
| 1965 | if (Style.Language == FormatStyle::LK_Java) { |
| 1966 | // Java enums are different. |
| 1967 | parseJavaEnumBody(); |
Daniel Jasper | 6f5a193 | 2015-12-29 08:54:23 +0000 | [diff] [blame] | 1968 | return true; |
| 1969 | } |
| 1970 | if (Style.Language == FormatStyle::LK_Proto) { |
Daniel Jasper | c6dd273 | 2015-07-16 14:25:43 +0000 | [diff] [blame] | 1971 | parseBlock(/*MustBeDeclaration=*/true); |
Daniel Jasper | 6f5a193 | 2015-12-29 08:54:23 +0000 | [diff] [blame] | 1972 | return true; |
Manuel Klimek | 2cec019 | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 1973 | } |
Daniel Jasper | 6be0f55 | 2014-11-13 15:56:28 +0000 | [diff] [blame] | 1974 | |
| 1975 | // Parse enum body. |
Krasimir Georgiev | 26b144c | 2017-07-03 15:05:14 +0000 | [diff] [blame] | 1976 | nextToken(); |
Daniel Jasper | 6be0f55 | 2014-11-13 15:56:28 +0000 | [diff] [blame] | 1977 | bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true); |
| 1978 | if (HasError) { |
| 1979 | if (FormatTok->is(tok::semi)) |
| 1980 | nextToken(); |
| 1981 | addUnwrappedLine(); |
| 1982 | } |
Daniel Jasper | 6f5a193 | 2015-12-29 08:54:23 +0000 | [diff] [blame] | 1983 | return true; |
Daniel Jasper | 6be0f55 | 2014-11-13 15:56:28 +0000 | [diff] [blame] | 1984 | |
Daniel Jasper | 90cf380 | 2015-06-17 09:44:02 +0000 | [diff] [blame] | 1985 | // There is no addUnwrappedLine() here so that we fall through to parsing a |
| 1986 | // structural element afterwards. Thus, in "enum A {} n, m;", |
Manuel Klimek | 2cec019 | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 1987 | // "} n, m;" will end up in one unwrapped line. |
Daniel Jasper | 6be0f55 | 2014-11-13 15:56:28 +0000 | [diff] [blame] | 1988 | } |
| 1989 | |
| 1990 | void UnwrappedLineParser::parseJavaEnumBody() { |
| 1991 | // Determine whether the enum is simple, i.e. does not have a semicolon or |
| 1992 | // constants with class bodies. Simple enums can be formatted like braced |
| 1993 | // lists, contracted to a single line, etc. |
| 1994 | unsigned StoredPosition = Tokens->getPosition(); |
| 1995 | bool IsSimple = true; |
| 1996 | FormatToken *Tok = Tokens->getNextToken(); |
| 1997 | while (Tok) { |
| 1998 | if (Tok->is(tok::r_brace)) |
| 1999 | break; |
| 2000 | if (Tok->isOneOf(tok::l_brace, tok::semi)) { |
| 2001 | IsSimple = false; |
| 2002 | break; |
| 2003 | } |
| 2004 | // FIXME: This will also mark enums with braces in the arguments to enum |
| 2005 | // constants as "not simple". This is probably fine in practice, though. |
| 2006 | Tok = Tokens->getNextToken(); |
| 2007 | } |
| 2008 | FormatTok = Tokens->setPosition(StoredPosition); |
| 2009 | |
| 2010 | if (IsSimple) { |
Krasimir Georgiev | 26b144c | 2017-07-03 15:05:14 +0000 | [diff] [blame] | 2011 | nextToken(); |
Daniel Jasper | 6be0f55 | 2014-11-13 15:56:28 +0000 | [diff] [blame] | 2012 | parseBracedList(); |
Daniel Jasper | df2ff00 | 2014-11-02 22:31:39 +0000 | [diff] [blame] | 2013 | addUnwrappedLine(); |
Daniel Jasper | 6be0f55 | 2014-11-13 15:56:28 +0000 | [diff] [blame] | 2014 | return; |
| 2015 | } |
| 2016 | |
| 2017 | // Parse the body of a more complex enum. |
| 2018 | // First add a line for everything up to the "{". |
| 2019 | nextToken(); |
| 2020 | addUnwrappedLine(); |
| 2021 | ++Line->Level; |
| 2022 | |
| 2023 | // Parse the enum constants. |
| 2024 | while (FormatTok) { |
| 2025 | if (FormatTok->is(tok::l_brace)) { |
| 2026 | // Parse the constant's class body. |
| 2027 | parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true, |
| 2028 | /*MunchSemi=*/false); |
| 2029 | } else if (FormatTok->is(tok::l_paren)) { |
| 2030 | parseParens(); |
| 2031 | } else if (FormatTok->is(tok::comma)) { |
| 2032 | nextToken(); |
| 2033 | addUnwrappedLine(); |
| 2034 | } else if (FormatTok->is(tok::semi)) { |
| 2035 | nextToken(); |
| 2036 | addUnwrappedLine(); |
| 2037 | break; |
| 2038 | } else if (FormatTok->is(tok::r_brace)) { |
| 2039 | addUnwrappedLine(); |
| 2040 | break; |
| 2041 | } else { |
| 2042 | nextToken(); |
| 2043 | } |
| 2044 | } |
| 2045 | |
| 2046 | // Parse the class body after the enum's ";" if any. |
| 2047 | parseLevel(/*HasOpeningBrace=*/true); |
| 2048 | nextToken(); |
| 2049 | --Line->Level; |
| 2050 | addUnwrappedLine(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 2051 | } |
| 2052 | |
Martin Probst | 1027fb8 | 2017-02-07 14:05:30 +0000 | [diff] [blame] | 2053 | void UnwrappedLineParser::parseRecord(bool ParseAsExpr) { |
Roman Kashitsyn | a043ced | 2014-08-11 12:18:01 +0000 | [diff] [blame] | 2054 | const FormatToken &InitialToken = *FormatTok; |
Manuel Klimek | 28cacc7 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 2055 | nextToken(); |
Daniel Jasper | 04785d0 | 2015-05-06 14:03:02 +0000 | [diff] [blame] | 2056 | |
Daniel Jasper | 04785d0 | 2015-05-06 14:03:02 +0000 | [diff] [blame] | 2057 | // The actual identifier can be a nested name specifier, and in macros |
| 2058 | // it is often token-pasted. |
| 2059 | while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash, |
| 2060 | tok::kw___attribute, tok::kw___declspec, |
| 2061 | tok::kw_alignas) || |
| 2062 | ((Style.Language == FormatStyle::LK_Java || |
| 2063 | Style.Language == FormatStyle::LK_JavaScript) && |
| 2064 | FormatTok->isOneOf(tok::period, tok::comma))) { |
Martin Probst | cb870c5 | 2017-08-01 15:46:10 +0000 | [diff] [blame] | 2065 | if (Style.Language == FormatStyle::LK_JavaScript && |
| 2066 | FormatTok->isOneOf(Keywords.kw_extends, Keywords.kw_implements)) { |
| 2067 | // JavaScript/TypeScript supports inline object types in |
| 2068 | // extends/implements positions: |
| 2069 | // class Foo implements {bar: number} { } |
| 2070 | nextToken(); |
| 2071 | if (FormatTok->is(tok::l_brace)) { |
| 2072 | tryToParseBracedList(); |
| 2073 | continue; |
| 2074 | } |
| 2075 | } |
Daniel Jasper | 04785d0 | 2015-05-06 14:03:02 +0000 | [diff] [blame] | 2076 | bool IsNonMacroIdentifier = |
| 2077 | FormatTok->is(tok::identifier) && |
| 2078 | FormatTok->TokenText != FormatTok->TokenText.upper(); |
Manuel Klimek | e01bab5 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 2079 | nextToken(); |
| 2080 | // We can have macros or attributes in between 'class' and the class name. |
Daniel Jasper | 04785d0 | 2015-05-06 14:03:02 +0000 | [diff] [blame] | 2081 | if (!IsNonMacroIdentifier && FormatTok->Tok.is(tok::l_paren)) |
Manuel Klimek | e01bab5 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 2082 | parseParens(); |
Daniel Jasper | 04785d0 | 2015-05-06 14:03:02 +0000 | [diff] [blame] | 2083 | } |
Manuel Klimek | e01bab5 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 2084 | |
Daniel Jasper | 04785d0 | 2015-05-06 14:03:02 +0000 | [diff] [blame] | 2085 | // Note that parsing away template declarations here leads to incorrectly |
| 2086 | // accepting function declarations as record declarations. |
| 2087 | // In general, we cannot solve this problem. Consider: |
| 2088 | // class A<int> B() {} |
| 2089 | // which can be a function definition or a class definition when B() is a |
| 2090 | // macro. If we find enough real-world cases where this is a problem, we |
| 2091 | // can parse for the 'template' keyword in the beginning of the statement, |
| 2092 | // and thus rule out the record production in case there is no template |
| 2093 | // (this would still leave us with an ambiguity between template function |
| 2094 | // and class declarations). |
Daniel Jasper | adba2aa | 2015-05-18 12:52:00 +0000 | [diff] [blame] | 2095 | if (FormatTok->isOneOf(tok::colon, tok::less)) { |
| 2096 | while (!eof()) { |
Daniel Jasper | 3c883d1 | 2015-05-18 14:49:19 +0000 | [diff] [blame] | 2097 | if (FormatTok->is(tok::l_brace)) { |
| 2098 | calculateBraceTypes(/*ExpectClassBody=*/true); |
| 2099 | if (!tryToParseBracedList()) |
| 2100 | break; |
| 2101 | } |
Daniel Jasper | 04785d0 | 2015-05-06 14:03:02 +0000 | [diff] [blame] | 2102 | if (FormatTok->Tok.is(tok::semi)) |
| 2103 | return; |
| 2104 | nextToken(); |
Manuel Klimek | e01bab5 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 2105 | } |
| 2106 | } |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 2107 | if (FormatTok->Tok.is(tok::l_brace)) { |
Martin Probst | 1027fb8 | 2017-02-07 14:05:30 +0000 | [diff] [blame] | 2108 | if (ParseAsExpr) { |
| 2109 | parseChildBlock(); |
| 2110 | } else { |
| 2111 | if (ShouldBreakBeforeBrace(Style, InitialToken)) |
| 2112 | addUnwrappedLine(); |
Manuel Klimek | a8eb914 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 2113 | |
Martin Probst | 1027fb8 | 2017-02-07 14:05:30 +0000 | [diff] [blame] | 2114 | parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true, |
| 2115 | /*MunchSemi=*/false); |
| 2116 | } |
Manuel Klimek | a8eb914 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 2117 | } |
Daniel Jasper | 90cf380 | 2015-06-17 09:44:02 +0000 | [diff] [blame] | 2118 | // There is no addUnwrappedLine() here so that we fall through to parsing a |
| 2119 | // structural element afterwards. Thus, in "class A {} n, m;", |
| 2120 | // "} n, m;" will end up in one unwrapped line. |
Manuel Klimek | 28cacc7 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 2121 | } |
| 2122 | |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 2123 | void UnwrappedLineParser::parseObjCProtocolList() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 2124 | assert(FormatTok->Tok.is(tok::less) && "'<' expected."); |
Ben Hamilton | 1462e84 | 2018-04-05 15:26:25 +0000 | [diff] [blame] | 2125 | do { |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 2126 | nextToken(); |
Ben Hamilton | 1462e84 | 2018-04-05 15:26:25 +0000 | [diff] [blame] | 2127 | // Early exit in case someone forgot a close angle. |
| 2128 | if (FormatTok->isOneOf(tok::semi, tok::l_brace) || |
| 2129 | FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) |
| 2130 | return; |
| 2131 | } while (!eof() && FormatTok->Tok.isNot(tok::greater)); |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 2132 | nextToken(); // Skip '>'. |
| 2133 | } |
| 2134 | |
| 2135 | void UnwrappedLineParser::parseObjCUntilAtEnd() { |
| 2136 | do { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 2137 | if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) { |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 2138 | nextToken(); |
| 2139 | addUnwrappedLine(); |
| 2140 | break; |
| 2141 | } |
Daniel Jasper | a15da30 | 2013-08-28 08:04:23 +0000 | [diff] [blame] | 2142 | if (FormatTok->is(tok::l_brace)) { |
| 2143 | parseBlock(/*MustBeDeclaration=*/false); |
| 2144 | // In ObjC interfaces, nothing should be following the "}". |
| 2145 | addUnwrappedLine(); |
Benjamin Kramer | e21cb74 | 2014-01-08 15:59:42 +0000 | [diff] [blame] | 2146 | } else if (FormatTok->is(tok::r_brace)) { |
| 2147 | // Ignore stray "}". parseStructuralElement doesn't consume them. |
| 2148 | nextToken(); |
| 2149 | addUnwrappedLine(); |
Daniel Jasper | a15da30 | 2013-08-28 08:04:23 +0000 | [diff] [blame] | 2150 | } else { |
| 2151 | parseStructuralElement(); |
| 2152 | } |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 2153 | } while (!eof()); |
| 2154 | } |
| 2155 | |
Nico Weber | 2ce0ac5 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 2156 | void UnwrappedLineParser::parseObjCInterfaceOrImplementation() { |
Nico Weber | c068ff7 | 2018-01-23 17:10:25 +0000 | [diff] [blame] | 2157 | assert(FormatTok->Tok.getObjCKeywordID() == tok::objc_interface || |
| 2158 | FormatTok->Tok.getObjCKeywordID() == tok::objc_implementation); |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2159 | nextToken(); |
Daniel Jasper | d1ae358 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 2160 | nextToken(); // interface name |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2161 | |
Ben Hamilton | 1462e84 | 2018-04-05 15:26:25 +0000 | [diff] [blame] | 2162 | // @interface can be followed by a lightweight generic |
| 2163 | // specialization list, then either a base class or a category. |
| 2164 | if (FormatTok->Tok.is(tok::less)) { |
| 2165 | // Unlike protocol lists, generic parameterizations support |
| 2166 | // nested angles: |
| 2167 | // |
| 2168 | // @interface Foo<ValueType : id <NSCopying, NSSecureCoding>> : |
| 2169 | // NSObject <NSCopying, NSSecureCoding> |
| 2170 | // |
| 2171 | // so we need to count how many open angles we have left. |
| 2172 | unsigned NumOpenAngles = 1; |
| 2173 | do { |
| 2174 | nextToken(); |
| 2175 | // Early exit in case someone forgot a close angle. |
| 2176 | if (FormatTok->isOneOf(tok::semi, tok::l_brace) || |
| 2177 | FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) |
| 2178 | break; |
| 2179 | if (FormatTok->Tok.is(tok::less)) |
| 2180 | ++NumOpenAngles; |
| 2181 | else if (FormatTok->Tok.is(tok::greater)) { |
| 2182 | assert(NumOpenAngles > 0 && "'>' makes NumOpenAngles negative"); |
| 2183 | --NumOpenAngles; |
| 2184 | } |
| 2185 | } while (!eof() && NumOpenAngles != 0); |
| 2186 | nextToken(); // Skip '>'. |
| 2187 | } |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 2188 | if (FormatTok->Tok.is(tok::colon)) { |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2189 | nextToken(); |
Daniel Jasper | d1ae358 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 2190 | nextToken(); // base class name |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 2191 | } else if (FormatTok->Tok.is(tok::l_paren)) |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2192 | // Skip category, if present. |
| 2193 | parseParens(); |
| 2194 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 2195 | if (FormatTok->Tok.is(tok::less)) |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 2196 | parseObjCProtocolList(); |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2197 | |
Dinesh Dwivedi | ea3aca8 | 2014-05-02 17:01:46 +0000 | [diff] [blame] | 2198 | if (FormatTok->Tok.is(tok::l_brace)) { |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 2199 | if (Style.BraceWrapping.AfterObjCDeclaration) |
Dinesh Dwivedi | ea3aca8 | 2014-05-02 17:01:46 +0000 | [diff] [blame] | 2200 | addUnwrappedLine(); |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 2201 | parseBlock(/*MustBeDeclaration=*/true); |
Dinesh Dwivedi | ea3aca8 | 2014-05-02 17:01:46 +0000 | [diff] [blame] | 2202 | } |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2203 | |
| 2204 | // With instance variables, this puts '}' on its own line. Without instance |
| 2205 | // variables, this ends the @interface line. |
| 2206 | addUnwrappedLine(); |
| 2207 | |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 2208 | parseObjCUntilAtEnd(); |
| 2209 | } |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2210 | |
Nico Weber | c068ff7 | 2018-01-23 17:10:25 +0000 | [diff] [blame] | 2211 | // Returns true for the declaration/definition form of @protocol, |
| 2212 | // false for the expression form. |
| 2213 | bool UnwrappedLineParser::parseObjCProtocol() { |
| 2214 | assert(FormatTok->Tok.getObjCKeywordID() == tok::objc_protocol); |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 2215 | nextToken(); |
Nico Weber | c068ff7 | 2018-01-23 17:10:25 +0000 | [diff] [blame] | 2216 | |
| 2217 | if (FormatTok->is(tok::l_paren)) |
| 2218 | // The expression form of @protocol, e.g. "Protocol* p = @protocol(foo);". |
| 2219 | return false; |
| 2220 | |
| 2221 | // The definition/declaration form, |
| 2222 | // @protocol Foo |
| 2223 | // - (int)someMethod; |
| 2224 | // @end |
| 2225 | |
Daniel Jasper | d1ae358 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 2226 | nextToken(); // protocol name |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 2227 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 2228 | if (FormatTok->Tok.is(tok::less)) |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 2229 | parseObjCProtocolList(); |
| 2230 | |
| 2231 | // Check for protocol declaration. |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 2232 | if (FormatTok->Tok.is(tok::semi)) { |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 2233 | nextToken(); |
Nico Weber | c068ff7 | 2018-01-23 17:10:25 +0000 | [diff] [blame] | 2234 | addUnwrappedLine(); |
| 2235 | return true; |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 2236 | } |
| 2237 | |
| 2238 | addUnwrappedLine(); |
| 2239 | parseObjCUntilAtEnd(); |
Nico Weber | c068ff7 | 2018-01-23 17:10:25 +0000 | [diff] [blame] | 2240 | return true; |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2241 | } |
| 2242 | |
Daniel Jasper | fca735c | 2015-02-19 16:14:18 +0000 | [diff] [blame] | 2243 | void UnwrappedLineParser::parseJavaScriptEs6ImportExport() { |
Martin Probst | 053f1aa | 2016-04-19 14:55:37 +0000 | [diff] [blame] | 2244 | bool IsImport = FormatTok->is(Keywords.kw_import); |
| 2245 | assert(IsImport || FormatTok->is(tok::kw_export)); |
Daniel Jasper | 354aa51 | 2015-02-19 16:07:32 +0000 | [diff] [blame] | 2246 | nextToken(); |
Daniel Jasper | fca735c | 2015-02-19 16:14:18 +0000 | [diff] [blame] | 2247 | |
Daniel Jasper | ec05fc7 | 2015-05-11 09:14:50 +0000 | [diff] [blame] | 2248 | // Consume the "default" in "export default class/function". |
Daniel Jasper | 668c7bb | 2015-05-11 09:03:10 +0000 | [diff] [blame] | 2249 | if (FormatTok->is(tok::kw_default)) |
| 2250 | nextToken(); |
Daniel Jasper | ec05fc7 | 2015-05-11 09:14:50 +0000 | [diff] [blame] | 2251 | |
Martin Probst | 5f8445b | 2016-04-24 22:05:09 +0000 | [diff] [blame] | 2252 | // Consume "async function", "function" and "default function", so that these |
| 2253 | // get parsed as free-standing JS functions, i.e. do not require a trailing |
| 2254 | // semicolon. |
| 2255 | if (FormatTok->is(Keywords.kw_async)) |
| 2256 | nextToken(); |
Daniel Jasper | 668c7bb | 2015-05-11 09:03:10 +0000 | [diff] [blame] | 2257 | if (FormatTok->is(Keywords.kw_function)) { |
| 2258 | nextToken(); |
| 2259 | return; |
| 2260 | } |
| 2261 | |
Martin Probst | 053f1aa | 2016-04-19 14:55:37 +0000 | [diff] [blame] | 2262 | // For imports, `export *`, `export {...}`, consume the rest of the line up |
| 2263 | // to the terminating `;`. For everything else, just return and continue |
| 2264 | // parsing the structural element, i.e. the declaration or expression for |
| 2265 | // `export default`. |
| 2266 | if (!IsImport && !FormatTok->isOneOf(tok::l_brace, tok::star) && |
| 2267 | !FormatTok->isStringLiteral()) |
| 2268 | return; |
Daniel Jasper | fca735c | 2015-02-19 16:14:18 +0000 | [diff] [blame] | 2269 | |
Martin Probst | d40bca4 | 2017-01-09 08:56:36 +0000 | [diff] [blame] | 2270 | while (!eof()) { |
| 2271 | if (FormatTok->is(tok::semi)) |
| 2272 | return; |
Krasimir Georgiev | 112c2e9 | 2017-11-09 13:22:03 +0000 | [diff] [blame] | 2273 | if (Line->Tokens.empty()) { |
Martin Probst | d40bca4 | 2017-01-09 08:56:36 +0000 | [diff] [blame] | 2274 | // Common issue: Automatic Semicolon Insertion wrapped the line, so the |
| 2275 | // import statement should terminate. |
| 2276 | return; |
| 2277 | } |
Daniel Jasper | efc1a83 | 2016-01-07 08:53:35 +0000 | [diff] [blame] | 2278 | if (FormatTok->is(tok::l_brace)) { |
| 2279 | FormatTok->BlockKind = BK_Block; |
Krasimir Georgiev | 26b144c | 2017-07-03 15:05:14 +0000 | [diff] [blame] | 2280 | nextToken(); |
Daniel Jasper | efc1a83 | 2016-01-07 08:53:35 +0000 | [diff] [blame] | 2281 | parseBracedList(); |
| 2282 | } else { |
| 2283 | nextToken(); |
| 2284 | } |
Daniel Jasper | 354aa51 | 2015-02-19 16:07:32 +0000 | [diff] [blame] | 2285 | } |
| 2286 | } |
| 2287 | |
Daniel Jasper | 3b203a6 | 2013-09-05 16:05:56 +0000 | [diff] [blame] | 2288 | LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line, |
| 2289 | StringRef Prefix = "") { |
Krasimir Georgiev | 9ad83fe | 2017-10-30 14:01:50 +0000 | [diff] [blame] | 2290 | llvm::dbgs() << Prefix << "Line(" << Line.Level |
| 2291 | << ", FSC=" << Line.FirstStartColumn << ")" |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 2292 | << (Line.InPPDirective ? " MACRO" : "") << ": "; |
| 2293 | for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(), |
| 2294 | E = Line.Tokens.end(); |
| 2295 | I != E; ++I) { |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 2296 | llvm::dbgs() << I->Tok->Tok.getName() << "[" |
Manuel Klimek | 89628f6 | 2017-09-20 09:51:03 +0000 | [diff] [blame] | 2297 | << "T=" << I->Tok->Type << ", OC=" << I->Tok->OriginalColumn |
| 2298 | << "] "; |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 2299 | } |
| 2300 | for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(), |
| 2301 | E = Line.Tokens.end(); |
| 2302 | I != E; ++I) { |
| 2303 | const UnwrappedLineNode &Node = *I; |
| 2304 | for (SmallVectorImpl<UnwrappedLine>::const_iterator |
| 2305 | I = Node.Children.begin(), |
| 2306 | E = Node.Children.end(); |
| 2307 | I != E; ++I) { |
| 2308 | printDebugInfo(*I, "\nChild: "); |
| 2309 | } |
| 2310 | } |
| 2311 | llvm::dbgs() << "\n"; |
| 2312 | } |
| 2313 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 2314 | void UnwrappedLineParser::addUnwrappedLine() { |
Daniel Jasper | daffc0d | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 2315 | if (Line->Tokens.empty()) |
Daniel Jasper | 7c85fde | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 2316 | return; |
Manuel Klimek | ab3dc00 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 2317 | DEBUG({ |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 2318 | if (CurrentLines == &Lines) |
| 2319 | printDebugInfo(*Line); |
Manuel Klimek | ab3dc00 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 2320 | }); |
Benjamin Kramer | c7551a4 | 2015-05-31 11:18:05 +0000 | [diff] [blame] | 2321 | CurrentLines->push_back(std::move(*Line)); |
Daniel Jasper | daffc0d | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 2322 | Line->Tokens.clear(); |
Krasimir Georgiev | 85c3704 | 2017-03-01 16:38:08 +0000 | [diff] [blame] | 2323 | Line->MatchingOpeningBlockLineIndex = UnwrappedLine::kInvalidIndex; |
Krasimir Georgiev | 9ad83fe | 2017-10-30 14:01:50 +0000 | [diff] [blame] | 2324 | Line->FirstStartColumn = 0; |
Manuel Klimek | d3b92fa | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 2325 | if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) { |
Benjamin Kramer | c7551a4 | 2015-05-31 11:18:05 +0000 | [diff] [blame] | 2326 | CurrentLines->append( |
| 2327 | std::make_move_iterator(PreprocessorDirectives.begin()), |
| 2328 | std::make_move_iterator(PreprocessorDirectives.end())); |
Manuel Klimek | d3b92fa | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 2329 | PreprocessorDirectives.clear(); |
| 2330 | } |
Manuel Klimek | e411aa8 | 2017-09-20 09:29:37 +0000 | [diff] [blame] | 2331 | // Disconnect the current token from the last token on the previous line. |
| 2332 | FormatTok->Previous = nullptr; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 2333 | } |
| 2334 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 2335 | bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 2336 | |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 2337 | bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) { |
Manuel Klimek | 1fcbe67 | 2014-04-11 12:27:47 +0000 | [diff] [blame] | 2338 | return (Line->InPPDirective || FormatTok.HasUnescapedNewline) && |
| 2339 | FormatTok.NewlinesBefore > 0; |
| 2340 | } |
| 2341 | |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 2342 | // Checks if \p FormatTok is a line comment that continues the line comment |
| 2343 | // section on \p Line. |
Krasimir Georgiev | ea222a7 | 2017-05-22 10:07:56 +0000 | [diff] [blame] | 2344 | static bool continuesLineCommentSection(const FormatToken &FormatTok, |
| 2345 | const UnwrappedLine &Line, |
| 2346 | llvm::Regex &CommentPragmasRegex) { |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 2347 | if (Line.Tokens.empty()) |
| 2348 | return false; |
Krasimir Georgiev | 8432161 | 2017-01-30 19:18:55 +0000 | [diff] [blame] | 2349 | |
Krasimir Georgiev | 00c5c72 | 2017-02-02 15:32:19 +0000 | [diff] [blame] | 2350 | StringRef IndentContent = FormatTok.TokenText; |
| 2351 | if (FormatTok.TokenText.startswith("//") || |
| 2352 | FormatTok.TokenText.startswith("/*")) |
| 2353 | IndentContent = FormatTok.TokenText.substr(2); |
| 2354 | if (CommentPragmasRegex.match(IndentContent)) |
| 2355 | return false; |
| 2356 | |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 2357 | // If Line starts with a line comment, then FormatTok continues the comment |
Krasimir Georgiev | 8432161 | 2017-01-30 19:18:55 +0000 | [diff] [blame] | 2358 | // section if its original column is greater or equal to the original start |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 2359 | // column of the line. |
| 2360 | // |
Krasimir Georgiev | 8432161 | 2017-01-30 19:18:55 +0000 | [diff] [blame] | 2361 | // Define the min column token of a line as follows: if a line ends in '{' or |
| 2362 | // contains a '{' followed by a line comment, then the min column token is |
| 2363 | // that '{'. Otherwise, the min column token of the line is the first token of |
| 2364 | // the line. |
| 2365 | // |
| 2366 | // If Line starts with a token other than a line comment, then FormatTok |
| 2367 | // continues the comment section if its original column is greater than the |
| 2368 | // original start column of the min column token of the line. |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 2369 | // |
| 2370 | // For example, the second line comment continues the first in these cases: |
Krasimir Georgiev | b6ccd38 | 2017-02-02 14:36:50 +0000 | [diff] [blame] | 2371 | // |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 2372 | // // first line |
| 2373 | // // second line |
Krasimir Georgiev | b6ccd38 | 2017-02-02 14:36:50 +0000 | [diff] [blame] | 2374 | // |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 2375 | // and: |
Krasimir Georgiev | b6ccd38 | 2017-02-02 14:36:50 +0000 | [diff] [blame] | 2376 | // |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 2377 | // // first line |
| 2378 | // // second line |
Krasimir Georgiev | b6ccd38 | 2017-02-02 14:36:50 +0000 | [diff] [blame] | 2379 | // |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 2380 | // and: |
Krasimir Georgiev | b6ccd38 | 2017-02-02 14:36:50 +0000 | [diff] [blame] | 2381 | // |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 2382 | // int i; // first line |
| 2383 | // // second line |
Krasimir Georgiev | b6ccd38 | 2017-02-02 14:36:50 +0000 | [diff] [blame] | 2384 | // |
Krasimir Georgiev | 8432161 | 2017-01-30 19:18:55 +0000 | [diff] [blame] | 2385 | // and: |
Krasimir Georgiev | b6ccd38 | 2017-02-02 14:36:50 +0000 | [diff] [blame] | 2386 | // |
Krasimir Georgiev | 8432161 | 2017-01-30 19:18:55 +0000 | [diff] [blame] | 2387 | // do { // first line |
| 2388 | // // second line |
| 2389 | // int i; |
| 2390 | // } while (true); |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 2391 | // |
Krasimir Georgiev | b6ccd38 | 2017-02-02 14:36:50 +0000 | [diff] [blame] | 2392 | // and: |
| 2393 | // |
| 2394 | // enum { |
| 2395 | // a, // first line |
| 2396 | // // second line |
| 2397 | // b |
| 2398 | // }; |
| 2399 | // |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 2400 | // The second line comment doesn't continue the first in these cases: |
Krasimir Georgiev | b6ccd38 | 2017-02-02 14:36:50 +0000 | [diff] [blame] | 2401 | // |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 2402 | // // first line |
| 2403 | // // second line |
Krasimir Georgiev | b6ccd38 | 2017-02-02 14:36:50 +0000 | [diff] [blame] | 2404 | // |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 2405 | // and: |
Krasimir Georgiev | b6ccd38 | 2017-02-02 14:36:50 +0000 | [diff] [blame] | 2406 | // |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 2407 | // int i; // first line |
| 2408 | // // second line |
Krasimir Georgiev | b6ccd38 | 2017-02-02 14:36:50 +0000 | [diff] [blame] | 2409 | // |
Krasimir Georgiev | 8432161 | 2017-01-30 19:18:55 +0000 | [diff] [blame] | 2410 | // and: |
Krasimir Georgiev | b6ccd38 | 2017-02-02 14:36:50 +0000 | [diff] [blame] | 2411 | // |
Krasimir Georgiev | 8432161 | 2017-01-30 19:18:55 +0000 | [diff] [blame] | 2412 | // do { // first line |
| 2413 | // // second line |
| 2414 | // int i; |
| 2415 | // } while (true); |
Krasimir Georgiev | b6ccd38 | 2017-02-02 14:36:50 +0000 | [diff] [blame] | 2416 | // |
| 2417 | // and: |
| 2418 | // |
| 2419 | // enum { |
| 2420 | // a, // first line |
| 2421 | // // second line |
| 2422 | // }; |
Krasimir Georgiev | 8432161 | 2017-01-30 19:18:55 +0000 | [diff] [blame] | 2423 | const FormatToken *MinColumnToken = Line.Tokens.front().Tok; |
| 2424 | |
| 2425 | // Scan for '{//'. If found, use the column of '{' as a min column for line |
| 2426 | // comment section continuation. |
| 2427 | const FormatToken *PreviousToken = nullptr; |
Krasimir Georgiev | d86c25d | 2017-03-10 13:09:29 +0000 | [diff] [blame] | 2428 | for (const UnwrappedLineNode &Node : Line.Tokens) { |
Krasimir Georgiev | 8432161 | 2017-01-30 19:18:55 +0000 | [diff] [blame] | 2429 | if (PreviousToken && PreviousToken->is(tok::l_brace) && |
| 2430 | isLineComment(*Node.Tok)) { |
| 2431 | MinColumnToken = PreviousToken; |
| 2432 | break; |
| 2433 | } |
| 2434 | PreviousToken = Node.Tok; |
Krasimir Georgiev | b6ccd38 | 2017-02-02 14:36:50 +0000 | [diff] [blame] | 2435 | |
| 2436 | // Grab the last newline preceding a token in this unwrapped line. |
| 2437 | if (Node.Tok->NewlinesBefore > 0) { |
| 2438 | MinColumnToken = Node.Tok; |
| 2439 | } |
Krasimir Georgiev | 8432161 | 2017-01-30 19:18:55 +0000 | [diff] [blame] | 2440 | } |
| 2441 | if (PreviousToken && PreviousToken->is(tok::l_brace)) { |
| 2442 | MinColumnToken = PreviousToken; |
| 2443 | } |
| 2444 | |
Krasimir Georgiev | ea222a7 | 2017-05-22 10:07:56 +0000 | [diff] [blame] | 2445 | return continuesLineComment(FormatTok, /*Previous=*/Line.Tokens.back().Tok, |
| 2446 | MinColumnToken); |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 2447 | } |
| 2448 | |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2449 | void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) { |
| 2450 | bool JustComments = Line->Tokens.empty(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 2451 | for (SmallVectorImpl<FormatToken *>::const_iterator |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2452 | I = CommentsBeforeNextToken.begin(), |
| 2453 | E = CommentsBeforeNextToken.end(); |
| 2454 | I != E; ++I) { |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 2455 | // Line comments that belong to the same line comment section are put on the |
| 2456 | // same line since later we might want to reflow content between them. |
Krasimir Georgiev | 753625b | 2017-01-31 13:32:38 +0000 | [diff] [blame] | 2457 | // Additional fine-grained breaking of line comment sections is controlled |
| 2458 | // by the class BreakableLineCommentSection in case it is desirable to keep |
| 2459 | // several line comment sections in the same unwrapped line. |
| 2460 | // |
| 2461 | // FIXME: Consider putting separate line comment sections as children to the |
| 2462 | // unwrapped line instead. |
Krasimir Georgiev | 00c5c72 | 2017-02-02 15:32:19 +0000 | [diff] [blame] | 2463 | (*I)->ContinuesLineCommentSection = |
Krasimir Georgiev | ea222a7 | 2017-05-22 10:07:56 +0000 | [diff] [blame] | 2464 | continuesLineCommentSection(**I, *Line, CommentPragmasRegex); |
Krasimir Georgiev | b6ccd38 | 2017-02-02 14:36:50 +0000 | [diff] [blame] | 2465 | if (isOnNewLine(**I) && JustComments && !(*I)->ContinuesLineCommentSection) |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2466 | addUnwrappedLine(); |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2467 | pushToken(*I); |
| 2468 | } |
Daniel Jasper | e60cba1 | 2015-05-13 11:35:53 +0000 | [diff] [blame] | 2469 | if (NewlineBeforeNext && JustComments) |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2470 | addUnwrappedLine(); |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2471 | CommentsBeforeNextToken.clear(); |
| 2472 | } |
| 2473 | |
Krasimir Georgiev | 3e05105 | 2017-07-24 14:51:59 +0000 | [diff] [blame] | 2474 | void UnwrappedLineParser::nextToken(int LevelDifference) { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 2475 | if (eof()) |
| 2476 | return; |
Manuel Klimek | 1fcbe67 | 2014-04-11 12:27:47 +0000 | [diff] [blame] | 2477 | flushComments(isOnNewLine(*FormatTok)); |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2478 | pushToken(FormatTok); |
Manuel Klimek | 89628f6 | 2017-09-20 09:51:03 +0000 | [diff] [blame] | 2479 | FormatToken *Previous = FormatTok; |
Daniel Jasper | 1dcbbcfc | 2016-03-14 19:21:36 +0000 | [diff] [blame] | 2480 | if (Style.Language != FormatStyle::LK_JavaScript) |
Krasimir Georgiev | 3e05105 | 2017-07-24 14:51:59 +0000 | [diff] [blame] | 2481 | readToken(LevelDifference); |
Daniel Jasper | 1dcbbcfc | 2016-03-14 19:21:36 +0000 | [diff] [blame] | 2482 | else |
| 2483 | readTokenWithJavaScriptASI(); |
Manuel Klimek | e411aa8 | 2017-09-20 09:29:37 +0000 | [diff] [blame] | 2484 | FormatTok->Previous = Previous; |
Daniel Jasper | b9a4990 | 2016-01-09 15:56:28 +0000 | [diff] [blame] | 2485 | } |
| 2486 | |
Krasimir Georgiev | f62f958 | 2017-02-08 10:30:44 +0000 | [diff] [blame] | 2487 | void UnwrappedLineParser::distributeComments( |
| 2488 | const SmallVectorImpl<FormatToken *> &Comments, |
| 2489 | const FormatToken *NextTok) { |
| 2490 | // Whether or not a line comment token continues a line is controlled by |
Krasimir Georgiev | ea222a7 | 2017-05-22 10:07:56 +0000 | [diff] [blame] | 2491 | // the method continuesLineCommentSection, with the following caveat: |
Krasimir Georgiev | f62f958 | 2017-02-08 10:30:44 +0000 | [diff] [blame] | 2492 | // |
| 2493 | // Define a trail of Comments to be a nonempty proper postfix of Comments such |
| 2494 | // that each comment line from the trail is aligned with the next token, if |
| 2495 | // the next token exists. If a trail exists, the beginning of the maximal |
| 2496 | // trail is marked as a start of a new comment section. |
| 2497 | // |
| 2498 | // For example in this code: |
| 2499 | // |
| 2500 | // int a; // line about a |
| 2501 | // // line 1 about b |
| 2502 | // // line 2 about b |
| 2503 | // int b; |
| 2504 | // |
| 2505 | // the two lines about b form a maximal trail, so there are two sections, the |
| 2506 | // first one consisting of the single comment "// line about a" and the |
| 2507 | // second one consisting of the next two comments. |
| 2508 | if (Comments.empty()) |
| 2509 | return; |
| 2510 | bool ShouldPushCommentsInCurrentLine = true; |
| 2511 | bool HasTrailAlignedWithNextToken = false; |
| 2512 | unsigned StartOfTrailAlignedWithNextToken = 0; |
| 2513 | if (NextTok) { |
| 2514 | // We are skipping the first element intentionally. |
| 2515 | for (unsigned i = Comments.size() - 1; i > 0; --i) { |
| 2516 | if (Comments[i]->OriginalColumn == NextTok->OriginalColumn) { |
| 2517 | HasTrailAlignedWithNextToken = true; |
| 2518 | StartOfTrailAlignedWithNextToken = i; |
| 2519 | } |
| 2520 | } |
| 2521 | } |
| 2522 | for (unsigned i = 0, e = Comments.size(); i < e; ++i) { |
| 2523 | FormatToken *FormatTok = Comments[i]; |
Manuel Klimek | 89628f6 | 2017-09-20 09:51:03 +0000 | [diff] [blame] | 2524 | if (HasTrailAlignedWithNextToken && i == StartOfTrailAlignedWithNextToken) { |
Krasimir Georgiev | f62f958 | 2017-02-08 10:30:44 +0000 | [diff] [blame] | 2525 | FormatTok->ContinuesLineCommentSection = false; |
| 2526 | } else { |
| 2527 | FormatTok->ContinuesLineCommentSection = |
Krasimir Georgiev | ea222a7 | 2017-05-22 10:07:56 +0000 | [diff] [blame] | 2528 | continuesLineCommentSection(*FormatTok, *Line, CommentPragmasRegex); |
Krasimir Georgiev | f62f958 | 2017-02-08 10:30:44 +0000 | [diff] [blame] | 2529 | } |
| 2530 | if (!FormatTok->ContinuesLineCommentSection && |
| 2531 | (isOnNewLine(*FormatTok) || FormatTok->IsFirst)) { |
| 2532 | ShouldPushCommentsInCurrentLine = false; |
| 2533 | } |
| 2534 | if (ShouldPushCommentsInCurrentLine) { |
| 2535 | pushToken(FormatTok); |
| 2536 | } else { |
| 2537 | CommentsBeforeNextToken.push_back(FormatTok); |
| 2538 | } |
| 2539 | } |
| 2540 | } |
| 2541 | |
Krasimir Georgiev | 3e05105 | 2017-07-24 14:51:59 +0000 | [diff] [blame] | 2542 | void UnwrappedLineParser::readToken(int LevelDifference) { |
Krasimir Georgiev | f62f958 | 2017-02-08 10:30:44 +0000 | [diff] [blame] | 2543 | SmallVector<FormatToken *, 1> Comments; |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2544 | do { |
| 2545 | FormatTok = Tokens->getNextToken(); |
Alexander Kornienko | c2ee9cf | 2014-03-13 13:59:48 +0000 | [diff] [blame] | 2546 | assert(FormatTok); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 2547 | while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) && |
| 2548 | (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) { |
Krasimir Georgiev | f62f958 | 2017-02-08 10:30:44 +0000 | [diff] [blame] | 2549 | distributeComments(Comments, FormatTok); |
| 2550 | Comments.clear(); |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2551 | // If there is an unfinished unwrapped line, we flush the preprocessor |
| 2552 | // directives only after that unwrapped line was finished later. |
Daniel Jasper | 29d39d5 | 2015-02-08 09:34:49 +0000 | [diff] [blame] | 2553 | bool SwitchToPreprocessorLines = !Line->Tokens.empty(); |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2554 | ScopedLineState BlockState(*this, SwitchToPreprocessorLines); |
Krasimir Georgiev | 3e05105 | 2017-07-24 14:51:59 +0000 | [diff] [blame] | 2555 | assert((LevelDifference >= 0 || |
| 2556 | static_cast<unsigned>(-LevelDifference) <= Line->Level) && |
| 2557 | "LevelDifference makes Line->Level negative"); |
| 2558 | Line->Level += LevelDifference; |
Alexander Kornienko | b1be9d6 | 2013-04-03 12:38:53 +0000 | [diff] [blame] | 2559 | // Comments stored before the preprocessor directive need to be output |
| 2560 | // before the preprocessor directive, at the same level as the |
| 2561 | // preprocessor directive, as we consider them to apply to the directive. |
Manuel Klimek | 1fcbe67 | 2014-04-11 12:27:47 +0000 | [diff] [blame] | 2562 | flushComments(isOnNewLine(*FormatTok)); |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2563 | parsePPDirective(); |
| 2564 | } |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 2565 | while (FormatTok->Type == TT_ConflictStart || |
| 2566 | FormatTok->Type == TT_ConflictEnd || |
| 2567 | FormatTok->Type == TT_ConflictAlternative) { |
| 2568 | if (FormatTok->Type == TT_ConflictStart) { |
| 2569 | conditionalCompilationStart(/*Unreachable=*/false); |
| 2570 | } else if (FormatTok->Type == TT_ConflictAlternative) { |
| 2571 | conditionalCompilationAlternative(); |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 2572 | } else if (FormatTok->Type == TT_ConflictEnd) { |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 2573 | conditionalCompilationEnd(); |
| 2574 | } |
| 2575 | FormatTok = Tokens->getNextToken(); |
| 2576 | FormatTok->MustBreakBefore = true; |
| 2577 | } |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 2578 | |
Francois Ferrand | a98a95c | 2017-07-28 07:56:14 +0000 | [diff] [blame] | 2579 | if (!PPStack.empty() && (PPStack.back().Kind == PP_Unreachable) && |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 2580 | !Line->InPPDirective) { |
| 2581 | continue; |
| 2582 | } |
| 2583 | |
Krasimir Georgiev | f62f958 | 2017-02-08 10:30:44 +0000 | [diff] [blame] | 2584 | if (!FormatTok->Tok.is(tok::comment)) { |
| 2585 | distributeComments(Comments, FormatTok); |
| 2586 | Comments.clear(); |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2587 | return; |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2588 | } |
Krasimir Georgiev | f62f958 | 2017-02-08 10:30:44 +0000 | [diff] [blame] | 2589 | |
| 2590 | Comments.push_back(FormatTok); |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2591 | } while (!eof()); |
Krasimir Georgiev | f62f958 | 2017-02-08 10:30:44 +0000 | [diff] [blame] | 2592 | |
| 2593 | distributeComments(Comments, nullptr); |
| 2594 | Comments.clear(); |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2595 | } |
| 2596 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 2597 | void UnwrappedLineParser::pushToken(FormatToken *Tok) { |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 2598 | Line->Tokens.push_back(UnwrappedLineNode(Tok)); |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2599 | if (MustBreakBeforeNextToken) { |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 2600 | Line->Tokens.back().Tok->MustBreakBefore = true; |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2601 | MustBreakBeforeNextToken = false; |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 2602 | } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 2603 | } |
| 2604 | |
Daniel Jasper | 8d1832e | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 2605 | } // end namespace format |
| 2606 | } // end namespace clang |