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