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