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