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