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