Daniel Jasper | f793511 | 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 | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Chandler Carruth | 4b41745 | 2013-01-19 08:09:44 +0000 | [diff] [blame] | 16 | #include "UnwrappedLineParser.h" |
Manuel Klimek | ab3dc00 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Debug.h" |
Manuel Klimek | ab3dc00 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 18 | |
Chandler Carruth | 1034666 | 2014-04-22 03:17:02 +0000 | [diff] [blame] | 19 | #define DEBUG_TYPE "format-parser" |
| 20 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 21 | namespace clang { |
| 22 | namespace format { |
| 23 | |
Manuel Klimek | 15dfe7a | 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 | 69665e1 | 2013-07-01 04:21:54 +0000 | [diff] [blame] | 33 | namespace { |
| 34 | |
Manuel Klimek | 0a3a3c9 | 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 | 0a3a3c9 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 40 | Line.MustBeDeclaration = MustBeDeclaration; |
Manuel Klimek | 3908057 | 2013-01-23 11:03:04 +0000 | [diff] [blame] | 41 | Stack.push_back(MustBeDeclaration); |
Manuel Klimek | 0a3a3c9 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 42 | } |
| 43 | ~ScopedDeclarationState() { |
Manuel Klimek | 0a3a3c9 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 44 | Stack.pop_back(); |
Manuel Klimek | c1237a8 | 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 | 0a3a3c9 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 49 | } |
Daniel Jasper | 393564f | 2013-05-31 14:56:29 +0000 | [diff] [blame] | 50 | |
Manuel Klimek | 0a3a3c9 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 51 | private: |
| 52 | UnwrappedLine &Line; |
| 53 | std::vector<bool> &Stack; |
| 54 | }; |
| 55 | |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 56 | class ScopedMacroState : public FormatTokenSource { |
| 57 | public: |
| 58 | ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource, |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 59 | FormatToken *&ResetToken, bool &StructuralError) |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 60 | : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken), |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 61 | PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource), |
| 62 | StructuralError(StructuralError), |
Craig Topper | 2145bc0 | 2014-05-09 08:15:10 +0000 | [diff] [blame] | 63 | PreviousStructuralError(StructuralError), Token(nullptr) { |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 64 | TokenSource = this; |
Manuel Klimek | ef2cfb1 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 65 | Line.Level = 0; |
Manuel Klimek | 1abf789 | 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 | ef2cfb1 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 73 | Line.Level = PreviousLineLevel; |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 74 | StructuralError = PreviousStructuralError; |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 77 | FormatToken *getNextToken() override { |
Manuel Klimek | 7872571 | 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 | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 81 | Token = PreviousTokenSource->getNextToken(); |
| 82 | if (eof()) |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 83 | return getFakeEOF(); |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 84 | return Token; |
| 85 | } |
| 86 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 87 | unsigned getPosition() override { return PreviousTokenSource->getPosition(); } |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 88 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 89 | FormatToken *setPosition(unsigned Position) override { |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 90 | Token = PreviousTokenSource->setPosition(Position); |
| 91 | return Token; |
| 92 | } |
| 93 | |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 94 | private: |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 95 | bool eof() { return Token && Token->HasUnescapedNewline; } |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 96 | |
Manuel Klimek | 15dfe7a | 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 | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | UnwrappedLine &Line; |
| 109 | FormatTokenSource *&TokenSource; |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 110 | FormatToken *&ResetToken; |
Manuel Klimek | ef2cfb1 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 111 | unsigned PreviousLineLevel; |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 112 | FormatTokenSource *PreviousTokenSource; |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 113 | bool &StructuralError; |
| 114 | bool PreviousStructuralError; |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 115 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 116 | FormatToken *Token; |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
Craig Topper | 69665e1 | 2013-07-01 04:21:54 +0000 | [diff] [blame] | 119 | } // end anonymous namespace |
| 120 | |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 121 | class ScopedLineState { |
| 122 | public: |
Manuel Klimek | d3b92fa | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 123 | ScopedLineState(UnwrappedLineParser &Parser, |
| 124 | bool SwitchToPreprocessorLines = false) |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 125 | : Parser(Parser) { |
| 126 | OriginalLines = Parser.CurrentLines; |
Manuel Klimek | d3b92fa | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 127 | if (SwitchToPreprocessorLines) |
| 128 | Parser.CurrentLines = &Parser.PreprocessorDirectives; |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 129 | else if (!Parser.Line->Tokens.empty()) |
| 130 | Parser.CurrentLines = &Parser.Line->Tokens.back().Children; |
Ahmed Charles | 9a16beb | 2014-03-07 19:33:25 +0000 | [diff] [blame] | 131 | PreBlockLine = Parser.Line.release(); |
Daniel Jasper | daffc0d | 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 | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | ~ScopedLineState() { |
Daniel Jasper | daffc0d | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 138 | if (!Parser.Line->Tokens.empty()) { |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 139 | Parser.addUnwrappedLine(); |
| 140 | } |
Daniel Jasper | daffc0d | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 141 | assert(Parser.Line->Tokens.empty()); |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 142 | Parser.Line.reset(PreBlockLine); |
Daniel Jasper | 9fe0e8d | 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 | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | private: |
| 149 | UnwrappedLineParser &Parser; |
| 150 | |
| 151 | UnwrappedLine *PreBlockLine; |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 152 | SmallVectorImpl<UnwrappedLine> *OriginalLines; |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 155 | class CompoundStatementIndenter { |
| 156 | public: |
| 157 | CompoundStatementIndenter(UnwrappedLineParser *Parser, |
| 158 | const FormatStyle &Style, unsigned &LineLevel) |
| 159 | : LineLevel(LineLevel), OldLineLevel(LineLevel) { |
| 160 | if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) { |
| 161 | Parser->addUnwrappedLine(); |
| 162 | } else if (Style.BreakBeforeBraces == FormatStyle::BS_GNU) { |
| 163 | Parser->addUnwrappedLine(); |
| 164 | ++LineLevel; |
| 165 | } |
| 166 | } |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 167 | ~CompoundStatementIndenter() { LineLevel = OldLineLevel; } |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 168 | |
| 169 | private: |
| 170 | unsigned &LineLevel; |
| 171 | unsigned OldLineLevel; |
| 172 | }; |
| 173 | |
Craig Topper | 69665e1 | 2013-07-01 04:21:54 +0000 | [diff] [blame] | 174 | namespace { |
| 175 | |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 176 | class IndexedTokenSource : public FormatTokenSource { |
| 177 | public: |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 178 | IndexedTokenSource(ArrayRef<FormatToken *> Tokens) |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 179 | : Tokens(Tokens), Position(-1) {} |
| 180 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 181 | FormatToken *getNextToken() override { |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 182 | ++Position; |
| 183 | return Tokens[Position]; |
| 184 | } |
| 185 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 186 | unsigned getPosition() override { |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 187 | assert(Position >= 0); |
| 188 | return Position; |
| 189 | } |
| 190 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 191 | FormatToken *setPosition(unsigned P) override { |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 192 | Position = P; |
| 193 | return Tokens[Position]; |
| 194 | } |
| 195 | |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 196 | void reset() { Position = -1; } |
| 197 | |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 198 | private: |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 199 | ArrayRef<FormatToken *> Tokens; |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 200 | int Position; |
| 201 | }; |
| 202 | |
Craig Topper | 69665e1 | 2013-07-01 04:21:54 +0000 | [diff] [blame] | 203 | } // end anonymous namespace |
| 204 | |
Daniel Jasper | d2ae41a | 2013-05-15 08:14:19 +0000 | [diff] [blame] | 205 | UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style, |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 206 | ArrayRef<FormatToken *> Tokens, |
Daniel Jasper | d2ae41a | 2013-05-15 08:14:19 +0000 | [diff] [blame] | 207 | UnwrappedLineConsumer &Callback) |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 208 | : Line(new UnwrappedLine), MustBreakBeforeNextToken(false), |
| 209 | CurrentLines(&Lines), StructuralError(false), Style(Style), |
| 210 | Tokens(nullptr), Callback(Callback), AllTokens(Tokens), |
| 211 | PPBranchLevel(-1) {} |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 212 | |
| 213 | void UnwrappedLineParser::reset() { |
| 214 | PPBranchLevel = -1; |
| 215 | Line.reset(new UnwrappedLine); |
| 216 | CommentsBeforeNextToken.clear(); |
Craig Topper | 2145bc0 | 2014-05-09 08:15:10 +0000 | [diff] [blame] | 217 | FormatTok = nullptr; |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 218 | MustBreakBeforeNextToken = false; |
| 219 | PreprocessorDirectives.clear(); |
| 220 | CurrentLines = &Lines; |
| 221 | DeclarationScopeStack.clear(); |
| 222 | StructuralError = false; |
| 223 | PPStack.clear(); |
| 224 | } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 225 | |
Alexander Kornienko | 870f9eb | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 226 | bool UnwrappedLineParser::parse() { |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 227 | IndexedTokenSource TokenSource(AllTokens); |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 228 | do { |
| 229 | DEBUG(llvm::dbgs() << "----\n"); |
| 230 | reset(); |
| 231 | Tokens = &TokenSource; |
| 232 | TokenSource.reset(); |
Daniel Jasper | a79064a | 2013-03-01 18:11:39 +0000 | [diff] [blame] | 233 | |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 234 | readToken(); |
| 235 | parseFile(); |
| 236 | // Create line with eof token. |
| 237 | pushToken(FormatTok); |
| 238 | addUnwrappedLine(); |
| 239 | |
| 240 | for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(), |
| 241 | E = Lines.end(); |
| 242 | I != E; ++I) { |
| 243 | Callback.consumeUnwrappedLine(*I); |
| 244 | } |
| 245 | Callback.finishRun(); |
| 246 | Lines.clear(); |
| 247 | while (!PPLevelBranchIndex.empty() && |
Daniel Jasper | 53bd167 | 2013-10-12 13:32:56 +0000 | [diff] [blame] | 248 | PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) { |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 249 | PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1); |
| 250 | PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1); |
| 251 | } |
| 252 | if (!PPLevelBranchIndex.empty()) { |
| 253 | ++PPLevelBranchIndex.back(); |
| 254 | assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size()); |
| 255 | assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back()); |
| 256 | } |
| 257 | } while (!PPLevelBranchIndex.empty()); |
| 258 | |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 259 | return StructuralError; |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 262 | void UnwrappedLineParser::parseFile() { |
Daniel Jasper | dd9276e | 2013-03-22 16:55:40 +0000 | [diff] [blame] | 263 | ScopedDeclarationState DeclarationState( |
| 264 | *Line, DeclarationScopeStack, |
| 265 | /*MustBeDeclaration=*/ !Line->InPPDirective); |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 266 | parseLevel(/*HasOpeningBrace=*/false); |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 267 | // Make sure to format the remaining tokens. |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 268 | flushComments(true); |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 269 | addUnwrappedLine(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 272 | void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) { |
Daniel Jasper | 516d797 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 273 | bool SwitchLabelEncountered = false; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 274 | do { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 275 | switch (FormatTok->Tok.getKind()) { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 276 | case tok::comment: |
Daniel Jasper | e25509f | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 277 | nextToken(); |
| 278 | addUnwrappedLine(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 279 | break; |
| 280 | case tok::l_brace: |
Manuel Klimek | 0a3a3c9 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 281 | // FIXME: Add parameter whether this can happen - if this happens, we must |
| 282 | // be in a non-declaration context. |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 283 | parseBlock(/*MustBeDeclaration=*/false); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 284 | addUnwrappedLine(); |
| 285 | break; |
| 286 | case tok::r_brace: |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 287 | if (HasOpeningBrace) |
| 288 | return; |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 289 | StructuralError = true; |
| 290 | nextToken(); |
| 291 | addUnwrappedLine(); |
Manuel Klimek | 1058d98 | 2013-01-06 20:07:31 +0000 | [diff] [blame] | 292 | break; |
Daniel Jasper | 516d797 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 293 | case tok::kw_default: |
| 294 | case tok::kw_case: |
Daniel Jasper | 7240762 | 2013-09-02 08:26:29 +0000 | [diff] [blame] | 295 | if (!SwitchLabelEncountered && |
| 296 | (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1))) |
| 297 | ++Line->Level; |
Daniel Jasper | 516d797 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 298 | SwitchLabelEncountered = true; |
| 299 | parseStructuralElement(); |
| 300 | break; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 301 | default: |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 302 | parseStructuralElement(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 303 | break; |
| 304 | } |
| 305 | } while (!eof()); |
| 306 | } |
| 307 | |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 308 | void UnwrappedLineParser::calculateBraceTypes() { |
| 309 | // We'll parse forward through the tokens until we hit |
| 310 | // a closing brace or eof - note that getNextToken() will |
| 311 | // parse macros, so this will magically work inside macro |
| 312 | // definitions, too. |
| 313 | unsigned StoredPosition = Tokens->getPosition(); |
| 314 | unsigned Position = StoredPosition; |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 315 | FormatToken *Tok = FormatTok; |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 316 | // Keep a stack of positions of lbrace tokens. We will |
| 317 | // update information about whether an lbrace starts a |
| 318 | // braced init list or a different block during the loop. |
Daniel Jasper | b1f74a8 | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 319 | SmallVector<FormatToken *, 8> LBraceStack; |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 320 | assert(Tok->Tok.is(tok::l_brace)); |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 321 | do { |
Daniel Jasper | 7f5d53e | 2013-07-01 09:15:46 +0000 | [diff] [blame] | 322 | // Get next none-comment token. |
| 323 | FormatToken *NextTok; |
Daniel Jasper | ca7bd72 | 2013-07-01 16:43:38 +0000 | [diff] [blame] | 324 | unsigned ReadTokens = 0; |
Daniel Jasper | 7f5d53e | 2013-07-01 09:15:46 +0000 | [diff] [blame] | 325 | do { |
| 326 | NextTok = Tokens->getNextToken(); |
Daniel Jasper | ca7bd72 | 2013-07-01 16:43:38 +0000 | [diff] [blame] | 327 | ++ReadTokens; |
Daniel Jasper | 7f5d53e | 2013-07-01 09:15:46 +0000 | [diff] [blame] | 328 | } while (NextTok->is(tok::comment)); |
| 329 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 330 | switch (Tok->Tok.getKind()) { |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 331 | case tok::l_brace: |
Daniel Jasper | b1f74a8 | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 332 | LBraceStack.push_back(Tok); |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 333 | break; |
| 334 | case tok::r_brace: |
| 335 | if (!LBraceStack.empty()) { |
Daniel Jasper | b1f74a8 | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 336 | if (LBraceStack.back()->BlockKind == BK_Unknown) { |
Daniel Jasper | 220c0d1 | 2014-04-10 07:27:12 +0000 | [diff] [blame] | 337 | bool ProbablyBracedList = false; |
| 338 | if (Style.Language == FormatStyle::LK_Proto) { |
| 339 | ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square); |
| 340 | } else { |
| 341 | // If there is a comma, semicolon or right paren after the closing |
| 342 | // brace, we assume this is a braced initializer list. Note that |
| 343 | // regardless how we mark inner braces here, we will overwrite the |
| 344 | // BlockKind later if we parse a braced list (where all blocks |
| 345 | // inside are by default braced lists), or when we explicitly detect |
| 346 | // blocks (for example while parsing lambdas). |
| 347 | // |
| 348 | // We exclude + and - as they can be ObjC visibility modifiers. |
| 349 | ProbablyBracedList = |
| 350 | NextTok->isOneOf(tok::comma, tok::semi, tok::period, tok::colon, |
Daniel Jasper | 438059e | 2014-05-22 12:11:13 +0000 | [diff] [blame^] | 351 | tok::r_paren, tok::r_square, tok::l_brace, |
| 352 | tok::l_paren) || |
Daniel Jasper | 220c0d1 | 2014-04-10 07:27:12 +0000 | [diff] [blame] | 353 | (NextTok->isBinaryOperator() && |
| 354 | !NextTok->isOneOf(tok::plus, tok::minus)); |
| 355 | } |
| 356 | if (ProbablyBracedList) { |
Daniel Jasper | b1f74a8 | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 357 | Tok->BlockKind = BK_BracedInit; |
| 358 | LBraceStack.back()->BlockKind = BK_BracedInit; |
| 359 | } else { |
| 360 | Tok->BlockKind = BK_Block; |
| 361 | LBraceStack.back()->BlockKind = BK_Block; |
| 362 | } |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 363 | } |
| 364 | LBraceStack.pop_back(); |
| 365 | } |
| 366 | break; |
Daniel Jasper | ac7e34e | 2014-03-13 10:11:17 +0000 | [diff] [blame] | 367 | case tok::at: |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 368 | case tok::semi: |
| 369 | case tok::kw_if: |
| 370 | case tok::kw_while: |
| 371 | case tok::kw_for: |
| 372 | case tok::kw_switch: |
| 373 | case tok::kw_try: |
Daniel Jasper | 393564f | 2013-05-31 14:56:29 +0000 | [diff] [blame] | 374 | if (!LBraceStack.empty()) |
Daniel Jasper | b1f74a8 | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 375 | LBraceStack.back()->BlockKind = BK_Block; |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 376 | break; |
| 377 | default: |
| 378 | break; |
| 379 | } |
| 380 | Tok = NextTok; |
Daniel Jasper | ca7bd72 | 2013-07-01 16:43:38 +0000 | [diff] [blame] | 381 | Position += ReadTokens; |
Manuel Klimek | bab25fd | 2013-09-04 08:20:47 +0000 | [diff] [blame] | 382 | } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty()); |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 383 | // Assume other blocks for all unclosed opening braces. |
| 384 | for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) { |
Daniel Jasper | b1f74a8 | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 385 | if (LBraceStack[i]->BlockKind == BK_Unknown) |
| 386 | LBraceStack[i]->BlockKind = BK_Block; |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 387 | } |
Manuel Klimek | bab25fd | 2013-09-04 08:20:47 +0000 | [diff] [blame] | 388 | |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 389 | FormatTok = Tokens->setPosition(StoredPosition); |
| 390 | } |
| 391 | |
Manuel Klimek | b212f3b | 2013-10-12 22:46:56 +0000 | [diff] [blame] | 392 | void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel, |
| 393 | bool MunchSemi) { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 394 | assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected"); |
Daniel Jasper | 516d797 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 395 | unsigned InitialLevel = Line->Level; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 396 | nextToken(); |
| 397 | |
Manuel Klimek | a4fe1c1 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 398 | addUnwrappedLine(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 399 | |
Manuel Klimek | 0a3a3c9 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 400 | ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, |
| 401 | MustBeDeclaration); |
Daniel Jasper | 65ee347 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 402 | if (AddLevel) |
| 403 | ++Line->Level; |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 404 | parseLevel(/*HasOpeningBrace=*/true); |
Alexander Kornienko | 578fdd8 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 405 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 406 | if (!FormatTok->Tok.is(tok::r_brace)) { |
Daniel Jasper | 516d797 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 407 | Line->Level = InitialLevel; |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 408 | StructuralError = true; |
| 409 | return; |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 410 | } |
Alexander Kornienko | 0ea8e10 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 411 | |
Daniel Jasper | d1ae358 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 412 | nextToken(); // Munch the closing brace. |
Manuel Klimek | b212f3b | 2013-10-12 22:46:56 +0000 | [diff] [blame] | 413 | if (MunchSemi && FormatTok->Tok.is(tok::semi)) |
| 414 | nextToken(); |
Daniel Jasper | 516d797 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 415 | Line->Level = InitialLevel; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Daniel Jasper | 4a39c84 | 2014-05-06 13:54:10 +0000 | [diff] [blame] | 418 | static bool IsGoogScope(const UnwrappedLine &Line) { |
| 419 | if (Line.Tokens.size() < 4) |
| 420 | return false; |
| 421 | auto I = Line.Tokens.begin(); |
| 422 | if (I->Tok->TokenText != "goog") |
| 423 | return false; |
| 424 | ++I; |
| 425 | if (I->Tok->isNot(tok::period)) |
| 426 | return false; |
| 427 | ++I; |
| 428 | if (I->Tok->TokenText != "scope") |
| 429 | return false; |
| 430 | ++I; |
| 431 | return I->Tok->is(tok::l_paren); |
| 432 | } |
| 433 | |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 434 | void UnwrappedLineParser::parseChildBlock() { |
| 435 | FormatTok->BlockKind = BK_Block; |
| 436 | nextToken(); |
| 437 | { |
Daniel Jasper | 4a39c84 | 2014-05-06 13:54:10 +0000 | [diff] [blame] | 438 | bool GoogScope = |
| 439 | Style.Language == FormatStyle::LK_JavaScript && IsGoogScope(*Line); |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 440 | ScopedLineState LineState(*this); |
| 441 | ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, |
| 442 | /*MustBeDeclaration=*/false); |
Daniel Jasper | 4a39c84 | 2014-05-06 13:54:10 +0000 | [diff] [blame] | 443 | Line->Level += GoogScope ? 0 : 1; |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 444 | parseLevel(/*HasOpeningBrace=*/true); |
Daniel Jasper | 4a39c84 | 2014-05-06 13:54:10 +0000 | [diff] [blame] | 445 | Line->Level -= GoogScope ? 0 : 1; |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 446 | } |
| 447 | nextToken(); |
| 448 | } |
| 449 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 450 | void UnwrappedLineParser::parsePPDirective() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 451 | assert(FormatTok->Tok.is(tok::hash) && "'#' expected"); |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 452 | ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError); |
Manuel Klimek | a71e5d8 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 453 | nextToken(); |
| 454 | |
Craig Topper | 2145bc0 | 2014-05-09 08:15:10 +0000 | [diff] [blame] | 455 | if (!FormatTok->Tok.getIdentifierInfo()) { |
Manuel Klimek | 591b580 | 2013-01-31 15:58:48 +0000 | [diff] [blame] | 456 | parsePPUnknown(); |
Manuel Klimek | a71e5d8 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 457 | return; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 458 | } |
Manuel Klimek | a71e5d8 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 459 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 460 | switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) { |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 461 | case tok::pp_define: |
| 462 | parsePPDefine(); |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 463 | return; |
| 464 | case tok::pp_if: |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 465 | parsePPIf(/*IfDef=*/false); |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 466 | break; |
| 467 | case tok::pp_ifdef: |
| 468 | case tok::pp_ifndef: |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 469 | parsePPIf(/*IfDef=*/true); |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 470 | break; |
| 471 | case tok::pp_else: |
| 472 | parsePPElse(); |
| 473 | break; |
| 474 | case tok::pp_elif: |
| 475 | parsePPElIf(); |
| 476 | break; |
| 477 | case tok::pp_endif: |
| 478 | parsePPEndIf(); |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 479 | break; |
| 480 | default: |
| 481 | parsePPUnknown(); |
| 482 | break; |
| 483 | } |
| 484 | } |
| 485 | |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 486 | void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) { |
| 487 | if (Unreachable || (!PPStack.empty() && PPStack.back() == PP_Unreachable)) |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 488 | PPStack.push_back(PP_Unreachable); |
| 489 | else |
| 490 | PPStack.push_back(PP_Conditional); |
| 491 | } |
| 492 | |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 493 | void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) { |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 494 | ++PPBranchLevel; |
| 495 | assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size()); |
| 496 | if (PPBranchLevel == (int)PPLevelBranchIndex.size()) { |
| 497 | PPLevelBranchIndex.push_back(0); |
| 498 | PPLevelBranchCount.push_back(0); |
| 499 | } |
| 500 | PPChainBranchIndex.push(0); |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 501 | bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0; |
| 502 | conditionalCompilationCondition(Unreachable || Skip); |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 505 | void UnwrappedLineParser::conditionalCompilationAlternative() { |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 506 | if (!PPStack.empty()) |
| 507 | PPStack.pop_back(); |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 508 | assert(PPBranchLevel < (int)PPLevelBranchIndex.size()); |
| 509 | if (!PPChainBranchIndex.empty()) |
| 510 | ++PPChainBranchIndex.top(); |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 511 | conditionalCompilationCondition( |
| 512 | PPBranchLevel >= 0 && !PPChainBranchIndex.empty() && |
| 513 | PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top()); |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 516 | void UnwrappedLineParser::conditionalCompilationEnd() { |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 517 | assert(PPBranchLevel < (int)PPLevelBranchIndex.size()); |
| 518 | if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) { |
| 519 | if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) { |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 520 | PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1; |
| 521 | } |
| 522 | } |
Manuel Klimek | 14bd917 | 2014-01-29 08:49:02 +0000 | [diff] [blame] | 523 | // Guard against #endif's without #if. |
| 524 | if (PPBranchLevel > 0) |
| 525 | --PPBranchLevel; |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 526 | if (!PPChainBranchIndex.empty()) |
| 527 | PPChainBranchIndex.pop(); |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 528 | if (!PPStack.empty()) |
| 529 | PPStack.pop_back(); |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | void UnwrappedLineParser::parsePPIf(bool IfDef) { |
| 533 | nextToken(); |
| 534 | bool IsLiteralFalse = (FormatTok->Tok.isLiteral() && |
| 535 | StringRef(FormatTok->Tok.getLiteralData(), |
| 536 | FormatTok->Tok.getLength()) == "0") || |
| 537 | FormatTok->Tok.is(tok::kw_false); |
| 538 | conditionalCompilationStart(!IfDef && IsLiteralFalse); |
| 539 | parsePPUnknown(); |
| 540 | } |
| 541 | |
| 542 | void UnwrappedLineParser::parsePPElse() { |
| 543 | conditionalCompilationAlternative(); |
| 544 | parsePPUnknown(); |
| 545 | } |
| 546 | |
| 547 | void UnwrappedLineParser::parsePPElIf() { parsePPElse(); } |
| 548 | |
| 549 | void UnwrappedLineParser::parsePPEndIf() { |
| 550 | conditionalCompilationEnd(); |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 551 | parsePPUnknown(); |
| 552 | } |
| 553 | |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 554 | void UnwrappedLineParser::parsePPDefine() { |
| 555 | nextToken(); |
| 556 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 557 | if (FormatTok->Tok.getKind() != tok::identifier) { |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 558 | parsePPUnknown(); |
| 559 | return; |
| 560 | } |
| 561 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 562 | if (FormatTok->Tok.getKind() == tok::l_paren && |
| 563 | FormatTok->WhitespaceRange.getBegin() == |
| 564 | FormatTok->WhitespaceRange.getEnd()) { |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 565 | parseParens(); |
| 566 | } |
| 567 | addUnwrappedLine(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 568 | Line->Level = 1; |
Manuel Klimek | 1b89629 | 2013-01-07 09:34:28 +0000 | [diff] [blame] | 569 | |
| 570 | // Errors during a preprocessor directive can only affect the layout of the |
| 571 | // preprocessor directive, and thus we ignore them. An alternative approach |
| 572 | // would be to use the same approach we use on the file level (no |
| 573 | // re-indentation if there was a structural error) within the macro |
| 574 | // definition. |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 575 | parseFile(); |
| 576 | } |
| 577 | |
| 578 | void UnwrappedLineParser::parsePPUnknown() { |
Manuel Klimek | a71e5d8 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 579 | do { |
Manuel Klimek | a71e5d8 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 580 | nextToken(); |
| 581 | } while (!eof()); |
| 582 | addUnwrappedLine(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Alexander Kornienko | a04e5e2 | 2013-04-09 16:15:19 +0000 | [diff] [blame] | 585 | // Here we blacklist certain tokens that are not usually the first token in an |
| 586 | // unwrapped line. This is used in attempt to distinguish macro calls without |
| 587 | // trailing semicolons from other constructs split to several lines. |
| 588 | bool tokenCanStartNewLine(clang::Token Tok) { |
| 589 | // Semicolon can be a null-statement, l_square can be a start of a macro or |
| 590 | // a C++11 attribute, but this doesn't seem to be common. |
| 591 | return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) && |
| 592 | Tok.isNot(tok::l_square) && |
| 593 | // Tokens that can only be used as binary operators and a part of |
| 594 | // overloaded operator names. |
| 595 | Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) && |
| 596 | Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) && |
| 597 | Tok.isNot(tok::less) && Tok.isNot(tok::greater) && |
| 598 | Tok.isNot(tok::slash) && Tok.isNot(tok::percent) && |
| 599 | Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) && |
| 600 | Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) && |
| 601 | Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) && |
| 602 | Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) && |
| 603 | Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) && |
| 604 | Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) && |
| 605 | Tok.isNot(tok::lesslessequal) && |
| 606 | // Colon is used in labels, base class lists, initializer lists, |
| 607 | // range-based for loops, ternary operator, but should never be the |
| 608 | // first token in an unwrapped line. |
Daniel Jasper | 5ebb2f3 | 2014-05-21 13:08:17 +0000 | [diff] [blame] | 609 | Tok.isNot(tok::colon) && |
| 610 | // 'noexcept' is a trailing annotation. |
| 611 | Tok.isNot(tok::kw_noexcept); |
Alexander Kornienko | a04e5e2 | 2013-04-09 16:15:19 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 614 | void UnwrappedLineParser::parseStructuralElement() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 615 | assert(!FormatTok->Tok.is(tok::l_brace)); |
| 616 | switch (FormatTok->Tok.getKind()) { |
Nico Weber | 04e9f1a | 2013-01-07 19:05:19 +0000 | [diff] [blame] | 617 | case tok::at: |
| 618 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 619 | if (FormatTok->Tok.is(tok::l_brace)) { |
Nico Weber | 372d8dc | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 620 | parseBracedList(); |
| 621 | break; |
| 622 | } |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 623 | switch (FormatTok->Tok.getObjCKeywordID()) { |
Nico Weber | 04e9f1a | 2013-01-07 19:05:19 +0000 | [diff] [blame] | 624 | case tok::objc_public: |
| 625 | case tok::objc_protected: |
| 626 | case tok::objc_package: |
| 627 | case tok::objc_private: |
| 628 | return parseAccessSpecifier(); |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 629 | case tok::objc_interface: |
Nico Weber | 2ce0ac5 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 630 | case tok::objc_implementation: |
| 631 | return parseObjCInterfaceOrImplementation(); |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 632 | case tok::objc_protocol: |
| 633 | return parseObjCProtocol(); |
Nico Weber | d8ffe75 | 2013-01-09 21:42:32 +0000 | [diff] [blame] | 634 | case tok::objc_end: |
| 635 | return; // Handled by the caller. |
Nico Weber | 51306d2 | 2013-01-10 00:25:19 +0000 | [diff] [blame] | 636 | case tok::objc_optional: |
| 637 | case tok::objc_required: |
| 638 | nextToken(); |
| 639 | addUnwrappedLine(); |
| 640 | return; |
Nico Weber | 04e9f1a | 2013-01-07 19:05:19 +0000 | [diff] [blame] | 641 | default: |
| 642 | break; |
| 643 | } |
| 644 | break; |
Alexander Kornienko | 578fdd8 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 645 | case tok::kw_namespace: |
| 646 | parseNamespace(); |
| 647 | return; |
Dmitri Gribenko | 58d64e2 | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 648 | case tok::kw_inline: |
| 649 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 650 | if (FormatTok->Tok.is(tok::kw_namespace)) { |
Dmitri Gribenko | 58d64e2 | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 651 | parseNamespace(); |
| 652 | return; |
| 653 | } |
| 654 | break; |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 655 | case tok::kw_public: |
| 656 | case tok::kw_protected: |
| 657 | case tok::kw_private: |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 658 | parseAccessSpecifier(); |
| 659 | return; |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 660 | case tok::kw_if: |
| 661 | parseIfThenElse(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 662 | return; |
Alexander Kornienko | 37d6c94 | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 663 | case tok::kw_for: |
| 664 | case tok::kw_while: |
| 665 | parseForOrWhileLoop(); |
| 666 | return; |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 667 | case tok::kw_do: |
| 668 | parseDoWhile(); |
| 669 | return; |
| 670 | case tok::kw_switch: |
| 671 | parseSwitch(); |
| 672 | return; |
| 673 | case tok::kw_default: |
| 674 | nextToken(); |
| 675 | parseLabel(); |
| 676 | return; |
| 677 | case tok::kw_case: |
| 678 | parseCaseLabel(); |
| 679 | return; |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 680 | case tok::kw_try: |
| 681 | parseTryCatch(); |
| 682 | return; |
Manuel Klimek | ae610d1 | 2013-01-21 14:32:05 +0000 | [diff] [blame] | 683 | case tok::kw_extern: |
| 684 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 685 | if (FormatTok->Tok.is(tok::string_literal)) { |
Manuel Klimek | ae610d1 | 2013-01-21 14:32:05 +0000 | [diff] [blame] | 686 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 687 | if (FormatTok->Tok.is(tok::l_brace)) { |
Daniel Jasper | 65ee347 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 688 | parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false); |
Manuel Klimek | ae610d1 | 2013-01-21 14:32:05 +0000 | [diff] [blame] | 689 | addUnwrappedLine(); |
| 690 | return; |
| 691 | } |
| 692 | } |
Daniel Jasper | e1e4319 | 2014-04-01 12:55:11 +0000 | [diff] [blame] | 693 | break; |
| 694 | case tok::identifier: |
| 695 | if (FormatTok->IsForEachMacro) { |
| 696 | parseForOrWhileLoop(); |
| 697 | return; |
| 698 | } |
Manuel Klimek | ae610d1 | 2013-01-21 14:32:05 +0000 | [diff] [blame] | 699 | // In all other cases, parse the declaration. |
| 700 | break; |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 701 | default: |
| 702 | break; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 703 | } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 704 | do { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 705 | switch (FormatTok->Tok.getKind()) { |
Nico Weber | 372d8dc | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 706 | case tok::at: |
| 707 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 708 | if (FormatTok->Tok.is(tok::l_brace)) |
Nico Weber | 372d8dc | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 709 | parseBracedList(); |
| 710 | break; |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 711 | case tok::kw_enum: |
| 712 | parseEnum(); |
Manuel Klimek | 2cec019 | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 713 | break; |
Daniel Jasper | a88f80a | 2014-01-30 14:38:37 +0000 | [diff] [blame] | 714 | case tok::kw_typedef: |
| 715 | nextToken(); |
| 716 | // FIXME: Use the IdentifierTable instead. |
| 717 | if (FormatTok->TokenText == "NS_ENUM") |
| 718 | parseEnum(); |
| 719 | break; |
Alexander Kornienko | 1231e06 | 2013-01-16 11:43:46 +0000 | [diff] [blame] | 720 | case tok::kw_struct: |
| 721 | case tok::kw_union: |
Manuel Klimek | 28cacc7 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 722 | case tok::kw_class: |
Manuel Klimek | e01bab5 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 723 | parseRecord(); |
| 724 | // A record declaration or definition is always the start of a structural |
| 725 | // element. |
| 726 | break; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 727 | case tok::semi: |
| 728 | nextToken(); |
| 729 | addUnwrappedLine(); |
| 730 | return; |
Alexander Kornienko | 1231e06 | 2013-01-16 11:43:46 +0000 | [diff] [blame] | 731 | case tok::r_brace: |
| 732 | addUnwrappedLine(); |
| 733 | return; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 734 | case tok::l_paren: |
| 735 | parseParens(); |
| 736 | break; |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 737 | case tok::caret: |
| 738 | nextToken(); |
Daniel Jasper | 395193c | 2014-03-28 07:48:59 +0000 | [diff] [blame] | 739 | if (FormatTok->Tok.isAnyIdentifier() || |
| 740 | FormatTok->isSimpleTypeSpecifier()) |
| 741 | nextToken(); |
| 742 | if (FormatTok->is(tok::l_paren)) |
| 743 | parseParens(); |
| 744 | if (FormatTok->is(tok::l_brace)) |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 745 | parseChildBlock(); |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 746 | break; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 747 | case tok::l_brace: |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 748 | if (!tryToParseBracedList()) { |
| 749 | // A block outside of parentheses must be the last part of a |
| 750 | // structural element. |
| 751 | // FIXME: Figure out cases where this is not true, and add projections |
| 752 | // for them (the one we know is missing are lambdas). |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 753 | if (Style.BreakBeforeBraces != FormatStyle::BS_Attach) |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 754 | addUnwrappedLine(); |
Alexander Kornienko | 3cfa973 | 2013-11-20 16:33:05 +0000 | [diff] [blame] | 755 | FormatTok->Type = TT_FunctionLBrace; |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 756 | parseBlock(/*MustBeDeclaration=*/false); |
Manuel Klimek | a8eb914 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 757 | addUnwrappedLine(); |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 758 | return; |
| 759 | } |
| 760 | // Otherwise this was a braced init list, and the structural |
| 761 | // element continues. |
| 762 | break; |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 763 | case tok::kw_try: |
| 764 | // We arrive here when parsing function-try blocks. |
| 765 | parseTryCatch(); |
| 766 | return; |
Daniel Jasper | 40e1921 | 2013-05-29 13:16:10 +0000 | [diff] [blame] | 767 | case tok::identifier: { |
| 768 | StringRef Text = FormatTok->TokenText; |
Daniel Jasper | 069e5f4 | 2014-05-20 11:14:57 +0000 | [diff] [blame] | 769 | if (Style.Language == FormatStyle::LK_JavaScript && Text == "function") { |
| 770 | tryToParseJSFunction(); |
| 771 | break; |
| 772 | } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 773 | nextToken(); |
Alexander Kornienko | de64427 | 2013-04-08 22:16:06 +0000 | [diff] [blame] | 774 | if (Line->Tokens.size() == 1) { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 775 | if (FormatTok->Tok.is(tok::colon)) { |
Alexander Kornienko | de64427 | 2013-04-08 22:16:06 +0000 | [diff] [blame] | 776 | parseLabel(); |
| 777 | return; |
| 778 | } |
Alexander Kornienko | a04e5e2 | 2013-04-09 16:15:19 +0000 | [diff] [blame] | 779 | // Recognize function-like macro usages without trailing semicolon. |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 780 | if (FormatTok->Tok.is(tok::l_paren)) { |
Alexander Kornienko | de64427 | 2013-04-08 22:16:06 +0000 | [diff] [blame] | 781 | parseParens(); |
Daniel Jasper | 352dae1 | 2014-01-03 11:50:46 +0000 | [diff] [blame] | 782 | if (FormatTok->NewlinesBefore > 0 && |
Alexander Kornienko | ce08126 | 2014-03-18 14:35:20 +0000 | [diff] [blame] | 783 | tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) { |
Alexander Kornienko | de64427 | 2013-04-08 22:16:06 +0000 | [diff] [blame] | 784 | addUnwrappedLine(); |
| 785 | return; |
| 786 | } |
Daniel Jasper | 40e1921 | 2013-05-29 13:16:10 +0000 | [diff] [blame] | 787 | } else if (FormatTok->HasUnescapedNewline && Text.size() >= 5 && |
| 788 | Text == Text.upper()) { |
| 789 | // Recognize free-standing macros like Q_OBJECT. |
| 790 | addUnwrappedLine(); |
Daniel Jasper | 41a0f78 | 2013-05-29 14:09:17 +0000 | [diff] [blame] | 791 | return; |
Alexander Kornienko | de64427 | 2013-04-08 22:16:06 +0000 | [diff] [blame] | 792 | } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 793 | } |
| 794 | break; |
Daniel Jasper | 40e1921 | 2013-05-29 13:16:10 +0000 | [diff] [blame] | 795 | } |
Daniel Jasper | e25509f | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 796 | case tok::equal: |
| 797 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 798 | if (FormatTok->Tok.is(tok::l_brace)) { |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 799 | parseBracedList(); |
| 800 | } |
Daniel Jasper | e25509f | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 801 | break; |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 802 | case tok::l_square: |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 803 | parseSquare(); |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 804 | break; |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 805 | default: |
| 806 | nextToken(); |
| 807 | break; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 808 | } |
| 809 | } while (!eof()); |
| 810 | } |
| 811 | |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 812 | bool UnwrappedLineParser::tryToParseLambda() { |
Daniel Jasper | bf02b2c1 | 2013-09-05 11:49:39 +0000 | [diff] [blame] | 813 | // FIXME: This is a dirty way to access the previous token. Find a better |
| 814 | // solution. |
Daniel Jasper | b4b9998 | 2013-09-06 21:25:51 +0000 | [diff] [blame] | 815 | if (!Line->Tokens.empty() && |
Daniel Jasper | a58dd5d | 2014-03-11 10:03:33 +0000 | [diff] [blame] | 816 | (Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator) || |
| 817 | Line->Tokens.back().Tok->closesScope() || |
Daniel Jasper | 6b70ec0 | 2014-01-22 17:01:47 +0000 | [diff] [blame] | 818 | Line->Tokens.back().Tok->isSimpleTypeSpecifier())) { |
Daniel Jasper | bf02b2c1 | 2013-09-05 11:49:39 +0000 | [diff] [blame] | 819 | nextToken(); |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 820 | return false; |
Daniel Jasper | bf02b2c1 | 2013-09-05 11:49:39 +0000 | [diff] [blame] | 821 | } |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 822 | assert(FormatTok->is(tok::l_square)); |
| 823 | FormatToken &LSquare = *FormatTok; |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 824 | if (!tryToParseLambdaIntroducer()) |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 825 | return false; |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 826 | |
Alexander Kornienko | c2ee9cf | 2014-03-13 13:59:48 +0000 | [diff] [blame] | 827 | while (FormatTok->isNot(tok::l_brace)) { |
Daniel Jasper | cb51cf4 | 2014-01-16 09:11:55 +0000 | [diff] [blame] | 828 | if (FormatTok->isSimpleTypeSpecifier()) { |
| 829 | nextToken(); |
| 830 | continue; |
| 831 | } |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 832 | switch (FormatTok->Tok.getKind()) { |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 833 | case tok::l_brace: |
| 834 | break; |
| 835 | case tok::l_paren: |
| 836 | parseParens(); |
| 837 | break; |
Daniel Jasper | cb51cf4 | 2014-01-16 09:11:55 +0000 | [diff] [blame] | 838 | case tok::less: |
| 839 | case tok::greater: |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 840 | case tok::identifier: |
Daniel Jasper | 1067ab0 | 2014-02-11 10:16:55 +0000 | [diff] [blame] | 841 | case tok::coloncolon: |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 842 | case tok::kw_mutable: |
Daniel Jasper | 81a2078 | 2014-03-10 10:02:02 +0000 | [diff] [blame] | 843 | nextToken(); |
| 844 | break; |
Daniel Jasper | cb51cf4 | 2014-01-16 09:11:55 +0000 | [diff] [blame] | 845 | case tok::arrow: |
Daniel Jasper | 81a2078 | 2014-03-10 10:02:02 +0000 | [diff] [blame] | 846 | FormatTok->Type = TT_TrailingReturnArrow; |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 847 | nextToken(); |
| 848 | break; |
| 849 | default: |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 850 | return true; |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 851 | } |
| 852 | } |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 853 | LSquare.Type = TT_LambdaLSquare; |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 854 | parseChildBlock(); |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 855 | return true; |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | bool UnwrappedLineParser::tryToParseLambdaIntroducer() { |
| 859 | nextToken(); |
| 860 | if (FormatTok->is(tok::equal)) { |
| 861 | nextToken(); |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 862 | if (FormatTok->is(tok::r_square)) { |
| 863 | nextToken(); |
| 864 | return true; |
| 865 | } |
| 866 | if (FormatTok->isNot(tok::comma)) |
| 867 | return false; |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 868 | nextToken(); |
| 869 | } else if (FormatTok->is(tok::amp)) { |
| 870 | nextToken(); |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 871 | if (FormatTok->is(tok::r_square)) { |
| 872 | nextToken(); |
| 873 | return true; |
| 874 | } |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 875 | if (!FormatTok->isOneOf(tok::comma, tok::identifier)) { |
| 876 | return false; |
| 877 | } |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 878 | if (FormatTok->is(tok::comma)) |
| 879 | nextToken(); |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 880 | } else if (FormatTok->is(tok::r_square)) { |
| 881 | nextToken(); |
| 882 | return true; |
| 883 | } |
| 884 | do { |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 885 | if (FormatTok->is(tok::amp)) |
| 886 | nextToken(); |
| 887 | if (!FormatTok->isOneOf(tok::identifier, tok::kw_this)) |
| 888 | return false; |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 889 | nextToken(); |
| 890 | if (FormatTok->is(tok::comma)) { |
| 891 | nextToken(); |
| 892 | } else if (FormatTok->is(tok::r_square)) { |
| 893 | nextToken(); |
| 894 | return true; |
| 895 | } else { |
| 896 | return false; |
| 897 | } |
| 898 | } while (!eof()); |
| 899 | return false; |
| 900 | } |
| 901 | |
Daniel Jasper | c03e16a | 2014-05-08 09:25:39 +0000 | [diff] [blame] | 902 | void UnwrappedLineParser::tryToParseJSFunction() { |
| 903 | nextToken(); |
| 904 | if (FormatTok->isNot(tok::l_paren)) |
| 905 | return; |
| 906 | nextToken(); |
| 907 | while (FormatTok->isNot(tok::l_brace)) { |
| 908 | // Err on the side of caution in order to avoid consuming the full file in |
| 909 | // case of incomplete code. |
| 910 | if (!FormatTok->isOneOf(tok::identifier, tok::comma, tok::r_paren, |
| 911 | tok::comment)) |
| 912 | return; |
| 913 | nextToken(); |
| 914 | } |
| 915 | parseChildBlock(); |
| 916 | } |
| 917 | |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 918 | bool UnwrappedLineParser::tryToParseBracedList() { |
Daniel Jasper | b1f74a8 | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 919 | if (FormatTok->BlockKind == BK_Unknown) |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 920 | calculateBraceTypes(); |
Daniel Jasper | b1f74a8 | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 921 | assert(FormatTok->BlockKind != BK_Unknown); |
| 922 | if (FormatTok->BlockKind == BK_Block) |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 923 | return false; |
| 924 | parseBracedList(); |
| 925 | return true; |
| 926 | } |
| 927 | |
Daniel Jasper | 015ed02 | 2013-09-13 09:20:45 +0000 | [diff] [blame] | 928 | bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) { |
| 929 | bool HasError = false; |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 930 | nextToken(); |
| 931 | |
Manuel Klimek | a3ff45e | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 932 | // FIXME: Once we have an expression parser in the UnwrappedLineParser, |
| 933 | // replace this by using parseAssigmentExpression() inside. |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 934 | do { |
Daniel Jasper | c03e16a | 2014-05-08 09:25:39 +0000 | [diff] [blame] | 935 | if (Style.Language == FormatStyle::LK_JavaScript && |
| 936 | FormatTok->TokenText == "function") { |
| 937 | tryToParseJSFunction(); |
| 938 | continue; |
| 939 | } |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 940 | switch (FormatTok->Tok.getKind()) { |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 941 | case tok::caret: |
| 942 | nextToken(); |
| 943 | if (FormatTok->is(tok::l_brace)) { |
| 944 | parseChildBlock(); |
| 945 | } |
| 946 | break; |
| 947 | case tok::l_square: |
| 948 | tryToParseLambda(); |
| 949 | break; |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 950 | case tok::l_brace: |
Manuel Klimek | bab25fd | 2013-09-04 08:20:47 +0000 | [diff] [blame] | 951 | // Assume there are no blocks inside a braced init list apart |
| 952 | // from the ones we explicitly parse out (like lambdas). |
| 953 | FormatTok->BlockKind = BK_BracedInit; |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 954 | parseBracedList(); |
| 955 | break; |
| 956 | case tok::r_brace: |
| 957 | nextToken(); |
Daniel Jasper | 015ed02 | 2013-09-13 09:20:45 +0000 | [diff] [blame] | 958 | return !HasError; |
Manuel Klimek | a3ff45e | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 959 | case tok::semi: |
Daniel Jasper | 015ed02 | 2013-09-13 09:20:45 +0000 | [diff] [blame] | 960 | HasError = true; |
| 961 | if (!ContinueOnSemicolons) |
| 962 | return !HasError; |
| 963 | nextToken(); |
| 964 | break; |
Manuel Klimek | a3ff45e | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 965 | case tok::comma: |
| 966 | nextToken(); |
Manuel Klimek | a3ff45e | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 967 | break; |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 968 | default: |
| 969 | nextToken(); |
| 970 | break; |
| 971 | } |
| 972 | } while (!eof()); |
Daniel Jasper | 015ed02 | 2013-09-13 09:20:45 +0000 | [diff] [blame] | 973 | return false; |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 974 | } |
| 975 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 976 | void UnwrappedLineParser::parseParens() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 977 | assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected."); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 978 | nextToken(); |
| 979 | do { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 980 | switch (FormatTok->Tok.getKind()) { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 981 | case tok::l_paren: |
| 982 | parseParens(); |
| 983 | break; |
| 984 | case tok::r_paren: |
| 985 | nextToken(); |
| 986 | return; |
Daniel Jasper | 393564f | 2013-05-31 14:56:29 +0000 | [diff] [blame] | 987 | case tok::r_brace: |
| 988 | // A "}" inside parenthesis is an error if there wasn't a matching "{". |
| 989 | return; |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 990 | case tok::l_square: |
| 991 | tryToParseLambda(); |
| 992 | break; |
Nico Weber | 67ffb33 | 2013-02-10 04:38:23 +0000 | [diff] [blame] | 993 | case tok::l_brace: { |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 994 | if (!tryToParseBracedList()) { |
Manuel Klimek | f017dc0 | 2013-09-04 13:34:14 +0000 | [diff] [blame] | 995 | parseChildBlock(); |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 996 | } |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 997 | break; |
Nico Weber | 67ffb33 | 2013-02-10 04:38:23 +0000 | [diff] [blame] | 998 | } |
Nico Weber | 372d8dc | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 999 | case tok::at: |
| 1000 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1001 | if (FormatTok->Tok.is(tok::l_brace)) |
Nico Weber | 372d8dc | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 1002 | parseBracedList(); |
| 1003 | break; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1004 | default: |
| 1005 | nextToken(); |
| 1006 | break; |
| 1007 | } |
| 1008 | } while (!eof()); |
| 1009 | } |
| 1010 | |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 1011 | void UnwrappedLineParser::parseSquare() { |
| 1012 | assert(FormatTok->Tok.is(tok::l_square) && "'[' expected."); |
| 1013 | if (tryToParseLambda()) |
| 1014 | return; |
| 1015 | do { |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 1016 | switch (FormatTok->Tok.getKind()) { |
| 1017 | case tok::l_paren: |
| 1018 | parseParens(); |
| 1019 | break; |
| 1020 | case tok::r_square: |
| 1021 | nextToken(); |
| 1022 | return; |
| 1023 | case tok::r_brace: |
| 1024 | // A "}" inside parenthesis is an error if there wasn't a matching "{". |
| 1025 | return; |
| 1026 | case tok::l_square: |
| 1027 | parseSquare(); |
| 1028 | break; |
| 1029 | case tok::l_brace: { |
| 1030 | if (!tryToParseBracedList()) { |
| 1031 | parseChildBlock(); |
| 1032 | } |
| 1033 | break; |
| 1034 | } |
| 1035 | case tok::at: |
| 1036 | nextToken(); |
| 1037 | if (FormatTok->Tok.is(tok::l_brace)) |
| 1038 | parseBracedList(); |
| 1039 | break; |
| 1040 | default: |
| 1041 | nextToken(); |
| 1042 | break; |
| 1043 | } |
| 1044 | } while (!eof()); |
| 1045 | } |
| 1046 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1047 | void UnwrappedLineParser::parseIfThenElse() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1048 | assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected"); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1049 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1050 | if (FormatTok->Tok.is(tok::l_paren)) |
Manuel Klimek | adededf | 2013-01-11 18:28:36 +0000 | [diff] [blame] | 1051 | parseParens(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1052 | bool NeedsUnwrappedLine = false; |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1053 | if (FormatTok->Tok.is(tok::l_brace)) { |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1054 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 1055 | parseBlock(/*MustBeDeclaration=*/false); |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1056 | if (Style.BreakBeforeBraces == FormatStyle::BS_Allman || |
| 1057 | Style.BreakBeforeBraces == FormatStyle::BS_GNU) { |
Manuel Klimek | d3ed59a | 2013-08-02 21:31:59 +0000 | [diff] [blame] | 1058 | addUnwrappedLine(); |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1059 | } else { |
Manuel Klimek | d3ed59a | 2013-08-02 21:31:59 +0000 | [diff] [blame] | 1060 | NeedsUnwrappedLine = true; |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1061 | } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1062 | } else { |
| 1063 | addUnwrappedLine(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1064 | ++Line->Level; |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 1065 | parseStructuralElement(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1066 | --Line->Level; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1067 | } |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1068 | if (FormatTok->Tok.is(tok::kw_else)) { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1069 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1070 | if (FormatTok->Tok.is(tok::l_brace)) { |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1071 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 1072 | parseBlock(/*MustBeDeclaration=*/false); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1073 | addUnwrappedLine(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1074 | } else if (FormatTok->Tok.is(tok::kw_if)) { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1075 | parseIfThenElse(); |
| 1076 | } else { |
| 1077 | addUnwrappedLine(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1078 | ++Line->Level; |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 1079 | parseStructuralElement(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1080 | --Line->Level; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1081 | } |
| 1082 | } else if (NeedsUnwrappedLine) { |
| 1083 | addUnwrappedLine(); |
| 1084 | } |
| 1085 | } |
| 1086 | |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 1087 | void UnwrappedLineParser::parseTryCatch() { |
| 1088 | assert(FormatTok->is(tok::kw_try) && "'try' expected"); |
| 1089 | nextToken(); |
| 1090 | bool NeedsUnwrappedLine = false; |
| 1091 | if (FormatTok->is(tok::colon)) { |
| 1092 | // We are in a function try block, what comes is an initializer list. |
| 1093 | nextToken(); |
| 1094 | while (FormatTok->is(tok::identifier)) { |
| 1095 | nextToken(); |
| 1096 | if (FormatTok->is(tok::l_paren)) |
| 1097 | parseParens(); |
| 1098 | else |
| 1099 | StructuralError = true; |
| 1100 | if (FormatTok->is(tok::comma)) |
| 1101 | nextToken(); |
| 1102 | } |
| 1103 | } |
| 1104 | if (FormatTok->is(tok::l_brace)) { |
| 1105 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
| 1106 | parseBlock(/*MustBeDeclaration=*/false); |
| 1107 | if (Style.BreakBeforeBraces == FormatStyle::BS_Allman || |
| 1108 | Style.BreakBeforeBraces == FormatStyle::BS_GNU || |
| 1109 | Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) { |
| 1110 | addUnwrappedLine(); |
| 1111 | } else { |
| 1112 | NeedsUnwrappedLine = true; |
| 1113 | } |
| 1114 | } else if (!FormatTok->is(tok::kw_catch)) { |
| 1115 | // The C++ standard requires a compound-statement after a try. |
| 1116 | // If there's none, we try to assume there's a structuralElement |
| 1117 | // and try to continue. |
| 1118 | StructuralError = true; |
| 1119 | addUnwrappedLine(); |
| 1120 | ++Line->Level; |
| 1121 | parseStructuralElement(); |
| 1122 | --Line->Level; |
| 1123 | } |
| 1124 | while (FormatTok->is(tok::kw_catch) || |
| 1125 | (Style.Language == FormatStyle::LK_JavaScript && |
| 1126 | FormatTok->TokenText == "finally")) { |
| 1127 | nextToken(); |
| 1128 | while (FormatTok->isNot(tok::l_brace)) { |
| 1129 | if (FormatTok->is(tok::l_paren)) { |
| 1130 | parseParens(); |
| 1131 | continue; |
| 1132 | } |
| 1133 | if (FormatTok->isOneOf(tok::semi, tok::r_brace)) |
| 1134 | return; |
| 1135 | nextToken(); |
| 1136 | } |
| 1137 | NeedsUnwrappedLine = false; |
| 1138 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
| 1139 | parseBlock(/*MustBeDeclaration=*/false); |
| 1140 | if (Style.BreakBeforeBraces == FormatStyle::BS_Allman || |
| 1141 | Style.BreakBeforeBraces == FormatStyle::BS_GNU || |
| 1142 | Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) { |
| 1143 | addUnwrappedLine(); |
| 1144 | } else { |
| 1145 | NeedsUnwrappedLine = true; |
| 1146 | } |
| 1147 | } |
| 1148 | if (NeedsUnwrappedLine) { |
| 1149 | addUnwrappedLine(); |
| 1150 | } |
| 1151 | } |
| 1152 | |
Alexander Kornienko | 578fdd8 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1153 | void UnwrappedLineParser::parseNamespace() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1154 | assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected"); |
Alexander Kornienko | 578fdd8 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1155 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1156 | if (FormatTok->Tok.is(tok::identifier)) |
Alexander Kornienko | 578fdd8 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1157 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1158 | if (FormatTok->Tok.is(tok::l_brace)) { |
Manuel Klimek | d3ed59a | 2013-08-02 21:31:59 +0000 | [diff] [blame] | 1159 | if (Style.BreakBeforeBraces == FormatStyle::BS_Linux || |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1160 | Style.BreakBeforeBraces == FormatStyle::BS_Allman || |
| 1161 | Style.BreakBeforeBraces == FormatStyle::BS_GNU) |
Manuel Klimek | a8eb914 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 1162 | addUnwrappedLine(); |
| 1163 | |
Daniel Jasper | 65ee347 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 1164 | bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All || |
| 1165 | (Style.NamespaceIndentation == FormatStyle::NI_Inner && |
| 1166 | DeclarationScopeStack.size() > 1); |
| 1167 | parseBlock(/*MustBeDeclaration=*/true, AddLevel); |
Manuel Klimek | 046b930 | 2013-02-06 16:08:09 +0000 | [diff] [blame] | 1168 | // Munch the semicolon after a namespace. This is more common than one would |
| 1169 | // think. Puttin the semicolon into its own line is very ugly. |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1170 | if (FormatTok->Tok.is(tok::semi)) |
Manuel Klimek | 046b930 | 2013-02-06 16:08:09 +0000 | [diff] [blame] | 1171 | nextToken(); |
Alexander Kornienko | 578fdd8 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1172 | addUnwrappedLine(); |
| 1173 | } |
| 1174 | // FIXME: Add error handling. |
| 1175 | } |
| 1176 | |
Alexander Kornienko | 37d6c94 | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 1177 | void UnwrappedLineParser::parseForOrWhileLoop() { |
Daniel Jasper | e1e4319 | 2014-04-01 12:55:11 +0000 | [diff] [blame] | 1178 | assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while) || |
| 1179 | FormatTok->IsForEachMacro) && |
| 1180 | "'for', 'while' or foreach macro expected"); |
Alexander Kornienko | 37d6c94 | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 1181 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1182 | if (FormatTok->Tok.is(tok::l_paren)) |
Manuel Klimek | 9fa8d55 | 2013-01-11 19:23:05 +0000 | [diff] [blame] | 1183 | parseParens(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1184 | if (FormatTok->Tok.is(tok::l_brace)) { |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1185 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 1186 | parseBlock(/*MustBeDeclaration=*/false); |
Alexander Kornienko | 37d6c94 | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 1187 | addUnwrappedLine(); |
| 1188 | } else { |
| 1189 | addUnwrappedLine(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1190 | ++Line->Level; |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 1191 | parseStructuralElement(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1192 | --Line->Level; |
Alexander Kornienko | 37d6c94 | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 1193 | } |
| 1194 | } |
| 1195 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1196 | void UnwrappedLineParser::parseDoWhile() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1197 | assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected"); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1198 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1199 | if (FormatTok->Tok.is(tok::l_brace)) { |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1200 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 1201 | parseBlock(/*MustBeDeclaration=*/false); |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1202 | if (Style.BreakBeforeBraces == FormatStyle::BS_GNU) |
| 1203 | addUnwrappedLine(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1204 | } else { |
| 1205 | addUnwrappedLine(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1206 | ++Line->Level; |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 1207 | parseStructuralElement(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1208 | --Line->Level; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
Alexander Kornienko | 0ea8e10 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 1211 | // FIXME: Add error handling. |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1212 | if (!FormatTok->Tok.is(tok::kw_while)) { |
Alexander Kornienko | 0ea8e10 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 1213 | addUnwrappedLine(); |
| 1214 | return; |
| 1215 | } |
| 1216 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1217 | nextToken(); |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 1218 | parseStructuralElement(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | void UnwrappedLineParser::parseLabel() { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1222 | nextToken(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1223 | unsigned OldLineLevel = Line->Level; |
Daniel Jasper | a127512 | 2013-03-20 10:23:53 +0000 | [diff] [blame] | 1224 | if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0)) |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1225 | --Line->Level; |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1226 | if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) { |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1227 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 1228 | parseBlock(/*MustBeDeclaration=*/false); |
Manuel Klimek | d3ed59a | 2013-08-02 21:31:59 +0000 | [diff] [blame] | 1229 | if (FormatTok->Tok.is(tok::kw_break)) { |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1230 | // "break;" after "}" on its own line only for BS_Allman and BS_GNU |
| 1231 | if (Style.BreakBeforeBraces == FormatStyle::BS_Allman || |
| 1232 | Style.BreakBeforeBraces == FormatStyle::BS_GNU) { |
Manuel Klimek | d3ed59a | 2013-08-02 21:31:59 +0000 | [diff] [blame] | 1233 | addUnwrappedLine(); |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1234 | } |
Manuel Klimek | d3ed59a | 2013-08-02 21:31:59 +0000 | [diff] [blame] | 1235 | parseStructuralElement(); |
| 1236 | } |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1237 | addUnwrappedLine(); |
| 1238 | } else { |
| 1239 | addUnwrappedLine(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1240 | } |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1241 | Line->Level = OldLineLevel; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
| 1244 | void UnwrappedLineParser::parseCaseLabel() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1245 | assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected"); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1246 | // FIXME: fix handling of complex expressions here. |
| 1247 | do { |
| 1248 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1249 | } while (!eof() && !FormatTok->Tok.is(tok::colon)); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1250 | parseLabel(); |
| 1251 | } |
| 1252 | |
| 1253 | void UnwrappedLineParser::parseSwitch() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1254 | assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected"); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1255 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1256 | if (FormatTok->Tok.is(tok::l_paren)) |
Manuel Klimek | 9fa8d55 | 2013-01-11 19:23:05 +0000 | [diff] [blame] | 1257 | parseParens(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1258 | if (FormatTok->Tok.is(tok::l_brace)) { |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1259 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
Daniel Jasper | 65ee347 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 1260 | parseBlock(/*MustBeDeclaration=*/false); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1261 | addUnwrappedLine(); |
| 1262 | } else { |
| 1263 | addUnwrappedLine(); |
Daniel Jasper | 516d797 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 1264 | ++Line->Level; |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 1265 | parseStructuralElement(); |
Daniel Jasper | 516d797 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 1266 | --Line->Level; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1267 | } |
| 1268 | } |
| 1269 | |
| 1270 | void UnwrappedLineParser::parseAccessSpecifier() { |
| 1271 | nextToken(); |
Daniel Jasper | 84c47a1 | 2013-11-23 17:53:41 +0000 | [diff] [blame] | 1272 | // Understand Qt's slots. |
Daniel Jasper | 1556b59 | 2013-11-29 08:51:56 +0000 | [diff] [blame] | 1273 | if (FormatTok->is(tok::identifier) && |
| 1274 | (FormatTok->TokenText == "slots" || FormatTok->TokenText == "Q_SLOTS")) |
Daniel Jasper | 84c47a1 | 2013-11-23 17:53:41 +0000 | [diff] [blame] | 1275 | nextToken(); |
Alexander Kornienko | 2ca766f | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1276 | // Otherwise, we don't know what it is, and we'd better keep the next token. |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1277 | if (FormatTok->Tok.is(tok::colon)) |
Alexander Kornienko | 2ca766f | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1278 | nextToken(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1279 | addUnwrappedLine(); |
| 1280 | } |
| 1281 | |
| 1282 | void UnwrappedLineParser::parseEnum() { |
Daniel Jasper | a88f80a | 2014-01-30 14:38:37 +0000 | [diff] [blame] | 1283 | if (FormatTok->Tok.is(tok::kw_enum)) { |
| 1284 | // Won't be 'enum' for NS_ENUMs. |
| 1285 | nextToken(); |
| 1286 | } |
Daniel Jasper | 2b41a82 | 2013-08-20 12:42:50 +0000 | [diff] [blame] | 1287 | // Eat up enum class ... |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 1288 | if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct)) |
| 1289 | nextToken(); |
Daniel Jasper | 786a550 | 2013-09-06 21:32:35 +0000 | [diff] [blame] | 1290 | while (FormatTok->Tok.getIdentifierInfo() || |
| 1291 | FormatTok->isOneOf(tok::colon, tok::coloncolon)) { |
Manuel Klimek | 2cec019 | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 1292 | nextToken(); |
| 1293 | // We can have macros or attributes in between 'enum' and the enum name. |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1294 | if (FormatTok->Tok.is(tok::l_paren)) { |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 1295 | parseParens(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1296 | } |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1297 | if (FormatTok->Tok.is(tok::identifier)) |
Manuel Klimek | 2cec019 | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 1298 | nextToken(); |
| 1299 | } |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1300 | if (FormatTok->Tok.is(tok::l_brace)) { |
Daniel Jasper | 015ed02 | 2013-09-13 09:20:45 +0000 | [diff] [blame] | 1301 | FormatTok->BlockKind = BK_Block; |
| 1302 | bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true); |
| 1303 | if (HasError) { |
| 1304 | if (FormatTok->is(tok::semi)) |
| 1305 | nextToken(); |
Manuel Klimek | a027f30 | 2013-08-07 19:20:45 +0000 | [diff] [blame] | 1306 | addUnwrappedLine(); |
Daniel Jasper | 015ed02 | 2013-09-13 09:20:45 +0000 | [diff] [blame] | 1307 | } |
Manuel Klimek | 2cec019 | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 1308 | } |
| 1309 | // We fall through to parsing a structural element afterwards, so that in |
| 1310 | // enum A {} n, m; |
| 1311 | // "} n, m;" will end up in one unwrapped line. |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1312 | } |
| 1313 | |
Manuel Klimek | e01bab5 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 1314 | void UnwrappedLineParser::parseRecord() { |
Manuel Klimek | 28cacc7 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 1315 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1316 | if (FormatTok->Tok.is(tok::identifier) || |
| 1317 | FormatTok->Tok.is(tok::kw___attribute) || |
Manuel Klimek | 91e4858 | 2013-10-07 09:15:41 +0000 | [diff] [blame] | 1318 | FormatTok->Tok.is(tok::kw___declspec) || |
| 1319 | FormatTok->Tok.is(tok::kw_alignas)) { |
Manuel Klimek | e01bab5 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 1320 | nextToken(); |
| 1321 | // We can have macros or attributes in between 'class' and the class name. |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1322 | if (FormatTok->Tok.is(tok::l_paren)) { |
Manuel Klimek | e01bab5 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 1323 | parseParens(); |
Manuel Klimek | 28cacc7 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 1324 | } |
Manuel Klimek | d265090 | 2013-02-06 15:57:54 +0000 | [diff] [blame] | 1325 | // The actual identifier can be a nested name specifier, and in macros |
| 1326 | // it is often token-pasted. |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1327 | while (FormatTok->Tok.is(tok::identifier) || |
| 1328 | FormatTok->Tok.is(tok::coloncolon) || |
| 1329 | FormatTok->Tok.is(tok::hashhash)) |
Manuel Klimek | e01bab5 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 1330 | nextToken(); |
| 1331 | |
Manuel Klimek | cdee74d | 2013-01-21 13:58:54 +0000 | [diff] [blame] | 1332 | // Note that parsing away template declarations here leads to incorrectly |
| 1333 | // accepting function declarations as record declarations. |
| 1334 | // In general, we cannot solve this problem. Consider: |
| 1335 | // class A<int> B() {} |
| 1336 | // which can be a function definition or a class definition when B() is a |
| 1337 | // macro. If we find enough real-world cases where this is a problem, we |
| 1338 | // can parse for the 'template' keyword in the beginning of the statement, |
| 1339 | // and thus rule out the record production in case there is no template |
| 1340 | // (this would still leave us with an ambiguity between template function |
| 1341 | // and class declarations). |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1342 | if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) { |
| 1343 | while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) { |
| 1344 | if (FormatTok->Tok.is(tok::semi)) |
Manuel Klimek | e01bab5 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 1345 | return; |
| 1346 | nextToken(); |
| 1347 | } |
| 1348 | } |
| 1349 | } |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1350 | if (FormatTok->Tok.is(tok::l_brace)) { |
Manuel Klimek | d3ed59a | 2013-08-02 21:31:59 +0000 | [diff] [blame] | 1351 | if (Style.BreakBeforeBraces == FormatStyle::BS_Linux || |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1352 | Style.BreakBeforeBraces == FormatStyle::BS_Allman || |
| 1353 | Style.BreakBeforeBraces == FormatStyle::BS_GNU) |
Manuel Klimek | a8eb914 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 1354 | addUnwrappedLine(); |
| 1355 | |
Daniel Jasper | 240dfda | 2014-03-31 14:23:49 +0000 | [diff] [blame] | 1356 | parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true, |
Manuel Klimek | b212f3b | 2013-10-12 22:46:56 +0000 | [diff] [blame] | 1357 | /*MunchSemi=*/false); |
Manuel Klimek | a8eb914 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 1358 | } |
Manuel Klimek | cdee74d | 2013-01-21 13:58:54 +0000 | [diff] [blame] | 1359 | // We fall through to parsing a structural element afterwards, so |
| 1360 | // class A {} n, m; |
| 1361 | // will end up in one unwrapped line. |
Manuel Klimek | 28cacc7 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1364 | void UnwrappedLineParser::parseObjCProtocolList() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1365 | assert(FormatTok->Tok.is(tok::less) && "'<' expected."); |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1366 | do |
| 1367 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1368 | while (!eof() && FormatTok->Tok.isNot(tok::greater)); |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1369 | nextToken(); // Skip '>'. |
| 1370 | } |
| 1371 | |
| 1372 | void UnwrappedLineParser::parseObjCUntilAtEnd() { |
| 1373 | do { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1374 | if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) { |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1375 | nextToken(); |
| 1376 | addUnwrappedLine(); |
| 1377 | break; |
| 1378 | } |
Daniel Jasper | a15da30 | 2013-08-28 08:04:23 +0000 | [diff] [blame] | 1379 | if (FormatTok->is(tok::l_brace)) { |
| 1380 | parseBlock(/*MustBeDeclaration=*/false); |
| 1381 | // In ObjC interfaces, nothing should be following the "}". |
| 1382 | addUnwrappedLine(); |
Benjamin Kramer | e21cb74 | 2014-01-08 15:59:42 +0000 | [diff] [blame] | 1383 | } else if (FormatTok->is(tok::r_brace)) { |
| 1384 | // Ignore stray "}". parseStructuralElement doesn't consume them. |
| 1385 | nextToken(); |
| 1386 | addUnwrappedLine(); |
Daniel Jasper | a15da30 | 2013-08-28 08:04:23 +0000 | [diff] [blame] | 1387 | } else { |
| 1388 | parseStructuralElement(); |
| 1389 | } |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1390 | } while (!eof()); |
| 1391 | } |
| 1392 | |
Nico Weber | 2ce0ac5 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 1393 | void UnwrappedLineParser::parseObjCInterfaceOrImplementation() { |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1394 | nextToken(); |
Daniel Jasper | d1ae358 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 1395 | nextToken(); // interface name |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1396 | |
| 1397 | // @interface can be followed by either a base class, or a category. |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1398 | if (FormatTok->Tok.is(tok::colon)) { |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1399 | nextToken(); |
Daniel Jasper | d1ae358 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 1400 | nextToken(); // base class name |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1401 | } else if (FormatTok->Tok.is(tok::l_paren)) |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1402 | // Skip category, if present. |
| 1403 | parseParens(); |
| 1404 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1405 | if (FormatTok->Tok.is(tok::less)) |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1406 | parseObjCProtocolList(); |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1407 | |
Dinesh Dwivedi | ea3aca8 | 2014-05-02 17:01:46 +0000 | [diff] [blame] | 1408 | if (FormatTok->Tok.is(tok::l_brace)) { |
| 1409 | if (Style.BreakBeforeBraces == FormatStyle::BS_Allman || |
| 1410 | Style.BreakBeforeBraces == FormatStyle::BS_GNU) |
| 1411 | addUnwrappedLine(); |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 1412 | parseBlock(/*MustBeDeclaration=*/true); |
Dinesh Dwivedi | ea3aca8 | 2014-05-02 17:01:46 +0000 | [diff] [blame] | 1413 | } |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1414 | |
| 1415 | // With instance variables, this puts '}' on its own line. Without instance |
| 1416 | // variables, this ends the @interface line. |
| 1417 | addUnwrappedLine(); |
| 1418 | |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1419 | parseObjCUntilAtEnd(); |
| 1420 | } |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1421 | |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1422 | void UnwrappedLineParser::parseObjCProtocol() { |
| 1423 | nextToken(); |
Daniel Jasper | d1ae358 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 1424 | nextToken(); // protocol name |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1425 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1426 | if (FormatTok->Tok.is(tok::less)) |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1427 | parseObjCProtocolList(); |
| 1428 | |
| 1429 | // Check for protocol declaration. |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1430 | if (FormatTok->Tok.is(tok::semi)) { |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1431 | nextToken(); |
| 1432 | return addUnwrappedLine(); |
| 1433 | } |
| 1434 | |
| 1435 | addUnwrappedLine(); |
| 1436 | parseObjCUntilAtEnd(); |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
Daniel Jasper | 3b203a6 | 2013-09-05 16:05:56 +0000 | [diff] [blame] | 1439 | LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line, |
| 1440 | StringRef Prefix = "") { |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1441 | llvm::dbgs() << Prefix << "Line(" << Line.Level << ")" |
| 1442 | << (Line.InPPDirective ? " MACRO" : "") << ": "; |
| 1443 | for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(), |
| 1444 | E = Line.Tokens.end(); |
| 1445 | I != E; ++I) { |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 1446 | llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] "; |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1447 | } |
| 1448 | for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(), |
| 1449 | E = Line.Tokens.end(); |
| 1450 | I != E; ++I) { |
| 1451 | const UnwrappedLineNode &Node = *I; |
| 1452 | for (SmallVectorImpl<UnwrappedLine>::const_iterator |
| 1453 | I = Node.Children.begin(), |
| 1454 | E = Node.Children.end(); |
| 1455 | I != E; ++I) { |
| 1456 | printDebugInfo(*I, "\nChild: "); |
| 1457 | } |
| 1458 | } |
| 1459 | llvm::dbgs() << "\n"; |
| 1460 | } |
| 1461 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1462 | void UnwrappedLineParser::addUnwrappedLine() { |
Daniel Jasper | daffc0d | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 1463 | if (Line->Tokens.empty()) |
Daniel Jasper | 7c85fde | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1464 | return; |
Manuel Klimek | ab3dc00 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 1465 | DEBUG({ |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1466 | if (CurrentLines == &Lines) |
| 1467 | printDebugInfo(*Line); |
Manuel Klimek | ab3dc00 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 1468 | }); |
Manuel Klimek | d3b92fa | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 1469 | CurrentLines->push_back(*Line); |
Daniel Jasper | daffc0d | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 1470 | Line->Tokens.clear(); |
Manuel Klimek | d3b92fa | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 1471 | if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) { |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1472 | for (SmallVectorImpl<UnwrappedLine>::iterator |
Daniel Jasper | a79064a | 2013-03-01 18:11:39 +0000 | [diff] [blame] | 1473 | I = PreprocessorDirectives.begin(), |
| 1474 | E = PreprocessorDirectives.end(); |
Manuel Klimek | d3b92fa | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 1475 | I != E; ++I) { |
| 1476 | CurrentLines->push_back(*I); |
| 1477 | } |
| 1478 | PreprocessorDirectives.clear(); |
| 1479 | } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1482 | bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1483 | |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 1484 | bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) { |
Manuel Klimek | 1fcbe67 | 2014-04-11 12:27:47 +0000 | [diff] [blame] | 1485 | return (Line->InPPDirective || FormatTok.HasUnescapedNewline) && |
| 1486 | FormatTok.NewlinesBefore > 0; |
| 1487 | } |
| 1488 | |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1489 | void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) { |
| 1490 | bool JustComments = Line->Tokens.empty(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1491 | for (SmallVectorImpl<FormatToken *>::const_iterator |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1492 | I = CommentsBeforeNextToken.begin(), |
| 1493 | E = CommentsBeforeNextToken.end(); |
| 1494 | I != E; ++I) { |
Manuel Klimek | 1fcbe67 | 2014-04-11 12:27:47 +0000 | [diff] [blame] | 1495 | if (isOnNewLine(**I) && JustComments) { |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1496 | addUnwrappedLine(); |
| 1497 | } |
| 1498 | pushToken(*I); |
| 1499 | } |
| 1500 | if (NewlineBeforeNext && JustComments) { |
| 1501 | addUnwrappedLine(); |
| 1502 | } |
| 1503 | CommentsBeforeNextToken.clear(); |
| 1504 | } |
| 1505 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1506 | void UnwrappedLineParser::nextToken() { |
| 1507 | if (eof()) |
| 1508 | return; |
Manuel Klimek | 1fcbe67 | 2014-04-11 12:27:47 +0000 | [diff] [blame] | 1509 | flushComments(isOnNewLine(*FormatTok)); |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1510 | pushToken(FormatTok); |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1511 | readToken(); |
| 1512 | } |
| 1513 | |
| 1514 | void UnwrappedLineParser::readToken() { |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1515 | bool CommentsInCurrentLine = true; |
| 1516 | do { |
| 1517 | FormatTok = Tokens->getNextToken(); |
Alexander Kornienko | c2ee9cf | 2014-03-13 13:59:48 +0000 | [diff] [blame] | 1518 | assert(FormatTok); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1519 | while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) && |
| 1520 | (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) { |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1521 | // If there is an unfinished unwrapped line, we flush the preprocessor |
| 1522 | // directives only after that unwrapped line was finished later. |
Daniel Jasper | d1ae358 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 1523 | bool SwitchToPreprocessorLines = |
| 1524 | !Line->Tokens.empty() && CurrentLines == &Lines; |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1525 | ScopedLineState BlockState(*this, SwitchToPreprocessorLines); |
Alexander Kornienko | b1be9d6 | 2013-04-03 12:38:53 +0000 | [diff] [blame] | 1526 | // Comments stored before the preprocessor directive need to be output |
| 1527 | // before the preprocessor directive, at the same level as the |
| 1528 | // preprocessor directive, as we consider them to apply to the directive. |
Manuel Klimek | 1fcbe67 | 2014-04-11 12:27:47 +0000 | [diff] [blame] | 1529 | flushComments(isOnNewLine(*FormatTok)); |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1530 | parsePPDirective(); |
| 1531 | } |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 1532 | while (FormatTok->Type == TT_ConflictStart || |
| 1533 | FormatTok->Type == TT_ConflictEnd || |
| 1534 | FormatTok->Type == TT_ConflictAlternative) { |
| 1535 | if (FormatTok->Type == TT_ConflictStart) { |
| 1536 | conditionalCompilationStart(/*Unreachable=*/false); |
| 1537 | } else if (FormatTok->Type == TT_ConflictAlternative) { |
| 1538 | conditionalCompilationAlternative(); |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 1539 | } else if (FormatTok->Type == TT_ConflictEnd) { |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 1540 | conditionalCompilationEnd(); |
| 1541 | } |
| 1542 | FormatTok = Tokens->getNextToken(); |
| 1543 | FormatTok->MustBreakBefore = true; |
| 1544 | } |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 1545 | |
| 1546 | if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) && |
| 1547 | !Line->InPPDirective) { |
| 1548 | continue; |
| 1549 | } |
| 1550 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1551 | if (!FormatTok->Tok.is(tok::comment)) |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1552 | return; |
Manuel Klimek | 1fcbe67 | 2014-04-11 12:27:47 +0000 | [diff] [blame] | 1553 | if (isOnNewLine(*FormatTok) || FormatTok->IsFirst) { |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1554 | CommentsInCurrentLine = false; |
| 1555 | } |
| 1556 | if (CommentsInCurrentLine) { |
| 1557 | pushToken(FormatTok); |
| 1558 | } else { |
| 1559 | CommentsBeforeNextToken.push_back(FormatTok); |
| 1560 | } |
| 1561 | } while (!eof()); |
| 1562 | } |
| 1563 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1564 | void UnwrappedLineParser::pushToken(FormatToken *Tok) { |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1565 | Line->Tokens.push_back(UnwrappedLineNode(Tok)); |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1566 | if (MustBreakBeforeNextToken) { |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1567 | Line->Tokens.back().Tok->MustBreakBefore = true; |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1568 | MustBreakBeforeNextToken = false; |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1569 | } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1570 | } |
| 1571 | |
Daniel Jasper | 8d1832e | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1572 | } // end namespace format |
| 1573 | } // end namespace clang |