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