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