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