Daniel Jasper | bac016b | 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 | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Manuel Klimek | 8fa3799 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 16 | #define DEBUG_TYPE "format-parser" |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 17 | |
Chandler Carruth | b1ba0ef | 2013-01-19 08:09:44 +0000 | [diff] [blame] | 18 | #include "UnwrappedLineParser.h" |
Manuel Klimek | 8fa3799 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Debug.h" |
Manuel Klimek | 8fa3799 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 20 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 21 | namespace clang { |
| 22 | namespace format { |
| 23 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 24 | class FormatTokenSource { |
| 25 | public: |
| 26 | virtual ~FormatTokenSource() {} |
| 27 | virtual FormatToken *getNextToken() = 0; |
| 28 | |
| 29 | virtual unsigned getPosition() = 0; |
| 30 | virtual FormatToken *setPosition(unsigned Position) = 0; |
| 31 | }; |
| 32 | |
Craig Topper | e50947f | 2013-07-01 04:21:54 +0000 | [diff] [blame] | 33 | namespace { |
| 34 | |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 35 | class ScopedDeclarationState { |
| 36 | public: |
| 37 | ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack, |
| 38 | bool MustBeDeclaration) |
| 39 | : Line(Line), Stack(Stack) { |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 40 | Line.MustBeDeclaration = MustBeDeclaration; |
Manuel Klimek | 836b58f | 2013-01-23 11:03:04 +0000 | [diff] [blame] | 41 | Stack.push_back(MustBeDeclaration); |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 42 | } |
| 43 | ~ScopedDeclarationState() { |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 44 | Stack.pop_back(); |
Manuel Klimek | a32a7fd | 2013-01-23 14:08:21 +0000 | [diff] [blame] | 45 | if (!Stack.empty()) |
| 46 | Line.MustBeDeclaration = Stack.back(); |
| 47 | else |
| 48 | Line.MustBeDeclaration = true; |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 49 | } |
Daniel Jasper | f7ec1cc | 2013-05-31 14:56:29 +0000 | [diff] [blame] | 50 | |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 51 | private: |
| 52 | UnwrappedLine &Line; |
| 53 | std::vector<bool> &Stack; |
| 54 | }; |
| 55 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 56 | class ScopedMacroState : public FormatTokenSource { |
| 57 | public: |
| 58 | ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource, |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 59 | FormatToken *&ResetToken, bool &StructuralError) |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 60 | : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken), |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 61 | PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource), |
| 62 | StructuralError(StructuralError), |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 63 | PreviousStructuralError(StructuralError), Token(NULL) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 64 | TokenSource = this; |
Manuel Klimek | c37b4d6 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 65 | Line.Level = 0; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 66 | Line.InPPDirective = true; |
| 67 | } |
| 68 | |
| 69 | ~ScopedMacroState() { |
| 70 | TokenSource = PreviousTokenSource; |
| 71 | ResetToken = Token; |
| 72 | Line.InPPDirective = false; |
Manuel Klimek | c37b4d6 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 73 | Line.Level = PreviousLineLevel; |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 74 | StructuralError = PreviousStructuralError; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 77 | virtual FormatToken *getNextToken() { |
Manuel Klimek | dd5b101 | 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 | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 81 | Token = PreviousTokenSource->getNextToken(); |
| 82 | if (eof()) |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 83 | return getFakeEOF(); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 84 | return Token; |
| 85 | } |
| 86 | |
Daniel Jasper | f7ec1cc | 2013-05-31 14:56:29 +0000 | [diff] [blame] | 87 | virtual unsigned getPosition() { return PreviousTokenSource->getPosition(); } |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 88 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 89 | virtual FormatToken *setPosition(unsigned Position) { |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 90 | Token = PreviousTokenSource->setPosition(Position); |
| 91 | return Token; |
| 92 | } |
| 93 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 94 | private: |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 95 | bool eof() { return Token && Token->HasUnescapedNewline; } |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 96 | |
Manuel Klimek | 96e888b | 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 | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | UnwrappedLine &Line; |
| 109 | FormatTokenSource *&TokenSource; |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 110 | FormatToken *&ResetToken; |
Manuel Klimek | c37b4d6 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 111 | unsigned PreviousLineLevel; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 112 | FormatTokenSource *PreviousTokenSource; |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 113 | bool &StructuralError; |
| 114 | bool PreviousStructuralError; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 115 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 116 | FormatToken *Token; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
Craig Topper | e50947f | 2013-07-01 04:21:54 +0000 | [diff] [blame] | 119 | } // end anonymous namespace |
| 120 | |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 121 | class ScopedLineState { |
| 122 | public: |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 123 | ScopedLineState(UnwrappedLineParser &Parser, |
| 124 | bool SwitchToPreprocessorLines = false) |
| 125 | : Parser(Parser), SwitchToPreprocessorLines(SwitchToPreprocessorLines) { |
| 126 | if (SwitchToPreprocessorLines) |
| 127 | Parser.CurrentLines = &Parser.PreprocessorDirectives; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 128 | PreBlockLine = Parser.Line.take(); |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 129 | Parser.Line.reset(new UnwrappedLine()); |
| 130 | Parser.Line->Level = PreBlockLine->Level; |
| 131 | Parser.Line->InPPDirective = PreBlockLine->InPPDirective; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | ~ScopedLineState() { |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 135 | if (!Parser.Line->Tokens.empty()) { |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 136 | Parser.addUnwrappedLine(); |
| 137 | } |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 138 | assert(Parser.Line->Tokens.empty()); |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 139 | Parser.Line.reset(PreBlockLine); |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 140 | Parser.MustBreakBeforeNextToken = true; |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 141 | if (SwitchToPreprocessorLines) |
| 142 | Parser.CurrentLines = &Parser.Lines; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | private: |
| 146 | UnwrappedLineParser &Parser; |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 147 | const bool SwitchToPreprocessorLines; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 148 | |
| 149 | UnwrappedLine *PreBlockLine; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 150 | }; |
| 151 | |
Craig Topper | e50947f | 2013-07-01 04:21:54 +0000 | [diff] [blame] | 152 | namespace { |
| 153 | |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 154 | class IndexedTokenSource : public FormatTokenSource { |
| 155 | public: |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 156 | IndexedTokenSource(ArrayRef<FormatToken *> Tokens) |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 157 | : Tokens(Tokens), Position(-1) {} |
| 158 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 159 | virtual FormatToken *getNextToken() { |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 160 | ++Position; |
| 161 | return Tokens[Position]; |
| 162 | } |
| 163 | |
| 164 | virtual unsigned getPosition() { |
| 165 | assert(Position >= 0); |
| 166 | return Position; |
| 167 | } |
| 168 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 169 | virtual FormatToken *setPosition(unsigned P) { |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 170 | Position = P; |
| 171 | return Tokens[Position]; |
| 172 | } |
| 173 | |
| 174 | private: |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 175 | ArrayRef<FormatToken *> Tokens; |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 176 | int Position; |
| 177 | }; |
| 178 | |
Craig Topper | e50947f | 2013-07-01 04:21:54 +0000 | [diff] [blame] | 179 | } // end anonymous namespace |
| 180 | |
Daniel Jasper | caf42a3 | 2013-05-15 08:14:19 +0000 | [diff] [blame] | 181 | UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style, |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 182 | ArrayRef<FormatToken *> Tokens, |
Daniel Jasper | caf42a3 | 2013-05-15 08:14:19 +0000 | [diff] [blame] | 183 | UnwrappedLineConsumer &Callback) |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 184 | : Line(new UnwrappedLine), MustBreakBeforeNextToken(false), |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 185 | CurrentLines(&Lines), StructuralError(false), Style(Style), Tokens(NULL), |
| 186 | Callback(Callback), AllTokens(Tokens) { |
| 187 | LBraces.resize(Tokens.size(), BS_Unknown); |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 188 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 189 | |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 190 | bool UnwrappedLineParser::parse() { |
Manuel Klimek | 8fa3799 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 191 | DEBUG(llvm::dbgs() << "----\n"); |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 192 | IndexedTokenSource TokenSource(AllTokens); |
| 193 | Tokens = &TokenSource; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 194 | readToken(); |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 195 | parseFile(); |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 196 | for (std::vector<UnwrappedLine>::iterator I = Lines.begin(), E = Lines.end(); |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 197 | I != E; ++I) { |
| 198 | Callback.consumeUnwrappedLine(*I); |
| 199 | } |
Daniel Jasper | 516fb31 | 2013-03-01 18:11:39 +0000 | [diff] [blame] | 200 | |
| 201 | // Create line with eof token. |
| 202 | pushToken(FormatTok); |
| 203 | Callback.consumeUnwrappedLine(*Line); |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 204 | return StructuralError; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 207 | void UnwrappedLineParser::parseFile() { |
Daniel Jasper | 627707b | 2013-03-22 16:55:40 +0000 | [diff] [blame] | 208 | ScopedDeclarationState DeclarationState( |
| 209 | *Line, DeclarationScopeStack, |
| 210 | /*MustBeDeclaration=*/ !Line->InPPDirective); |
Nico Weber | 2726877 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 211 | parseLevel(/*HasOpeningBrace=*/false); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 212 | // Make sure to format the remaining tokens. |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 213 | flushComments(true); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 214 | addUnwrappedLine(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 217 | void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 218 | do { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 219 | switch (FormatTok->Tok.getKind()) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 220 | case tok::comment: |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 221 | nextToken(); |
| 222 | addUnwrappedLine(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 223 | break; |
| 224 | case tok::l_brace: |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 225 | // FIXME: Add parameter whether this can happen - if this happens, we must |
| 226 | // be in a non-declaration context. |
Nico Weber | 2726877 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 227 | parseBlock(/*MustBeDeclaration=*/false); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 228 | addUnwrappedLine(); |
| 229 | break; |
| 230 | case tok::r_brace: |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 231 | if (HasOpeningBrace) |
| 232 | return; |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 233 | StructuralError = true; |
| 234 | nextToken(); |
| 235 | addUnwrappedLine(); |
Manuel Klimek | a5342db | 2013-01-06 20:07:31 +0000 | [diff] [blame] | 236 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 237 | default: |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 238 | parseStructuralElement(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 239 | break; |
| 240 | } |
| 241 | } while (!eof()); |
| 242 | } |
| 243 | |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 244 | void UnwrappedLineParser::calculateBraceTypes() { |
| 245 | // We'll parse forward through the tokens until we hit |
| 246 | // a closing brace or eof - note that getNextToken() will |
| 247 | // parse macros, so this will magically work inside macro |
| 248 | // definitions, too. |
| 249 | unsigned StoredPosition = Tokens->getPosition(); |
| 250 | unsigned Position = StoredPosition; |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 251 | FormatToken *Tok = FormatTok; |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 252 | // Keep a stack of positions of lbrace tokens. We will |
| 253 | // update information about whether an lbrace starts a |
| 254 | // braced init list or a different block during the loop. |
| 255 | SmallVector<unsigned, 8> LBraceStack; |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 256 | assert(Tok->Tok.is(tok::l_brace)); |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 257 | do { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 258 | FormatToken *NextTok = Tokens->getNextToken(); |
| 259 | switch (Tok->Tok.getKind()) { |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 260 | case tok::l_brace: |
| 261 | LBraceStack.push_back(Position); |
| 262 | break; |
| 263 | case tok::r_brace: |
| 264 | if (!LBraceStack.empty()) { |
| 265 | if (LBraces[LBraceStack.back()] == BS_Unknown) { |
| 266 | // If there is a comma, semicolon or right paren after the closing |
| 267 | // brace, we assume this is a braced initializer list. |
| 268 | |
| 269 | // FIXME: Note that this currently works only because we do not |
| 270 | // use the brace information while inside a braced init list. |
| 271 | // Thus, if the parent is a braced init list, we consider all |
| 272 | // brace blocks inside it braced init list. That works good enough |
| 273 | // for now, but we will need to fix it to correctly handle lambdas. |
Daniel Jasper | eb48366 | 2013-05-31 10:09:55 +0000 | [diff] [blame] | 274 | if (NextTok->isOneOf(tok::comma, tok::semi, tok::r_paren, |
| 275 | tok::l_brace, tok::colon)) |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 276 | LBraces[LBraceStack.back()] = BS_BracedInit; |
| 277 | else |
| 278 | LBraces[LBraceStack.back()] = BS_Block; |
| 279 | } |
| 280 | LBraceStack.pop_back(); |
| 281 | } |
| 282 | break; |
| 283 | case tok::semi: |
| 284 | case tok::kw_if: |
| 285 | case tok::kw_while: |
| 286 | case tok::kw_for: |
| 287 | case tok::kw_switch: |
| 288 | case tok::kw_try: |
Daniel Jasper | f7ec1cc | 2013-05-31 14:56:29 +0000 | [diff] [blame] | 289 | if (!LBraceStack.empty()) |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 290 | LBraces[LBraceStack.back()] = BS_Block; |
| 291 | break; |
| 292 | default: |
| 293 | break; |
| 294 | } |
| 295 | Tok = NextTok; |
| 296 | ++Position; |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 297 | } while (Tok->Tok.isNot(tok::eof)); |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 298 | // Assume other blocks for all unclosed opening braces. |
| 299 | for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) { |
| 300 | if (LBraces[LBraceStack[i]] == BS_Unknown) |
| 301 | LBraces[LBraceStack[i]] = BS_Block; |
| 302 | } |
| 303 | FormatTok = Tokens->setPosition(StoredPosition); |
| 304 | } |
| 305 | |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 306 | void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, |
Nico Weber | d74fcdb | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 307 | unsigned AddLevels) { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 308 | assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 309 | nextToken(); |
| 310 | |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 311 | addUnwrappedLine(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 312 | |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 313 | ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, |
| 314 | MustBeDeclaration); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 315 | Line->Level += AddLevels; |
Nico Weber | 2726877 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 316 | parseLevel(/*HasOpeningBrace=*/true); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 317 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 318 | if (!FormatTok->Tok.is(tok::r_brace)) { |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 319 | Line->Level -= AddLevels; |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 320 | StructuralError = true; |
| 321 | return; |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 322 | } |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 323 | |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 324 | nextToken(); // Munch the closing brace. |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 325 | Line->Level -= AddLevels; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | void UnwrappedLineParser::parsePPDirective() { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 329 | assert(FormatTok->Tok.is(tok::hash) && "'#' expected"); |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 330 | ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 331 | nextToken(); |
| 332 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 333 | if (FormatTok->Tok.getIdentifierInfo() == NULL) { |
Manuel Klimek | bd04f2a | 2013-01-31 15:58:48 +0000 | [diff] [blame] | 334 | parsePPUnknown(); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 335 | return; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 336 | } |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 337 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 338 | switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 339 | case tok::pp_define: |
| 340 | parsePPDefine(); |
Alexander Kornienko | 6fb46b0 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 341 | return; |
| 342 | case tok::pp_if: |
| 343 | parsePPIf(); |
| 344 | break; |
| 345 | case tok::pp_ifdef: |
| 346 | case tok::pp_ifndef: |
| 347 | parsePPIfdef(); |
| 348 | break; |
| 349 | case tok::pp_else: |
| 350 | parsePPElse(); |
| 351 | break; |
| 352 | case tok::pp_elif: |
| 353 | parsePPElIf(); |
| 354 | break; |
| 355 | case tok::pp_endif: |
| 356 | parsePPEndIf(); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 357 | break; |
| 358 | default: |
| 359 | parsePPUnknown(); |
| 360 | break; |
| 361 | } |
| 362 | } |
| 363 | |
Alexander Kornienko | 6fb46b0 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 364 | void UnwrappedLineParser::pushPPConditional() { |
| 365 | if (!PPStack.empty() && PPStack.back() == PP_Unreachable) |
| 366 | PPStack.push_back(PP_Unreachable); |
| 367 | else |
| 368 | PPStack.push_back(PP_Conditional); |
| 369 | } |
| 370 | |
| 371 | void UnwrappedLineParser::parsePPIf() { |
| 372 | nextToken(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 373 | if ((FormatTok->Tok.isLiteral() && |
| 374 | StringRef(FormatTok->Tok.getLiteralData(), FormatTok->Tok.getLength()) == |
Alexander Kornienko | 6fb46b0 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 375 | "0") || |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 376 | FormatTok->Tok.is(tok::kw_false)) { |
Alexander Kornienko | 6fb46b0 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 377 | PPStack.push_back(PP_Unreachable); |
| 378 | } else { |
| 379 | pushPPConditional(); |
| 380 | } |
| 381 | parsePPUnknown(); |
| 382 | } |
| 383 | |
| 384 | void UnwrappedLineParser::parsePPIfdef() { |
| 385 | pushPPConditional(); |
| 386 | parsePPUnknown(); |
| 387 | } |
| 388 | |
| 389 | void UnwrappedLineParser::parsePPElse() { |
| 390 | if (!PPStack.empty()) |
| 391 | PPStack.pop_back(); |
| 392 | pushPPConditional(); |
| 393 | parsePPUnknown(); |
| 394 | } |
| 395 | |
Daniel Jasper | f7ec1cc | 2013-05-31 14:56:29 +0000 | [diff] [blame] | 396 | void UnwrappedLineParser::parsePPElIf() { parsePPElse(); } |
Alexander Kornienko | 6fb46b0 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 397 | |
| 398 | void UnwrappedLineParser::parsePPEndIf() { |
| 399 | if (!PPStack.empty()) |
| 400 | PPStack.pop_back(); |
| 401 | parsePPUnknown(); |
| 402 | } |
| 403 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 404 | void UnwrappedLineParser::parsePPDefine() { |
| 405 | nextToken(); |
| 406 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 407 | if (FormatTok->Tok.getKind() != tok::identifier) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 408 | parsePPUnknown(); |
| 409 | return; |
| 410 | } |
| 411 | nextToken(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 412 | if (FormatTok->Tok.getKind() == tok::l_paren && |
| 413 | FormatTok->WhitespaceRange.getBegin() == |
| 414 | FormatTok->WhitespaceRange.getEnd()) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 415 | parseParens(); |
| 416 | } |
| 417 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 418 | Line->Level = 1; |
Manuel Klimek | c3d0c82 | 2013-01-07 09:34:28 +0000 | [diff] [blame] | 419 | |
| 420 | // Errors during a preprocessor directive can only affect the layout of the |
| 421 | // preprocessor directive, and thus we ignore them. An alternative approach |
| 422 | // would be to use the same approach we use on the file level (no |
| 423 | // re-indentation if there was a structural error) within the macro |
| 424 | // definition. |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 425 | parseFile(); |
| 426 | } |
| 427 | |
| 428 | void UnwrappedLineParser::parsePPUnknown() { |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 429 | do { |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 430 | nextToken(); |
| 431 | } while (!eof()); |
| 432 | addUnwrappedLine(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Alexander Kornienko | 99b0e14 | 2013-04-09 16:15:19 +0000 | [diff] [blame] | 435 | // Here we blacklist certain tokens that are not usually the first token in an |
| 436 | // unwrapped line. This is used in attempt to distinguish macro calls without |
| 437 | // trailing semicolons from other constructs split to several lines. |
| 438 | bool tokenCanStartNewLine(clang::Token Tok) { |
| 439 | // Semicolon can be a null-statement, l_square can be a start of a macro or |
| 440 | // a C++11 attribute, but this doesn't seem to be common. |
| 441 | return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) && |
| 442 | Tok.isNot(tok::l_square) && |
| 443 | // Tokens that can only be used as binary operators and a part of |
| 444 | // overloaded operator names. |
| 445 | Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) && |
| 446 | Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) && |
| 447 | Tok.isNot(tok::less) && Tok.isNot(tok::greater) && |
| 448 | Tok.isNot(tok::slash) && Tok.isNot(tok::percent) && |
| 449 | Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) && |
| 450 | Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) && |
| 451 | Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) && |
| 452 | Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) && |
| 453 | Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) && |
| 454 | Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) && |
| 455 | Tok.isNot(tok::lesslessequal) && |
| 456 | // Colon is used in labels, base class lists, initializer lists, |
| 457 | // range-based for loops, ternary operator, but should never be the |
| 458 | // first token in an unwrapped line. |
| 459 | Tok.isNot(tok::colon); |
| 460 | } |
| 461 | |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 462 | void UnwrappedLineParser::parseStructuralElement() { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 463 | assert(!FormatTok->Tok.is(tok::l_brace)); |
| 464 | switch (FormatTok->Tok.getKind()) { |
Nico Weber | 6092d4e | 2013-01-07 19:05:19 +0000 | [diff] [blame] | 465 | case tok::at: |
| 466 | nextToken(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 467 | if (FormatTok->Tok.is(tok::l_brace)) { |
Nico Weber | d74fcdb | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 468 | parseBracedList(); |
| 469 | break; |
| 470 | } |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 471 | switch (FormatTok->Tok.getObjCKeywordID()) { |
Nico Weber | 6092d4e | 2013-01-07 19:05:19 +0000 | [diff] [blame] | 472 | case tok::objc_public: |
| 473 | case tok::objc_protected: |
| 474 | case tok::objc_package: |
| 475 | case tok::objc_private: |
| 476 | return parseAccessSpecifier(); |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 477 | case tok::objc_interface: |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 478 | case tok::objc_implementation: |
| 479 | return parseObjCInterfaceOrImplementation(); |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 480 | case tok::objc_protocol: |
| 481 | return parseObjCProtocol(); |
Nico Weber | 049c447 | 2013-01-09 21:42:32 +0000 | [diff] [blame] | 482 | case tok::objc_end: |
| 483 | return; // Handled by the caller. |
Nico Weber | b530fa3 | 2013-01-10 00:25:19 +0000 | [diff] [blame] | 484 | case tok::objc_optional: |
| 485 | case tok::objc_required: |
| 486 | nextToken(); |
| 487 | addUnwrappedLine(); |
| 488 | return; |
Nico Weber | 6092d4e | 2013-01-07 19:05:19 +0000 | [diff] [blame] | 489 | default: |
| 490 | break; |
| 491 | } |
| 492 | break; |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 493 | case tok::kw_namespace: |
| 494 | parseNamespace(); |
| 495 | return; |
Dmitri Gribenko | 1f94f2b | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 496 | case tok::kw_inline: |
| 497 | nextToken(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 498 | if (FormatTok->Tok.is(tok::kw_namespace)) { |
Dmitri Gribenko | 1f94f2b | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 499 | parseNamespace(); |
| 500 | return; |
| 501 | } |
| 502 | break; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 503 | case tok::kw_public: |
| 504 | case tok::kw_protected: |
| 505 | case tok::kw_private: |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 506 | parseAccessSpecifier(); |
| 507 | return; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 508 | case tok::kw_if: |
| 509 | parseIfThenElse(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 510 | return; |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 511 | case tok::kw_for: |
| 512 | case tok::kw_while: |
| 513 | parseForOrWhileLoop(); |
| 514 | return; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 515 | case tok::kw_do: |
| 516 | parseDoWhile(); |
| 517 | return; |
| 518 | case tok::kw_switch: |
| 519 | parseSwitch(); |
| 520 | return; |
| 521 | case tok::kw_default: |
| 522 | nextToken(); |
| 523 | parseLabel(); |
| 524 | return; |
| 525 | case tok::kw_case: |
| 526 | parseCaseLabel(); |
| 527 | return; |
Manuel Klimek | c44ee89 | 2013-01-21 10:07:49 +0000 | [diff] [blame] | 528 | case tok::kw_return: |
| 529 | parseReturn(); |
| 530 | return; |
Manuel Klimek | d19dc2d | 2013-01-21 14:32:05 +0000 | [diff] [blame] | 531 | case tok::kw_extern: |
| 532 | nextToken(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 533 | if (FormatTok->Tok.is(tok::string_literal)) { |
Manuel Klimek | d19dc2d | 2013-01-21 14:32:05 +0000 | [diff] [blame] | 534 | nextToken(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 535 | if (FormatTok->Tok.is(tok::l_brace)) { |
Nico Weber | 2726877 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 536 | parseBlock(/*MustBeDeclaration=*/true, 0); |
Manuel Klimek | d19dc2d | 2013-01-21 14:32:05 +0000 | [diff] [blame] | 537 | addUnwrappedLine(); |
| 538 | return; |
| 539 | } |
| 540 | } |
| 541 | // In all other cases, parse the declaration. |
| 542 | break; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 543 | default: |
| 544 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 545 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 546 | do { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 547 | switch (FormatTok->Tok.getKind()) { |
Nico Weber | d74fcdb | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 548 | case tok::at: |
| 549 | nextToken(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 550 | if (FormatTok->Tok.is(tok::l_brace)) |
Nico Weber | d74fcdb | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 551 | parseBracedList(); |
| 552 | break; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 553 | case tok::kw_enum: |
| 554 | parseEnum(); |
Manuel Klimek | 308232c | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 555 | break; |
Alexander Kornienko | d881875 | 2013-01-16 11:43:46 +0000 | [diff] [blame] | 556 | case tok::kw_struct: |
| 557 | case tok::kw_union: |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 558 | case tok::kw_class: |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 559 | parseRecord(); |
| 560 | // A record declaration or definition is always the start of a structural |
| 561 | // element. |
| 562 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 563 | case tok::semi: |
| 564 | nextToken(); |
| 565 | addUnwrappedLine(); |
| 566 | return; |
Alexander Kornienko | d881875 | 2013-01-16 11:43:46 +0000 | [diff] [blame] | 567 | case tok::r_brace: |
| 568 | addUnwrappedLine(); |
| 569 | return; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 570 | case tok::l_paren: |
| 571 | parseParens(); |
| 572 | break; |
| 573 | case tok::l_brace: |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 574 | if (!tryToParseBracedList()) { |
| 575 | // A block outside of parentheses must be the last part of a |
| 576 | // structural element. |
| 577 | // FIXME: Figure out cases where this is not true, and add projections |
| 578 | // for them (the one we know is missing are lambdas). |
| 579 | if (Style.BreakBeforeBraces == FormatStyle::BS_Linux || |
| 580 | Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) |
| 581 | addUnwrappedLine(); |
Nico Weber | 2726877 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 582 | parseBlock(/*MustBeDeclaration=*/false); |
Manuel Klimek | 44135b8 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 583 | addUnwrappedLine(); |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 584 | return; |
| 585 | } |
| 586 | // Otherwise this was a braced init list, and the structural |
| 587 | // element continues. |
| 588 | break; |
Daniel Jasper | 7e70f4c | 2013-05-29 13:16:10 +0000 | [diff] [blame] | 589 | case tok::identifier: { |
| 590 | StringRef Text = FormatTok->TokenText; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 591 | nextToken(); |
Alexander Kornienko | 3d713a7 | 2013-04-08 22:16:06 +0000 | [diff] [blame] | 592 | if (Line->Tokens.size() == 1) { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 593 | if (FormatTok->Tok.is(tok::colon)) { |
Alexander Kornienko | 3d713a7 | 2013-04-08 22:16:06 +0000 | [diff] [blame] | 594 | parseLabel(); |
| 595 | return; |
| 596 | } |
Alexander Kornienko | 99b0e14 | 2013-04-09 16:15:19 +0000 | [diff] [blame] | 597 | // Recognize function-like macro usages without trailing semicolon. |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 598 | if (FormatTok->Tok.is(tok::l_paren)) { |
Alexander Kornienko | 3d713a7 | 2013-04-08 22:16:06 +0000 | [diff] [blame] | 599 | parseParens(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 600 | if (FormatTok->HasUnescapedNewline && |
| 601 | tokenCanStartNewLine(FormatTok->Tok)) { |
Alexander Kornienko | 3d713a7 | 2013-04-08 22:16:06 +0000 | [diff] [blame] | 602 | addUnwrappedLine(); |
| 603 | return; |
| 604 | } |
Daniel Jasper | 7e70f4c | 2013-05-29 13:16:10 +0000 | [diff] [blame] | 605 | } else if (FormatTok->HasUnescapedNewline && Text.size() >= 5 && |
| 606 | Text == Text.upper()) { |
| 607 | // Recognize free-standing macros like Q_OBJECT. |
| 608 | addUnwrappedLine(); |
Daniel Jasper | c76d59d | 2013-05-29 14:09:17 +0000 | [diff] [blame] | 609 | return; |
Alexander Kornienko | 3d713a7 | 2013-04-08 22:16:06 +0000 | [diff] [blame] | 610 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 611 | } |
| 612 | break; |
Daniel Jasper | 7e70f4c | 2013-05-29 13:16:10 +0000 | [diff] [blame] | 613 | } |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 614 | case tok::equal: |
| 615 | nextToken(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 616 | if (FormatTok->Tok.is(tok::l_brace)) { |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 617 | parseBracedList(); |
| 618 | } |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 619 | break; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 620 | default: |
| 621 | nextToken(); |
| 622 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 623 | } |
| 624 | } while (!eof()); |
| 625 | } |
| 626 | |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 627 | bool UnwrappedLineParser::tryToParseBracedList() { |
| 628 | if (LBraces[Tokens->getPosition()] == BS_Unknown) |
| 629 | calculateBraceTypes(); |
| 630 | assert(LBraces[Tokens->getPosition()] != BS_Unknown); |
| 631 | if (LBraces[Tokens->getPosition()] == BS_Block) |
| 632 | return false; |
| 633 | parseBracedList(); |
| 634 | return true; |
| 635 | } |
| 636 | |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 637 | void UnwrappedLineParser::parseBracedList() { |
| 638 | nextToken(); |
| 639 | |
Manuel Klimek | 423dd93 | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 640 | // FIXME: Once we have an expression parser in the UnwrappedLineParser, |
| 641 | // replace this by using parseAssigmentExpression() inside. |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 642 | do { |
Manuel Klimek | 423dd93 | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 643 | // FIXME: When we start to support lambdas, we'll want to parse them away |
| 644 | // here, otherwise our bail-out scenarios below break. The better solution |
| 645 | // might be to just implement a more or less complete expression parser. |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 646 | switch (FormatTok->Tok.getKind()) { |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 647 | case tok::l_brace: |
| 648 | parseBracedList(); |
| 649 | break; |
| 650 | case tok::r_brace: |
| 651 | nextToken(); |
| 652 | return; |
Manuel Klimek | 423dd93 | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 653 | case tok::semi: |
| 654 | // Probably a missing closing brace. Bail out. |
| 655 | return; |
| 656 | case tok::comma: |
| 657 | nextToken(); |
Manuel Klimek | 423dd93 | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 658 | break; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 659 | default: |
| 660 | nextToken(); |
| 661 | break; |
| 662 | } |
| 663 | } while (!eof()); |
| 664 | } |
| 665 | |
Manuel Klimek | c44ee89 | 2013-01-21 10:07:49 +0000 | [diff] [blame] | 666 | void UnwrappedLineParser::parseReturn() { |
| 667 | nextToken(); |
| 668 | |
| 669 | do { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 670 | switch (FormatTok->Tok.getKind()) { |
Manuel Klimek | c44ee89 | 2013-01-21 10:07:49 +0000 | [diff] [blame] | 671 | case tok::l_brace: |
| 672 | parseBracedList(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 673 | if (FormatTok->Tok.isNot(tok::semi)) { |
Manuel Klimek | 423dd93 | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 674 | // Assume missing ';'. |
| 675 | addUnwrappedLine(); |
| 676 | return; |
| 677 | } |
Manuel Klimek | c44ee89 | 2013-01-21 10:07:49 +0000 | [diff] [blame] | 678 | break; |
| 679 | case tok::l_paren: |
| 680 | parseParens(); |
| 681 | break; |
| 682 | case tok::r_brace: |
| 683 | // Assume missing ';'. |
| 684 | addUnwrappedLine(); |
| 685 | return; |
| 686 | case tok::semi: |
| 687 | nextToken(); |
| 688 | addUnwrappedLine(); |
| 689 | return; |
| 690 | default: |
| 691 | nextToken(); |
| 692 | break; |
| 693 | } |
| 694 | } while (!eof()); |
| 695 | } |
| 696 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 697 | void UnwrappedLineParser::parseParens() { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 698 | assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected."); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 699 | nextToken(); |
| 700 | do { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 701 | switch (FormatTok->Tok.getKind()) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 702 | case tok::l_paren: |
| 703 | parseParens(); |
| 704 | break; |
| 705 | case tok::r_paren: |
| 706 | nextToken(); |
| 707 | return; |
Daniel Jasper | f7ec1cc | 2013-05-31 14:56:29 +0000 | [diff] [blame] | 708 | case tok::r_brace: |
| 709 | // A "}" inside parenthesis is an error if there wasn't a matching "{". |
| 710 | return; |
Nico Weber | 2afbe52 | 2013-02-10 04:38:23 +0000 | [diff] [blame] | 711 | case tok::l_brace: { |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 712 | if (!tryToParseBracedList()) { |
| 713 | nextToken(); |
Daniel Jasper | f7ec1cc | 2013-05-31 14:56:29 +0000 | [diff] [blame] | 714 | { |
| 715 | ScopedLineState LineState(*this); |
| 716 | ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, |
Nico Weber | 2726877 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 717 | /*MustBeDeclaration=*/false); |
Daniel Jasper | f7ec1cc | 2013-05-31 14:56:29 +0000 | [diff] [blame] | 718 | Line->Level += 1; |
Nico Weber | 2726877 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 719 | parseLevel(/*HasOpeningBrace=*/true); |
Daniel Jasper | f7ec1cc | 2013-05-31 14:56:29 +0000 | [diff] [blame] | 720 | Line->Level -= 1; |
| 721 | } |
| 722 | nextToken(); |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 723 | } |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 724 | break; |
Nico Weber | 2afbe52 | 2013-02-10 04:38:23 +0000 | [diff] [blame] | 725 | } |
Nico Weber | d74fcdb | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 726 | case tok::at: |
| 727 | nextToken(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 728 | if (FormatTok->Tok.is(tok::l_brace)) |
Nico Weber | d74fcdb | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 729 | parseBracedList(); |
| 730 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 731 | default: |
| 732 | nextToken(); |
| 733 | break; |
| 734 | } |
| 735 | } while (!eof()); |
| 736 | } |
| 737 | |
| 738 | void UnwrappedLineParser::parseIfThenElse() { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 739 | assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 740 | nextToken(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 741 | if (FormatTok->Tok.is(tok::l_paren)) |
Manuel Klimek | d465843 | 2013-01-11 18:28:36 +0000 | [diff] [blame] | 742 | parseParens(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 743 | bool NeedsUnwrappedLine = false; |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 744 | if (FormatTok->Tok.is(tok::l_brace)) { |
Nico Weber | 2726877 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 745 | parseBlock(/*MustBeDeclaration=*/false); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 746 | NeedsUnwrappedLine = true; |
| 747 | } else { |
| 748 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 749 | ++Line->Level; |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 750 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 751 | --Line->Level; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 752 | } |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 753 | if (FormatTok->Tok.is(tok::kw_else)) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 754 | nextToken(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 755 | if (FormatTok->Tok.is(tok::l_brace)) { |
Nico Weber | 2726877 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 756 | parseBlock(/*MustBeDeclaration=*/false); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 757 | addUnwrappedLine(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 758 | } else if (FormatTok->Tok.is(tok::kw_if)) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 759 | parseIfThenElse(); |
| 760 | } else { |
| 761 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 762 | ++Line->Level; |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 763 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 764 | --Line->Level; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 765 | } |
| 766 | } else if (NeedsUnwrappedLine) { |
| 767 | addUnwrappedLine(); |
| 768 | } |
| 769 | } |
| 770 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 771 | void UnwrappedLineParser::parseNamespace() { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 772 | assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 773 | nextToken(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 774 | if (FormatTok->Tok.is(tok::identifier)) |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 775 | nextToken(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 776 | if (FormatTok->Tok.is(tok::l_brace)) { |
Manuel Klimek | 44135b8 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 777 | if (Style.BreakBeforeBraces == FormatStyle::BS_Linux) |
| 778 | addUnwrappedLine(); |
| 779 | |
Nico Weber | 2726877 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 780 | parseBlock(/*MustBeDeclaration=*/true, 0); |
Manuel Klimek | 7fc2db0 | 2013-02-06 16:08:09 +0000 | [diff] [blame] | 781 | // Munch the semicolon after a namespace. This is more common than one would |
| 782 | // think. Puttin the semicolon into its own line is very ugly. |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 783 | if (FormatTok->Tok.is(tok::semi)) |
Manuel Klimek | 7fc2db0 | 2013-02-06 16:08:09 +0000 | [diff] [blame] | 784 | nextToken(); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 785 | addUnwrappedLine(); |
| 786 | } |
| 787 | // FIXME: Add error handling. |
| 788 | } |
| 789 | |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 790 | void UnwrappedLineParser::parseForOrWhileLoop() { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 791 | assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while)) && |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 792 | "'for' or 'while' expected"); |
| 793 | nextToken(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 794 | if (FormatTok->Tok.is(tok::l_paren)) |
Manuel Klimek | 6eca03f | 2013-01-11 19:23:05 +0000 | [diff] [blame] | 795 | parseParens(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 796 | if (FormatTok->Tok.is(tok::l_brace)) { |
Nico Weber | 2726877 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 797 | parseBlock(/*MustBeDeclaration=*/false); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 798 | addUnwrappedLine(); |
| 799 | } else { |
| 800 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 801 | ++Line->Level; |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 802 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 803 | --Line->Level; |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 804 | } |
| 805 | } |
| 806 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 807 | void UnwrappedLineParser::parseDoWhile() { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 808 | assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 809 | nextToken(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 810 | if (FormatTok->Tok.is(tok::l_brace)) { |
Nico Weber | 2726877 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 811 | parseBlock(/*MustBeDeclaration=*/false); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 812 | } else { |
| 813 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 814 | ++Line->Level; |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 815 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 816 | --Line->Level; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 817 | } |
| 818 | |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 819 | // FIXME: Add error handling. |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 820 | if (!FormatTok->Tok.is(tok::kw_while)) { |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 821 | addUnwrappedLine(); |
| 822 | return; |
| 823 | } |
| 824 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 825 | nextToken(); |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 826 | parseStructuralElement(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | void UnwrappedLineParser::parseLabel() { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 830 | if (FormatTok->Tok.isNot(tok::colon)) |
Daniel Jasper | 89a0daa | 2013-02-12 20:17:17 +0000 | [diff] [blame] | 831 | return; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 832 | nextToken(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 833 | unsigned OldLineLevel = Line->Level; |
Daniel Jasper | bcca7e4 | 2013-03-20 10:23:53 +0000 | [diff] [blame] | 834 | if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0)) |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 835 | --Line->Level; |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 836 | if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) { |
Nico Weber | 2726877 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 837 | parseBlock(/*MustBeDeclaration=*/false); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 838 | if (FormatTok->Tok.is(tok::kw_break)) |
Nico Weber | 94fb729 | 2013-01-18 05:50:57 +0000 | [diff] [blame] | 839 | parseStructuralElement(); // "break;" after "}" goes on the same line. |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 840 | } |
| 841 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 842 | Line->Level = OldLineLevel; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | void UnwrappedLineParser::parseCaseLabel() { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 846 | assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 847 | // FIXME: fix handling of complex expressions here. |
| 848 | do { |
| 849 | nextToken(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 850 | } while (!eof() && !FormatTok->Tok.is(tok::colon)); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 851 | parseLabel(); |
| 852 | } |
| 853 | |
| 854 | void UnwrappedLineParser::parseSwitch() { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 855 | assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 856 | nextToken(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 857 | if (FormatTok->Tok.is(tok::l_paren)) |
Manuel Klimek | 6eca03f | 2013-01-11 19:23:05 +0000 | [diff] [blame] | 858 | parseParens(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 859 | if (FormatTok->Tok.is(tok::l_brace)) { |
Nico Weber | 2726877 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 860 | parseBlock(/*MustBeDeclaration=*/false, Style.IndentCaseLabels ? 2 : 1); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 861 | addUnwrappedLine(); |
| 862 | } else { |
| 863 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 864 | Line->Level += (Style.IndentCaseLabels ? 2 : 1); |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 865 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 866 | Line->Level -= (Style.IndentCaseLabels ? 2 : 1); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 867 | } |
| 868 | } |
| 869 | |
| 870 | void UnwrappedLineParser::parseAccessSpecifier() { |
| 871 | nextToken(); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 872 | // Otherwise, we don't know what it is, and we'd better keep the next token. |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 873 | if (FormatTok->Tok.is(tok::colon)) |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 874 | nextToken(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 875 | addUnwrappedLine(); |
| 876 | } |
| 877 | |
| 878 | void UnwrappedLineParser::parseEnum() { |
Manuel Klimek | 308232c | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 879 | nextToken(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 880 | if (FormatTok->Tok.is(tok::identifier) || |
| 881 | FormatTok->Tok.is(tok::kw___attribute) || |
| 882 | FormatTok->Tok.is(tok::kw___declspec)) { |
Manuel Klimek | 308232c | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 883 | nextToken(); |
| 884 | // We can have macros or attributes in between 'enum' and the enum name. |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 885 | if (FormatTok->Tok.is(tok::l_paren)) { |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 886 | parseParens(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 887 | } |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 888 | if (FormatTok->Tok.is(tok::identifier)) |
Manuel Klimek | 308232c | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 889 | nextToken(); |
| 890 | } |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 891 | if (FormatTok->Tok.is(tok::l_brace)) { |
Manuel Klimek | 308232c | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 892 | nextToken(); |
| 893 | addUnwrappedLine(); |
| 894 | ++Line->Level; |
| 895 | do { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 896 | switch (FormatTok->Tok.getKind()) { |
Manuel Klimek | 308232c | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 897 | case tok::l_paren: |
| 898 | parseParens(); |
| 899 | break; |
| 900 | case tok::r_brace: |
| 901 | addUnwrappedLine(); |
| 902 | nextToken(); |
| 903 | --Line->Level; |
| 904 | return; |
| 905 | case tok::comma: |
| 906 | nextToken(); |
| 907 | addUnwrappedLine(); |
| 908 | break; |
| 909 | default: |
| 910 | nextToken(); |
| 911 | break; |
| 912 | } |
| 913 | } while (!eof()); |
| 914 | } |
| 915 | // We fall through to parsing a structural element afterwards, so that in |
| 916 | // enum A {} n, m; |
| 917 | // "} n, m;" will end up in one unwrapped line. |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 918 | } |
| 919 | |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 920 | void UnwrappedLineParser::parseRecord() { |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 921 | nextToken(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 922 | if (FormatTok->Tok.is(tok::identifier) || |
| 923 | FormatTok->Tok.is(tok::kw___attribute) || |
| 924 | FormatTok->Tok.is(tok::kw___declspec)) { |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 925 | nextToken(); |
| 926 | // We can have macros or attributes in between 'class' and the class name. |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 927 | if (FormatTok->Tok.is(tok::l_paren)) { |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 928 | parseParens(); |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 929 | } |
Manuel Klimek | b8b1ce1 | 2013-02-06 15:57:54 +0000 | [diff] [blame] | 930 | // The actual identifier can be a nested name specifier, and in macros |
| 931 | // it is often token-pasted. |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 932 | while (FormatTok->Tok.is(tok::identifier) || |
| 933 | FormatTok->Tok.is(tok::coloncolon) || |
| 934 | FormatTok->Tok.is(tok::hashhash)) |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 935 | nextToken(); |
| 936 | |
Manuel Klimek | 3a3408c | 2013-01-21 13:58:54 +0000 | [diff] [blame] | 937 | // Note that parsing away template declarations here leads to incorrectly |
| 938 | // accepting function declarations as record declarations. |
| 939 | // In general, we cannot solve this problem. Consider: |
| 940 | // class A<int> B() {} |
| 941 | // which can be a function definition or a class definition when B() is a |
| 942 | // macro. If we find enough real-world cases where this is a problem, we |
| 943 | // can parse for the 'template' keyword in the beginning of the statement, |
| 944 | // and thus rule out the record production in case there is no template |
| 945 | // (this would still leave us with an ambiguity between template function |
| 946 | // and class declarations). |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 947 | if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) { |
| 948 | while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) { |
| 949 | if (FormatTok->Tok.is(tok::semi)) |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 950 | return; |
| 951 | nextToken(); |
| 952 | } |
| 953 | } |
| 954 | } |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 955 | if (FormatTok->Tok.is(tok::l_brace)) { |
Manuel Klimek | 44135b8 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 956 | if (Style.BreakBeforeBraces == FormatStyle::BS_Linux) |
| 957 | addUnwrappedLine(); |
| 958 | |
Nico Weber | 2726877 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 959 | parseBlock(/*MustBeDeclaration=*/true); |
Manuel Klimek | 44135b8 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 960 | } |
Manuel Klimek | 3a3408c | 2013-01-21 13:58:54 +0000 | [diff] [blame] | 961 | // We fall through to parsing a structural element afterwards, so |
| 962 | // class A {} n, m; |
| 963 | // will end up in one unwrapped line. |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 964 | } |
| 965 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 966 | void UnwrappedLineParser::parseObjCProtocolList() { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 967 | assert(FormatTok->Tok.is(tok::less) && "'<' expected."); |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 968 | do |
| 969 | nextToken(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 970 | while (!eof() && FormatTok->Tok.isNot(tok::greater)); |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 971 | nextToken(); // Skip '>'. |
| 972 | } |
| 973 | |
| 974 | void UnwrappedLineParser::parseObjCUntilAtEnd() { |
| 975 | do { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 976 | if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) { |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 977 | nextToken(); |
| 978 | addUnwrappedLine(); |
| 979 | break; |
| 980 | } |
| 981 | parseStructuralElement(); |
| 982 | } while (!eof()); |
| 983 | } |
| 984 | |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 985 | void UnwrappedLineParser::parseObjCInterfaceOrImplementation() { |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 986 | nextToken(); |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 987 | nextToken(); // interface name |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 988 | |
| 989 | // @interface can be followed by either a base class, or a category. |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 990 | if (FormatTok->Tok.is(tok::colon)) { |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 991 | nextToken(); |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 992 | nextToken(); // base class name |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 993 | } else if (FormatTok->Tok.is(tok::l_paren)) |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 994 | // Skip category, if present. |
| 995 | parseParens(); |
| 996 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 997 | if (FormatTok->Tok.is(tok::less)) |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 998 | parseObjCProtocolList(); |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 999 | |
| 1000 | // If instance variables are present, keep the '{' on the first line too. |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1001 | if (FormatTok->Tok.is(tok::l_brace)) |
Nico Weber | 2726877 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 1002 | parseBlock(/*MustBeDeclaration=*/true); |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1003 | |
| 1004 | // With instance variables, this puts '}' on its own line. Without instance |
| 1005 | // variables, this ends the @interface line. |
| 1006 | addUnwrappedLine(); |
| 1007 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1008 | parseObjCUntilAtEnd(); |
| 1009 | } |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1010 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1011 | void UnwrappedLineParser::parseObjCProtocol() { |
| 1012 | nextToken(); |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 1013 | nextToken(); // protocol name |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1014 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1015 | if (FormatTok->Tok.is(tok::less)) |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1016 | parseObjCProtocolList(); |
| 1017 | |
| 1018 | // Check for protocol declaration. |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1019 | if (FormatTok->Tok.is(tok::semi)) { |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1020 | nextToken(); |
| 1021 | return addUnwrappedLine(); |
| 1022 | } |
| 1023 | |
| 1024 | addUnwrappedLine(); |
| 1025 | parseObjCUntilAtEnd(); |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1028 | void UnwrappedLineParser::addUnwrappedLine() { |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 1029 | if (Line->Tokens.empty()) |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1030 | return; |
Manuel Klimek | 8fa3799 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 1031 | DEBUG({ |
Manuel Klimek | a28fc06 | 2013-02-11 12:33:24 +0000 | [diff] [blame] | 1032 | llvm::dbgs() << "Line(" << Line->Level << ")" |
| 1033 | << (Line->InPPDirective ? " MACRO" : "") << ": "; |
Manuel Klimek | dcb3f2a | 2013-05-28 13:42:28 +0000 | [diff] [blame] | 1034 | for (std::list<FormatToken *>::iterator I = Line->Tokens.begin(), |
| 1035 | E = Line->Tokens.end(); |
Manuel Klimek | 8fa3799 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 1036 | I != E; ++I) { |
Manuel Klimek | dcb3f2a | 2013-05-28 13:42:28 +0000 | [diff] [blame] | 1037 | llvm::dbgs() << (*I)->Tok.getName() << " "; |
Manuel Klimek | 8fa3799 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 1038 | } |
| 1039 | llvm::dbgs() << "\n"; |
| 1040 | }); |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 1041 | CurrentLines->push_back(*Line); |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 1042 | Line->Tokens.clear(); |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 1043 | if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) { |
Daniel Jasper | 516fb31 | 2013-03-01 18:11:39 +0000 | [diff] [blame] | 1044 | for (std::vector<UnwrappedLine>::iterator |
| 1045 | I = PreprocessorDirectives.begin(), |
| 1046 | E = PreprocessorDirectives.end(); |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 1047 | I != E; ++I) { |
| 1048 | CurrentLines->push_back(*I); |
| 1049 | } |
| 1050 | PreprocessorDirectives.clear(); |
| 1051 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1054 | bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1055 | |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1056 | void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) { |
| 1057 | bool JustComments = Line->Tokens.empty(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1058 | for (SmallVectorImpl<FormatToken *>::const_iterator |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1059 | I = CommentsBeforeNextToken.begin(), |
| 1060 | E = CommentsBeforeNextToken.end(); |
| 1061 | I != E; ++I) { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1062 | if ((*I)->NewlinesBefore && JustComments) { |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1063 | addUnwrappedLine(); |
| 1064 | } |
| 1065 | pushToken(*I); |
| 1066 | } |
| 1067 | if (NewlineBeforeNext && JustComments) { |
| 1068 | addUnwrappedLine(); |
| 1069 | } |
| 1070 | CommentsBeforeNextToken.clear(); |
| 1071 | } |
| 1072 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1073 | void UnwrappedLineParser::nextToken() { |
| 1074 | if (eof()) |
| 1075 | return; |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1076 | flushComments(FormatTok->NewlinesBefore > 0); |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1077 | pushToken(FormatTok); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1078 | readToken(); |
| 1079 | } |
| 1080 | |
| 1081 | void UnwrappedLineParser::readToken() { |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1082 | bool CommentsInCurrentLine = true; |
| 1083 | do { |
| 1084 | FormatTok = Tokens->getNextToken(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1085 | while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) && |
| 1086 | (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) { |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1087 | // If there is an unfinished unwrapped line, we flush the preprocessor |
| 1088 | // directives only after that unwrapped line was finished later. |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 1089 | bool SwitchToPreprocessorLines = |
| 1090 | !Line->Tokens.empty() && CurrentLines == &Lines; |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1091 | ScopedLineState BlockState(*this, SwitchToPreprocessorLines); |
Alexander Kornienko | 4128e19 | 2013-04-03 12:38:53 +0000 | [diff] [blame] | 1092 | // Comments stored before the preprocessor directive need to be output |
| 1093 | // before the preprocessor directive, at the same level as the |
| 1094 | // preprocessor directive, as we consider them to apply to the directive. |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1095 | flushComments(FormatTok->NewlinesBefore > 0); |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1096 | parsePPDirective(); |
| 1097 | } |
Alexander Kornienko | 6fb46b0 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 1098 | |
| 1099 | if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) && |
| 1100 | !Line->InPPDirective) { |
| 1101 | continue; |
| 1102 | } |
| 1103 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1104 | if (!FormatTok->Tok.is(tok::comment)) |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1105 | return; |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1106 | if (FormatTok->NewlinesBefore > 0 || FormatTok->IsFirst) { |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1107 | CommentsInCurrentLine = false; |
| 1108 | } |
| 1109 | if (CommentsInCurrentLine) { |
| 1110 | pushToken(FormatTok); |
| 1111 | } else { |
| 1112 | CommentsBeforeNextToken.push_back(FormatTok); |
| 1113 | } |
| 1114 | } while (!eof()); |
| 1115 | } |
| 1116 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1117 | void UnwrappedLineParser::pushToken(FormatToken *Tok) { |
Manuel Klimek | dcb3f2a | 2013-05-28 13:42:28 +0000 | [diff] [blame] | 1118 | Line->Tokens.push_back(Tok); |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1119 | if (MustBreakBeforeNextToken) { |
Manuel Klimek | dcb3f2a | 2013-05-28 13:42:28 +0000 | [diff] [blame] | 1120 | Line->Tokens.back()->MustBreakBefore = true; |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1121 | MustBreakBeforeNextToken = false; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1122 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1125 | } // end namespace format |
| 1126 | } // end namespace clang |