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