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