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