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, |
| 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: |
Daniel Jasper | f7ec1cc | 2013-05-31 14:56:29 +0000 | [diff] [blame] | 377 | if (!LBraceStack.empty()) |
Daniel Jasper | 0de1c4d | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 378 | LBraceStack.back()->BlockKind = BK_Block; |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 379 | break; |
| 380 | default: |
| 381 | break; |
| 382 | } |
| 383 | Tok = NextTok; |
Manuel Klimek | 31e44f7 | 2013-09-04 08:20:47 +0000 | [diff] [blame] | 384 | } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty()); |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 385 | // Assume other blocks for all unclosed opening braces. |
| 386 | for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) { |
Daniel Jasper | 0de1c4d | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 387 | if (LBraceStack[i]->BlockKind == BK_Unknown) |
| 388 | LBraceStack[i]->BlockKind = BK_Block; |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 389 | } |
Manuel Klimek | 31e44f7 | 2013-09-04 08:20:47 +0000 | [diff] [blame] | 390 | |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 391 | FormatTok = Tokens->setPosition(StoredPosition); |
| 392 | } |
| 393 | |
Manuel Klimek | aabfb27 | 2013-10-12 22:46:56 +0000 | [diff] [blame] | 394 | void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel, |
| 395 | bool MunchSemi) { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 396 | assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected"); |
Daniel Jasper | e865cc5 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 397 | unsigned InitialLevel = Line->Level; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 398 | nextToken(); |
| 399 | |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 400 | addUnwrappedLine(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 401 | |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 402 | ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, |
| 403 | MustBeDeclaration); |
Daniel Jasper | eff18b9 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 404 | if (AddLevel) |
| 405 | ++Line->Level; |
Nico Weber | 2726877 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 406 | parseLevel(/*HasOpeningBrace=*/true); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 407 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 408 | if (!FormatTok->Tok.is(tok::r_brace)) { |
Daniel Jasper | e865cc5 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 409 | Line->Level = InitialLevel; |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 410 | StructuralError = true; |
| 411 | return; |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 412 | } |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 413 | |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 414 | nextToken(); // Munch the closing brace. |
Manuel Klimek | aabfb27 | 2013-10-12 22:46:56 +0000 | [diff] [blame] | 415 | if (MunchSemi && FormatTok->Tok.is(tok::semi)) |
| 416 | nextToken(); |
Daniel Jasper | e865cc5 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 417 | Line->Level = InitialLevel; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 420 | static bool IsGoogScope(const UnwrappedLine &Line) { |
| 421 | if (Line.Tokens.size() < 4) |
| 422 | return false; |
| 423 | auto I = Line.Tokens.begin(); |
| 424 | if (I->Tok->TokenText != "goog") |
| 425 | return false; |
| 426 | ++I; |
| 427 | if (I->Tok->isNot(tok::period)) |
| 428 | return false; |
| 429 | ++I; |
| 430 | if (I->Tok->TokenText != "scope") |
| 431 | return false; |
| 432 | ++I; |
| 433 | return I->Tok->is(tok::l_paren); |
| 434 | } |
| 435 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 436 | static bool ShouldBreakBeforeBrace(const FormatStyle &Style, |
| 437 | const FormatToken &InitialToken) { |
| 438 | switch (Style.BreakBeforeBraces) { |
| 439 | case FormatStyle::BS_Linux: |
| 440 | return InitialToken.isOneOf(tok::kw_namespace, tok::kw_class); |
| 441 | case FormatStyle::BS_Allman: |
| 442 | case FormatStyle::BS_GNU: |
| 443 | return true; |
| 444 | default: |
| 445 | return false; |
| 446 | } |
| 447 | } |
| 448 | |
Manuel Klimek | 753a511 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 449 | void UnwrappedLineParser::parseChildBlock() { |
| 450 | FormatTok->BlockKind = BK_Block; |
| 451 | nextToken(); |
| 452 | { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 453 | bool GoogScope = |
| 454 | Style.Language == FormatStyle::LK_JavaScript && IsGoogScope(*Line); |
Manuel Klimek | 753a511 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 455 | ScopedLineState LineState(*this); |
| 456 | ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, |
| 457 | /*MustBeDeclaration=*/false); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 458 | Line->Level += GoogScope ? 0 : 1; |
Manuel Klimek | 753a511 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 459 | parseLevel(/*HasOpeningBrace=*/true); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 460 | Line->Level -= GoogScope ? 0 : 1; |
Manuel Klimek | 753a511 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 461 | } |
| 462 | nextToken(); |
| 463 | } |
| 464 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 465 | void UnwrappedLineParser::parsePPDirective() { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 466 | assert(FormatTok->Tok.is(tok::hash) && "'#' expected"); |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 467 | ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 468 | nextToken(); |
| 469 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 470 | if (!FormatTok->Tok.getIdentifierInfo()) { |
Manuel Klimek | bd04f2a | 2013-01-31 15:58:48 +0000 | [diff] [blame] | 471 | parsePPUnknown(); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 472 | return; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 473 | } |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 474 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 475 | switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 476 | case tok::pp_define: |
| 477 | parsePPDefine(); |
Alexander Kornienko | 6fb46b0 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 478 | return; |
| 479 | case tok::pp_if: |
Manuel Klimek | ae76f7f | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 480 | parsePPIf(/*IfDef=*/false); |
Alexander Kornienko | 6fb46b0 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 481 | break; |
| 482 | case tok::pp_ifdef: |
| 483 | case tok::pp_ifndef: |
Manuel Klimek | ae76f7f | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 484 | parsePPIf(/*IfDef=*/true); |
Alexander Kornienko | 6fb46b0 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 485 | break; |
| 486 | case tok::pp_else: |
| 487 | parsePPElse(); |
| 488 | break; |
| 489 | case tok::pp_elif: |
| 490 | parsePPElIf(); |
| 491 | break; |
| 492 | case tok::pp_endif: |
| 493 | parsePPEndIf(); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 494 | break; |
| 495 | default: |
| 496 | parsePPUnknown(); |
| 497 | break; |
| 498 | } |
| 499 | } |
| 500 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 501 | void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) { |
| 502 | if (Unreachable || (!PPStack.empty() && PPStack.back() == PP_Unreachable)) |
Alexander Kornienko | 6fb46b0 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 503 | PPStack.push_back(PP_Unreachable); |
| 504 | else |
| 505 | PPStack.push_back(PP_Conditional); |
| 506 | } |
| 507 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 508 | void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) { |
Manuel Klimek | ae76f7f | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 509 | ++PPBranchLevel; |
| 510 | assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size()); |
| 511 | if (PPBranchLevel == (int)PPLevelBranchIndex.size()) { |
| 512 | PPLevelBranchIndex.push_back(0); |
| 513 | PPLevelBranchCount.push_back(0); |
| 514 | } |
| 515 | PPChainBranchIndex.push(0); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 516 | bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0; |
| 517 | conditionalCompilationCondition(Unreachable || Skip); |
Alexander Kornienko | 6fb46b0 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 518 | } |
| 519 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 520 | void UnwrappedLineParser::conditionalCompilationAlternative() { |
Alexander Kornienko | 6fb46b0 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 521 | if (!PPStack.empty()) |
| 522 | PPStack.pop_back(); |
Manuel Klimek | ae76f7f | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 523 | assert(PPBranchLevel < (int)PPLevelBranchIndex.size()); |
| 524 | if (!PPChainBranchIndex.empty()) |
| 525 | ++PPChainBranchIndex.top(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 526 | conditionalCompilationCondition( |
| 527 | PPBranchLevel >= 0 && !PPChainBranchIndex.empty() && |
| 528 | PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top()); |
Alexander Kornienko | 6fb46b0 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 531 | void UnwrappedLineParser::conditionalCompilationEnd() { |
Manuel Klimek | ae76f7f | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 532 | assert(PPBranchLevel < (int)PPLevelBranchIndex.size()); |
| 533 | if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) { |
| 534 | if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) { |
Manuel Klimek | ae76f7f | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 535 | PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1; |
| 536 | } |
| 537 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 538 | // Guard against #endif's without #if. |
| 539 | if (PPBranchLevel > 0) |
| 540 | --PPBranchLevel; |
Manuel Klimek | ae76f7f | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 541 | if (!PPChainBranchIndex.empty()) |
| 542 | PPChainBranchIndex.pop(); |
Alexander Kornienko | 6fb46b0 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 543 | if (!PPStack.empty()) |
| 544 | PPStack.pop_back(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | void UnwrappedLineParser::parsePPIf(bool IfDef) { |
| 548 | nextToken(); |
| 549 | bool IsLiteralFalse = (FormatTok->Tok.isLiteral() && |
| 550 | StringRef(FormatTok->Tok.getLiteralData(), |
| 551 | FormatTok->Tok.getLength()) == "0") || |
| 552 | FormatTok->Tok.is(tok::kw_false); |
| 553 | conditionalCompilationStart(!IfDef && IsLiteralFalse); |
| 554 | parsePPUnknown(); |
| 555 | } |
| 556 | |
| 557 | void UnwrappedLineParser::parsePPElse() { |
| 558 | conditionalCompilationAlternative(); |
| 559 | parsePPUnknown(); |
| 560 | } |
| 561 | |
| 562 | void UnwrappedLineParser::parsePPElIf() { parsePPElse(); } |
| 563 | |
| 564 | void UnwrappedLineParser::parsePPEndIf() { |
| 565 | conditionalCompilationEnd(); |
Alexander Kornienko | 6fb46b0 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 566 | parsePPUnknown(); |
| 567 | } |
| 568 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 569 | void UnwrappedLineParser::parsePPDefine() { |
| 570 | nextToken(); |
| 571 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 572 | if (FormatTok->Tok.getKind() != tok::identifier) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 573 | parsePPUnknown(); |
| 574 | return; |
| 575 | } |
| 576 | nextToken(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 577 | if (FormatTok->Tok.getKind() == tok::l_paren && |
| 578 | FormatTok->WhitespaceRange.getBegin() == |
| 579 | FormatTok->WhitespaceRange.getEnd()) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 580 | parseParens(); |
| 581 | } |
| 582 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 583 | Line->Level = 1; |
Manuel Klimek | c3d0c82 | 2013-01-07 09:34:28 +0000 | [diff] [blame] | 584 | |
| 585 | // Errors during a preprocessor directive can only affect the layout of the |
| 586 | // preprocessor directive, and thus we ignore them. An alternative approach |
| 587 | // would be to use the same approach we use on the file level (no |
| 588 | // re-indentation if there was a structural error) within the macro |
| 589 | // definition. |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 590 | parseFile(); |
| 591 | } |
| 592 | |
| 593 | void UnwrappedLineParser::parsePPUnknown() { |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 594 | do { |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 595 | nextToken(); |
| 596 | } while (!eof()); |
| 597 | addUnwrappedLine(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Alexander Kornienko | 99b0e14 | 2013-04-09 16:15:19 +0000 | [diff] [blame] | 600 | // Here we blacklist certain tokens that are not usually the first token in an |
| 601 | // unwrapped line. This is used in attempt to distinguish macro calls without |
| 602 | // trailing semicolons from other constructs split to several lines. |
| 603 | bool tokenCanStartNewLine(clang::Token Tok) { |
| 604 | // Semicolon can be a null-statement, l_square can be a start of a macro or |
| 605 | // a C++11 attribute, but this doesn't seem to be common. |
| 606 | return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) && |
| 607 | Tok.isNot(tok::l_square) && |
| 608 | // Tokens that can only be used as binary operators and a part of |
| 609 | // overloaded operator names. |
| 610 | Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) && |
| 611 | Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) && |
| 612 | Tok.isNot(tok::less) && Tok.isNot(tok::greater) && |
| 613 | Tok.isNot(tok::slash) && Tok.isNot(tok::percent) && |
| 614 | Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) && |
| 615 | Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) && |
| 616 | Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) && |
| 617 | Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) && |
| 618 | Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) && |
| 619 | Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) && |
| 620 | Tok.isNot(tok::lesslessequal) && |
| 621 | // Colon is used in labels, base class lists, initializer lists, |
| 622 | // range-based for loops, ternary operator, but should never be the |
| 623 | // first token in an unwrapped line. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 624 | Tok.isNot(tok::colon) && |
| 625 | // 'noexcept' is a trailing annotation. |
| 626 | Tok.isNot(tok::kw_noexcept); |
Alexander Kornienko | 99b0e14 | 2013-04-09 16:15:19 +0000 | [diff] [blame] | 627 | } |
| 628 | |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 629 | void UnwrappedLineParser::parseStructuralElement() { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 630 | assert(!FormatTok->Tok.is(tok::l_brace)); |
| 631 | switch (FormatTok->Tok.getKind()) { |
Nico Weber | 6092d4e | 2013-01-07 19:05:19 +0000 | [diff] [blame] | 632 | case tok::at: |
| 633 | nextToken(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 634 | if (FormatTok->Tok.is(tok::l_brace)) { |
Nico Weber | d74fcdb | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 635 | parseBracedList(); |
| 636 | break; |
| 637 | } |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 638 | switch (FormatTok->Tok.getObjCKeywordID()) { |
Nico Weber | 6092d4e | 2013-01-07 19:05:19 +0000 | [diff] [blame] | 639 | case tok::objc_public: |
| 640 | case tok::objc_protected: |
| 641 | case tok::objc_package: |
| 642 | case tok::objc_private: |
| 643 | return parseAccessSpecifier(); |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 644 | case tok::objc_interface: |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 645 | case tok::objc_implementation: |
| 646 | return parseObjCInterfaceOrImplementation(); |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 647 | case tok::objc_protocol: |
| 648 | return parseObjCProtocol(); |
Nico Weber | 049c447 | 2013-01-09 21:42:32 +0000 | [diff] [blame] | 649 | case tok::objc_end: |
| 650 | return; // Handled by the caller. |
Nico Weber | b530fa3 | 2013-01-10 00:25:19 +0000 | [diff] [blame] | 651 | case tok::objc_optional: |
| 652 | case tok::objc_required: |
| 653 | nextToken(); |
| 654 | addUnwrappedLine(); |
| 655 | return; |
Nico Weber | 6092d4e | 2013-01-07 19:05:19 +0000 | [diff] [blame] | 656 | default: |
| 657 | break; |
| 658 | } |
| 659 | break; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 660 | case tok::kw_asm: |
| 661 | FormatTok->Finalized = true; |
| 662 | nextToken(); |
| 663 | if (FormatTok->is(tok::l_brace)) { |
| 664 | while (FormatTok && FormatTok->isNot(tok::eof)) { |
| 665 | FormatTok->Finalized = true; |
| 666 | if (FormatTok->is(tok::r_brace)) { |
| 667 | nextToken(); |
| 668 | break; |
| 669 | } |
| 670 | nextToken(); |
| 671 | } |
| 672 | } |
| 673 | break; |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 674 | case tok::kw_namespace: |
| 675 | parseNamespace(); |
| 676 | return; |
Dmitri Gribenko | 1f94f2b | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 677 | case tok::kw_inline: |
| 678 | nextToken(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 679 | if (FormatTok->Tok.is(tok::kw_namespace)) { |
Dmitri Gribenko | 1f94f2b | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 680 | parseNamespace(); |
| 681 | return; |
| 682 | } |
| 683 | break; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 684 | case tok::kw_public: |
| 685 | case tok::kw_protected: |
| 686 | case tok::kw_private: |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 687 | if (Style.Language == FormatStyle::LK_Java) |
| 688 | nextToken(); |
| 689 | else |
| 690 | parseAccessSpecifier(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 691 | return; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 692 | case tok::kw_if: |
| 693 | parseIfThenElse(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 694 | return; |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 695 | case tok::kw_for: |
| 696 | case tok::kw_while: |
| 697 | parseForOrWhileLoop(); |
| 698 | return; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 699 | case tok::kw_do: |
| 700 | parseDoWhile(); |
| 701 | return; |
| 702 | case tok::kw_switch: |
| 703 | parseSwitch(); |
| 704 | return; |
| 705 | case tok::kw_default: |
| 706 | nextToken(); |
| 707 | parseLabel(); |
| 708 | return; |
| 709 | case tok::kw_case: |
| 710 | parseCaseLabel(); |
| 711 | return; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 712 | case tok::kw_try: |
| 713 | parseTryCatch(); |
| 714 | return; |
Manuel Klimek | d19dc2d | 2013-01-21 14:32:05 +0000 | [diff] [blame] | 715 | case tok::kw_extern: |
| 716 | nextToken(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 717 | if (FormatTok->Tok.is(tok::string_literal)) { |
Manuel Klimek | d19dc2d | 2013-01-21 14:32:05 +0000 | [diff] [blame] | 718 | nextToken(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 719 | if (FormatTok->Tok.is(tok::l_brace)) { |
Daniel Jasper | eff18b9 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 720 | parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false); |
Manuel Klimek | d19dc2d | 2013-01-21 14:32:05 +0000 | [diff] [blame] | 721 | addUnwrappedLine(); |
| 722 | return; |
| 723 | } |
| 724 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 725 | break; |
| 726 | case tok::identifier: |
| 727 | if (FormatTok->IsForEachMacro) { |
| 728 | parseForOrWhileLoop(); |
| 729 | return; |
| 730 | } |
Manuel Klimek | d19dc2d | 2013-01-21 14:32:05 +0000 | [diff] [blame] | 731 | // In all other cases, parse the declaration. |
| 732 | break; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 733 | default: |
| 734 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 735 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 736 | do { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 737 | switch (FormatTok->Tok.getKind()) { |
Nico Weber | d74fcdb | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 738 | case tok::at: |
| 739 | nextToken(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 740 | if (FormatTok->Tok.is(tok::l_brace)) |
Nico Weber | d74fcdb | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 741 | parseBracedList(); |
| 742 | break; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 743 | case tok::kw_enum: |
| 744 | parseEnum(); |
Manuel Klimek | 308232c | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 745 | break; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 746 | case tok::kw_typedef: |
| 747 | nextToken(); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 748 | if (FormatTok->is(Keywords.kw_NS_ENUM)) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 749 | parseEnum(); |
| 750 | break; |
Alexander Kornienko | d881875 | 2013-01-16 11:43:46 +0000 | [diff] [blame] | 751 | case tok::kw_struct: |
| 752 | case tok::kw_union: |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 753 | case tok::kw_class: |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 754 | parseRecord(); |
| 755 | // A record declaration or definition is always the start of a structural |
| 756 | // element. |
| 757 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 758 | case tok::semi: |
| 759 | nextToken(); |
| 760 | addUnwrappedLine(); |
| 761 | return; |
Alexander Kornienko | d881875 | 2013-01-16 11:43:46 +0000 | [diff] [blame] | 762 | case tok::r_brace: |
| 763 | addUnwrappedLine(); |
| 764 | return; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 765 | case tok::l_paren: |
| 766 | parseParens(); |
| 767 | break; |
Manuel Klimek | 753a511 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 768 | case tok::caret: |
| 769 | nextToken(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 770 | if (FormatTok->Tok.isAnyIdentifier() || |
| 771 | FormatTok->isSimpleTypeSpecifier()) |
| 772 | nextToken(); |
| 773 | if (FormatTok->is(tok::l_paren)) |
| 774 | parseParens(); |
| 775 | if (FormatTok->is(tok::l_brace)) |
Manuel Klimek | 753a511 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 776 | parseChildBlock(); |
Manuel Klimek | 753a511 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 777 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 778 | case tok::l_brace: |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 779 | if (!tryToParseBracedList()) { |
| 780 | // A block outside of parentheses must be the last part of a |
| 781 | // structural element. |
| 782 | // FIXME: Figure out cases where this is not true, and add projections |
| 783 | // for them (the one we know is missing are lambdas). |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 784 | if (Style.BreakBeforeBraces != FormatStyle::BS_Attach) |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 785 | addUnwrappedLine(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 786 | FormatTok->Type = TT_FunctionLBrace; |
Nico Weber | 2726877 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 787 | parseBlock(/*MustBeDeclaration=*/false); |
Manuel Klimek | 44135b8 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 788 | addUnwrappedLine(); |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 789 | return; |
| 790 | } |
| 791 | // Otherwise this was a braced init list, and the structural |
| 792 | // element continues. |
| 793 | break; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 794 | case tok::kw_try: |
| 795 | // We arrive here when parsing function-try blocks. |
| 796 | parseTryCatch(); |
| 797 | return; |
Daniel Jasper | 7e70f4c | 2013-05-29 13:16:10 +0000 | [diff] [blame] | 798 | case tok::identifier: { |
| 799 | StringRef Text = FormatTok->TokenText; |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 800 | // Parse function literal unless 'function' is the first token in a line |
| 801 | // in which case this should be treated as a free-standing function. |
| 802 | if (Style.Language == FormatStyle::LK_JavaScript && Text == "function" && |
| 803 | Line->Tokens.size() > 0) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 804 | tryToParseJSFunction(); |
| 805 | break; |
| 806 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 807 | nextToken(); |
Alexander Kornienko | 3d713a7 | 2013-04-08 22:16:06 +0000 | [diff] [blame] | 808 | if (Line->Tokens.size() == 1) { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 809 | if (FormatTok->Tok.is(tok::colon)) { |
Alexander Kornienko | 3d713a7 | 2013-04-08 22:16:06 +0000 | [diff] [blame] | 810 | parseLabel(); |
| 811 | return; |
| 812 | } |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 813 | // Recognize function-like macro usages without trailing semicolon as |
| 814 | // well as free-standing macrose like Q_OBJECT. |
| 815 | bool FunctionLike = FormatTok->is(tok::l_paren); |
| 816 | if (FunctionLike) |
Alexander Kornienko | 3d713a7 | 2013-04-08 22:16:06 +0000 | [diff] [blame] | 817 | parseParens(); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 818 | if (FormatTok->NewlinesBefore > 0 && |
| 819 | (Text.size() >= 5 || FunctionLike) && |
| 820 | tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) { |
Daniel Jasper | 7e70f4c | 2013-05-29 13:16:10 +0000 | [diff] [blame] | 821 | addUnwrappedLine(); |
Daniel Jasper | c76d59d | 2013-05-29 14:09:17 +0000 | [diff] [blame] | 822 | return; |
Alexander Kornienko | 3d713a7 | 2013-04-08 22:16:06 +0000 | [diff] [blame] | 823 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 824 | } |
| 825 | break; |
Daniel Jasper | 7e70f4c | 2013-05-29 13:16:10 +0000 | [diff] [blame] | 826 | } |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 827 | case tok::equal: |
| 828 | nextToken(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 829 | if (FormatTok->Tok.is(tok::l_brace)) { |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 830 | parseBracedList(); |
| 831 | } |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 832 | break; |
Manuel Klimek | b61a8af | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 833 | case tok::l_square: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 834 | parseSquare(); |
Manuel Klimek | b61a8af | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 835 | break; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 836 | default: |
| 837 | nextToken(); |
| 838 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 839 | } |
| 840 | } while (!eof()); |
| 841 | } |
| 842 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 843 | bool UnwrappedLineParser::tryToParseLambda() { |
Daniel Jasper | 2d65705 | 2013-09-05 11:49:39 +0000 | [diff] [blame] | 844 | // FIXME: This is a dirty way to access the previous token. Find a better |
| 845 | // solution. |
Daniel Jasper | 520cca8 | 2013-09-06 21:25:51 +0000 | [diff] [blame] | 846 | if (!Line->Tokens.empty() && |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 847 | (Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator, |
| 848 | tok::kw_new, tok::kw_delete) || |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 849 | Line->Tokens.back().Tok->closesScope() || |
| 850 | Line->Tokens.back().Tok->isSimpleTypeSpecifier())) { |
Daniel Jasper | 2d65705 | 2013-09-05 11:49:39 +0000 | [diff] [blame] | 851 | nextToken(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 852 | return false; |
Daniel Jasper | 2d65705 | 2013-09-05 11:49:39 +0000 | [diff] [blame] | 853 | } |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 854 | assert(FormatTok->is(tok::l_square)); |
| 855 | FormatToken &LSquare = *FormatTok; |
Daniel Jasper | ac2c974 | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 856 | if (!tryToParseLambdaIntroducer()) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 857 | return false; |
Manuel Klimek | b61a8af | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 858 | |
| 859 | while (FormatTok->isNot(tok::l_brace)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 860 | if (FormatTok->isSimpleTypeSpecifier()) { |
| 861 | nextToken(); |
| 862 | continue; |
| 863 | } |
Manuel Klimek | b61a8af | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 864 | switch (FormatTok->Tok.getKind()) { |
Daniel Jasper | ac2c974 | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 865 | case tok::l_brace: |
| 866 | break; |
| 867 | case tok::l_paren: |
| 868 | parseParens(); |
| 869 | break; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 870 | case tok::less: |
| 871 | case tok::greater: |
Daniel Jasper | ac2c974 | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 872 | case tok::identifier: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 873 | case tok::coloncolon: |
Daniel Jasper | ac2c974 | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 874 | case tok::kw_mutable: |
| 875 | nextToken(); |
| 876 | break; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 877 | case tok::arrow: |
| 878 | FormatTok->Type = TT_TrailingReturnArrow; |
| 879 | nextToken(); |
| 880 | break; |
Daniel Jasper | ac2c974 | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 881 | default: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 882 | return true; |
Manuel Klimek | b61a8af | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 883 | } |
| 884 | } |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 885 | LSquare.Type = TT_LambdaLSquare; |
Manuel Klimek | 753a511 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 886 | parseChildBlock(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 887 | return true; |
Manuel Klimek | b61a8af | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | bool UnwrappedLineParser::tryToParseLambdaIntroducer() { |
| 891 | nextToken(); |
| 892 | if (FormatTok->is(tok::equal)) { |
| 893 | nextToken(); |
Daniel Jasper | ac2c974 | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 894 | if (FormatTok->is(tok::r_square)) { |
| 895 | nextToken(); |
| 896 | return true; |
| 897 | } |
| 898 | if (FormatTok->isNot(tok::comma)) |
| 899 | return false; |
Manuel Klimek | b61a8af | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 900 | nextToken(); |
| 901 | } else if (FormatTok->is(tok::amp)) { |
| 902 | nextToken(); |
Daniel Jasper | ac2c974 | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 903 | if (FormatTok->is(tok::r_square)) { |
| 904 | nextToken(); |
| 905 | return true; |
| 906 | } |
Manuel Klimek | b61a8af | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 907 | if (!FormatTok->isOneOf(tok::comma, tok::identifier)) { |
| 908 | return false; |
| 909 | } |
Daniel Jasper | ac2c974 | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 910 | if (FormatTok->is(tok::comma)) |
| 911 | nextToken(); |
Manuel Klimek | b61a8af | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 912 | } else if (FormatTok->is(tok::r_square)) { |
| 913 | nextToken(); |
| 914 | return true; |
| 915 | } |
| 916 | do { |
Daniel Jasper | ac2c974 | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 917 | if (FormatTok->is(tok::amp)) |
| 918 | nextToken(); |
| 919 | if (!FormatTok->isOneOf(tok::identifier, tok::kw_this)) |
| 920 | return false; |
Manuel Klimek | b61a8af | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 921 | nextToken(); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 922 | if (FormatTok->is(tok::ellipsis)) |
| 923 | nextToken(); |
Manuel Klimek | b61a8af | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 924 | if (FormatTok->is(tok::comma)) { |
| 925 | nextToken(); |
| 926 | } else if (FormatTok->is(tok::r_square)) { |
| 927 | nextToken(); |
| 928 | return true; |
| 929 | } else { |
| 930 | return false; |
| 931 | } |
| 932 | } while (!eof()); |
| 933 | return false; |
| 934 | } |
| 935 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 936 | void UnwrappedLineParser::tryToParseJSFunction() { |
| 937 | nextToken(); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 938 | |
| 939 | // Consume function name. |
| 940 | if (FormatTok->is(tok::identifier)) |
| 941 | nextToken(); |
| 942 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 943 | if (FormatTok->isNot(tok::l_paren)) |
| 944 | return; |
| 945 | nextToken(); |
| 946 | while (FormatTok->isNot(tok::l_brace)) { |
| 947 | // Err on the side of caution in order to avoid consuming the full file in |
| 948 | // case of incomplete code. |
| 949 | if (!FormatTok->isOneOf(tok::identifier, tok::comma, tok::r_paren, |
| 950 | tok::comment)) |
| 951 | return; |
| 952 | nextToken(); |
| 953 | } |
| 954 | parseChildBlock(); |
| 955 | } |
| 956 | |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 957 | bool UnwrappedLineParser::tryToParseBracedList() { |
Daniel Jasper | 0de1c4d | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 958 | if (FormatTok->BlockKind == BK_Unknown) |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 959 | calculateBraceTypes(); |
Daniel Jasper | 0de1c4d | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 960 | assert(FormatTok->BlockKind != BK_Unknown); |
| 961 | if (FormatTok->BlockKind == BK_Block) |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 962 | return false; |
| 963 | parseBracedList(); |
| 964 | return true; |
| 965 | } |
| 966 | |
Daniel Jasper | 5798120 | 2013-09-13 09:20:45 +0000 | [diff] [blame] | 967 | bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) { |
| 968 | bool HasError = false; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 969 | nextToken(); |
| 970 | |
Manuel Klimek | 423dd93 | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 971 | // FIXME: Once we have an expression parser in the UnwrappedLineParser, |
| 972 | // replace this by using parseAssigmentExpression() inside. |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 973 | do { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 974 | if (Style.Language == FormatStyle::LK_JavaScript && |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 975 | FormatTok->is(Keywords.kw_function)) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 976 | tryToParseJSFunction(); |
| 977 | continue; |
| 978 | } |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 979 | switch (FormatTok->Tok.getKind()) { |
Manuel Klimek | 753a511 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 980 | case tok::caret: |
| 981 | nextToken(); |
| 982 | if (FormatTok->is(tok::l_brace)) { |
| 983 | parseChildBlock(); |
| 984 | } |
| 985 | break; |
| 986 | case tok::l_square: |
| 987 | tryToParseLambda(); |
| 988 | break; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 989 | case tok::l_brace: |
Manuel Klimek | 31e44f7 | 2013-09-04 08:20:47 +0000 | [diff] [blame] | 990 | // Assume there are no blocks inside a braced init list apart |
| 991 | // from the ones we explicitly parse out (like lambdas). |
| 992 | FormatTok->BlockKind = BK_BracedInit; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 993 | parseBracedList(); |
| 994 | break; |
| 995 | case tok::r_brace: |
| 996 | nextToken(); |
Daniel Jasper | 5798120 | 2013-09-13 09:20:45 +0000 | [diff] [blame] | 997 | return !HasError; |
Manuel Klimek | 423dd93 | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 998 | case tok::semi: |
Daniel Jasper | 5798120 | 2013-09-13 09:20:45 +0000 | [diff] [blame] | 999 | HasError = true; |
| 1000 | if (!ContinueOnSemicolons) |
| 1001 | return !HasError; |
| 1002 | nextToken(); |
| 1003 | break; |
Manuel Klimek | 423dd93 | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 1004 | case tok::comma: |
| 1005 | nextToken(); |
Manuel Klimek | 423dd93 | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 1006 | break; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 1007 | default: |
| 1008 | nextToken(); |
| 1009 | break; |
| 1010 | } |
| 1011 | } while (!eof()); |
Daniel Jasper | 5798120 | 2013-09-13 09:20:45 +0000 | [diff] [blame] | 1012 | return false; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1015 | void UnwrappedLineParser::parseParens() { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1016 | assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected."); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1017 | nextToken(); |
| 1018 | do { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1019 | switch (FormatTok->Tok.getKind()) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1020 | case tok::l_paren: |
| 1021 | parseParens(); |
| 1022 | break; |
| 1023 | case tok::r_paren: |
| 1024 | nextToken(); |
| 1025 | return; |
Daniel Jasper | f7ec1cc | 2013-05-31 14:56:29 +0000 | [diff] [blame] | 1026 | case tok::r_brace: |
| 1027 | // A "}" inside parenthesis is an error if there wasn't a matching "{". |
| 1028 | return; |
Daniel Jasper | ac2c974 | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 1029 | case tok::l_square: |
| 1030 | tryToParseLambda(); |
| 1031 | break; |
Nico Weber | 2afbe52 | 2013-02-10 04:38:23 +0000 | [diff] [blame] | 1032 | case tok::l_brace: { |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 1033 | if (!tryToParseBracedList()) { |
Manuel Klimek | b7d98a1 | 2013-09-04 13:34:14 +0000 | [diff] [blame] | 1034 | parseChildBlock(); |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 1035 | } |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 1036 | break; |
Nico Weber | 2afbe52 | 2013-02-10 04:38:23 +0000 | [diff] [blame] | 1037 | } |
Nico Weber | d74fcdb | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 1038 | case tok::at: |
| 1039 | nextToken(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1040 | if (FormatTok->Tok.is(tok::l_brace)) |
Nico Weber | d74fcdb | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 1041 | parseBracedList(); |
| 1042 | break; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 1043 | case tok::identifier: |
| 1044 | if (Style.Language == FormatStyle::LK_JavaScript && |
| 1045 | FormatTok->is(Keywords.kw_function)) |
| 1046 | tryToParseJSFunction(); |
| 1047 | else |
| 1048 | nextToken(); |
| 1049 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1050 | default: |
| 1051 | nextToken(); |
| 1052 | break; |
| 1053 | } |
| 1054 | } while (!eof()); |
| 1055 | } |
| 1056 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1057 | void UnwrappedLineParser::parseSquare() { |
| 1058 | assert(FormatTok->Tok.is(tok::l_square) && "'[' expected."); |
| 1059 | if (tryToParseLambda()) |
| 1060 | return; |
| 1061 | do { |
| 1062 | switch (FormatTok->Tok.getKind()) { |
| 1063 | case tok::l_paren: |
| 1064 | parseParens(); |
| 1065 | break; |
| 1066 | case tok::r_square: |
| 1067 | nextToken(); |
| 1068 | return; |
| 1069 | case tok::r_brace: |
| 1070 | // A "}" inside parenthesis is an error if there wasn't a matching "{". |
| 1071 | return; |
| 1072 | case tok::l_square: |
| 1073 | parseSquare(); |
| 1074 | break; |
| 1075 | case tok::l_brace: { |
| 1076 | if (!tryToParseBracedList()) { |
| 1077 | parseChildBlock(); |
| 1078 | } |
| 1079 | break; |
| 1080 | } |
| 1081 | case tok::at: |
| 1082 | nextToken(); |
| 1083 | if (FormatTok->Tok.is(tok::l_brace)) |
| 1084 | parseBracedList(); |
| 1085 | break; |
| 1086 | default: |
| 1087 | nextToken(); |
| 1088 | break; |
| 1089 | } |
| 1090 | } while (!eof()); |
| 1091 | } |
| 1092 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1093 | void UnwrappedLineParser::parseIfThenElse() { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1094 | assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1095 | nextToken(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1096 | if (FormatTok->Tok.is(tok::l_paren)) |
Manuel Klimek | d465843 | 2013-01-11 18:28:36 +0000 | [diff] [blame] | 1097 | parseParens(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1098 | bool NeedsUnwrappedLine = false; |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1099 | if (FormatTok->Tok.is(tok::l_brace)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1100 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
Nico Weber | 2726877 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 1101 | parseBlock(/*MustBeDeclaration=*/false); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1102 | if (Style.BreakBeforeBraces == FormatStyle::BS_Allman || |
| 1103 | Style.BreakBeforeBraces == FormatStyle::BS_GNU) { |
Manuel Klimek | e490705 | 2013-08-02 21:31:59 +0000 | [diff] [blame] | 1104 | addUnwrappedLine(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1105 | } else { |
Manuel Klimek | e490705 | 2013-08-02 21:31:59 +0000 | [diff] [blame] | 1106 | NeedsUnwrappedLine = true; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1107 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1108 | } else { |
| 1109 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1110 | ++Line->Level; |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 1111 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1112 | --Line->Level; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1113 | } |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1114 | if (FormatTok->Tok.is(tok::kw_else)) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 1115 | if (Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) |
| 1116 | addUnwrappedLine(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1117 | nextToken(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1118 | if (FormatTok->Tok.is(tok::l_brace)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1119 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
Nico Weber | 2726877 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 1120 | parseBlock(/*MustBeDeclaration=*/false); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1121 | addUnwrappedLine(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1122 | } else if (FormatTok->Tok.is(tok::kw_if)) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1123 | parseIfThenElse(); |
| 1124 | } else { |
| 1125 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1126 | ++Line->Level; |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 1127 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1128 | --Line->Level; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1129 | } |
| 1130 | } else if (NeedsUnwrappedLine) { |
| 1131 | addUnwrappedLine(); |
| 1132 | } |
| 1133 | } |
| 1134 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1135 | void UnwrappedLineParser::parseTryCatch() { |
| 1136 | assert(FormatTok->is(tok::kw_try) && "'try' expected"); |
| 1137 | nextToken(); |
| 1138 | bool NeedsUnwrappedLine = false; |
| 1139 | if (FormatTok->is(tok::colon)) { |
| 1140 | // We are in a function try block, what comes is an initializer list. |
| 1141 | nextToken(); |
| 1142 | while (FormatTok->is(tok::identifier)) { |
| 1143 | nextToken(); |
| 1144 | if (FormatTok->is(tok::l_paren)) |
| 1145 | parseParens(); |
| 1146 | else |
| 1147 | StructuralError = true; |
| 1148 | if (FormatTok->is(tok::comma)) |
| 1149 | nextToken(); |
| 1150 | } |
| 1151 | } |
| 1152 | if (FormatTok->is(tok::l_brace)) { |
| 1153 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
| 1154 | parseBlock(/*MustBeDeclaration=*/false); |
| 1155 | if (Style.BreakBeforeBraces == FormatStyle::BS_Allman || |
| 1156 | Style.BreakBeforeBraces == FormatStyle::BS_GNU || |
| 1157 | Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) { |
| 1158 | addUnwrappedLine(); |
| 1159 | } else { |
| 1160 | NeedsUnwrappedLine = true; |
| 1161 | } |
| 1162 | } else if (!FormatTok->is(tok::kw_catch)) { |
| 1163 | // The C++ standard requires a compound-statement after a try. |
| 1164 | // If there's none, we try to assume there's a structuralElement |
| 1165 | // and try to continue. |
| 1166 | StructuralError = true; |
| 1167 | addUnwrappedLine(); |
| 1168 | ++Line->Level; |
| 1169 | parseStructuralElement(); |
| 1170 | --Line->Level; |
| 1171 | } |
| 1172 | while (FormatTok->is(tok::kw_catch) || |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 1173 | ((Style.Language == FormatStyle::LK_Java || |
| 1174 | Style.Language == FormatStyle::LK_JavaScript) && |
| 1175 | FormatTok->is(Keywords.kw_finally))) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1176 | nextToken(); |
| 1177 | while (FormatTok->isNot(tok::l_brace)) { |
| 1178 | if (FormatTok->is(tok::l_paren)) { |
| 1179 | parseParens(); |
| 1180 | continue; |
| 1181 | } |
| 1182 | if (FormatTok->isOneOf(tok::semi, tok::r_brace)) |
| 1183 | return; |
| 1184 | nextToken(); |
| 1185 | } |
| 1186 | NeedsUnwrappedLine = false; |
| 1187 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
| 1188 | parseBlock(/*MustBeDeclaration=*/false); |
| 1189 | if (Style.BreakBeforeBraces == FormatStyle::BS_Allman || |
| 1190 | Style.BreakBeforeBraces == FormatStyle::BS_GNU || |
| 1191 | Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) { |
| 1192 | addUnwrappedLine(); |
| 1193 | } else { |
| 1194 | NeedsUnwrappedLine = true; |
| 1195 | } |
| 1196 | } |
| 1197 | if (NeedsUnwrappedLine) { |
| 1198 | addUnwrappedLine(); |
| 1199 | } |
| 1200 | } |
| 1201 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1202 | void UnwrappedLineParser::parseNamespace() { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1203 | assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected"); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 1204 | |
| 1205 | const FormatToken &InitialToken = *FormatTok; |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1206 | nextToken(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1207 | if (FormatTok->Tok.is(tok::identifier)) |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1208 | nextToken(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1209 | if (FormatTok->Tok.is(tok::l_brace)) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 1210 | if (ShouldBreakBeforeBrace(Style, InitialToken)) |
Manuel Klimek | 44135b8 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 1211 | addUnwrappedLine(); |
| 1212 | |
Daniel Jasper | eff18b9 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 1213 | bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All || |
| 1214 | (Style.NamespaceIndentation == FormatStyle::NI_Inner && |
| 1215 | DeclarationScopeStack.size() > 1); |
| 1216 | parseBlock(/*MustBeDeclaration=*/true, AddLevel); |
Manuel Klimek | 7fc2db0 | 2013-02-06 16:08:09 +0000 | [diff] [blame] | 1217 | // Munch the semicolon after a namespace. This is more common than one would |
| 1218 | // think. Puttin the semicolon into its own line is very ugly. |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1219 | if (FormatTok->Tok.is(tok::semi)) |
Manuel Klimek | 7fc2db0 | 2013-02-06 16:08:09 +0000 | [diff] [blame] | 1220 | nextToken(); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1221 | addUnwrappedLine(); |
| 1222 | } |
| 1223 | // FIXME: Add error handling. |
| 1224 | } |
| 1225 | |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 1226 | void UnwrappedLineParser::parseForOrWhileLoop() { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1227 | assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while) || |
| 1228 | FormatTok->IsForEachMacro) && |
| 1229 | "'for', 'while' or foreach macro expected"); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 1230 | nextToken(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1231 | if (FormatTok->Tok.is(tok::l_paren)) |
Manuel Klimek | 6eca03f | 2013-01-11 19:23:05 +0000 | [diff] [blame] | 1232 | parseParens(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1233 | if (FormatTok->Tok.is(tok::l_brace)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1234 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
Nico Weber | 2726877 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 1235 | parseBlock(/*MustBeDeclaration=*/false); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 1236 | addUnwrappedLine(); |
| 1237 | } else { |
| 1238 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1239 | ++Line->Level; |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 1240 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1241 | --Line->Level; |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 1242 | } |
| 1243 | } |
| 1244 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1245 | void UnwrappedLineParser::parseDoWhile() { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1246 | assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1247 | nextToken(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1248 | if (FormatTok->Tok.is(tok::l_brace)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1249 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
Nico Weber | 2726877 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 1250 | parseBlock(/*MustBeDeclaration=*/false); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1251 | if (Style.BreakBeforeBraces == FormatStyle::BS_GNU) |
| 1252 | addUnwrappedLine(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1253 | } else { |
| 1254 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1255 | ++Line->Level; |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 1256 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1257 | --Line->Level; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 1260 | // FIXME: Add error handling. |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1261 | if (!FormatTok->Tok.is(tok::kw_while)) { |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 1262 | addUnwrappedLine(); |
| 1263 | return; |
| 1264 | } |
| 1265 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1266 | nextToken(); |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 1267 | parseStructuralElement(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | void UnwrappedLineParser::parseLabel() { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1271 | nextToken(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1272 | unsigned OldLineLevel = Line->Level; |
Daniel Jasper | bcca7e4 | 2013-03-20 10:23:53 +0000 | [diff] [blame] | 1273 | if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0)) |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1274 | --Line->Level; |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1275 | if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1276 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
Nico Weber | 2726877 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 1277 | parseBlock(/*MustBeDeclaration=*/false); |
Manuel Klimek | e490705 | 2013-08-02 21:31:59 +0000 | [diff] [blame] | 1278 | if (FormatTok->Tok.is(tok::kw_break)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1279 | // "break;" after "}" on its own line only for BS_Allman and BS_GNU |
| 1280 | if (Style.BreakBeforeBraces == FormatStyle::BS_Allman || |
| 1281 | Style.BreakBeforeBraces == FormatStyle::BS_GNU) { |
Manuel Klimek | e490705 | 2013-08-02 21:31:59 +0000 | [diff] [blame] | 1282 | addUnwrappedLine(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1283 | } |
Manuel Klimek | e490705 | 2013-08-02 21:31:59 +0000 | [diff] [blame] | 1284 | parseStructuralElement(); |
| 1285 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1286 | addUnwrappedLine(); |
| 1287 | } else { |
| 1288 | addUnwrappedLine(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1289 | } |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1290 | Line->Level = OldLineLevel; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | void UnwrappedLineParser::parseCaseLabel() { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1294 | assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1295 | // FIXME: fix handling of complex expressions here. |
| 1296 | do { |
| 1297 | nextToken(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1298 | } while (!eof() && !FormatTok->Tok.is(tok::colon)); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1299 | parseLabel(); |
| 1300 | } |
| 1301 | |
| 1302 | void UnwrappedLineParser::parseSwitch() { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1303 | assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1304 | nextToken(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1305 | if (FormatTok->Tok.is(tok::l_paren)) |
Manuel Klimek | 6eca03f | 2013-01-11 19:23:05 +0000 | [diff] [blame] | 1306 | parseParens(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1307 | if (FormatTok->Tok.is(tok::l_brace)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1308 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
Daniel Jasper | eff18b9 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 1309 | parseBlock(/*MustBeDeclaration=*/false); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1310 | addUnwrappedLine(); |
| 1311 | } else { |
| 1312 | addUnwrappedLine(); |
Daniel Jasper | e865cc5 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 1313 | ++Line->Level; |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 1314 | parseStructuralElement(); |
Daniel Jasper | e865cc5 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 1315 | --Line->Level; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1316 | } |
| 1317 | } |
| 1318 | |
| 1319 | void UnwrappedLineParser::parseAccessSpecifier() { |
| 1320 | nextToken(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1321 | // Understand Qt's slots. |
| 1322 | if (FormatTok->is(tok::identifier) && |
| 1323 | (FormatTok->TokenText == "slots" || FormatTok->TokenText == "Q_SLOTS")) |
| 1324 | nextToken(); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1325 | // 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] | 1326 | if (FormatTok->Tok.is(tok::colon)) |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1327 | nextToken(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1328 | addUnwrappedLine(); |
| 1329 | } |
| 1330 | |
| 1331 | void UnwrappedLineParser::parseEnum() { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 1332 | // Won't be 'enum' for NS_ENUMs. |
| 1333 | if (FormatTok->Tok.is(tok::kw_enum)) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1334 | nextToken(); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 1335 | |
Daniel Jasper | cbeb1c6 | 2013-08-20 12:42:50 +0000 | [diff] [blame] | 1336 | // Eat up enum class ... |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1337 | if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct)) |
| 1338 | nextToken(); |
Daniel Jasper | e27dc5d | 2013-09-06 21:32:35 +0000 | [diff] [blame] | 1339 | while (FormatTok->Tok.getIdentifierInfo() || |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 1340 | FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less, |
| 1341 | tok::greater, tok::comma, tok::question)) { |
Manuel Klimek | 308232c | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 1342 | nextToken(); |
| 1343 | // 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^] | 1344 | if (FormatTok->is(tok::l_paren)) |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 1345 | parseParens(); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 1346 | if (FormatTok->is(tok::identifier)) |
Manuel Klimek | 308232c | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 1347 | nextToken(); |
| 1348 | } |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 1349 | |
| 1350 | // Just a declaration or something is wrong. |
| 1351 | if (FormatTok->isNot(tok::l_brace)) |
| 1352 | return; |
| 1353 | FormatTok->BlockKind = BK_Block; |
| 1354 | |
| 1355 | if (Style.Language == FormatStyle::LK_Java) { |
| 1356 | // Java enums are different. |
| 1357 | parseJavaEnumBody(); |
| 1358 | return; |
Manuel Klimek | 308232c | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 1359 | } |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 1360 | |
| 1361 | // Parse enum body. |
| 1362 | bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true); |
| 1363 | if (HasError) { |
| 1364 | if (FormatTok->is(tok::semi)) |
| 1365 | nextToken(); |
| 1366 | addUnwrappedLine(); |
| 1367 | } |
| 1368 | |
Manuel Klimek | 308232c | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 1369 | // We fall through to parsing a structural element afterwards, so that in |
| 1370 | // enum A {} n, m; |
| 1371 | // "} n, m;" will end up in one unwrapped line. |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 1374 | void UnwrappedLineParser::parseJavaEnumBody() { |
| 1375 | // Determine whether the enum is simple, i.e. does not have a semicolon or |
| 1376 | // constants with class bodies. Simple enums can be formatted like braced |
| 1377 | // lists, contracted to a single line, etc. |
| 1378 | unsigned StoredPosition = Tokens->getPosition(); |
| 1379 | bool IsSimple = true; |
| 1380 | FormatToken *Tok = Tokens->getNextToken(); |
| 1381 | while (Tok) { |
| 1382 | if (Tok->is(tok::r_brace)) |
| 1383 | break; |
| 1384 | if (Tok->isOneOf(tok::l_brace, tok::semi)) { |
| 1385 | IsSimple = false; |
| 1386 | break; |
| 1387 | } |
| 1388 | // FIXME: This will also mark enums with braces in the arguments to enum |
| 1389 | // constants as "not simple". This is probably fine in practice, though. |
| 1390 | Tok = Tokens->getNextToken(); |
| 1391 | } |
| 1392 | FormatTok = Tokens->setPosition(StoredPosition); |
| 1393 | |
| 1394 | if (IsSimple) { |
| 1395 | parseBracedList(); |
| 1396 | addUnwrappedLine(); |
| 1397 | return; |
| 1398 | } |
| 1399 | |
| 1400 | // Parse the body of a more complex enum. |
| 1401 | // First add a line for everything up to the "{". |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 1402 | nextToken(); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 1403 | addUnwrappedLine(); |
| 1404 | ++Line->Level; |
| 1405 | |
| 1406 | // Parse the enum constants. |
| 1407 | while (FormatTok) { |
| 1408 | if (FormatTok->is(tok::l_brace)) { |
| 1409 | // Parse the constant's class body. |
| 1410 | parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true, |
| 1411 | /*MunchSemi=*/false); |
| 1412 | } else if (FormatTok->is(tok::l_paren)) { |
| 1413 | parseParens(); |
| 1414 | } else if (FormatTok->is(tok::comma)) { |
| 1415 | nextToken(); |
| 1416 | addUnwrappedLine(); |
| 1417 | } else if (FormatTok->is(tok::semi)) { |
| 1418 | nextToken(); |
| 1419 | addUnwrappedLine(); |
| 1420 | break; |
| 1421 | } else if (FormatTok->is(tok::r_brace)) { |
| 1422 | addUnwrappedLine(); |
| 1423 | break; |
| 1424 | } else { |
| 1425 | nextToken(); |
| 1426 | } |
| 1427 | } |
| 1428 | |
| 1429 | // Parse the class body after the enum's ";" if any. |
| 1430 | parseLevel(/*HasOpeningBrace=*/true); |
| 1431 | nextToken(); |
| 1432 | --Line->Level; |
| 1433 | addUnwrappedLine(); |
| 1434 | } |
| 1435 | |
| 1436 | void UnwrappedLineParser::parseRecord() { |
| 1437 | const FormatToken &InitialToken = *FormatTok; |
| 1438 | nextToken(); |
| 1439 | if (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::kw___attribute, |
| 1440 | tok::kw___declspec, tok::kw_alignas)) { |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 1441 | nextToken(); |
| 1442 | // 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] | 1443 | if (FormatTok->Tok.is(tok::l_paren)) { |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 1444 | parseParens(); |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 1445 | } |
Manuel Klimek | b8b1ce1 | 2013-02-06 15:57:54 +0000 | [diff] [blame] | 1446 | // The actual identifier can be a nested name specifier, and in macros |
| 1447 | // it is often token-pasted. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 1448 | while (FormatTok->is(tok::identifier) || FormatTok->is(tok::coloncolon) || |
| 1449 | FormatTok->is(tok::hashhash) || |
| 1450 | (Style.Language == FormatStyle::LK_Java && |
| 1451 | FormatTok->isOneOf(tok::period, tok::comma))) |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 1452 | nextToken(); |
| 1453 | |
Manuel Klimek | 3a3408c | 2013-01-21 13:58:54 +0000 | [diff] [blame] | 1454 | // Note that parsing away template declarations here leads to incorrectly |
| 1455 | // accepting function declarations as record declarations. |
| 1456 | // In general, we cannot solve this problem. Consider: |
| 1457 | // class A<int> B() {} |
| 1458 | // which can be a function definition or a class definition when B() is a |
| 1459 | // macro. If we find enough real-world cases where this is a problem, we |
| 1460 | // can parse for the 'template' keyword in the beginning of the statement, |
| 1461 | // and thus rule out the record production in case there is no template |
| 1462 | // (this would still leave us with an ambiguity between template function |
| 1463 | // and class declarations). |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1464 | if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) { |
| 1465 | while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) { |
| 1466 | if (FormatTok->Tok.is(tok::semi)) |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 1467 | return; |
| 1468 | nextToken(); |
| 1469 | } |
| 1470 | } |
| 1471 | } |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1472 | if (FormatTok->Tok.is(tok::l_brace)) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 1473 | if (ShouldBreakBeforeBrace(Style, InitialToken)) |
Manuel Klimek | 44135b8 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 1474 | addUnwrappedLine(); |
| 1475 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1476 | parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true, |
Manuel Klimek | aabfb27 | 2013-10-12 22:46:56 +0000 | [diff] [blame] | 1477 | /*MunchSemi=*/false); |
Manuel Klimek | 44135b8 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 1478 | } |
Manuel Klimek | 3a3408c | 2013-01-21 13:58:54 +0000 | [diff] [blame] | 1479 | // We fall through to parsing a structural element afterwards, so |
| 1480 | // class A {} n, m; |
| 1481 | // will end up in one unwrapped line. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 1482 | // This does not apply for Java. |
| 1483 | if (Style.Language == FormatStyle::LK_Java) |
| 1484 | addUnwrappedLine(); |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1487 | void UnwrappedLineParser::parseObjCProtocolList() { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1488 | assert(FormatTok->Tok.is(tok::less) && "'<' expected."); |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1489 | do |
| 1490 | nextToken(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1491 | while (!eof() && FormatTok->Tok.isNot(tok::greater)); |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1492 | nextToken(); // Skip '>'. |
| 1493 | } |
| 1494 | |
| 1495 | void UnwrappedLineParser::parseObjCUntilAtEnd() { |
| 1496 | do { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1497 | if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) { |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1498 | nextToken(); |
| 1499 | addUnwrappedLine(); |
| 1500 | break; |
| 1501 | } |
Daniel Jasper | 7186ccc | 2013-08-28 08:04:23 +0000 | [diff] [blame] | 1502 | if (FormatTok->is(tok::l_brace)) { |
| 1503 | parseBlock(/*MustBeDeclaration=*/false); |
| 1504 | // In ObjC interfaces, nothing should be following the "}". |
| 1505 | addUnwrappedLine(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1506 | } else if (FormatTok->is(tok::r_brace)) { |
| 1507 | // Ignore stray "}". parseStructuralElement doesn't consume them. |
| 1508 | nextToken(); |
| 1509 | addUnwrappedLine(); |
Daniel Jasper | 7186ccc | 2013-08-28 08:04:23 +0000 | [diff] [blame] | 1510 | } else { |
| 1511 | parseStructuralElement(); |
| 1512 | } |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1513 | } while (!eof()); |
| 1514 | } |
| 1515 | |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 1516 | void UnwrappedLineParser::parseObjCInterfaceOrImplementation() { |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1517 | nextToken(); |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 1518 | nextToken(); // interface name |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1519 | |
| 1520 | // @interface can be followed by either a base class, or a category. |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1521 | if (FormatTok->Tok.is(tok::colon)) { |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1522 | nextToken(); |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 1523 | nextToken(); // base class name |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1524 | } else if (FormatTok->Tok.is(tok::l_paren)) |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1525 | // Skip category, if present. |
| 1526 | parseParens(); |
| 1527 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1528 | if (FormatTok->Tok.is(tok::less)) |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1529 | parseObjCProtocolList(); |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1530 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1531 | if (FormatTok->Tok.is(tok::l_brace)) { |
| 1532 | if (Style.BreakBeforeBraces == FormatStyle::BS_Allman || |
| 1533 | Style.BreakBeforeBraces == FormatStyle::BS_GNU) |
| 1534 | addUnwrappedLine(); |
Nico Weber | 2726877 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 1535 | parseBlock(/*MustBeDeclaration=*/true); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1536 | } |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1537 | |
| 1538 | // With instance variables, this puts '}' on its own line. Without instance |
| 1539 | // variables, this ends the @interface line. |
| 1540 | addUnwrappedLine(); |
| 1541 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1542 | parseObjCUntilAtEnd(); |
| 1543 | } |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1544 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1545 | void UnwrappedLineParser::parseObjCProtocol() { |
| 1546 | nextToken(); |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 1547 | nextToken(); // protocol name |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1548 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1549 | if (FormatTok->Tok.is(tok::less)) |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1550 | parseObjCProtocolList(); |
| 1551 | |
| 1552 | // Check for protocol declaration. |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1553 | if (FormatTok->Tok.is(tok::semi)) { |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1554 | nextToken(); |
| 1555 | return addUnwrappedLine(); |
| 1556 | } |
| 1557 | |
| 1558 | addUnwrappedLine(); |
| 1559 | parseObjCUntilAtEnd(); |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1560 | } |
| 1561 | |
Daniel Jasper | d98927d | 2013-09-05 16:05:56 +0000 | [diff] [blame] | 1562 | LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line, |
| 1563 | StringRef Prefix = "") { |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1564 | llvm::dbgs() << Prefix << "Line(" << Line.Level << ")" |
| 1565 | << (Line.InPPDirective ? " MACRO" : "") << ": "; |
| 1566 | for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(), |
| 1567 | E = Line.Tokens.end(); |
| 1568 | I != E; ++I) { |
Daniel Jasper | ac2c974 | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 1569 | llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] "; |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1570 | } |
| 1571 | for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(), |
| 1572 | E = Line.Tokens.end(); |
| 1573 | I != E; ++I) { |
| 1574 | const UnwrappedLineNode &Node = *I; |
| 1575 | for (SmallVectorImpl<UnwrappedLine>::const_iterator |
| 1576 | I = Node.Children.begin(), |
| 1577 | E = Node.Children.end(); |
| 1578 | I != E; ++I) { |
| 1579 | printDebugInfo(*I, "\nChild: "); |
| 1580 | } |
| 1581 | } |
| 1582 | llvm::dbgs() << "\n"; |
| 1583 | } |
| 1584 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1585 | void UnwrappedLineParser::addUnwrappedLine() { |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 1586 | if (Line->Tokens.empty()) |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1587 | return; |
Manuel Klimek | 8fa3799 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 1588 | DEBUG({ |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1589 | if (CurrentLines == &Lines) |
| 1590 | printDebugInfo(*Line); |
Manuel Klimek | 8fa3799 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 1591 | }); |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 1592 | CurrentLines->push_back(*Line); |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 1593 | Line->Tokens.clear(); |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 1594 | if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) { |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1595 | for (SmallVectorImpl<UnwrappedLine>::iterator |
Daniel Jasper | 516fb31 | 2013-03-01 18:11:39 +0000 | [diff] [blame] | 1596 | I = PreprocessorDirectives.begin(), |
| 1597 | E = PreprocessorDirectives.end(); |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 1598 | I != E; ++I) { |
| 1599 | CurrentLines->push_back(*I); |
| 1600 | } |
| 1601 | PreprocessorDirectives.clear(); |
| 1602 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1603 | } |
| 1604 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1605 | bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1606 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1607 | bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) { |
| 1608 | return (Line->InPPDirective || FormatTok.HasUnescapedNewline) && |
| 1609 | FormatTok.NewlinesBefore > 0; |
| 1610 | } |
| 1611 | |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1612 | void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) { |
| 1613 | bool JustComments = Line->Tokens.empty(); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1614 | for (SmallVectorImpl<FormatToken *>::const_iterator |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1615 | I = CommentsBeforeNextToken.begin(), |
| 1616 | E = CommentsBeforeNextToken.end(); |
| 1617 | I != E; ++I) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1618 | if (isOnNewLine(**I) && JustComments) { |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1619 | addUnwrappedLine(); |
| 1620 | } |
| 1621 | pushToken(*I); |
| 1622 | } |
| 1623 | if (NewlineBeforeNext && JustComments) { |
| 1624 | addUnwrappedLine(); |
| 1625 | } |
| 1626 | CommentsBeforeNextToken.clear(); |
| 1627 | } |
| 1628 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1629 | void UnwrappedLineParser::nextToken() { |
| 1630 | if (eof()) |
| 1631 | return; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1632 | flushComments(isOnNewLine(*FormatTok)); |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1633 | pushToken(FormatTok); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1634 | readToken(); |
| 1635 | } |
| 1636 | |
| 1637 | void UnwrappedLineParser::readToken() { |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1638 | bool CommentsInCurrentLine = true; |
| 1639 | do { |
| 1640 | FormatTok = Tokens->getNextToken(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1641 | assert(FormatTok); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1642 | while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) && |
| 1643 | (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) { |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1644 | // If there is an unfinished unwrapped line, we flush the preprocessor |
| 1645 | // directives only after that unwrapped line was finished later. |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 1646 | bool SwitchToPreprocessorLines = |
| 1647 | !Line->Tokens.empty() && CurrentLines == &Lines; |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1648 | ScopedLineState BlockState(*this, SwitchToPreprocessorLines); |
Alexander Kornienko | 4128e19 | 2013-04-03 12:38:53 +0000 | [diff] [blame] | 1649 | // Comments stored before the preprocessor directive need to be output |
| 1650 | // before the preprocessor directive, at the same level as the |
| 1651 | // preprocessor directive, as we consider them to apply to the directive. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1652 | flushComments(isOnNewLine(*FormatTok)); |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1653 | parsePPDirective(); |
| 1654 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1655 | while (FormatTok->Type == TT_ConflictStart || |
| 1656 | FormatTok->Type == TT_ConflictEnd || |
| 1657 | FormatTok->Type == TT_ConflictAlternative) { |
| 1658 | if (FormatTok->Type == TT_ConflictStart) { |
| 1659 | conditionalCompilationStart(/*Unreachable=*/false); |
| 1660 | } else if (FormatTok->Type == TT_ConflictAlternative) { |
| 1661 | conditionalCompilationAlternative(); |
| 1662 | } else if (FormatTok->Type == TT_ConflictEnd) { |
| 1663 | conditionalCompilationEnd(); |
| 1664 | } |
| 1665 | FormatTok = Tokens->getNextToken(); |
| 1666 | FormatTok->MustBreakBefore = true; |
| 1667 | } |
Alexander Kornienko | 6fb46b0 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 1668 | |
| 1669 | if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) && |
| 1670 | !Line->InPPDirective) { |
| 1671 | continue; |
| 1672 | } |
| 1673 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1674 | if (!FormatTok->Tok.is(tok::comment)) |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1675 | return; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1676 | if (isOnNewLine(*FormatTok) || FormatTok->IsFirst) { |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1677 | CommentsInCurrentLine = false; |
| 1678 | } |
| 1679 | if (CommentsInCurrentLine) { |
| 1680 | pushToken(FormatTok); |
| 1681 | } else { |
| 1682 | CommentsBeforeNextToken.push_back(FormatTok); |
| 1683 | } |
| 1684 | } while (!eof()); |
| 1685 | } |
| 1686 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1687 | void UnwrappedLineParser::pushToken(FormatToken *Tok) { |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1688 | Line->Tokens.push_back(UnwrappedLineNode(Tok)); |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1689 | if (MustBreakBeforeNextToken) { |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1690 | Line->Tokens.back().Tok->MustBreakBefore = true; |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1691 | MustBreakBeforeNextToken = false; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1692 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1693 | } |
| 1694 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1695 | } // end namespace format |
| 1696 | } // end namespace clang |