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