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