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 | /// |
| 14 | /// This is EXPERIMENTAL code under heavy development. It is not in a state yet, |
| 15 | /// where it can be used to format real code. |
| 16 | /// |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
Manuel Klimek | 8fa3799 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 19 | #define DEBUG_TYPE "format-parser" |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 20 | |
Chandler Carruth | b1ba0ef | 2013-01-19 08:09:44 +0000 | [diff] [blame] | 21 | #include "UnwrappedLineParser.h" |
Manuel Klimek | 8fa3799 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 22 | #include "clang/Basic/Diagnostic.h" |
| 23 | #include "llvm/Support/Debug.h" |
Manuel Klimek | 8fa3799 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 24 | |
| 25 | // Uncomment to get debug output from tests: |
| 26 | // #define DEBUG_WITH_TYPE(T, X) do { X; } while(0) |
Manuel Klimek | 4c60fc6 | 2013-01-10 10:05:08 +0000 | [diff] [blame] | 27 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 28 | namespace clang { |
| 29 | namespace format { |
| 30 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 31 | class ScopedMacroState : public FormatTokenSource { |
| 32 | public: |
| 33 | ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource, |
| 34 | FormatToken &ResetToken) |
| 35 | : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken), |
Manuel Klimek | c37b4d6 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 36 | PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 37 | TokenSource = this; |
Manuel Klimek | c37b4d6 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 38 | Line.Level = 0; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 39 | Line.InPPDirective = true; |
| 40 | } |
| 41 | |
| 42 | ~ScopedMacroState() { |
| 43 | TokenSource = PreviousTokenSource; |
| 44 | ResetToken = Token; |
| 45 | Line.InPPDirective = false; |
Manuel Klimek | c37b4d6 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 46 | Line.Level = PreviousLineLevel; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | virtual FormatToken getNextToken() { |
Manuel Klimek | dd5b101 | 2013-01-07 10:03:37 +0000 | [diff] [blame] | 50 | // The \c UnwrappedLineParser guards against this by never calling |
| 51 | // \c getNextToken() after it has encountered the first eof token. |
| 52 | assert(!eof()); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 53 | Token = PreviousTokenSource->getNextToken(); |
| 54 | if (eof()) |
| 55 | return createEOF(); |
| 56 | return Token; |
| 57 | } |
| 58 | |
| 59 | private: |
| 60 | bool eof() { |
| 61 | return Token.NewlinesBefore > 0 && Token.HasUnescapedNewline; |
| 62 | } |
| 63 | |
| 64 | FormatToken createEOF() { |
| 65 | FormatToken FormatTok; |
| 66 | FormatTok.Tok.startToken(); |
| 67 | FormatTok.Tok.setKind(tok::eof); |
| 68 | return FormatTok; |
| 69 | } |
| 70 | |
| 71 | UnwrappedLine &Line; |
| 72 | FormatTokenSource *&TokenSource; |
| 73 | FormatToken &ResetToken; |
Manuel Klimek | c37b4d6 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 74 | unsigned PreviousLineLevel; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 75 | FormatTokenSource *PreviousTokenSource; |
| 76 | |
| 77 | FormatToken Token; |
| 78 | }; |
| 79 | |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 80 | class ScopedLineState { |
| 81 | public: |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 82 | ScopedLineState(UnwrappedLineParser &Parser, |
| 83 | bool SwitchToPreprocessorLines = false) |
| 84 | : Parser(Parser), SwitchToPreprocessorLines(SwitchToPreprocessorLines) { |
| 85 | if (SwitchToPreprocessorLines) |
| 86 | Parser.CurrentLines = &Parser.PreprocessorDirectives; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 87 | PreBlockLine = Parser.Line.take(); |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 88 | Parser.Line.reset(new UnwrappedLine()); |
| 89 | Parser.Line->Level = PreBlockLine->Level; |
| 90 | Parser.Line->InPPDirective = PreBlockLine->InPPDirective; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | ~ScopedLineState() { |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 94 | if (!Parser.Line->Tokens.empty()) { |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 95 | Parser.addUnwrappedLine(); |
| 96 | } |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 97 | assert(Parser.Line->Tokens.empty()); |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 98 | Parser.Line.reset(PreBlockLine); |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 99 | Parser.MustBreakBeforeNextToken = true; |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 100 | if (SwitchToPreprocessorLines) |
| 101 | Parser.CurrentLines = &Parser.Lines; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | private: |
| 105 | UnwrappedLineParser &Parser; |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 106 | const bool SwitchToPreprocessorLines; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 107 | |
| 108 | UnwrappedLine *PreBlockLine; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
Alexander Kornienko | 3048aea | 2013-01-10 15:05:09 +0000 | [diff] [blame] | 111 | UnwrappedLineParser::UnwrappedLineParser( |
| 112 | clang::DiagnosticsEngine &Diag, const FormatStyle &Style, |
| 113 | FormatTokenSource &Tokens, UnwrappedLineConsumer &Callback) |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 114 | : Line(new UnwrappedLine), MustBreakBeforeNextToken(false), |
| 115 | CurrentLines(&Lines), Diag(Diag), Style(Style), Tokens(&Tokens), |
| 116 | Callback(Callback) {} |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 117 | |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 118 | bool UnwrappedLineParser::parse() { |
Manuel Klimek | 8fa3799 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 119 | DEBUG(llvm::dbgs() << "----\n"); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 120 | readToken(); |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 121 | bool Error = parseFile(); |
| 122 | for (std::vector<UnwrappedLine>::iterator I = Lines.begin(), |
| 123 | E = Lines.end(); |
| 124 | I != E; ++I) { |
| 125 | Callback.consumeUnwrappedLine(*I); |
| 126 | } |
| 127 | return Error; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | bool UnwrappedLineParser::parseFile() { |
Manuel Klimek | a5342db | 2013-01-06 20:07:31 +0000 | [diff] [blame] | 131 | bool Error = parseLevel(/*HasOpeningBrace=*/false); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 132 | // Make sure to format the remaining tokens. |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame^] | 133 | flushComments(true); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 134 | addUnwrappedLine(); |
| 135 | return Error; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Manuel Klimek | a5342db | 2013-01-06 20:07:31 +0000 | [diff] [blame] | 138 | bool UnwrappedLineParser::parseLevel(bool HasOpeningBrace) { |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 139 | bool Error = false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 140 | do { |
| 141 | switch (FormatTok.Tok.getKind()) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 142 | case tok::comment: |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 143 | nextToken(); |
| 144 | addUnwrappedLine(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 145 | break; |
| 146 | case tok::l_brace: |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 147 | Error |= parseBlock(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 148 | addUnwrappedLine(); |
| 149 | break; |
| 150 | case tok::r_brace: |
Manuel Klimek | a5342db | 2013-01-06 20:07:31 +0000 | [diff] [blame] | 151 | if (HasOpeningBrace) { |
| 152 | return false; |
| 153 | } else { |
Alexander Kornienko | 3048aea | 2013-01-10 15:05:09 +0000 | [diff] [blame] | 154 | Diag.Report(FormatTok.Tok.getLocation(), |
| 155 | Diag.getCustomDiagID(clang::DiagnosticsEngine::Error, |
Alexander Kornienko | 276a209 | 2013-01-11 16:03:45 +0000 | [diff] [blame] | 156 | "unexpected '}'")); |
Manuel Klimek | a5342db | 2013-01-06 20:07:31 +0000 | [diff] [blame] | 157 | Error = true; |
| 158 | nextToken(); |
| 159 | addUnwrappedLine(); |
| 160 | } |
| 161 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 162 | default: |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 163 | parseStructuralElement(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 164 | break; |
| 165 | } |
| 166 | } while (!eof()); |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 167 | return Error; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 170 | bool UnwrappedLineParser::parseBlock(unsigned AddLevels) { |
Alexander Kornienko | a3a2b3a | 2012-12-06 17:49:17 +0000 | [diff] [blame] | 171 | assert(FormatTok.Tok.is(tok::l_brace) && "'{' expected"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 172 | nextToken(); |
| 173 | |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 174 | addUnwrappedLine(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 175 | |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 176 | Line->Level += AddLevels; |
| 177 | parseLevel(/*HasOpeningBrace=*/true); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 178 | |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame^] | 179 | if (!FormatTok.Tok.is(tok::r_brace)) { |
| 180 | Line->Level -= AddLevels; |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 181 | return true; |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame^] | 182 | } |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 183 | |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 184 | nextToken(); // Munch the closing brace. |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame^] | 185 | Line->Level -= AddLevels; |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 186 | return false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | void UnwrappedLineParser::parsePPDirective() { |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 190 | assert(FormatTok.Tok.is(tok::hash) && "'#' expected"); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 191 | ScopedMacroState MacroState(*Line, Tokens, FormatTok); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 192 | nextToken(); |
| 193 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 194 | if (FormatTok.Tok.getIdentifierInfo() == NULL) { |
| 195 | addUnwrappedLine(); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 196 | return; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 197 | } |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 198 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 199 | switch (FormatTok.Tok.getIdentifierInfo()->getPPKeywordID()) { |
| 200 | case tok::pp_define: |
| 201 | parsePPDefine(); |
| 202 | break; |
| 203 | default: |
| 204 | parsePPUnknown(); |
| 205 | break; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | void UnwrappedLineParser::parsePPDefine() { |
| 210 | nextToken(); |
| 211 | |
| 212 | if (FormatTok.Tok.getKind() != tok::identifier) { |
| 213 | parsePPUnknown(); |
| 214 | return; |
| 215 | } |
| 216 | nextToken(); |
| 217 | if (FormatTok.Tok.getKind() == tok::l_paren) { |
| 218 | parseParens(); |
| 219 | } |
| 220 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 221 | Line->Level = 1; |
Manuel Klimek | c3d0c82 | 2013-01-07 09:34:28 +0000 | [diff] [blame] | 222 | |
| 223 | // Errors during a preprocessor directive can only affect the layout of the |
| 224 | // preprocessor directive, and thus we ignore them. An alternative approach |
| 225 | // would be to use the same approach we use on the file level (no |
| 226 | // re-indentation if there was a structural error) within the macro |
| 227 | // definition. |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 228 | parseFile(); |
| 229 | } |
| 230 | |
| 231 | void UnwrappedLineParser::parsePPUnknown() { |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 232 | do { |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 233 | nextToken(); |
| 234 | } while (!eof()); |
| 235 | addUnwrappedLine(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 238 | void UnwrappedLineParser::parseStructuralElement() { |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 239 | assert(!FormatTok.Tok.is(tok::l_brace)); |
Dmitri Gribenko | 1f94f2b | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 240 | int TokenNumber = 0; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 241 | switch (FormatTok.Tok.getKind()) { |
Nico Weber | 6092d4e | 2013-01-07 19:05:19 +0000 | [diff] [blame] | 242 | case tok::at: |
| 243 | nextToken(); |
| 244 | switch (FormatTok.Tok.getObjCKeywordID()) { |
| 245 | case tok::objc_public: |
| 246 | case tok::objc_protected: |
| 247 | case tok::objc_package: |
| 248 | case tok::objc_private: |
| 249 | return parseAccessSpecifier(); |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 250 | case tok::objc_interface: |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 251 | case tok::objc_implementation: |
| 252 | return parseObjCInterfaceOrImplementation(); |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 253 | case tok::objc_protocol: |
| 254 | return parseObjCProtocol(); |
Nico Weber | 049c447 | 2013-01-09 21:42:32 +0000 | [diff] [blame] | 255 | case tok::objc_end: |
| 256 | return; // Handled by the caller. |
Nico Weber | b530fa3 | 2013-01-10 00:25:19 +0000 | [diff] [blame] | 257 | case tok::objc_optional: |
| 258 | case tok::objc_required: |
| 259 | nextToken(); |
| 260 | addUnwrappedLine(); |
| 261 | return; |
Nico Weber | 6092d4e | 2013-01-07 19:05:19 +0000 | [diff] [blame] | 262 | default: |
| 263 | break; |
| 264 | } |
| 265 | break; |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 266 | case tok::kw_namespace: |
| 267 | parseNamespace(); |
| 268 | return; |
Dmitri Gribenko | 1f94f2b | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 269 | case tok::kw_inline: |
| 270 | nextToken(); |
| 271 | TokenNumber++; |
| 272 | if (FormatTok.Tok.is(tok::kw_namespace)) { |
| 273 | parseNamespace(); |
| 274 | return; |
| 275 | } |
| 276 | break; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 277 | case tok::kw_public: |
| 278 | case tok::kw_protected: |
| 279 | case tok::kw_private: |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 280 | parseAccessSpecifier(); |
| 281 | return; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 282 | case tok::kw_if: |
| 283 | parseIfThenElse(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 284 | return; |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 285 | case tok::kw_for: |
| 286 | case tok::kw_while: |
| 287 | parseForOrWhileLoop(); |
| 288 | return; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 289 | case tok::kw_do: |
| 290 | parseDoWhile(); |
| 291 | return; |
| 292 | case tok::kw_switch: |
| 293 | parseSwitch(); |
| 294 | return; |
| 295 | case tok::kw_default: |
| 296 | nextToken(); |
| 297 | parseLabel(); |
| 298 | return; |
| 299 | case tok::kw_case: |
| 300 | parseCaseLabel(); |
| 301 | return; |
Manuel Klimek | c44ee89 | 2013-01-21 10:07:49 +0000 | [diff] [blame] | 302 | case tok::kw_return: |
| 303 | parseReturn(); |
| 304 | return; |
Manuel Klimek | d19dc2d | 2013-01-21 14:32:05 +0000 | [diff] [blame] | 305 | case tok::kw_extern: |
| 306 | nextToken(); |
| 307 | if (FormatTok.Tok.is(tok::string_literal)) { |
| 308 | nextToken(); |
| 309 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 310 | parseBlock(0); |
| 311 | addUnwrappedLine(); |
| 312 | return; |
| 313 | } |
| 314 | } |
| 315 | // In all other cases, parse the declaration. |
| 316 | break; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 317 | default: |
| 318 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 319 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 320 | do { |
| 321 | ++TokenNumber; |
| 322 | switch (FormatTok.Tok.getKind()) { |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 323 | case tok::kw_enum: |
| 324 | parseEnum(); |
Manuel Klimek | 308232c | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 325 | break; |
Alexander Kornienko | d881875 | 2013-01-16 11:43:46 +0000 | [diff] [blame] | 326 | case tok::kw_struct: |
| 327 | case tok::kw_union: |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 328 | case tok::kw_class: |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 329 | parseRecord(); |
| 330 | // A record declaration or definition is always the start of a structural |
| 331 | // element. |
| 332 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 333 | case tok::semi: |
| 334 | nextToken(); |
| 335 | addUnwrappedLine(); |
| 336 | return; |
Alexander Kornienko | d881875 | 2013-01-16 11:43:46 +0000 | [diff] [blame] | 337 | case tok::r_brace: |
| 338 | addUnwrappedLine(); |
| 339 | return; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 340 | case tok::l_paren: |
| 341 | parseParens(); |
| 342 | break; |
| 343 | case tok::l_brace: |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 344 | // A block outside of parentheses must be the last part of a |
| 345 | // structural element. |
| 346 | // FIXME: Figure out cases where this is not true, and add projections for |
| 347 | // them (the one we know is missing are lambdas). |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 348 | parseBlock(); |
| 349 | addUnwrappedLine(); |
| 350 | return; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 351 | case tok::identifier: |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 352 | nextToken(); |
| 353 | if (TokenNumber == 1 && FormatTok.Tok.is(tok::colon)) { |
| 354 | parseLabel(); |
| 355 | return; |
| 356 | } |
| 357 | break; |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 358 | case tok::equal: |
| 359 | nextToken(); |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 360 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 361 | parseBracedList(); |
| 362 | } |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 363 | break; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 364 | default: |
| 365 | nextToken(); |
| 366 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 367 | } |
| 368 | } while (!eof()); |
| 369 | } |
| 370 | |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 371 | void UnwrappedLineParser::parseBracedList() { |
| 372 | nextToken(); |
| 373 | |
| 374 | do { |
| 375 | switch (FormatTok.Tok.getKind()) { |
| 376 | case tok::l_brace: |
| 377 | parseBracedList(); |
| 378 | break; |
| 379 | case tok::r_brace: |
| 380 | nextToken(); |
| 381 | return; |
| 382 | default: |
| 383 | nextToken(); |
| 384 | break; |
| 385 | } |
| 386 | } while (!eof()); |
| 387 | } |
| 388 | |
Manuel Klimek | c44ee89 | 2013-01-21 10:07:49 +0000 | [diff] [blame] | 389 | void UnwrappedLineParser::parseReturn() { |
| 390 | nextToken(); |
| 391 | |
| 392 | do { |
| 393 | switch (FormatTok.Tok.getKind()) { |
| 394 | case tok::l_brace: |
| 395 | parseBracedList(); |
| 396 | break; |
| 397 | case tok::l_paren: |
| 398 | parseParens(); |
| 399 | break; |
| 400 | case tok::r_brace: |
| 401 | // Assume missing ';'. |
| 402 | addUnwrappedLine(); |
| 403 | return; |
| 404 | case tok::semi: |
| 405 | nextToken(); |
| 406 | addUnwrappedLine(); |
| 407 | return; |
| 408 | default: |
| 409 | nextToken(); |
| 410 | break; |
| 411 | } |
| 412 | } while (!eof()); |
| 413 | } |
| 414 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 415 | void UnwrappedLineParser::parseParens() { |
| 416 | assert(FormatTok.Tok.is(tok::l_paren) && "'(' expected."); |
| 417 | nextToken(); |
| 418 | do { |
| 419 | switch (FormatTok.Tok.getKind()) { |
| 420 | case tok::l_paren: |
| 421 | parseParens(); |
| 422 | break; |
| 423 | case tok::r_paren: |
| 424 | nextToken(); |
| 425 | return; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 426 | case tok::l_brace: |
| 427 | { |
| 428 | nextToken(); |
| 429 | ScopedLineState LineState(*this); |
| 430 | Line->Level += 1; |
| 431 | parseLevel(/*HasOpeningBrace=*/true); |
| 432 | Line->Level -= 1; |
| 433 | } |
| 434 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 435 | default: |
| 436 | nextToken(); |
| 437 | break; |
| 438 | } |
| 439 | } while (!eof()); |
| 440 | } |
| 441 | |
| 442 | void UnwrappedLineParser::parseIfThenElse() { |
| 443 | assert(FormatTok.Tok.is(tok::kw_if) && "'if' expected"); |
| 444 | nextToken(); |
Manuel Klimek | d465843 | 2013-01-11 18:28:36 +0000 | [diff] [blame] | 445 | if (FormatTok.Tok.is(tok::l_paren)) |
| 446 | parseParens(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 447 | bool NeedsUnwrappedLine = false; |
| 448 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 449 | parseBlock(); |
| 450 | NeedsUnwrappedLine = true; |
| 451 | } else { |
| 452 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 453 | ++Line->Level; |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 454 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 455 | --Line->Level; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 456 | } |
| 457 | if (FormatTok.Tok.is(tok::kw_else)) { |
| 458 | nextToken(); |
| 459 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 460 | parseBlock(); |
| 461 | addUnwrappedLine(); |
| 462 | } else if (FormatTok.Tok.is(tok::kw_if)) { |
| 463 | parseIfThenElse(); |
| 464 | } else { |
| 465 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 466 | ++Line->Level; |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 467 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 468 | --Line->Level; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 469 | } |
| 470 | } else if (NeedsUnwrappedLine) { |
| 471 | addUnwrappedLine(); |
| 472 | } |
| 473 | } |
| 474 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 475 | void UnwrappedLineParser::parseNamespace() { |
| 476 | assert(FormatTok.Tok.is(tok::kw_namespace) && "'namespace' expected"); |
| 477 | nextToken(); |
| 478 | if (FormatTok.Tok.is(tok::identifier)) |
| 479 | nextToken(); |
| 480 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 481 | parseBlock(0); |
| 482 | addUnwrappedLine(); |
| 483 | } |
| 484 | // FIXME: Add error handling. |
| 485 | } |
| 486 | |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 487 | void UnwrappedLineParser::parseForOrWhileLoop() { |
| 488 | assert((FormatTok.Tok.is(tok::kw_for) || FormatTok.Tok.is(tok::kw_while)) && |
| 489 | "'for' or 'while' expected"); |
| 490 | nextToken(); |
Manuel Klimek | 6eca03f | 2013-01-11 19:23:05 +0000 | [diff] [blame] | 491 | if (FormatTok.Tok.is(tok::l_paren)) |
| 492 | parseParens(); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 493 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 494 | parseBlock(); |
| 495 | addUnwrappedLine(); |
| 496 | } else { |
| 497 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 498 | ++Line->Level; |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 499 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 500 | --Line->Level; |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 504 | void UnwrappedLineParser::parseDoWhile() { |
| 505 | assert(FormatTok.Tok.is(tok::kw_do) && "'do' expected"); |
| 506 | nextToken(); |
| 507 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 508 | parseBlock(); |
| 509 | } else { |
| 510 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 511 | ++Line->Level; |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 512 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 513 | --Line->Level; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 516 | // FIXME: Add error handling. |
| 517 | if (!FormatTok.Tok.is(tok::kw_while)) { |
| 518 | addUnwrappedLine(); |
| 519 | return; |
| 520 | } |
| 521 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 522 | nextToken(); |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 523 | parseStructuralElement(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | void UnwrappedLineParser::parseLabel() { |
| 527 | // FIXME: remove all asserts. |
| 528 | assert(FormatTok.Tok.is(tok::colon) && "':' expected"); |
| 529 | nextToken(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 530 | unsigned OldLineLevel = Line->Level; |
| 531 | if (Line->Level > 0) |
| 532 | --Line->Level; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 533 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 534 | parseBlock(); |
Nico Weber | 94fb729 | 2013-01-18 05:50:57 +0000 | [diff] [blame] | 535 | if (FormatTok.Tok.is(tok::kw_break)) |
| 536 | parseStructuralElement(); // "break;" after "}" goes on the same line. |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 537 | } |
| 538 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 539 | Line->Level = OldLineLevel; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | void UnwrappedLineParser::parseCaseLabel() { |
| 543 | assert(FormatTok.Tok.is(tok::kw_case) && "'case' expected"); |
| 544 | // FIXME: fix handling of complex expressions here. |
| 545 | do { |
| 546 | nextToken(); |
| 547 | } while (!eof() && !FormatTok.Tok.is(tok::colon)); |
| 548 | parseLabel(); |
| 549 | } |
| 550 | |
| 551 | void UnwrappedLineParser::parseSwitch() { |
| 552 | assert(FormatTok.Tok.is(tok::kw_switch) && "'switch' expected"); |
| 553 | nextToken(); |
Manuel Klimek | 6eca03f | 2013-01-11 19:23:05 +0000 | [diff] [blame] | 554 | if (FormatTok.Tok.is(tok::l_paren)) |
| 555 | parseParens(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 556 | if (FormatTok.Tok.is(tok::l_brace)) { |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 557 | parseBlock(Style.IndentCaseLabels ? 2 : 1); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 558 | addUnwrappedLine(); |
| 559 | } else { |
| 560 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 561 | Line->Level += (Style.IndentCaseLabels ? 2 : 1); |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 562 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 563 | Line->Level -= (Style.IndentCaseLabels ? 2 : 1); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 564 | } |
| 565 | } |
| 566 | |
| 567 | void UnwrappedLineParser::parseAccessSpecifier() { |
| 568 | nextToken(); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 569 | // Otherwise, we don't know what it is, and we'd better keep the next token. |
| 570 | if (FormatTok.Tok.is(tok::colon)) |
| 571 | nextToken(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 572 | addUnwrappedLine(); |
| 573 | } |
| 574 | |
| 575 | void UnwrappedLineParser::parseEnum() { |
Manuel Klimek | 308232c | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 576 | nextToken(); |
| 577 | if (FormatTok.Tok.is(tok::identifier) || |
| 578 | FormatTok.Tok.is(tok::kw___attribute) || |
| 579 | FormatTok.Tok.is(tok::kw___declspec)) { |
| 580 | nextToken(); |
| 581 | // We can have macros or attributes in between 'enum' and the enum name. |
| 582 | if (FormatTok.Tok.is(tok::l_paren)) { |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 583 | parseParens(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 584 | } |
Manuel Klimek | 308232c | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 585 | if (FormatTok.Tok.is(tok::identifier)) |
| 586 | nextToken(); |
| 587 | } |
| 588 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 589 | nextToken(); |
| 590 | addUnwrappedLine(); |
| 591 | ++Line->Level; |
| 592 | do { |
| 593 | switch (FormatTok.Tok.getKind()) { |
Manuel Klimek | 308232c | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 594 | case tok::l_paren: |
| 595 | parseParens(); |
| 596 | break; |
| 597 | case tok::r_brace: |
| 598 | addUnwrappedLine(); |
| 599 | nextToken(); |
| 600 | --Line->Level; |
| 601 | return; |
| 602 | case tok::comma: |
| 603 | nextToken(); |
| 604 | addUnwrappedLine(); |
| 605 | break; |
| 606 | default: |
| 607 | nextToken(); |
| 608 | break; |
| 609 | } |
| 610 | } while (!eof()); |
| 611 | } |
| 612 | // We fall through to parsing a structural element afterwards, so that in |
| 613 | // enum A {} n, m; |
| 614 | // "} n, m;" will end up in one unwrapped line. |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 617 | void UnwrappedLineParser::parseRecord() { |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 618 | nextToken(); |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 619 | if (FormatTok.Tok.is(tok::identifier) || |
| 620 | FormatTok.Tok.is(tok::kw___attribute) || |
| 621 | FormatTok.Tok.is(tok::kw___declspec)) { |
| 622 | nextToken(); |
| 623 | // We can have macros or attributes in between 'class' and the class name. |
| 624 | if (FormatTok.Tok.is(tok::l_paren)) { |
| 625 | parseParens(); |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 626 | } |
Manuel Klimek | 7f5b025 | 2013-01-21 10:17:14 +0000 | [diff] [blame] | 627 | // The actual identifier can be a nested name specifier. |
| 628 | while (FormatTok.Tok.is(tok::identifier) || |
| 629 | FormatTok.Tok.is(tok::coloncolon)) |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 630 | nextToken(); |
| 631 | |
Manuel Klimek | 3a3408c | 2013-01-21 13:58:54 +0000 | [diff] [blame] | 632 | // Note that parsing away template declarations here leads to incorrectly |
| 633 | // accepting function declarations as record declarations. |
| 634 | // In general, we cannot solve this problem. Consider: |
| 635 | // class A<int> B() {} |
| 636 | // which can be a function definition or a class definition when B() is a |
| 637 | // macro. If we find enough real-world cases where this is a problem, we |
| 638 | // can parse for the 'template' keyword in the beginning of the statement, |
| 639 | // and thus rule out the record production in case there is no template |
| 640 | // (this would still leave us with an ambiguity between template function |
| 641 | // and class declarations). |
| 642 | if (FormatTok.Tok.is(tok::colon) || FormatTok.Tok.is(tok::less)) { |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 643 | while (FormatTok.Tok.isNot(tok::l_brace)) { |
| 644 | if (FormatTok.Tok.is(tok::semi)) |
| 645 | return; |
| 646 | nextToken(); |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | if (FormatTok.Tok.is(tok::l_brace)) |
| 651 | parseBlock(); |
Manuel Klimek | 3a3408c | 2013-01-21 13:58:54 +0000 | [diff] [blame] | 652 | // We fall through to parsing a structural element afterwards, so |
| 653 | // class A {} n, m; |
| 654 | // will end up in one unwrapped line. |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 657 | void UnwrappedLineParser::parseObjCProtocolList() { |
| 658 | assert(FormatTok.Tok.is(tok::less) && "'<' expected."); |
| 659 | do |
| 660 | nextToken(); |
| 661 | while (!eof() && FormatTok.Tok.isNot(tok::greater)); |
| 662 | nextToken(); // Skip '>'. |
| 663 | } |
| 664 | |
| 665 | void UnwrappedLineParser::parseObjCUntilAtEnd() { |
| 666 | do { |
| 667 | if (FormatTok.Tok.isObjCAtKeyword(tok::objc_end)) { |
| 668 | nextToken(); |
| 669 | addUnwrappedLine(); |
| 670 | break; |
| 671 | } |
| 672 | parseStructuralElement(); |
| 673 | } while (!eof()); |
| 674 | } |
| 675 | |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 676 | void UnwrappedLineParser::parseObjCInterfaceOrImplementation() { |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 677 | nextToken(); |
| 678 | nextToken(); // interface name |
| 679 | |
| 680 | // @interface can be followed by either a base class, or a category. |
| 681 | if (FormatTok.Tok.is(tok::colon)) { |
| 682 | nextToken(); |
| 683 | nextToken(); // base class name |
| 684 | } else if (FormatTok.Tok.is(tok::l_paren)) |
| 685 | // Skip category, if present. |
| 686 | parseParens(); |
| 687 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 688 | if (FormatTok.Tok.is(tok::less)) |
| 689 | parseObjCProtocolList(); |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 690 | |
| 691 | // If instance variables are present, keep the '{' on the first line too. |
| 692 | if (FormatTok.Tok.is(tok::l_brace)) |
| 693 | parseBlock(); |
| 694 | |
| 695 | // With instance variables, this puts '}' on its own line. Without instance |
| 696 | // variables, this ends the @interface line. |
| 697 | addUnwrappedLine(); |
| 698 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 699 | parseObjCUntilAtEnd(); |
| 700 | } |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 701 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 702 | void UnwrappedLineParser::parseObjCProtocol() { |
| 703 | nextToken(); |
| 704 | nextToken(); // protocol name |
| 705 | |
| 706 | if (FormatTok.Tok.is(tok::less)) |
| 707 | parseObjCProtocolList(); |
| 708 | |
| 709 | // Check for protocol declaration. |
| 710 | if (FormatTok.Tok.is(tok::semi)) { |
| 711 | nextToken(); |
| 712 | return addUnwrappedLine(); |
| 713 | } |
| 714 | |
| 715 | addUnwrappedLine(); |
| 716 | parseObjCUntilAtEnd(); |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 719 | void UnwrappedLineParser::addUnwrappedLine() { |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 720 | if (Line->Tokens.empty()) |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 721 | return; |
Manuel Klimek | 8fa3799 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 722 | DEBUG({ |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame^] | 723 | llvm::dbgs() << "Line(" << Line->Level << "): "; |
Manuel Klimek | 8fa3799 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 724 | for (std::list<FormatToken>::iterator I = Line->Tokens.begin(), |
| 725 | E = Line->Tokens.end(); |
| 726 | I != E; ++I) { |
| 727 | llvm::dbgs() << I->Tok.getName() << " "; |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 728 | |
Manuel Klimek | 8fa3799 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 729 | } |
| 730 | llvm::dbgs() << "\n"; |
| 731 | }); |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 732 | CurrentLines->push_back(*Line); |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 733 | Line->Tokens.clear(); |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 734 | if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) { |
| 735 | for (std::vector<UnwrappedLine>::iterator I = PreprocessorDirectives |
| 736 | .begin(), E = PreprocessorDirectives.end(); |
| 737 | I != E; ++I) { |
| 738 | CurrentLines->push_back(*I); |
| 739 | } |
| 740 | PreprocessorDirectives.clear(); |
| 741 | } |
| 742 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | bool UnwrappedLineParser::eof() const { |
| 746 | return FormatTok.Tok.is(tok::eof); |
| 747 | } |
| 748 | |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame^] | 749 | void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) { |
| 750 | bool JustComments = Line->Tokens.empty(); |
| 751 | for (SmallVectorImpl<FormatToken>::const_iterator |
| 752 | I = CommentsBeforeNextToken.begin(), |
| 753 | E = CommentsBeforeNextToken.end(); |
| 754 | I != E; ++I) { |
| 755 | if (I->HasUnescapedNewline && JustComments) { |
| 756 | addUnwrappedLine(); |
| 757 | } |
| 758 | pushToken(*I); |
| 759 | } |
| 760 | if (NewlineBeforeNext && JustComments) { |
| 761 | addUnwrappedLine(); |
| 762 | } |
| 763 | CommentsBeforeNextToken.clear(); |
| 764 | } |
| 765 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 766 | void UnwrappedLineParser::nextToken() { |
| 767 | if (eof()) |
| 768 | return; |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame^] | 769 | flushComments(FormatTok.HasUnescapedNewline); |
| 770 | pushToken(FormatTok); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 771 | readToken(); |
| 772 | } |
| 773 | |
| 774 | void UnwrappedLineParser::readToken() { |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame^] | 775 | bool CommentsInCurrentLine = true; |
| 776 | do { |
| 777 | FormatTok = Tokens->getNextToken(); |
| 778 | while (!Line->InPPDirective && FormatTok.Tok.is(tok::hash) && |
| 779 | ((FormatTok.NewlinesBefore > 0 && FormatTok.HasUnescapedNewline) || |
| 780 | FormatTok.IsFirst)) { |
| 781 | // If there is an unfinished unwrapped line, we flush the preprocessor |
| 782 | // directives only after that unwrapped line was finished later. |
| 783 | bool SwitchToPreprocessorLines = !Line->Tokens.empty() && |
| 784 | CurrentLines == &Lines; |
| 785 | ScopedLineState BlockState(*this, SwitchToPreprocessorLines); |
| 786 | parsePPDirective(); |
| 787 | } |
| 788 | if (!FormatTok.Tok.is(tok::comment)) |
| 789 | return; |
| 790 | if (FormatTok.HasUnescapedNewline || FormatTok.IsFirst) { |
| 791 | CommentsInCurrentLine = false; |
| 792 | } |
| 793 | if (CommentsInCurrentLine) { |
| 794 | pushToken(FormatTok); |
| 795 | } else { |
| 796 | CommentsBeforeNextToken.push_back(FormatTok); |
| 797 | } |
| 798 | } while (!eof()); |
| 799 | } |
| 800 | |
| 801 | void UnwrappedLineParser::pushToken(const FormatToken &Tok) { |
| 802 | Line->Tokens.push_back(Tok); |
| 803 | if (MustBreakBeforeNextToken) { |
| 804 | Line->Tokens.back().MustBreakBefore = true; |
| 805 | MustBreakBeforeNextToken = false; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 806 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 807 | } |
| 808 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 809 | } // end namespace format |
| 810 | } // end namespace clang |