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