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