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