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: |
| 28 | virtual ~FormatTokenSource() {} |
| 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 | |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 58 | class ScopedMacroState : public FormatTokenSource { |
| 59 | public: |
| 60 | ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource, |
Manuel Klimek | 20e0af6 | 2015-05-06 11:56:29 +0000 | [diff] [blame] | 61 | FormatToken *&ResetToken) |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 62 | : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken), |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 63 | PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource), |
Manuel Klimek | 20e0af6 | 2015-05-06 11:56:29 +0000 | [diff] [blame] | 64 | Token(nullptr) { |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 65 | TokenSource = this; |
Manuel Klimek | ef2cfb1 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 66 | Line.Level = 0; |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 67 | Line.InPPDirective = true; |
| 68 | } |
| 69 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 70 | ~ScopedMacroState() override { |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 71 | TokenSource = PreviousTokenSource; |
| 72 | ResetToken = Token; |
| 73 | Line.InPPDirective = false; |
Manuel Klimek | ef2cfb1 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 74 | Line.Level = PreviousLineLevel; |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 77 | FormatToken *getNextToken() override { |
Manuel Klimek | 7872571 | 2013-01-07 10:03:37 +0000 | [diff] [blame] | 78 | // The \c UnwrappedLineParser guards against this by never calling |
| 79 | // \c getNextToken() after it has encountered the first eof token. |
| 80 | assert(!eof()); |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 81 | Token = PreviousTokenSource->getNextToken(); |
| 82 | if (eof()) |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 83 | return getFakeEOF(); |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 84 | return Token; |
| 85 | } |
| 86 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 87 | unsigned getPosition() override { return PreviousTokenSource->getPosition(); } |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 88 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 89 | FormatToken *setPosition(unsigned Position) override { |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 90 | Token = PreviousTokenSource->setPosition(Position); |
| 91 | return Token; |
| 92 | } |
| 93 | |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 94 | private: |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 95 | bool eof() { return Token && Token->HasUnescapedNewline; } |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 96 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 97 | FormatToken *getFakeEOF() { |
| 98 | static bool EOFInitialized = false; |
| 99 | static FormatToken FormatTok; |
| 100 | if (!EOFInitialized) { |
| 101 | FormatTok.Tok.startToken(); |
| 102 | FormatTok.Tok.setKind(tok::eof); |
| 103 | EOFInitialized = true; |
| 104 | } |
| 105 | return &FormatTok; |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | UnwrappedLine &Line; |
| 109 | FormatTokenSource *&TokenSource; |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 110 | FormatToken *&ResetToken; |
Manuel Klimek | ef2cfb1 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 111 | unsigned PreviousLineLevel; |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 112 | FormatTokenSource *PreviousTokenSource; |
| 113 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 114 | FormatToken *Token; |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 115 | }; |
| 116 | |
Craig Topper | 69665e1 | 2013-07-01 04:21:54 +0000 | [diff] [blame] | 117 | } // end anonymous namespace |
| 118 | |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 119 | class ScopedLineState { |
| 120 | public: |
Manuel Klimek | d3b92fa | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 121 | ScopedLineState(UnwrappedLineParser &Parser, |
| 122 | bool SwitchToPreprocessorLines = false) |
David Blaikie | efb6eb2 | 2014-08-09 20:02:07 +0000 | [diff] [blame] | 123 | : Parser(Parser), OriginalLines(Parser.CurrentLines) { |
Manuel Klimek | d3b92fa | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 124 | if (SwitchToPreprocessorLines) |
| 125 | Parser.CurrentLines = &Parser.PreprocessorDirectives; |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 126 | else if (!Parser.Line->Tokens.empty()) |
| 127 | Parser.CurrentLines = &Parser.Line->Tokens.back().Children; |
David Blaikie | efb6eb2 | 2014-08-09 20:02:07 +0000 | [diff] [blame] | 128 | PreBlockLine = std::move(Parser.Line); |
| 129 | Parser.Line = llvm::make_unique<UnwrappedLine>(); |
Daniel Jasper | daffc0d | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 130 | Parser.Line->Level = PreBlockLine->Level; |
| 131 | Parser.Line->InPPDirective = PreBlockLine->InPPDirective; |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | ~ScopedLineState() { |
Daniel Jasper | daffc0d | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 135 | if (!Parser.Line->Tokens.empty()) { |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 136 | Parser.addUnwrappedLine(); |
| 137 | } |
Daniel Jasper | daffc0d | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 138 | assert(Parser.Line->Tokens.empty()); |
David Blaikie | efb6eb2 | 2014-08-09 20:02:07 +0000 | [diff] [blame] | 139 | Parser.Line = std::move(PreBlockLine); |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 140 | if (Parser.CurrentLines == &Parser.PreprocessorDirectives) |
| 141 | Parser.MustBreakBeforeNextToken = true; |
| 142 | Parser.CurrentLines = OriginalLines; |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | private: |
| 146 | UnwrappedLineParser &Parser; |
| 147 | |
David Blaikie | efb6eb2 | 2014-08-09 20:02:07 +0000 | [diff] [blame] | 148 | std::unique_ptr<UnwrappedLine> PreBlockLine; |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 149 | SmallVectorImpl<UnwrappedLine> *OriginalLines; |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 150 | }; |
| 151 | |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 152 | class CompoundStatementIndenter { |
| 153 | public: |
| 154 | CompoundStatementIndenter(UnwrappedLineParser *Parser, |
| 155 | const FormatStyle &Style, unsigned &LineLevel) |
| 156 | : LineLevel(LineLevel), OldLineLevel(LineLevel) { |
| 157 | if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) { |
| 158 | Parser->addUnwrappedLine(); |
| 159 | } else if (Style.BreakBeforeBraces == FormatStyle::BS_GNU) { |
| 160 | Parser->addUnwrappedLine(); |
| 161 | ++LineLevel; |
| 162 | } |
| 163 | } |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 164 | ~CompoundStatementIndenter() { LineLevel = OldLineLevel; } |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 165 | |
| 166 | private: |
| 167 | unsigned &LineLevel; |
| 168 | unsigned OldLineLevel; |
| 169 | }; |
| 170 | |
Craig Topper | 69665e1 | 2013-07-01 04:21:54 +0000 | [diff] [blame] | 171 | namespace { |
| 172 | |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 173 | class IndexedTokenSource : public FormatTokenSource { |
| 174 | public: |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 175 | IndexedTokenSource(ArrayRef<FormatToken *> Tokens) |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 176 | : Tokens(Tokens), Position(-1) {} |
| 177 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 178 | FormatToken *getNextToken() override { |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 179 | ++Position; |
| 180 | return Tokens[Position]; |
| 181 | } |
| 182 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 183 | unsigned getPosition() override { |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 184 | assert(Position >= 0); |
| 185 | return Position; |
| 186 | } |
| 187 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 188 | FormatToken *setPosition(unsigned P) override { |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 189 | Position = P; |
| 190 | return Tokens[Position]; |
| 191 | } |
| 192 | |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 193 | void reset() { Position = -1; } |
| 194 | |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 195 | private: |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 196 | ArrayRef<FormatToken *> Tokens; |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 197 | int Position; |
| 198 | }; |
| 199 | |
Craig Topper | 69665e1 | 2013-07-01 04:21:54 +0000 | [diff] [blame] | 200 | } // end anonymous namespace |
| 201 | |
Daniel Jasper | d2ae41a | 2013-05-15 08:14:19 +0000 | [diff] [blame] | 202 | UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style, |
Daniel Jasper | d0ec0d6 | 2014-11-04 12:41:02 +0000 | [diff] [blame] | 203 | const AdditionalKeywords &Keywords, |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 204 | ArrayRef<FormatToken *> Tokens, |
Daniel Jasper | d2ae41a | 2013-05-15 08:14:19 +0000 | [diff] [blame] | 205 | UnwrappedLineConsumer &Callback) |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 206 | : Line(new UnwrappedLine), MustBreakBeforeNextToken(false), |
Manuel Klimek | 20e0af6 | 2015-05-06 11:56:29 +0000 | [diff] [blame] | 207 | CurrentLines(&Lines), Style(Style), Keywords(Keywords), Tokens(nullptr), |
| 208 | Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1) {} |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 209 | |
| 210 | void UnwrappedLineParser::reset() { |
| 211 | PPBranchLevel = -1; |
| 212 | Line.reset(new UnwrappedLine); |
| 213 | CommentsBeforeNextToken.clear(); |
Craig Topper | 2145bc0 | 2014-05-09 08:15:10 +0000 | [diff] [blame] | 214 | FormatTok = nullptr; |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 215 | MustBreakBeforeNextToken = false; |
| 216 | PreprocessorDirectives.clear(); |
| 217 | CurrentLines = &Lines; |
| 218 | DeclarationScopeStack.clear(); |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 219 | PPStack.clear(); |
| 220 | } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 221 | |
Manuel Klimek | 20e0af6 | 2015-05-06 11:56:29 +0000 | [diff] [blame] | 222 | void UnwrappedLineParser::parse() { |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 223 | IndexedTokenSource TokenSource(AllTokens); |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 224 | do { |
| 225 | DEBUG(llvm::dbgs() << "----\n"); |
| 226 | reset(); |
| 227 | Tokens = &TokenSource; |
| 228 | TokenSource.reset(); |
Daniel Jasper | a79064a | 2013-03-01 18:11:39 +0000 | [diff] [blame] | 229 | |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 230 | readToken(); |
| 231 | parseFile(); |
| 232 | // Create line with eof token. |
| 233 | pushToken(FormatTok); |
| 234 | addUnwrappedLine(); |
| 235 | |
| 236 | for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(), |
| 237 | E = Lines.end(); |
| 238 | I != E; ++I) { |
| 239 | Callback.consumeUnwrappedLine(*I); |
| 240 | } |
| 241 | Callback.finishRun(); |
| 242 | Lines.clear(); |
| 243 | while (!PPLevelBranchIndex.empty() && |
Daniel Jasper | 53bd167 | 2013-10-12 13:32:56 +0000 | [diff] [blame] | 244 | PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) { |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 245 | PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1); |
| 246 | PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1); |
| 247 | } |
| 248 | if (!PPLevelBranchIndex.empty()) { |
| 249 | ++PPLevelBranchIndex.back(); |
| 250 | assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size()); |
| 251 | assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back()); |
| 252 | } |
| 253 | } while (!PPLevelBranchIndex.empty()); |
| 254 | |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 257 | void UnwrappedLineParser::parseFile() { |
Daniel Jasper | 9326f91 | 2015-05-05 08:40:32 +0000 | [diff] [blame] | 258 | // The top-level context in a file always has declarations, except for pre- |
| 259 | // processor directives and JavaScript files. |
| 260 | bool MustBeDeclaration = |
| 261 | !Line->InPPDirective && Style.Language != FormatStyle::LK_JavaScript; |
| 262 | ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, |
| 263 | MustBeDeclaration); |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 264 | parseLevel(/*HasOpeningBrace=*/false); |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 265 | // Make sure to format the remaining tokens. |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 266 | flushComments(true); |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 267 | addUnwrappedLine(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 270 | void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) { |
Daniel Jasper | 516d797 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 271 | bool SwitchLabelEncountered = false; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 272 | do { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 273 | switch (FormatTok->Tok.getKind()) { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 274 | case tok::comment: |
Daniel Jasper | e25509f | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 275 | nextToken(); |
| 276 | addUnwrappedLine(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 277 | break; |
| 278 | case tok::l_brace: |
Manuel Klimek | 0a3a3c9 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 279 | // FIXME: Add parameter whether this can happen - if this happens, we must |
| 280 | // be in a non-declaration context. |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 281 | parseBlock(/*MustBeDeclaration=*/false); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 282 | addUnwrappedLine(); |
| 283 | break; |
| 284 | case tok::r_brace: |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 285 | if (HasOpeningBrace) |
| 286 | return; |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 287 | nextToken(); |
| 288 | addUnwrappedLine(); |
Manuel Klimek | 1058d98 | 2013-01-06 20:07:31 +0000 | [diff] [blame] | 289 | break; |
Daniel Jasper | 516d797 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 290 | case tok::kw_default: |
| 291 | case tok::kw_case: |
Daniel Jasper | 7240762 | 2013-09-02 08:26:29 +0000 | [diff] [blame] | 292 | if (!SwitchLabelEncountered && |
| 293 | (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1))) |
| 294 | ++Line->Level; |
Daniel Jasper | 516d797 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 295 | SwitchLabelEncountered = true; |
| 296 | parseStructuralElement(); |
| 297 | break; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 298 | default: |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 299 | parseStructuralElement(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 300 | break; |
| 301 | } |
| 302 | } while (!eof()); |
| 303 | } |
| 304 | |
Daniel Jasper | adba2aa | 2015-05-18 12:52:00 +0000 | [diff] [blame] | 305 | void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) { |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 306 | // We'll parse forward through the tokens until we hit |
| 307 | // a closing brace or eof - note that getNextToken() will |
| 308 | // parse macros, so this will magically work inside macro |
| 309 | // definitions, too. |
| 310 | unsigned StoredPosition = Tokens->getPosition(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 311 | FormatToken *Tok = FormatTok; |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 312 | // Keep a stack of positions of lbrace tokens. We will |
| 313 | // update information about whether an lbrace starts a |
| 314 | // braced init list or a different block during the loop. |
Daniel Jasper | b1f74a8 | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 315 | SmallVector<FormatToken *, 8> LBraceStack; |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 316 | assert(Tok->Tok.is(tok::l_brace)); |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 317 | do { |
Daniel Jasper | 7f5d53e | 2013-07-01 09:15:46 +0000 | [diff] [blame] | 318 | // Get next none-comment token. |
| 319 | FormatToken *NextTok; |
Daniel Jasper | ca7bd72 | 2013-07-01 16:43:38 +0000 | [diff] [blame] | 320 | unsigned ReadTokens = 0; |
Daniel Jasper | 7f5d53e | 2013-07-01 09:15:46 +0000 | [diff] [blame] | 321 | do { |
| 322 | NextTok = Tokens->getNextToken(); |
Daniel Jasper | ca7bd72 | 2013-07-01 16:43:38 +0000 | [diff] [blame] | 323 | ++ReadTokens; |
Daniel Jasper | 7f5d53e | 2013-07-01 09:15:46 +0000 | [diff] [blame] | 324 | } while (NextTok->is(tok::comment)); |
| 325 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 326 | switch (Tok->Tok.getKind()) { |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 327 | case tok::l_brace: |
Daniel Jasper | 3c883d1 | 2015-05-18 14:49:19 +0000 | [diff] [blame] | 328 | Tok->BlockKind = BK_Unknown; |
Daniel Jasper | b1f74a8 | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 329 | LBraceStack.push_back(Tok); |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 330 | break; |
| 331 | case tok::r_brace: |
| 332 | if (!LBraceStack.empty()) { |
Daniel Jasper | b1f74a8 | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 333 | if (LBraceStack.back()->BlockKind == BK_Unknown) { |
Daniel Jasper | 220c0d1 | 2014-04-10 07:27:12 +0000 | [diff] [blame] | 334 | bool ProbablyBracedList = false; |
| 335 | if (Style.Language == FormatStyle::LK_Proto) { |
| 336 | ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square); |
| 337 | } else { |
Daniel Jasper | 91b032a | 2014-05-22 12:46:38 +0000 | [diff] [blame] | 338 | // Using OriginalColumn to distinguish between ObjC methods and |
| 339 | // binary operators is a bit hacky. |
| 340 | bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) && |
| 341 | NextTok->OriginalColumn == 0; |
| 342 | |
Daniel Jasper | 220c0d1 | 2014-04-10 07:27:12 +0000 | [diff] [blame] | 343 | // If there is a comma, semicolon or right paren after the closing |
| 344 | // brace, we assume this is a braced initializer list. Note that |
| 345 | // regardless how we mark inner braces here, we will overwrite the |
| 346 | // BlockKind later if we parse a braced list (where all blocks |
| 347 | // inside are by default braced lists), or when we explicitly detect |
| 348 | // blocks (for example while parsing lambdas). |
| 349 | // |
| 350 | // We exclude + and - as they can be ObjC visibility modifiers. |
| 351 | ProbablyBracedList = |
Daniel Jasper | adba2aa | 2015-05-18 12:52:00 +0000 | [diff] [blame] | 352 | NextTok->isOneOf(tok::comma, tok::period, tok::colon, |
Daniel Jasper | 438059e | 2014-05-22 12:11:13 +0000 | [diff] [blame] | 353 | tok::r_paren, tok::r_square, tok::l_brace, |
Daniel Jasper | 65df5aa | 2014-08-04 14:51:02 +0000 | [diff] [blame] | 354 | tok::l_paren, tok::ellipsis) || |
Daniel Jasper | cec9ffd | 2015-05-18 14:12:24 +0000 | [diff] [blame] | 355 | (NextTok->is(tok::semi) && |
| 356 | (!ExpectClassBody || LBraceStack.size() != 1)) || |
Daniel Jasper | 91b032a | 2014-05-22 12:46:38 +0000 | [diff] [blame] | 357 | (NextTok->isBinaryOperator() && !NextIsObjCMethod); |
Daniel Jasper | 220c0d1 | 2014-04-10 07:27:12 +0000 | [diff] [blame] | 358 | } |
| 359 | if (ProbablyBracedList) { |
Daniel Jasper | b1f74a8 | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 360 | Tok->BlockKind = BK_BracedInit; |
| 361 | LBraceStack.back()->BlockKind = BK_BracedInit; |
| 362 | } else { |
| 363 | Tok->BlockKind = BK_Block; |
| 364 | LBraceStack.back()->BlockKind = BK_Block; |
| 365 | } |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 366 | } |
| 367 | LBraceStack.pop_back(); |
| 368 | } |
| 369 | break; |
Daniel Jasper | ac7e34e | 2014-03-13 10:11:17 +0000 | [diff] [blame] | 370 | case tok::at: |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 371 | case tok::semi: |
| 372 | case tok::kw_if: |
| 373 | case tok::kw_while: |
| 374 | case tok::kw_for: |
| 375 | case tok::kw_switch: |
| 376 | case tok::kw_try: |
Nico Weber | fac2371 | 2015-02-04 15:26:27 +0000 | [diff] [blame] | 377 | case tok::kw___try: |
Daniel Jasper | 393564f | 2013-05-31 14:56:29 +0000 | [diff] [blame] | 378 | if (!LBraceStack.empty()) |
Daniel Jasper | b1f74a8 | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 379 | LBraceStack.back()->BlockKind = BK_Block; |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 380 | break; |
| 381 | default: |
| 382 | break; |
| 383 | } |
| 384 | Tok = NextTok; |
Manuel Klimek | bab25fd | 2013-09-04 08:20:47 +0000 | [diff] [blame] | 385 | } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty()); |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 386 | // Assume other blocks for all unclosed opening braces. |
| 387 | for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) { |
Daniel Jasper | b1f74a8 | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 388 | if (LBraceStack[i]->BlockKind == BK_Unknown) |
| 389 | LBraceStack[i]->BlockKind = BK_Block; |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 390 | } |
Manuel Klimek | bab25fd | 2013-09-04 08:20:47 +0000 | [diff] [blame] | 391 | |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 392 | FormatTok = Tokens->setPosition(StoredPosition); |
| 393 | } |
| 394 | |
Manuel Klimek | b212f3b | 2013-10-12 22:46:56 +0000 | [diff] [blame] | 395 | void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel, |
| 396 | bool MunchSemi) { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 397 | assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected"); |
Daniel Jasper | 516d797 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 398 | unsigned InitialLevel = Line->Level; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 399 | nextToken(); |
| 400 | |
Manuel Klimek | a4fe1c1 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 401 | addUnwrappedLine(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 402 | |
Manuel Klimek | 0a3a3c9 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 403 | ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, |
| 404 | MustBeDeclaration); |
Daniel Jasper | 65ee347 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 405 | if (AddLevel) |
| 406 | ++Line->Level; |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 407 | parseLevel(/*HasOpeningBrace=*/true); |
Alexander Kornienko | 578fdd8 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 408 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 409 | if (!FormatTok->Tok.is(tok::r_brace)) { |
Daniel Jasper | 516d797 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 410 | Line->Level = InitialLevel; |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 411 | return; |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 412 | } |
Alexander Kornienko | 0ea8e10 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 413 | |
Daniel Jasper | d1ae358 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 414 | nextToken(); // Munch the closing brace. |
Manuel Klimek | b212f3b | 2013-10-12 22:46:56 +0000 | [diff] [blame] | 415 | if (MunchSemi && FormatTok->Tok.is(tok::semi)) |
| 416 | nextToken(); |
Daniel Jasper | 516d797 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 417 | Line->Level = InitialLevel; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Daniel Jasper | 02c7bca | 2015-03-30 09:56:50 +0000 | [diff] [blame] | 420 | static bool isGoogScope(const UnwrappedLine &Line) { |
Daniel Jasper | 616de864 | 2014-11-23 16:46:28 +0000 | [diff] [blame] | 421 | // FIXME: Closure-library specific stuff should not be hard-coded but be |
| 422 | // configurable. |
Daniel Jasper | 4a39c84 | 2014-05-06 13:54:10 +0000 | [diff] [blame] | 423 | if (Line.Tokens.size() < 4) |
| 424 | return false; |
| 425 | auto I = Line.Tokens.begin(); |
| 426 | if (I->Tok->TokenText != "goog") |
| 427 | return false; |
| 428 | ++I; |
| 429 | if (I->Tok->isNot(tok::period)) |
| 430 | return false; |
| 431 | ++I; |
| 432 | if (I->Tok->TokenText != "scope") |
| 433 | return false; |
| 434 | ++I; |
| 435 | return I->Tok->is(tok::l_paren); |
| 436 | } |
| 437 | |
Roman Kashitsyn | a043ced | 2014-08-11 12:18:01 +0000 | [diff] [blame] | 438 | static bool ShouldBreakBeforeBrace(const FormatStyle &Style, |
| 439 | const FormatToken &InitialToken) { |
| 440 | switch (Style.BreakBeforeBraces) { |
| 441 | case FormatStyle::BS_Linux: |
| 442 | return InitialToken.isOneOf(tok::kw_namespace, tok::kw_class); |
| 443 | case FormatStyle::BS_Allman: |
| 444 | case FormatStyle::BS_GNU: |
| 445 | return true; |
| 446 | default: |
| 447 | return false; |
| 448 | } |
| 449 | } |
| 450 | |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 451 | void UnwrappedLineParser::parseChildBlock() { |
| 452 | FormatTok->BlockKind = BK_Block; |
| 453 | nextToken(); |
| 454 | { |
Daniel Jasper | 4a39c84 | 2014-05-06 13:54:10 +0000 | [diff] [blame] | 455 | bool GoogScope = |
Daniel Jasper | 02c7bca | 2015-03-30 09:56:50 +0000 | [diff] [blame] | 456 | Style.Language == FormatStyle::LK_JavaScript && isGoogScope(*Line); |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 457 | ScopedLineState LineState(*this); |
| 458 | ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, |
| 459 | /*MustBeDeclaration=*/false); |
Daniel Jasper | 4a39c84 | 2014-05-06 13:54:10 +0000 | [diff] [blame] | 460 | Line->Level += GoogScope ? 0 : 1; |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 461 | parseLevel(/*HasOpeningBrace=*/true); |
Daniel Jasper | 02c7bca | 2015-03-30 09:56:50 +0000 | [diff] [blame] | 462 | flushComments(isOnNewLine(*FormatTok)); |
Daniel Jasper | 4a39c84 | 2014-05-06 13:54:10 +0000 | [diff] [blame] | 463 | Line->Level -= GoogScope ? 0 : 1; |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 464 | } |
| 465 | nextToken(); |
| 466 | } |
| 467 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 468 | void UnwrappedLineParser::parsePPDirective() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 469 | assert(FormatTok->Tok.is(tok::hash) && "'#' expected"); |
Manuel Klimek | 20e0af6 | 2015-05-06 11:56:29 +0000 | [diff] [blame] | 470 | ScopedMacroState MacroState(*Line, Tokens, FormatTok); |
Manuel Klimek | a71e5d8 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 471 | nextToken(); |
| 472 | |
Craig Topper | 2145bc0 | 2014-05-09 08:15:10 +0000 | [diff] [blame] | 473 | if (!FormatTok->Tok.getIdentifierInfo()) { |
Manuel Klimek | 591b580 | 2013-01-31 15:58:48 +0000 | [diff] [blame] | 474 | parsePPUnknown(); |
Manuel Klimek | a71e5d8 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 475 | return; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 476 | } |
Manuel Klimek | a71e5d8 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 477 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 478 | switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) { |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 479 | case tok::pp_define: |
| 480 | parsePPDefine(); |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 481 | return; |
| 482 | case tok::pp_if: |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 483 | parsePPIf(/*IfDef=*/false); |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 484 | break; |
| 485 | case tok::pp_ifdef: |
| 486 | case tok::pp_ifndef: |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 487 | parsePPIf(/*IfDef=*/true); |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 488 | break; |
| 489 | case tok::pp_else: |
| 490 | parsePPElse(); |
| 491 | break; |
| 492 | case tok::pp_elif: |
| 493 | parsePPElIf(); |
| 494 | break; |
| 495 | case tok::pp_endif: |
| 496 | parsePPEndIf(); |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 497 | break; |
| 498 | default: |
| 499 | parsePPUnknown(); |
| 500 | break; |
| 501 | } |
| 502 | } |
| 503 | |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 504 | void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) { |
| 505 | if (Unreachable || (!PPStack.empty() && PPStack.back() == PP_Unreachable)) |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 506 | PPStack.push_back(PP_Unreachable); |
| 507 | else |
| 508 | PPStack.push_back(PP_Conditional); |
| 509 | } |
| 510 | |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 511 | void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) { |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 512 | ++PPBranchLevel; |
| 513 | assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size()); |
| 514 | if (PPBranchLevel == (int)PPLevelBranchIndex.size()) { |
| 515 | PPLevelBranchIndex.push_back(0); |
| 516 | PPLevelBranchCount.push_back(0); |
| 517 | } |
| 518 | PPChainBranchIndex.push(0); |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 519 | bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0; |
| 520 | conditionalCompilationCondition(Unreachable || Skip); |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 523 | void UnwrappedLineParser::conditionalCompilationAlternative() { |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 524 | if (!PPStack.empty()) |
| 525 | PPStack.pop_back(); |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 526 | assert(PPBranchLevel < (int)PPLevelBranchIndex.size()); |
| 527 | if (!PPChainBranchIndex.empty()) |
| 528 | ++PPChainBranchIndex.top(); |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 529 | conditionalCompilationCondition( |
| 530 | PPBranchLevel >= 0 && !PPChainBranchIndex.empty() && |
| 531 | PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top()); |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 534 | void UnwrappedLineParser::conditionalCompilationEnd() { |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 535 | assert(PPBranchLevel < (int)PPLevelBranchIndex.size()); |
| 536 | if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) { |
| 537 | if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) { |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 538 | PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1; |
| 539 | } |
| 540 | } |
Manuel Klimek | 14bd917 | 2014-01-29 08:49:02 +0000 | [diff] [blame] | 541 | // Guard against #endif's without #if. |
| 542 | if (PPBranchLevel > 0) |
| 543 | --PPBranchLevel; |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 544 | if (!PPChainBranchIndex.empty()) |
| 545 | PPChainBranchIndex.pop(); |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 546 | if (!PPStack.empty()) |
| 547 | PPStack.pop_back(); |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | void UnwrappedLineParser::parsePPIf(bool IfDef) { |
| 551 | nextToken(); |
| 552 | bool IsLiteralFalse = (FormatTok->Tok.isLiteral() && |
Daniel Jasper | 9d22bcc | 2015-01-19 10:51:05 +0000 | [diff] [blame] | 553 | FormatTok->Tok.getLiteralData() != nullptr && |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 554 | StringRef(FormatTok->Tok.getLiteralData(), |
| 555 | FormatTok->Tok.getLength()) == "0") || |
| 556 | FormatTok->Tok.is(tok::kw_false); |
| 557 | conditionalCompilationStart(!IfDef && IsLiteralFalse); |
| 558 | parsePPUnknown(); |
| 559 | } |
| 560 | |
| 561 | void UnwrappedLineParser::parsePPElse() { |
| 562 | conditionalCompilationAlternative(); |
| 563 | parsePPUnknown(); |
| 564 | } |
| 565 | |
| 566 | void UnwrappedLineParser::parsePPElIf() { parsePPElse(); } |
| 567 | |
| 568 | void UnwrappedLineParser::parsePPEndIf() { |
| 569 | conditionalCompilationEnd(); |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 570 | parsePPUnknown(); |
| 571 | } |
| 572 | |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 573 | void UnwrappedLineParser::parsePPDefine() { |
| 574 | nextToken(); |
| 575 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 576 | if (FormatTok->Tok.getKind() != tok::identifier) { |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 577 | parsePPUnknown(); |
| 578 | return; |
| 579 | } |
| 580 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 581 | if (FormatTok->Tok.getKind() == tok::l_paren && |
| 582 | FormatTok->WhitespaceRange.getBegin() == |
| 583 | FormatTok->WhitespaceRange.getEnd()) { |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 584 | parseParens(); |
| 585 | } |
| 586 | addUnwrappedLine(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 587 | Line->Level = 1; |
Manuel Klimek | 1b89629 | 2013-01-07 09:34:28 +0000 | [diff] [blame] | 588 | |
| 589 | // Errors during a preprocessor directive can only affect the layout of the |
| 590 | // preprocessor directive, and thus we ignore them. An alternative approach |
| 591 | // would be to use the same approach we use on the file level (no |
| 592 | // re-indentation if there was a structural error) within the macro |
| 593 | // definition. |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 594 | parseFile(); |
| 595 | } |
| 596 | |
| 597 | void UnwrappedLineParser::parsePPUnknown() { |
Manuel Klimek | a71e5d8 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 598 | do { |
Manuel Klimek | a71e5d8 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 599 | nextToken(); |
| 600 | } while (!eof()); |
| 601 | addUnwrappedLine(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Alexander Kornienko | a04e5e2 | 2013-04-09 16:15:19 +0000 | [diff] [blame] | 604 | // Here we blacklist certain tokens that are not usually the first token in an |
| 605 | // unwrapped line. This is used in attempt to distinguish macro calls without |
| 606 | // trailing semicolons from other constructs split to several lines. |
Benjamin Kramer | 8407df7 | 2015-03-09 16:47:52 +0000 | [diff] [blame] | 607 | static bool tokenCanStartNewLine(const clang::Token &Tok) { |
Alexander Kornienko | a04e5e2 | 2013-04-09 16:15:19 +0000 | [diff] [blame] | 608 | // Semicolon can be a null-statement, l_square can be a start of a macro or |
| 609 | // a C++11 attribute, but this doesn't seem to be common. |
| 610 | return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) && |
| 611 | Tok.isNot(tok::l_square) && |
| 612 | // Tokens that can only be used as binary operators and a part of |
| 613 | // overloaded operator names. |
| 614 | Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) && |
| 615 | Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) && |
| 616 | Tok.isNot(tok::less) && Tok.isNot(tok::greater) && |
| 617 | Tok.isNot(tok::slash) && Tok.isNot(tok::percent) && |
| 618 | Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) && |
| 619 | Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) && |
| 620 | Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) && |
| 621 | Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) && |
| 622 | Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) && |
| 623 | Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) && |
| 624 | Tok.isNot(tok::lesslessequal) && |
| 625 | // Colon is used in labels, base class lists, initializer lists, |
| 626 | // range-based for loops, ternary operator, but should never be the |
| 627 | // first token in an unwrapped line. |
Daniel Jasper | 5ebb2f3 | 2014-05-21 13:08:17 +0000 | [diff] [blame] | 628 | Tok.isNot(tok::colon) && |
| 629 | // 'noexcept' is a trailing annotation. |
| 630 | Tok.isNot(tok::kw_noexcept); |
Alexander Kornienko | a04e5e2 | 2013-04-09 16:15:19 +0000 | [diff] [blame] | 631 | } |
| 632 | |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 633 | void UnwrappedLineParser::parseStructuralElement() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 634 | assert(!FormatTok->Tok.is(tok::l_brace)); |
| 635 | switch (FormatTok->Tok.getKind()) { |
Nico Weber | 04e9f1a | 2013-01-07 19:05:19 +0000 | [diff] [blame] | 636 | case tok::at: |
| 637 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 638 | if (FormatTok->Tok.is(tok::l_brace)) { |
Nico Weber | 372d8dc | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 639 | parseBracedList(); |
| 640 | break; |
| 641 | } |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 642 | switch (FormatTok->Tok.getObjCKeywordID()) { |
Nico Weber | 04e9f1a | 2013-01-07 19:05:19 +0000 | [diff] [blame] | 643 | case tok::objc_public: |
| 644 | case tok::objc_protected: |
| 645 | case tok::objc_package: |
| 646 | case tok::objc_private: |
| 647 | return parseAccessSpecifier(); |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 648 | case tok::objc_interface: |
Nico Weber | 2ce0ac5 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 649 | case tok::objc_implementation: |
| 650 | return parseObjCInterfaceOrImplementation(); |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 651 | case tok::objc_protocol: |
| 652 | return parseObjCProtocol(); |
Nico Weber | d8ffe75 | 2013-01-09 21:42:32 +0000 | [diff] [blame] | 653 | case tok::objc_end: |
| 654 | return; // Handled by the caller. |
Nico Weber | 51306d2 | 2013-01-10 00:25:19 +0000 | [diff] [blame] | 655 | case tok::objc_optional: |
| 656 | case tok::objc_required: |
| 657 | nextToken(); |
| 658 | addUnwrappedLine(); |
| 659 | return; |
Nico Weber | 33381f5 | 2015-02-07 01:57:32 +0000 | [diff] [blame] | 660 | case tok::objc_try: |
| 661 | // This branch isn't strictly necessary (the kw_try case below would |
| 662 | // do this too after the tok::at is parsed above). But be explicit. |
| 663 | parseTryCatch(); |
| 664 | return; |
Nico Weber | 04e9f1a | 2013-01-07 19:05:19 +0000 | [diff] [blame] | 665 | default: |
| 666 | break; |
| 667 | } |
| 668 | break; |
Daniel Jasper | 8f46365 | 2014-08-26 23:15:12 +0000 | [diff] [blame] | 669 | case tok::kw_asm: |
Daniel Jasper | 8f46365 | 2014-08-26 23:15:12 +0000 | [diff] [blame] | 670 | nextToken(); |
| 671 | if (FormatTok->is(tok::l_brace)) { |
Daniel Jasper | c636607 | 2015-05-10 08:42:04 +0000 | [diff] [blame] | 672 | FormatTok->Type = TT_InlineASMBrace; |
Daniel Jasper | 2337f28 | 2015-01-12 10:14:56 +0000 | [diff] [blame] | 673 | nextToken(); |
Daniel Jasper | 4429f14 | 2014-08-27 17:16:46 +0000 | [diff] [blame] | 674 | while (FormatTok && FormatTok->isNot(tok::eof)) { |
Daniel Jasper | 8f46365 | 2014-08-26 23:15:12 +0000 | [diff] [blame] | 675 | if (FormatTok->is(tok::r_brace)) { |
Daniel Jasper | c636607 | 2015-05-10 08:42:04 +0000 | [diff] [blame] | 676 | FormatTok->Type = TT_InlineASMBrace; |
Daniel Jasper | 8f46365 | 2014-08-26 23:15:12 +0000 | [diff] [blame] | 677 | nextToken(); |
Daniel Jasper | 790d4f9 | 2015-05-11 11:59:46 +0000 | [diff] [blame] | 678 | addUnwrappedLine(); |
Daniel Jasper | 8f46365 | 2014-08-26 23:15:12 +0000 | [diff] [blame] | 679 | break; |
| 680 | } |
Daniel Jasper | 2337f28 | 2015-01-12 10:14:56 +0000 | [diff] [blame] | 681 | FormatTok->Finalized = true; |
Daniel Jasper | 8f46365 | 2014-08-26 23:15:12 +0000 | [diff] [blame] | 682 | nextToken(); |
| 683 | } |
| 684 | } |
| 685 | break; |
Alexander Kornienko | 578fdd8 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 686 | case tok::kw_namespace: |
| 687 | parseNamespace(); |
| 688 | return; |
Dmitri Gribenko | 58d64e2 | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 689 | case tok::kw_inline: |
| 690 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 691 | if (FormatTok->Tok.is(tok::kw_namespace)) { |
Dmitri Gribenko | 58d64e2 | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 692 | parseNamespace(); |
| 693 | return; |
| 694 | } |
| 695 | break; |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 696 | case tok::kw_public: |
| 697 | case tok::kw_protected: |
| 698 | case tok::kw_private: |
Daniel Jasper | 8370908 | 2015-02-18 17:14:05 +0000 | [diff] [blame] | 699 | if (Style.Language == FormatStyle::LK_Java || |
| 700 | Style.Language == FormatStyle::LK_JavaScript) |
Daniel Jasper | c58c70e | 2014-09-15 11:21:46 +0000 | [diff] [blame] | 701 | nextToken(); |
| 702 | else |
| 703 | parseAccessSpecifier(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 704 | return; |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 705 | case tok::kw_if: |
| 706 | parseIfThenElse(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 707 | return; |
Alexander Kornienko | 37d6c94 | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 708 | case tok::kw_for: |
| 709 | case tok::kw_while: |
| 710 | parseForOrWhileLoop(); |
| 711 | return; |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 712 | case tok::kw_do: |
| 713 | parseDoWhile(); |
| 714 | return; |
| 715 | case tok::kw_switch: |
| 716 | parseSwitch(); |
| 717 | return; |
| 718 | case tok::kw_default: |
| 719 | nextToken(); |
| 720 | parseLabel(); |
| 721 | return; |
| 722 | case tok::kw_case: |
| 723 | parseCaseLabel(); |
| 724 | return; |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 725 | case tok::kw_try: |
Nico Weber | fac2371 | 2015-02-04 15:26:27 +0000 | [diff] [blame] | 726 | case tok::kw___try: |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 727 | parseTryCatch(); |
| 728 | return; |
Manuel Klimek | ae610d1 | 2013-01-21 14:32:05 +0000 | [diff] [blame] | 729 | case tok::kw_extern: |
| 730 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 731 | if (FormatTok->Tok.is(tok::string_literal)) { |
Manuel Klimek | ae610d1 | 2013-01-21 14:32:05 +0000 | [diff] [blame] | 732 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 733 | if (FormatTok->Tok.is(tok::l_brace)) { |
Daniel Jasper | 65ee347 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 734 | parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false); |
Manuel Klimek | ae610d1 | 2013-01-21 14:32:05 +0000 | [diff] [blame] | 735 | addUnwrappedLine(); |
| 736 | return; |
| 737 | } |
| 738 | } |
Daniel Jasper | e1e4319 | 2014-04-01 12:55:11 +0000 | [diff] [blame] | 739 | break; |
Daniel Jasper | fca735c | 2015-02-19 16:14:18 +0000 | [diff] [blame] | 740 | case tok::kw_export: |
| 741 | if (Style.Language == FormatStyle::LK_JavaScript) { |
| 742 | parseJavaScriptEs6ImportExport(); |
| 743 | return; |
| 744 | } |
| 745 | break; |
Daniel Jasper | e1e4319 | 2014-04-01 12:55:11 +0000 | [diff] [blame] | 746 | case tok::identifier: |
Daniel Jasper | 66cb8c5 | 2015-05-04 09:22:29 +0000 | [diff] [blame] | 747 | if (FormatTok->is(TT_ForEachMacro)) { |
Daniel Jasper | e1e4319 | 2014-04-01 12:55:11 +0000 | [diff] [blame] | 748 | parseForOrWhileLoop(); |
| 749 | return; |
| 750 | } |
Daniel Jasper | 354aa51 | 2015-02-19 16:07:32 +0000 | [diff] [blame] | 751 | if (Style.Language == FormatStyle::LK_JavaScript && |
| 752 | FormatTok->is(Keywords.kw_import)) { |
Daniel Jasper | fca735c | 2015-02-19 16:14:18 +0000 | [diff] [blame] | 753 | parseJavaScriptEs6ImportExport(); |
Daniel Jasper | 354aa51 | 2015-02-19 16:07:32 +0000 | [diff] [blame] | 754 | return; |
| 755 | } |
Daniel Jasper | 5339540 | 2015-04-07 15:04:40 +0000 | [diff] [blame] | 756 | if (FormatTok->is(Keywords.kw_signals)) { |
Daniel Jasper | de0d1f3 | 2015-04-24 07:50:34 +0000 | [diff] [blame] | 757 | nextToken(); |
| 758 | if (FormatTok->is(tok::colon)) { |
| 759 | nextToken(); |
| 760 | addUnwrappedLine(); |
| 761 | } |
Daniel Jasper | 5339540 | 2015-04-07 15:04:40 +0000 | [diff] [blame] | 762 | return; |
| 763 | } |
Manuel Klimek | ae610d1 | 2013-01-21 14:32:05 +0000 | [diff] [blame] | 764 | // In all other cases, parse the declaration. |
| 765 | break; |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 766 | default: |
| 767 | break; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 768 | } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 769 | do { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 770 | switch (FormatTok->Tok.getKind()) { |
Nico Weber | 372d8dc | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 771 | case tok::at: |
| 772 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 773 | if (FormatTok->Tok.is(tok::l_brace)) |
Nico Weber | 372d8dc | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 774 | parseBracedList(); |
| 775 | break; |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 776 | case tok::kw_enum: |
| 777 | parseEnum(); |
Manuel Klimek | 2cec019 | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 778 | break; |
Daniel Jasper | a88f80a | 2014-01-30 14:38:37 +0000 | [diff] [blame] | 779 | case tok::kw_typedef: |
| 780 | nextToken(); |
Daniel Jasper | 31f6c54 | 2014-12-05 10:42:21 +0000 | [diff] [blame] | 781 | if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS, |
| 782 | Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS)) |
Daniel Jasper | a88f80a | 2014-01-30 14:38:37 +0000 | [diff] [blame] | 783 | parseEnum(); |
| 784 | break; |
Alexander Kornienko | 1231e06 | 2013-01-16 11:43:46 +0000 | [diff] [blame] | 785 | case tok::kw_struct: |
| 786 | case tok::kw_union: |
Manuel Klimek | 28cacc7 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 787 | case tok::kw_class: |
Manuel Klimek | e01bab5 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 788 | parseRecord(); |
| 789 | // A record declaration or definition is always the start of a structural |
| 790 | // element. |
| 791 | break; |
Daniel Jasper | e5d7486 | 2014-11-26 08:17:08 +0000 | [diff] [blame] | 792 | case tok::period: |
| 793 | nextToken(); |
| 794 | // In Java, classes have an implicit static member "class". |
| 795 | if (Style.Language == FormatStyle::LK_Java && FormatTok && |
| 796 | FormatTok->is(tok::kw_class)) |
| 797 | nextToken(); |
| 798 | break; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 799 | case tok::semi: |
| 800 | nextToken(); |
| 801 | addUnwrappedLine(); |
| 802 | return; |
Alexander Kornienko | 1231e06 | 2013-01-16 11:43:46 +0000 | [diff] [blame] | 803 | case tok::r_brace: |
| 804 | addUnwrappedLine(); |
| 805 | return; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 806 | case tok::l_paren: |
| 807 | parseParens(); |
| 808 | break; |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 809 | case tok::caret: |
| 810 | nextToken(); |
Daniel Jasper | 395193c | 2014-03-28 07:48:59 +0000 | [diff] [blame] | 811 | if (FormatTok->Tok.isAnyIdentifier() || |
| 812 | FormatTok->isSimpleTypeSpecifier()) |
| 813 | nextToken(); |
| 814 | if (FormatTok->is(tok::l_paren)) |
| 815 | parseParens(); |
| 816 | if (FormatTok->is(tok::l_brace)) |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 817 | parseChildBlock(); |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 818 | break; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 819 | case tok::l_brace: |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 820 | if (!tryToParseBracedList()) { |
| 821 | // A block outside of parentheses must be the last part of a |
| 822 | // structural element. |
| 823 | // FIXME: Figure out cases where this is not true, and add projections |
| 824 | // for them (the one we know is missing are lambdas). |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 825 | if (Style.BreakBeforeBraces != FormatStyle::BS_Attach) |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 826 | addUnwrappedLine(); |
Alexander Kornienko | 3cfa973 | 2013-11-20 16:33:05 +0000 | [diff] [blame] | 827 | FormatTok->Type = TT_FunctionLBrace; |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 828 | parseBlock(/*MustBeDeclaration=*/false); |
Manuel Klimek | a8eb914 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 829 | addUnwrappedLine(); |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 830 | return; |
| 831 | } |
| 832 | // Otherwise this was a braced init list, and the structural |
| 833 | // element continues. |
| 834 | break; |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 835 | case tok::kw_try: |
| 836 | // We arrive here when parsing function-try blocks. |
| 837 | parseTryCatch(); |
| 838 | return; |
Daniel Jasper | 40e1921 | 2013-05-29 13:16:10 +0000 | [diff] [blame] | 839 | case tok::identifier: { |
Daniel Jasper | ad9eb0d | 2014-06-30 13:24:54 +0000 | [diff] [blame] | 840 | // Parse function literal unless 'function' is the first token in a line |
| 841 | // in which case this should be treated as a free-standing function. |
Daniel Jasper | 9326f91 | 2015-05-05 08:40:32 +0000 | [diff] [blame] | 842 | if (Style.Language == FormatStyle::LK_JavaScript && |
| 843 | FormatTok->is(Keywords.kw_function) && Line->Tokens.size() > 0) { |
Daniel Jasper | 069e5f4 | 2014-05-20 11:14:57 +0000 | [diff] [blame] | 844 | tryToParseJSFunction(); |
| 845 | break; |
| 846 | } |
Daniel Jasper | 9326f91 | 2015-05-05 08:40:32 +0000 | [diff] [blame] | 847 | if ((Style.Language == FormatStyle::LK_JavaScript || |
| 848 | Style.Language == FormatStyle::LK_Java) && |
| 849 | FormatTok->is(Keywords.kw_interface)) { |
| 850 | parseRecord(); |
| 851 | break; |
| 852 | } |
| 853 | |
| 854 | StringRef Text = FormatTok->TokenText; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 855 | nextToken(); |
Daniel Jasper | 8370908 | 2015-02-18 17:14:05 +0000 | [diff] [blame] | 856 | if (Line->Tokens.size() == 1 && |
| 857 | // JS doesn't have macros, and within classes colons indicate fields, |
| 858 | // not labels. |
Daniel Jasper | 676e516 | 2015-04-07 14:36:33 +0000 | [diff] [blame] | 859 | Style.Language != FormatStyle::LK_JavaScript) { |
| 860 | if (FormatTok->Tok.is(tok::colon) && !Line->MustBeDeclaration) { |
Alexander Kornienko | de64427 | 2013-04-08 22:16:06 +0000 | [diff] [blame] | 861 | parseLabel(); |
| 862 | return; |
| 863 | } |
Daniel Jasper | 680b09b | 2014-11-05 10:48:04 +0000 | [diff] [blame] | 864 | // Recognize function-like macro usages without trailing semicolon as |
Daniel Jasper | 8370908 | 2015-02-18 17:14:05 +0000 | [diff] [blame] | 865 | // well as free-standing macros like Q_OBJECT. |
Daniel Jasper | 680b09b | 2014-11-05 10:48:04 +0000 | [diff] [blame] | 866 | bool FunctionLike = FormatTok->is(tok::l_paren); |
| 867 | if (FunctionLike) |
Alexander Kornienko | de64427 | 2013-04-08 22:16:06 +0000 | [diff] [blame] | 868 | parseParens(); |
Daniel Jasper | e60cba1 | 2015-05-13 11:35:53 +0000 | [diff] [blame] | 869 | |
| 870 | bool FollowedByNewline = |
| 871 | CommentsBeforeNextToken.empty() |
| 872 | ? FormatTok->NewlinesBefore > 0 |
| 873 | : CommentsBeforeNextToken.front()->NewlinesBefore > 0; |
| 874 | |
| 875 | if (FollowedByNewline && |
Daniel Jasper | 680b09b | 2014-11-05 10:48:04 +0000 | [diff] [blame] | 876 | (Text.size() >= 5 || FunctionLike) && |
| 877 | tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) { |
Daniel Jasper | 40e1921 | 2013-05-29 13:16:10 +0000 | [diff] [blame] | 878 | addUnwrappedLine(); |
Daniel Jasper | 41a0f78 | 2013-05-29 14:09:17 +0000 | [diff] [blame] | 879 | return; |
Alexander Kornienko | de64427 | 2013-04-08 22:16:06 +0000 | [diff] [blame] | 880 | } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 881 | } |
| 882 | break; |
Daniel Jasper | 40e1921 | 2013-05-29 13:16:10 +0000 | [diff] [blame] | 883 | } |
Daniel Jasper | e25509f | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 884 | case tok::equal: |
Manuel Klimek | 79e0608 | 2015-05-21 12:23:34 +0000 | [diff] [blame^] | 885 | // Fat arrows (=>) have tok::TokenKind tok::equal but TokenType |
| 886 | // TT_JsFatArrow. The always start an expression or a child block if |
| 887 | // followed by a curly. |
| 888 | if (FormatTok->is(TT_JsFatArrow)) { |
| 889 | nextToken(); |
| 890 | if (FormatTok->is(tok::l_brace)) { |
| 891 | parseChildBlock(); |
| 892 | } |
| 893 | break; |
| 894 | } |
| 895 | |
Daniel Jasper | e25509f | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 896 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 897 | if (FormatTok->Tok.is(tok::l_brace)) { |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 898 | parseBracedList(); |
| 899 | } |
Daniel Jasper | e25509f | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 900 | break; |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 901 | case tok::l_square: |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 902 | parseSquare(); |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 903 | break; |
Daniel Jasper | 6acf513 | 2015-03-12 14:44:29 +0000 | [diff] [blame] | 904 | case tok::kw_new: |
| 905 | parseNew(); |
| 906 | break; |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 907 | default: |
| 908 | nextToken(); |
| 909 | break; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 910 | } |
| 911 | } while (!eof()); |
| 912 | } |
| 913 | |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 914 | bool UnwrappedLineParser::tryToParseLambda() { |
Daniel Jasper | bf02b2c1 | 2013-09-05 11:49:39 +0000 | [diff] [blame] | 915 | // FIXME: This is a dirty way to access the previous token. Find a better |
| 916 | // solution. |
Daniel Jasper | b4b9998 | 2013-09-06 21:25:51 +0000 | [diff] [blame] | 917 | if (!Line->Tokens.empty() && |
Daniel Jasper | 8022226 | 2014-11-02 22:46:42 +0000 | [diff] [blame] | 918 | (Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator, |
| 919 | tok::kw_new, tok::kw_delete) || |
Daniel Jasper | a58dd5d | 2014-03-11 10:03:33 +0000 | [diff] [blame] | 920 | Line->Tokens.back().Tok->closesScope() || |
Daniel Jasper | 6b70ec0 | 2014-01-22 17:01:47 +0000 | [diff] [blame] | 921 | Line->Tokens.back().Tok->isSimpleTypeSpecifier())) { |
Daniel Jasper | bf02b2c1 | 2013-09-05 11:49:39 +0000 | [diff] [blame] | 922 | nextToken(); |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 923 | return false; |
Daniel Jasper | bf02b2c1 | 2013-09-05 11:49:39 +0000 | [diff] [blame] | 924 | } |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 925 | assert(FormatTok->is(tok::l_square)); |
| 926 | FormatToken &LSquare = *FormatTok; |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 927 | if (!tryToParseLambdaIntroducer()) |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 928 | return false; |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 929 | |
Alexander Kornienko | c2ee9cf | 2014-03-13 13:59:48 +0000 | [diff] [blame] | 930 | while (FormatTok->isNot(tok::l_brace)) { |
Daniel Jasper | cb51cf4 | 2014-01-16 09:11:55 +0000 | [diff] [blame] | 931 | if (FormatTok->isSimpleTypeSpecifier()) { |
| 932 | nextToken(); |
| 933 | continue; |
| 934 | } |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 935 | switch (FormatTok->Tok.getKind()) { |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 936 | case tok::l_brace: |
| 937 | break; |
| 938 | case tok::l_paren: |
| 939 | parseParens(); |
| 940 | break; |
Daniel Jasper | bcb55ee | 2014-11-21 14:08:38 +0000 | [diff] [blame] | 941 | case tok::amp: |
| 942 | case tok::star: |
| 943 | case tok::kw_const: |
Daniel Jasper | 3431b75 | 2014-12-08 13:22:37 +0000 | [diff] [blame] | 944 | case tok::comma: |
Daniel Jasper | cb51cf4 | 2014-01-16 09:11:55 +0000 | [diff] [blame] | 945 | case tok::less: |
| 946 | case tok::greater: |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 947 | case tok::identifier: |
Daniel Jasper | 1067ab0 | 2014-02-11 10:16:55 +0000 | [diff] [blame] | 948 | case tok::coloncolon: |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 949 | case tok::kw_mutable: |
Daniel Jasper | 81a2078 | 2014-03-10 10:02:02 +0000 | [diff] [blame] | 950 | nextToken(); |
| 951 | break; |
Daniel Jasper | cb51cf4 | 2014-01-16 09:11:55 +0000 | [diff] [blame] | 952 | case tok::arrow: |
Daniel Jasper | 81a2078 | 2014-03-10 10:02:02 +0000 | [diff] [blame] | 953 | FormatTok->Type = TT_TrailingReturnArrow; |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 954 | nextToken(); |
| 955 | break; |
| 956 | default: |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 957 | return true; |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 958 | } |
| 959 | } |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 960 | LSquare.Type = TT_LambdaLSquare; |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 961 | parseChildBlock(); |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 962 | return true; |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | bool UnwrappedLineParser::tryToParseLambdaIntroducer() { |
| 966 | nextToken(); |
| 967 | if (FormatTok->is(tok::equal)) { |
| 968 | nextToken(); |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 969 | if (FormatTok->is(tok::r_square)) { |
| 970 | nextToken(); |
| 971 | return true; |
| 972 | } |
| 973 | if (FormatTok->isNot(tok::comma)) |
| 974 | return false; |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 975 | nextToken(); |
| 976 | } else if (FormatTok->is(tok::amp)) { |
| 977 | nextToken(); |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 978 | if (FormatTok->is(tok::r_square)) { |
| 979 | nextToken(); |
| 980 | return true; |
| 981 | } |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 982 | if (!FormatTok->isOneOf(tok::comma, tok::identifier)) { |
| 983 | return false; |
| 984 | } |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 985 | if (FormatTok->is(tok::comma)) |
| 986 | nextToken(); |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 987 | } else if (FormatTok->is(tok::r_square)) { |
| 988 | nextToken(); |
| 989 | return true; |
| 990 | } |
| 991 | do { |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 992 | if (FormatTok->is(tok::amp)) |
| 993 | nextToken(); |
| 994 | if (!FormatTok->isOneOf(tok::identifier, tok::kw_this)) |
| 995 | return false; |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 996 | nextToken(); |
Daniel Jasper | da18fd8 | 2014-06-10 06:39:03 +0000 | [diff] [blame] | 997 | if (FormatTok->is(tok::ellipsis)) |
| 998 | nextToken(); |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 999 | if (FormatTok->is(tok::comma)) { |
| 1000 | nextToken(); |
| 1001 | } else if (FormatTok->is(tok::r_square)) { |
| 1002 | nextToken(); |
| 1003 | return true; |
| 1004 | } else { |
| 1005 | return false; |
| 1006 | } |
| 1007 | } while (!eof()); |
| 1008 | return false; |
| 1009 | } |
| 1010 | |
Daniel Jasper | c03e16a | 2014-05-08 09:25:39 +0000 | [diff] [blame] | 1011 | void UnwrappedLineParser::tryToParseJSFunction() { |
| 1012 | nextToken(); |
Daniel Jasper | 5217a8b | 2014-06-13 07:02:04 +0000 | [diff] [blame] | 1013 | |
| 1014 | // Consume function name. |
| 1015 | if (FormatTok->is(tok::identifier)) |
Daniel Jasper | fca735c | 2015-02-19 16:14:18 +0000 | [diff] [blame] | 1016 | nextToken(); |
Daniel Jasper | 5217a8b | 2014-06-13 07:02:04 +0000 | [diff] [blame] | 1017 | |
Daniel Jasper | c03e16a | 2014-05-08 09:25:39 +0000 | [diff] [blame] | 1018 | if (FormatTok->isNot(tok::l_paren)) |
| 1019 | return; |
Manuel Klimek | 79e0608 | 2015-05-21 12:23:34 +0000 | [diff] [blame^] | 1020 | |
| 1021 | // Parse formal parameter list. |
| 1022 | parseBalanced(tok::l_paren, tok::r_paren); |
| 1023 | |
| 1024 | if (FormatTok->is(tok::colon)) { |
| 1025 | // Parse a type definition. |
| 1026 | nextToken(); |
| 1027 | |
| 1028 | // Eat the type declaration. For braced inline object types, balance braces, |
| 1029 | // otherwise just parse until finding an l_brace for the function body. |
| 1030 | if (FormatTok->is(tok::l_brace)) { |
| 1031 | parseBalanced(tok::l_brace, tok::r_brace); |
| 1032 | } else { |
| 1033 | while(FormatTok->isNot(tok::l_brace) && !eof()) { |
| 1034 | nextToken(); |
| 1035 | } |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | parseChildBlock(); |
| 1040 | } |
| 1041 | |
| 1042 | void UnwrappedLineParser::parseBalanced(tok::TokenKind OpenKind, |
| 1043 | tok::TokenKind CloseKind) { |
| 1044 | assert(FormatTok->is(OpenKind)); |
Daniel Jasper | c03e16a | 2014-05-08 09:25:39 +0000 | [diff] [blame] | 1045 | nextToken(); |
Manuel Klimek | 79e0608 | 2015-05-21 12:23:34 +0000 | [diff] [blame^] | 1046 | int Depth = 1; |
| 1047 | while (Depth > 0 && !eof()) { |
| 1048 | // Parse the formal parameter list. |
| 1049 | if (FormatTok->is(OpenKind)) { |
| 1050 | ++Depth; |
| 1051 | } else if (FormatTok->is(CloseKind)) { |
| 1052 | --Depth; |
| 1053 | } |
Daniel Jasper | c03e16a | 2014-05-08 09:25:39 +0000 | [diff] [blame] | 1054 | nextToken(); |
| 1055 | } |
Daniel Jasper | c03e16a | 2014-05-08 09:25:39 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
Daniel Jasper | 3c883d1 | 2015-05-18 14:49:19 +0000 | [diff] [blame] | 1058 | bool UnwrappedLineParser::tryToParseBracedList() { |
Daniel Jasper | b1f74a8 | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 1059 | if (FormatTok->BlockKind == BK_Unknown) |
Daniel Jasper | 3c883d1 | 2015-05-18 14:49:19 +0000 | [diff] [blame] | 1060 | calculateBraceTypes(); |
Daniel Jasper | b1f74a8 | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 1061 | assert(FormatTok->BlockKind != BK_Unknown); |
| 1062 | if (FormatTok->BlockKind == BK_Block) |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 1063 | return false; |
| 1064 | parseBracedList(); |
| 1065 | return true; |
| 1066 | } |
| 1067 | |
Daniel Jasper | 015ed02 | 2013-09-13 09:20:45 +0000 | [diff] [blame] | 1068 | bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) { |
| 1069 | bool HasError = false; |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 1070 | nextToken(); |
| 1071 | |
Manuel Klimek | a3ff45e | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 1072 | // FIXME: Once we have an expression parser in the UnwrappedLineParser, |
| 1073 | // replace this by using parseAssigmentExpression() inside. |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 1074 | do { |
Manuel Klimek | 79e0608 | 2015-05-21 12:23:34 +0000 | [diff] [blame^] | 1075 | if (Style.Language == FormatStyle::LK_JavaScript) { |
| 1076 | if (FormatTok->is(Keywords.kw_function)) { |
| 1077 | tryToParseJSFunction(); |
| 1078 | continue; |
| 1079 | } else if (FormatTok->is(TT_JsFatArrow)) { |
| 1080 | nextToken(); |
| 1081 | // Fat arrows can be followed by simple expressions or by child blocks |
| 1082 | // in curly braces. |
| 1083 | if (FormatTok->is(tok::l_brace)){ |
| 1084 | parseChildBlock(); |
| 1085 | continue; |
| 1086 | } |
| 1087 | } |
Daniel Jasper | c03e16a | 2014-05-08 09:25:39 +0000 | [diff] [blame] | 1088 | } |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1089 | switch (FormatTok->Tok.getKind()) { |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 1090 | case tok::caret: |
| 1091 | nextToken(); |
| 1092 | if (FormatTok->is(tok::l_brace)) { |
| 1093 | parseChildBlock(); |
| 1094 | } |
| 1095 | break; |
| 1096 | case tok::l_square: |
| 1097 | tryToParseLambda(); |
| 1098 | break; |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 1099 | case tok::l_brace: |
Manuel Klimek | bab25fd | 2013-09-04 08:20:47 +0000 | [diff] [blame] | 1100 | // Assume there are no blocks inside a braced init list apart |
| 1101 | // from the ones we explicitly parse out (like lambdas). |
| 1102 | FormatTok->BlockKind = BK_BracedInit; |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 1103 | parseBracedList(); |
| 1104 | break; |
Daniel Jasper | f46dec8 | 2015-03-31 14:34:15 +0000 | [diff] [blame] | 1105 | case tok::r_paren: |
| 1106 | // JavaScript can just have free standing methods and getters/setters in |
| 1107 | // object literals. Detect them by a "{" following ")". |
| 1108 | if (Style.Language == FormatStyle::LK_JavaScript) { |
| 1109 | nextToken(); |
| 1110 | if (FormatTok->is(tok::l_brace)) |
| 1111 | parseChildBlock(); |
| 1112 | break; |
| 1113 | } |
| 1114 | nextToken(); |
| 1115 | break; |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 1116 | case tok::r_brace: |
| 1117 | nextToken(); |
Daniel Jasper | 015ed02 | 2013-09-13 09:20:45 +0000 | [diff] [blame] | 1118 | return !HasError; |
Manuel Klimek | a3ff45e | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 1119 | case tok::semi: |
Daniel Jasper | 015ed02 | 2013-09-13 09:20:45 +0000 | [diff] [blame] | 1120 | HasError = true; |
| 1121 | if (!ContinueOnSemicolons) |
| 1122 | return !HasError; |
| 1123 | nextToken(); |
| 1124 | break; |
Manuel Klimek | a3ff45e | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 1125 | case tok::comma: |
| 1126 | nextToken(); |
Manuel Klimek | a3ff45e | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 1127 | break; |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 1128 | default: |
| 1129 | nextToken(); |
| 1130 | break; |
| 1131 | } |
| 1132 | } while (!eof()); |
Daniel Jasper | 015ed02 | 2013-09-13 09:20:45 +0000 | [diff] [blame] | 1133 | return false; |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 1134 | } |
| 1135 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1136 | void UnwrappedLineParser::parseParens() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1137 | assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected."); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1138 | nextToken(); |
| 1139 | do { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1140 | switch (FormatTok->Tok.getKind()) { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1141 | case tok::l_paren: |
| 1142 | parseParens(); |
Daniel Jasper | 5f1fa85 | 2015-01-04 20:40:51 +0000 | [diff] [blame] | 1143 | if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace)) |
| 1144 | parseChildBlock(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1145 | break; |
| 1146 | case tok::r_paren: |
| 1147 | nextToken(); |
| 1148 | return; |
Daniel Jasper | 393564f | 2013-05-31 14:56:29 +0000 | [diff] [blame] | 1149 | case tok::r_brace: |
| 1150 | // A "}" inside parenthesis is an error if there wasn't a matching "{". |
| 1151 | return; |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 1152 | case tok::l_square: |
| 1153 | tryToParseLambda(); |
| 1154 | break; |
Daniel Jasper | 5f1fa85 | 2015-01-04 20:40:51 +0000 | [diff] [blame] | 1155 | case tok::l_brace: |
Daniel Jasper | adba2aa | 2015-05-18 12:52:00 +0000 | [diff] [blame] | 1156 | if (!tryToParseBracedList()) |
Manuel Klimek | f017dc0 | 2013-09-04 13:34:14 +0000 | [diff] [blame] | 1157 | parseChildBlock(); |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 1158 | break; |
Nico Weber | 372d8dc | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 1159 | case tok::at: |
| 1160 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1161 | if (FormatTok->Tok.is(tok::l_brace)) |
Nico Weber | 372d8dc | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 1162 | parseBracedList(); |
| 1163 | break; |
Daniel Jasper | 3f69ba1 | 2014-09-05 08:42:27 +0000 | [diff] [blame] | 1164 | case tok::identifier: |
| 1165 | if (Style.Language == FormatStyle::LK_JavaScript && |
Daniel Jasper | d0ec0d6 | 2014-11-04 12:41:02 +0000 | [diff] [blame] | 1166 | FormatTok->is(Keywords.kw_function)) |
Daniel Jasper | 3f69ba1 | 2014-09-05 08:42:27 +0000 | [diff] [blame] | 1167 | tryToParseJSFunction(); |
| 1168 | else |
| 1169 | nextToken(); |
| 1170 | break; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1171 | default: |
| 1172 | nextToken(); |
| 1173 | break; |
| 1174 | } |
| 1175 | } while (!eof()); |
| 1176 | } |
| 1177 | |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 1178 | void UnwrappedLineParser::parseSquare() { |
| 1179 | assert(FormatTok->Tok.is(tok::l_square) && "'[' expected."); |
| 1180 | if (tryToParseLambda()) |
| 1181 | return; |
| 1182 | do { |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 1183 | switch (FormatTok->Tok.getKind()) { |
| 1184 | case tok::l_paren: |
| 1185 | parseParens(); |
| 1186 | break; |
| 1187 | case tok::r_square: |
| 1188 | nextToken(); |
| 1189 | return; |
| 1190 | case tok::r_brace: |
| 1191 | // A "}" inside parenthesis is an error if there wasn't a matching "{". |
| 1192 | return; |
| 1193 | case tok::l_square: |
| 1194 | parseSquare(); |
| 1195 | break; |
| 1196 | case tok::l_brace: { |
Daniel Jasper | adba2aa | 2015-05-18 12:52:00 +0000 | [diff] [blame] | 1197 | if (!tryToParseBracedList()) |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 1198 | parseChildBlock(); |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 1199 | break; |
| 1200 | } |
| 1201 | case tok::at: |
| 1202 | nextToken(); |
| 1203 | if (FormatTok->Tok.is(tok::l_brace)) |
| 1204 | parseBracedList(); |
| 1205 | break; |
| 1206 | default: |
| 1207 | nextToken(); |
| 1208 | break; |
| 1209 | } |
| 1210 | } while (!eof()); |
| 1211 | } |
| 1212 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1213 | void UnwrappedLineParser::parseIfThenElse() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1214 | assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected"); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1215 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1216 | if (FormatTok->Tok.is(tok::l_paren)) |
Manuel Klimek | adededf | 2013-01-11 18:28:36 +0000 | [diff] [blame] | 1217 | parseParens(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1218 | bool NeedsUnwrappedLine = false; |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1219 | if (FormatTok->Tok.is(tok::l_brace)) { |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1220 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 1221 | parseBlock(/*MustBeDeclaration=*/false); |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1222 | if (Style.BreakBeforeBraces == FormatStyle::BS_Allman || |
| 1223 | Style.BreakBeforeBraces == FormatStyle::BS_GNU) { |
Manuel Klimek | d3ed59a | 2013-08-02 21:31:59 +0000 | [diff] [blame] | 1224 | addUnwrappedLine(); |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1225 | } else { |
Manuel Klimek | d3ed59a | 2013-08-02 21:31:59 +0000 | [diff] [blame] | 1226 | NeedsUnwrappedLine = true; |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1227 | } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1228 | } else { |
| 1229 | addUnwrappedLine(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1230 | ++Line->Level; |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 1231 | parseStructuralElement(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1232 | --Line->Level; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1233 | } |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1234 | if (FormatTok->Tok.is(tok::kw_else)) { |
Daniel Jasper | d967087 | 2014-08-05 12:06:20 +0000 | [diff] [blame] | 1235 | if (Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) |
| 1236 | addUnwrappedLine(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1237 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1238 | if (FormatTok->Tok.is(tok::l_brace)) { |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1239 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 1240 | parseBlock(/*MustBeDeclaration=*/false); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1241 | addUnwrappedLine(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1242 | } else if (FormatTok->Tok.is(tok::kw_if)) { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1243 | parseIfThenElse(); |
| 1244 | } else { |
| 1245 | addUnwrappedLine(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1246 | ++Line->Level; |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 1247 | parseStructuralElement(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1248 | --Line->Level; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1249 | } |
| 1250 | } else if (NeedsUnwrappedLine) { |
| 1251 | addUnwrappedLine(); |
| 1252 | } |
| 1253 | } |
| 1254 | |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 1255 | void UnwrappedLineParser::parseTryCatch() { |
Nico Weber | fac2371 | 2015-02-04 15:26:27 +0000 | [diff] [blame] | 1256 | assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected"); |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 1257 | nextToken(); |
| 1258 | bool NeedsUnwrappedLine = false; |
| 1259 | if (FormatTok->is(tok::colon)) { |
| 1260 | // We are in a function try block, what comes is an initializer list. |
| 1261 | nextToken(); |
| 1262 | while (FormatTok->is(tok::identifier)) { |
| 1263 | nextToken(); |
| 1264 | if (FormatTok->is(tok::l_paren)) |
| 1265 | parseParens(); |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 1266 | if (FormatTok->is(tok::comma)) |
| 1267 | nextToken(); |
| 1268 | } |
| 1269 | } |
Daniel Jasper | e189d46 | 2015-01-14 10:48:41 +0000 | [diff] [blame] | 1270 | // Parse try with resource. |
| 1271 | if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) { |
| 1272 | parseParens(); |
| 1273 | } |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 1274 | if (FormatTok->is(tok::l_brace)) { |
| 1275 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
| 1276 | parseBlock(/*MustBeDeclaration=*/false); |
| 1277 | if (Style.BreakBeforeBraces == FormatStyle::BS_Allman || |
| 1278 | Style.BreakBeforeBraces == FormatStyle::BS_GNU || |
| 1279 | Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) { |
| 1280 | addUnwrappedLine(); |
| 1281 | } else { |
| 1282 | NeedsUnwrappedLine = true; |
| 1283 | } |
| 1284 | } else if (!FormatTok->is(tok::kw_catch)) { |
| 1285 | // The C++ standard requires a compound-statement after a try. |
| 1286 | // If there's none, we try to assume there's a structuralElement |
| 1287 | // and try to continue. |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 1288 | addUnwrappedLine(); |
| 1289 | ++Line->Level; |
| 1290 | parseStructuralElement(); |
| 1291 | --Line->Level; |
| 1292 | } |
Nico Weber | 33381f5 | 2015-02-07 01:57:32 +0000 | [diff] [blame] | 1293 | while (1) { |
| 1294 | if (FormatTok->is(tok::at)) |
| 1295 | nextToken(); |
| 1296 | if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except, |
| 1297 | tok::kw___finally) || |
| 1298 | ((Style.Language == FormatStyle::LK_Java || |
| 1299 | Style.Language == FormatStyle::LK_JavaScript) && |
| 1300 | FormatTok->is(Keywords.kw_finally)) || |
| 1301 | (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) || |
| 1302 | FormatTok->Tok.isObjCAtKeyword(tok::objc_finally)))) |
| 1303 | break; |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 1304 | nextToken(); |
| 1305 | while (FormatTok->isNot(tok::l_brace)) { |
| 1306 | if (FormatTok->is(tok::l_paren)) { |
| 1307 | parseParens(); |
| 1308 | continue; |
| 1309 | } |
Daniel Jasper | 2bd7a64 | 2015-01-19 10:50:51 +0000 | [diff] [blame] | 1310 | if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof)) |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 1311 | return; |
| 1312 | nextToken(); |
| 1313 | } |
| 1314 | NeedsUnwrappedLine = false; |
| 1315 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
| 1316 | parseBlock(/*MustBeDeclaration=*/false); |
| 1317 | if (Style.BreakBeforeBraces == FormatStyle::BS_Allman || |
| 1318 | Style.BreakBeforeBraces == FormatStyle::BS_GNU || |
| 1319 | Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) { |
| 1320 | addUnwrappedLine(); |
| 1321 | } else { |
| 1322 | NeedsUnwrappedLine = true; |
| 1323 | } |
| 1324 | } |
| 1325 | if (NeedsUnwrappedLine) { |
| 1326 | addUnwrappedLine(); |
| 1327 | } |
| 1328 | } |
| 1329 | |
Alexander Kornienko | 578fdd8 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1330 | void UnwrappedLineParser::parseNamespace() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1331 | assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected"); |
Roman Kashitsyn | a043ced | 2014-08-11 12:18:01 +0000 | [diff] [blame] | 1332 | |
| 1333 | const FormatToken &InitialToken = *FormatTok; |
Alexander Kornienko | 578fdd8 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1334 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1335 | if (FormatTok->Tok.is(tok::identifier)) |
Alexander Kornienko | 578fdd8 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1336 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1337 | if (FormatTok->Tok.is(tok::l_brace)) { |
Roman Kashitsyn | a043ced | 2014-08-11 12:18:01 +0000 | [diff] [blame] | 1338 | if (ShouldBreakBeforeBrace(Style, InitialToken)) |
Manuel Klimek | a8eb914 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 1339 | addUnwrappedLine(); |
| 1340 | |
Daniel Jasper | 65ee347 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 1341 | bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All || |
| 1342 | (Style.NamespaceIndentation == FormatStyle::NI_Inner && |
| 1343 | DeclarationScopeStack.size() > 1); |
| 1344 | parseBlock(/*MustBeDeclaration=*/true, AddLevel); |
Manuel Klimek | 046b930 | 2013-02-06 16:08:09 +0000 | [diff] [blame] | 1345 | // Munch the semicolon after a namespace. This is more common than one would |
| 1346 | // think. Puttin the semicolon into its own line is very ugly. |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1347 | if (FormatTok->Tok.is(tok::semi)) |
Manuel Klimek | 046b930 | 2013-02-06 16:08:09 +0000 | [diff] [blame] | 1348 | nextToken(); |
Alexander Kornienko | 578fdd8 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1349 | addUnwrappedLine(); |
| 1350 | } |
| 1351 | // FIXME: Add error handling. |
| 1352 | } |
| 1353 | |
Daniel Jasper | 6acf513 | 2015-03-12 14:44:29 +0000 | [diff] [blame] | 1354 | void UnwrappedLineParser::parseNew() { |
| 1355 | assert(FormatTok->is(tok::kw_new) && "'new' expected"); |
| 1356 | nextToken(); |
| 1357 | if (Style.Language != FormatStyle::LK_Java) |
| 1358 | return; |
| 1359 | |
| 1360 | // In Java, we can parse everything up to the parens, which aren't optional. |
| 1361 | do { |
| 1362 | // There should not be a ;, { or } before the new's open paren. |
| 1363 | if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace)) |
| 1364 | return; |
| 1365 | |
| 1366 | // Consume the parens. |
| 1367 | if (FormatTok->is(tok::l_paren)) { |
| 1368 | parseParens(); |
| 1369 | |
| 1370 | // If there is a class body of an anonymous class, consume that as child. |
| 1371 | if (FormatTok->is(tok::l_brace)) |
| 1372 | parseChildBlock(); |
| 1373 | return; |
| 1374 | } |
| 1375 | nextToken(); |
| 1376 | } while (!eof()); |
| 1377 | } |
| 1378 | |
Alexander Kornienko | 37d6c94 | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 1379 | void UnwrappedLineParser::parseForOrWhileLoop() { |
Daniel Jasper | 66cb8c5 | 2015-05-04 09:22:29 +0000 | [diff] [blame] | 1380 | assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) && |
Daniel Jasper | e1e4319 | 2014-04-01 12:55:11 +0000 | [diff] [blame] | 1381 | "'for', 'while' or foreach macro expected"); |
Alexander Kornienko | 37d6c94 | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 1382 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1383 | if (FormatTok->Tok.is(tok::l_paren)) |
Manuel Klimek | 9fa8d55 | 2013-01-11 19:23:05 +0000 | [diff] [blame] | 1384 | parseParens(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1385 | if (FormatTok->Tok.is(tok::l_brace)) { |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1386 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 1387 | parseBlock(/*MustBeDeclaration=*/false); |
Alexander Kornienko | 37d6c94 | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 1388 | addUnwrappedLine(); |
| 1389 | } else { |
| 1390 | addUnwrappedLine(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1391 | ++Line->Level; |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 1392 | parseStructuralElement(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1393 | --Line->Level; |
Alexander Kornienko | 37d6c94 | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 1394 | } |
| 1395 | } |
| 1396 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1397 | void UnwrappedLineParser::parseDoWhile() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1398 | assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected"); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1399 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1400 | if (FormatTok->Tok.is(tok::l_brace)) { |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1401 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 1402 | parseBlock(/*MustBeDeclaration=*/false); |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1403 | if (Style.BreakBeforeBraces == FormatStyle::BS_GNU) |
| 1404 | addUnwrappedLine(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1405 | } else { |
| 1406 | addUnwrappedLine(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1407 | ++Line->Level; |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 1408 | parseStructuralElement(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1409 | --Line->Level; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
Alexander Kornienko | 0ea8e10 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 1412 | // FIXME: Add error handling. |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1413 | if (!FormatTok->Tok.is(tok::kw_while)) { |
Alexander Kornienko | 0ea8e10 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 1414 | addUnwrappedLine(); |
| 1415 | return; |
| 1416 | } |
| 1417 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1418 | nextToken(); |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 1419 | parseStructuralElement(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | void UnwrappedLineParser::parseLabel() { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1423 | nextToken(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1424 | unsigned OldLineLevel = Line->Level; |
Daniel Jasper | a127512 | 2013-03-20 10:23:53 +0000 | [diff] [blame] | 1425 | if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0)) |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1426 | --Line->Level; |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1427 | if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) { |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1428 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 1429 | parseBlock(/*MustBeDeclaration=*/false); |
Manuel Klimek | d3ed59a | 2013-08-02 21:31:59 +0000 | [diff] [blame] | 1430 | if (FormatTok->Tok.is(tok::kw_break)) { |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1431 | // "break;" after "}" on its own line only for BS_Allman and BS_GNU |
| 1432 | if (Style.BreakBeforeBraces == FormatStyle::BS_Allman || |
| 1433 | Style.BreakBeforeBraces == FormatStyle::BS_GNU) { |
Manuel Klimek | d3ed59a | 2013-08-02 21:31:59 +0000 | [diff] [blame] | 1434 | addUnwrappedLine(); |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1435 | } |
Manuel Klimek | d3ed59a | 2013-08-02 21:31:59 +0000 | [diff] [blame] | 1436 | parseStructuralElement(); |
| 1437 | } |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1438 | addUnwrappedLine(); |
| 1439 | } else { |
Daniel Jasper | 1fe0d5c | 2015-05-06 15:19:47 +0000 | [diff] [blame] | 1440 | if (FormatTok->is(tok::semi)) |
| 1441 | nextToken(); |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1442 | addUnwrappedLine(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1443 | } |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1444 | Line->Level = OldLineLevel; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
| 1447 | void UnwrappedLineParser::parseCaseLabel() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1448 | assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected"); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1449 | // FIXME: fix handling of complex expressions here. |
| 1450 | do { |
| 1451 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1452 | } while (!eof() && !FormatTok->Tok.is(tok::colon)); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1453 | parseLabel(); |
| 1454 | } |
| 1455 | |
| 1456 | void UnwrappedLineParser::parseSwitch() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1457 | assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected"); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1458 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1459 | if (FormatTok->Tok.is(tok::l_paren)) |
Manuel Klimek | 9fa8d55 | 2013-01-11 19:23:05 +0000 | [diff] [blame] | 1460 | parseParens(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1461 | if (FormatTok->Tok.is(tok::l_brace)) { |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1462 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
Daniel Jasper | 65ee347 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 1463 | parseBlock(/*MustBeDeclaration=*/false); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1464 | addUnwrappedLine(); |
| 1465 | } else { |
| 1466 | addUnwrappedLine(); |
Daniel Jasper | 516d797 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 1467 | ++Line->Level; |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 1468 | parseStructuralElement(); |
Daniel Jasper | 516d797 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 1469 | --Line->Level; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | void UnwrappedLineParser::parseAccessSpecifier() { |
| 1474 | nextToken(); |
Daniel Jasper | 84c47a1 | 2013-11-23 17:53:41 +0000 | [diff] [blame] | 1475 | // Understand Qt's slots. |
Daniel Jasper | 5339540 | 2015-04-07 15:04:40 +0000 | [diff] [blame] | 1476 | if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots)) |
Daniel Jasper | 84c47a1 | 2013-11-23 17:53:41 +0000 | [diff] [blame] | 1477 | nextToken(); |
Alexander Kornienko | 2ca766f | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1478 | // 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] | 1479 | if (FormatTok->Tok.is(tok::colon)) |
Alexander Kornienko | 2ca766f | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1480 | nextToken(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1481 | addUnwrappedLine(); |
| 1482 | } |
| 1483 | |
| 1484 | void UnwrappedLineParser::parseEnum() { |
Daniel Jasper | 6be0f55 | 2014-11-13 15:56:28 +0000 | [diff] [blame] | 1485 | // Won't be 'enum' for NS_ENUMs. |
| 1486 | if (FormatTok->Tok.is(tok::kw_enum)) |
Daniel Jasper | ccb68b4 | 2014-11-19 22:38:18 +0000 | [diff] [blame] | 1487 | nextToken(); |
Daniel Jasper | 6be0f55 | 2014-11-13 15:56:28 +0000 | [diff] [blame] | 1488 | |
Daniel Jasper | 2b41a82 | 2013-08-20 12:42:50 +0000 | [diff] [blame] | 1489 | // Eat up enum class ... |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 1490 | if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct)) |
| 1491 | nextToken(); |
Daniel Jasper | 786a550 | 2013-09-06 21:32:35 +0000 | [diff] [blame] | 1492 | while (FormatTok->Tok.getIdentifierInfo() || |
Daniel Jasper | ccb68b4 | 2014-11-19 22:38:18 +0000 | [diff] [blame] | 1493 | FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less, |
| 1494 | tok::greater, tok::comma, tok::question)) { |
Manuel Klimek | 2cec019 | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 1495 | nextToken(); |
| 1496 | // 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] | 1497 | if (FormatTok->is(tok::l_paren)) |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 1498 | parseParens(); |
Daniel Jasper | ccb68b4 | 2014-11-19 22:38:18 +0000 | [diff] [blame] | 1499 | if (FormatTok->is(tok::identifier)) |
Manuel Klimek | 2cec019 | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 1500 | nextToken(); |
| 1501 | } |
Daniel Jasper | 6be0f55 | 2014-11-13 15:56:28 +0000 | [diff] [blame] | 1502 | |
| 1503 | // Just a declaration or something is wrong. |
Daniel Jasper | ccb68b4 | 2014-11-19 22:38:18 +0000 | [diff] [blame] | 1504 | if (FormatTok->isNot(tok::l_brace)) |
Daniel Jasper | 6be0f55 | 2014-11-13 15:56:28 +0000 | [diff] [blame] | 1505 | return; |
| 1506 | FormatTok->BlockKind = BK_Block; |
| 1507 | |
| 1508 | if (Style.Language == FormatStyle::LK_Java) { |
| 1509 | // Java enums are different. |
| 1510 | parseJavaEnumBody(); |
| 1511 | return; |
Manuel Klimek | 2cec019 | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 1512 | } |
Daniel Jasper | 6be0f55 | 2014-11-13 15:56:28 +0000 | [diff] [blame] | 1513 | |
| 1514 | // Parse enum body. |
| 1515 | bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true); |
| 1516 | if (HasError) { |
| 1517 | if (FormatTok->is(tok::semi)) |
| 1518 | nextToken(); |
| 1519 | addUnwrappedLine(); |
| 1520 | } |
| 1521 | |
Manuel Klimek | 2cec019 | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 1522 | // We fall through to parsing a structural element afterwards, so that in |
| 1523 | // enum A {} n, m; |
| 1524 | // "} n, m;" will end up in one unwrapped line. |
Daniel Jasper | 6be0f55 | 2014-11-13 15:56:28 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
| 1527 | void UnwrappedLineParser::parseJavaEnumBody() { |
| 1528 | // Determine whether the enum is simple, i.e. does not have a semicolon or |
| 1529 | // constants with class bodies. Simple enums can be formatted like braced |
| 1530 | // lists, contracted to a single line, etc. |
| 1531 | unsigned StoredPosition = Tokens->getPosition(); |
| 1532 | bool IsSimple = true; |
| 1533 | FormatToken *Tok = Tokens->getNextToken(); |
| 1534 | while (Tok) { |
| 1535 | if (Tok->is(tok::r_brace)) |
| 1536 | break; |
| 1537 | if (Tok->isOneOf(tok::l_brace, tok::semi)) { |
| 1538 | IsSimple = false; |
| 1539 | break; |
| 1540 | } |
| 1541 | // FIXME: This will also mark enums with braces in the arguments to enum |
| 1542 | // constants as "not simple". This is probably fine in practice, though. |
| 1543 | Tok = Tokens->getNextToken(); |
| 1544 | } |
| 1545 | FormatTok = Tokens->setPosition(StoredPosition); |
| 1546 | |
| 1547 | if (IsSimple) { |
| 1548 | parseBracedList(); |
Daniel Jasper | df2ff00 | 2014-11-02 22:31:39 +0000 | [diff] [blame] | 1549 | addUnwrappedLine(); |
Daniel Jasper | 6be0f55 | 2014-11-13 15:56:28 +0000 | [diff] [blame] | 1550 | return; |
| 1551 | } |
| 1552 | |
| 1553 | // Parse the body of a more complex enum. |
| 1554 | // First add a line for everything up to the "{". |
| 1555 | nextToken(); |
| 1556 | addUnwrappedLine(); |
| 1557 | ++Line->Level; |
| 1558 | |
| 1559 | // Parse the enum constants. |
| 1560 | while (FormatTok) { |
| 1561 | if (FormatTok->is(tok::l_brace)) { |
| 1562 | // Parse the constant's class body. |
| 1563 | parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true, |
| 1564 | /*MunchSemi=*/false); |
| 1565 | } else if (FormatTok->is(tok::l_paren)) { |
| 1566 | parseParens(); |
| 1567 | } else if (FormatTok->is(tok::comma)) { |
| 1568 | nextToken(); |
| 1569 | addUnwrappedLine(); |
| 1570 | } else if (FormatTok->is(tok::semi)) { |
| 1571 | nextToken(); |
| 1572 | addUnwrappedLine(); |
| 1573 | break; |
| 1574 | } else if (FormatTok->is(tok::r_brace)) { |
| 1575 | addUnwrappedLine(); |
| 1576 | break; |
| 1577 | } else { |
| 1578 | nextToken(); |
| 1579 | } |
| 1580 | } |
| 1581 | |
| 1582 | // Parse the class body after the enum's ";" if any. |
| 1583 | parseLevel(/*HasOpeningBrace=*/true); |
| 1584 | nextToken(); |
| 1585 | --Line->Level; |
| 1586 | addUnwrappedLine(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
Manuel Klimek | e01bab5 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 1589 | void UnwrappedLineParser::parseRecord() { |
Roman Kashitsyn | a043ced | 2014-08-11 12:18:01 +0000 | [diff] [blame] | 1590 | const FormatToken &InitialToken = *FormatTok; |
Manuel Klimek | 28cacc7 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 1591 | nextToken(); |
Daniel Jasper | 04785d0 | 2015-05-06 14:03:02 +0000 | [diff] [blame] | 1592 | |
| 1593 | |
| 1594 | // The actual identifier can be a nested name specifier, and in macros |
| 1595 | // it is often token-pasted. |
| 1596 | while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash, |
| 1597 | tok::kw___attribute, tok::kw___declspec, |
| 1598 | tok::kw_alignas) || |
| 1599 | ((Style.Language == FormatStyle::LK_Java || |
| 1600 | Style.Language == FormatStyle::LK_JavaScript) && |
| 1601 | FormatTok->isOneOf(tok::period, tok::comma))) { |
| 1602 | bool IsNonMacroIdentifier = |
| 1603 | FormatTok->is(tok::identifier) && |
| 1604 | FormatTok->TokenText != FormatTok->TokenText.upper(); |
Manuel Klimek | e01bab5 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 1605 | nextToken(); |
| 1606 | // 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] | 1607 | if (!IsNonMacroIdentifier && FormatTok->Tok.is(tok::l_paren)) |
Manuel Klimek | e01bab5 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 1608 | parseParens(); |
Daniel Jasper | 04785d0 | 2015-05-06 14:03:02 +0000 | [diff] [blame] | 1609 | } |
Manuel Klimek | e01bab5 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 1610 | |
Daniel Jasper | 04785d0 | 2015-05-06 14:03:02 +0000 | [diff] [blame] | 1611 | // Note that parsing away template declarations here leads to incorrectly |
| 1612 | // accepting function declarations as record declarations. |
| 1613 | // In general, we cannot solve this problem. Consider: |
| 1614 | // class A<int> B() {} |
| 1615 | // which can be a function definition or a class definition when B() is a |
| 1616 | // macro. If we find enough real-world cases where this is a problem, we |
| 1617 | // can parse for the 'template' keyword in the beginning of the statement, |
| 1618 | // and thus rule out the record production in case there is no template |
| 1619 | // (this would still leave us with an ambiguity between template function |
| 1620 | // and class declarations). |
Daniel Jasper | adba2aa | 2015-05-18 12:52:00 +0000 | [diff] [blame] | 1621 | if (FormatTok->isOneOf(tok::colon, tok::less)) { |
| 1622 | while (!eof()) { |
Daniel Jasper | 3c883d1 | 2015-05-18 14:49:19 +0000 | [diff] [blame] | 1623 | if (FormatTok->is(tok::l_brace)) { |
| 1624 | calculateBraceTypes(/*ExpectClassBody=*/true); |
| 1625 | if (!tryToParseBracedList()) |
| 1626 | break; |
| 1627 | } |
Daniel Jasper | 04785d0 | 2015-05-06 14:03:02 +0000 | [diff] [blame] | 1628 | if (FormatTok->Tok.is(tok::semi)) |
| 1629 | return; |
| 1630 | nextToken(); |
Manuel Klimek | e01bab5 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 1631 | } |
| 1632 | } |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1633 | if (FormatTok->Tok.is(tok::l_brace)) { |
Roman Kashitsyn | a043ced | 2014-08-11 12:18:01 +0000 | [diff] [blame] | 1634 | if (ShouldBreakBeforeBrace(Style, InitialToken)) |
Manuel Klimek | a8eb914 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 1635 | addUnwrappedLine(); |
| 1636 | |
Daniel Jasper | 240dfda | 2014-03-31 14:23:49 +0000 | [diff] [blame] | 1637 | parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true, |
Manuel Klimek | b212f3b | 2013-10-12 22:46:56 +0000 | [diff] [blame] | 1638 | /*MunchSemi=*/false); |
Manuel Klimek | a8eb914 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 1639 | } |
Manuel Klimek | cdee74d | 2013-01-21 13:58:54 +0000 | [diff] [blame] | 1640 | // We fall through to parsing a structural element afterwards, so |
| 1641 | // class A {} n, m; |
| 1642 | // will end up in one unwrapped line. |
Daniel Jasper | 9326f91 | 2015-05-05 08:40:32 +0000 | [diff] [blame] | 1643 | // This does not apply for Java and JavaScript. |
Daniel Jasper | 6fa9ec7 | 2015-02-19 16:03:16 +0000 | [diff] [blame] | 1644 | if (Style.Language == FormatStyle::LK_Java || |
| 1645 | Style.Language == FormatStyle::LK_JavaScript) |
Daniel Jasper | c58c70e | 2014-09-15 11:21:46 +0000 | [diff] [blame] | 1646 | addUnwrappedLine(); |
Manuel Klimek | 28cacc7 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1649 | void UnwrappedLineParser::parseObjCProtocolList() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1650 | assert(FormatTok->Tok.is(tok::less) && "'<' expected."); |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1651 | do |
| 1652 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1653 | while (!eof() && FormatTok->Tok.isNot(tok::greater)); |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1654 | nextToken(); // Skip '>'. |
| 1655 | } |
| 1656 | |
| 1657 | void UnwrappedLineParser::parseObjCUntilAtEnd() { |
| 1658 | do { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1659 | if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) { |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1660 | nextToken(); |
| 1661 | addUnwrappedLine(); |
| 1662 | break; |
| 1663 | } |
Daniel Jasper | a15da30 | 2013-08-28 08:04:23 +0000 | [diff] [blame] | 1664 | if (FormatTok->is(tok::l_brace)) { |
| 1665 | parseBlock(/*MustBeDeclaration=*/false); |
| 1666 | // In ObjC interfaces, nothing should be following the "}". |
| 1667 | addUnwrappedLine(); |
Benjamin Kramer | e21cb74 | 2014-01-08 15:59:42 +0000 | [diff] [blame] | 1668 | } else if (FormatTok->is(tok::r_brace)) { |
| 1669 | // Ignore stray "}". parseStructuralElement doesn't consume them. |
| 1670 | nextToken(); |
| 1671 | addUnwrappedLine(); |
Daniel Jasper | a15da30 | 2013-08-28 08:04:23 +0000 | [diff] [blame] | 1672 | } else { |
| 1673 | parseStructuralElement(); |
| 1674 | } |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1675 | } while (!eof()); |
| 1676 | } |
| 1677 | |
Nico Weber | 2ce0ac5 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 1678 | void UnwrappedLineParser::parseObjCInterfaceOrImplementation() { |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1679 | nextToken(); |
Daniel Jasper | d1ae358 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 1680 | nextToken(); // interface name |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1681 | |
| 1682 | // @interface can be followed by either a base class, or a category. |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1683 | if (FormatTok->Tok.is(tok::colon)) { |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1684 | nextToken(); |
Daniel Jasper | d1ae358 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 1685 | nextToken(); // base class name |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1686 | } else if (FormatTok->Tok.is(tok::l_paren)) |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1687 | // Skip category, if present. |
| 1688 | parseParens(); |
| 1689 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1690 | if (FormatTok->Tok.is(tok::less)) |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1691 | parseObjCProtocolList(); |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1692 | |
Dinesh Dwivedi | ea3aca8 | 2014-05-02 17:01:46 +0000 | [diff] [blame] | 1693 | if (FormatTok->Tok.is(tok::l_brace)) { |
| 1694 | if (Style.BreakBeforeBraces == FormatStyle::BS_Allman || |
| 1695 | Style.BreakBeforeBraces == FormatStyle::BS_GNU) |
| 1696 | addUnwrappedLine(); |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 1697 | parseBlock(/*MustBeDeclaration=*/true); |
Dinesh Dwivedi | ea3aca8 | 2014-05-02 17:01:46 +0000 | [diff] [blame] | 1698 | } |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1699 | |
| 1700 | // With instance variables, this puts '}' on its own line. Without instance |
| 1701 | // variables, this ends the @interface line. |
| 1702 | addUnwrappedLine(); |
| 1703 | |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1704 | parseObjCUntilAtEnd(); |
| 1705 | } |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1706 | |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1707 | void UnwrappedLineParser::parseObjCProtocol() { |
| 1708 | nextToken(); |
Daniel Jasper | d1ae358 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 1709 | nextToken(); // protocol name |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1710 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1711 | if (FormatTok->Tok.is(tok::less)) |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1712 | parseObjCProtocolList(); |
| 1713 | |
| 1714 | // Check for protocol declaration. |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1715 | if (FormatTok->Tok.is(tok::semi)) { |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1716 | nextToken(); |
| 1717 | return addUnwrappedLine(); |
| 1718 | } |
| 1719 | |
| 1720 | addUnwrappedLine(); |
| 1721 | parseObjCUntilAtEnd(); |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1722 | } |
| 1723 | |
Daniel Jasper | fca735c | 2015-02-19 16:14:18 +0000 | [diff] [blame] | 1724 | void UnwrappedLineParser::parseJavaScriptEs6ImportExport() { |
| 1725 | assert(FormatTok->isOneOf(Keywords.kw_import, tok::kw_export)); |
Daniel Jasper | 354aa51 | 2015-02-19 16:07:32 +0000 | [diff] [blame] | 1726 | nextToken(); |
Daniel Jasper | fca735c | 2015-02-19 16:14:18 +0000 | [diff] [blame] | 1727 | |
Daniel Jasper | ec05fc7 | 2015-05-11 09:14:50 +0000 | [diff] [blame] | 1728 | // Consume the "default" in "export default class/function". |
Daniel Jasper | 668c7bb | 2015-05-11 09:03:10 +0000 | [diff] [blame] | 1729 | if (FormatTok->is(tok::kw_default)) |
| 1730 | nextToken(); |
Daniel Jasper | ec05fc7 | 2015-05-11 09:14:50 +0000 | [diff] [blame] | 1731 | |
| 1732 | // Consume "function" and "default function", so that these get parsed as |
| 1733 | // free-standing JS functions, i.e. do not require a trailing semicolon. |
Daniel Jasper | 668c7bb | 2015-05-11 09:03:10 +0000 | [diff] [blame] | 1734 | if (FormatTok->is(Keywords.kw_function)) { |
| 1735 | nextToken(); |
| 1736 | return; |
| 1737 | } |
| 1738 | |
| 1739 | if (FormatTok->isOneOf(tok::kw_const, tok::kw_class, Keywords.kw_var)) |
Daniel Jasper | fca735c | 2015-02-19 16:14:18 +0000 | [diff] [blame] | 1740 | return; // Fall through to parsing the corresponding structure. |
| 1741 | |
Daniel Jasper | 354aa51 | 2015-02-19 16:07:32 +0000 | [diff] [blame] | 1742 | if (FormatTok->is(tok::l_brace)) { |
| 1743 | FormatTok->BlockKind = BK_Block; |
| 1744 | parseBracedList(); |
| 1745 | } |
Daniel Jasper | fca735c | 2015-02-19 16:14:18 +0000 | [diff] [blame] | 1746 | |
Daniel Jasper | 354aa51 | 2015-02-19 16:07:32 +0000 | [diff] [blame] | 1747 | while (!eof() && FormatTok->isNot(tok::semi) && |
| 1748 | FormatTok->isNot(tok::l_brace)) { |
| 1749 | nextToken(); |
| 1750 | } |
| 1751 | } |
| 1752 | |
Daniel Jasper | 3b203a6 | 2013-09-05 16:05:56 +0000 | [diff] [blame] | 1753 | LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line, |
| 1754 | StringRef Prefix = "") { |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1755 | llvm::dbgs() << Prefix << "Line(" << Line.Level << ")" |
| 1756 | << (Line.InPPDirective ? " MACRO" : "") << ": "; |
| 1757 | for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(), |
| 1758 | E = Line.Tokens.end(); |
| 1759 | I != E; ++I) { |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 1760 | llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] "; |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1761 | } |
| 1762 | for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(), |
| 1763 | E = Line.Tokens.end(); |
| 1764 | I != E; ++I) { |
| 1765 | const UnwrappedLineNode &Node = *I; |
| 1766 | for (SmallVectorImpl<UnwrappedLine>::const_iterator |
| 1767 | I = Node.Children.begin(), |
| 1768 | E = Node.Children.end(); |
| 1769 | I != E; ++I) { |
| 1770 | printDebugInfo(*I, "\nChild: "); |
| 1771 | } |
| 1772 | } |
| 1773 | llvm::dbgs() << "\n"; |
| 1774 | } |
| 1775 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1776 | void UnwrappedLineParser::addUnwrappedLine() { |
Daniel Jasper | daffc0d | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 1777 | if (Line->Tokens.empty()) |
Daniel Jasper | 7c85fde | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1778 | return; |
Manuel Klimek | ab3dc00 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 1779 | DEBUG({ |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1780 | if (CurrentLines == &Lines) |
| 1781 | printDebugInfo(*Line); |
Manuel Klimek | ab3dc00 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 1782 | }); |
Manuel Klimek | d3b92fa | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 1783 | CurrentLines->push_back(*Line); |
Daniel Jasper | daffc0d | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 1784 | Line->Tokens.clear(); |
Manuel Klimek | d3b92fa | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 1785 | if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) { |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1786 | for (SmallVectorImpl<UnwrappedLine>::iterator |
Daniel Jasper | a79064a | 2013-03-01 18:11:39 +0000 | [diff] [blame] | 1787 | I = PreprocessorDirectives.begin(), |
| 1788 | E = PreprocessorDirectives.end(); |
Manuel Klimek | d3b92fa | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 1789 | I != E; ++I) { |
| 1790 | CurrentLines->push_back(*I); |
| 1791 | } |
| 1792 | PreprocessorDirectives.clear(); |
| 1793 | } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1796 | bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1797 | |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 1798 | bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) { |
Manuel Klimek | 1fcbe67 | 2014-04-11 12:27:47 +0000 | [diff] [blame] | 1799 | return (Line->InPPDirective || FormatTok.HasUnescapedNewline) && |
| 1800 | FormatTok.NewlinesBefore > 0; |
| 1801 | } |
| 1802 | |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1803 | void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) { |
| 1804 | bool JustComments = Line->Tokens.empty(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1805 | for (SmallVectorImpl<FormatToken *>::const_iterator |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1806 | I = CommentsBeforeNextToken.begin(), |
| 1807 | E = CommentsBeforeNextToken.end(); |
| 1808 | I != E; ++I) { |
Daniel Jasper | e60cba1 | 2015-05-13 11:35:53 +0000 | [diff] [blame] | 1809 | if (isOnNewLine(**I) && JustComments) |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1810 | addUnwrappedLine(); |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1811 | pushToken(*I); |
| 1812 | } |
Daniel Jasper | e60cba1 | 2015-05-13 11:35:53 +0000 | [diff] [blame] | 1813 | if (NewlineBeforeNext && JustComments) |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1814 | addUnwrappedLine(); |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1815 | CommentsBeforeNextToken.clear(); |
| 1816 | } |
| 1817 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1818 | void UnwrappedLineParser::nextToken() { |
| 1819 | if (eof()) |
| 1820 | return; |
Manuel Klimek | 1fcbe67 | 2014-04-11 12:27:47 +0000 | [diff] [blame] | 1821 | flushComments(isOnNewLine(*FormatTok)); |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1822 | pushToken(FormatTok); |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1823 | readToken(); |
| 1824 | } |
| 1825 | |
| 1826 | void UnwrappedLineParser::readToken() { |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1827 | bool CommentsInCurrentLine = true; |
| 1828 | do { |
| 1829 | FormatTok = Tokens->getNextToken(); |
Alexander Kornienko | c2ee9cf | 2014-03-13 13:59:48 +0000 | [diff] [blame] | 1830 | assert(FormatTok); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1831 | while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) && |
| 1832 | (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) { |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1833 | // If there is an unfinished unwrapped line, we flush the preprocessor |
| 1834 | // directives only after that unwrapped line was finished later. |
Daniel Jasper | 29d39d5 | 2015-02-08 09:34:49 +0000 | [diff] [blame] | 1835 | bool SwitchToPreprocessorLines = !Line->Tokens.empty(); |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1836 | ScopedLineState BlockState(*this, SwitchToPreprocessorLines); |
Alexander Kornienko | b1be9d6 | 2013-04-03 12:38:53 +0000 | [diff] [blame] | 1837 | // Comments stored before the preprocessor directive need to be output |
| 1838 | // before the preprocessor directive, at the same level as the |
| 1839 | // preprocessor directive, as we consider them to apply to the directive. |
Manuel Klimek | 1fcbe67 | 2014-04-11 12:27:47 +0000 | [diff] [blame] | 1840 | flushComments(isOnNewLine(*FormatTok)); |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1841 | parsePPDirective(); |
| 1842 | } |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 1843 | while (FormatTok->Type == TT_ConflictStart || |
| 1844 | FormatTok->Type == TT_ConflictEnd || |
| 1845 | FormatTok->Type == TT_ConflictAlternative) { |
| 1846 | if (FormatTok->Type == TT_ConflictStart) { |
| 1847 | conditionalCompilationStart(/*Unreachable=*/false); |
| 1848 | } else if (FormatTok->Type == TT_ConflictAlternative) { |
| 1849 | conditionalCompilationAlternative(); |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 1850 | } else if (FormatTok->Type == TT_ConflictEnd) { |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 1851 | conditionalCompilationEnd(); |
| 1852 | } |
| 1853 | FormatTok = Tokens->getNextToken(); |
| 1854 | FormatTok->MustBreakBefore = true; |
| 1855 | } |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 1856 | |
| 1857 | if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) && |
| 1858 | !Line->InPPDirective) { |
| 1859 | continue; |
| 1860 | } |
| 1861 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1862 | if (!FormatTok->Tok.is(tok::comment)) |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1863 | return; |
Manuel Klimek | 1fcbe67 | 2014-04-11 12:27:47 +0000 | [diff] [blame] | 1864 | if (isOnNewLine(*FormatTok) || FormatTok->IsFirst) { |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1865 | CommentsInCurrentLine = false; |
| 1866 | } |
| 1867 | if (CommentsInCurrentLine) { |
| 1868 | pushToken(FormatTok); |
| 1869 | } else { |
| 1870 | CommentsBeforeNextToken.push_back(FormatTok); |
| 1871 | } |
| 1872 | } while (!eof()); |
| 1873 | } |
| 1874 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1875 | void UnwrappedLineParser::pushToken(FormatToken *Tok) { |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1876 | Line->Tokens.push_back(UnwrappedLineNode(Tok)); |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1877 | if (MustBreakBeforeNextToken) { |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1878 | Line->Tokens.back().Tok->MustBreakBefore = true; |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1879 | MustBreakBeforeNextToken = false; |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1880 | } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1881 | } |
| 1882 | |
Daniel Jasper | 8d1832e | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1883 | } // end namespace format |
| 1884 | } // end namespace clang |