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