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