Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1 | //===--- UnwrappedLineParser.cpp - Format C++ code ------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// |
| 10 | /// \file |
| 11 | /// \brief This file contains the implementation of the UnwrappedLineParser, |
| 12 | /// which turns a stream of tokens into UnwrappedLines. |
| 13 | /// |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Chandler Carruth | 4b41745 | 2013-01-19 08:09:44 +0000 | [diff] [blame] | 16 | #include "UnwrappedLineParser.h" |
Benjamin Kramer | 33335df | 2015-03-01 21:36:40 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/STLExtras.h" |
Manuel Klimek | ab3dc00 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | 53f5e89 | 2015-03-23 18:05:43 +0000 | [diff] [blame] | 19 | #include "llvm/Support/raw_ostream.h" |
Manuel Klimek | ab3dc00 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 20 | |
Chandler Carruth | 1034666 | 2014-04-22 03:17:02 +0000 | [diff] [blame] | 21 | #define DEBUG_TYPE "format-parser" |
| 22 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 23 | namespace clang { |
| 24 | namespace format { |
| 25 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 26 | class FormatTokenSource { |
| 27 | public: |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 28 | virtual ~FormatTokenSource() {} |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 29 | virtual FormatToken *getNextToken() = 0; |
| 30 | |
| 31 | virtual unsigned getPosition() = 0; |
| 32 | virtual FormatToken *setPosition(unsigned Position) = 0; |
| 33 | }; |
| 34 | |
Craig Topper | 69665e1 | 2013-07-01 04:21:54 +0000 | [diff] [blame] | 35 | namespace { |
| 36 | |
Manuel Klimek | 0a3a3c9 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 37 | class ScopedDeclarationState { |
| 38 | public: |
| 39 | ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack, |
| 40 | bool MustBeDeclaration) |
| 41 | : Line(Line), Stack(Stack) { |
Manuel Klimek | 0a3a3c9 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 42 | Line.MustBeDeclaration = MustBeDeclaration; |
Manuel Klimek | 3908057 | 2013-01-23 11:03:04 +0000 | [diff] [blame] | 43 | Stack.push_back(MustBeDeclaration); |
Manuel Klimek | 0a3a3c9 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 44 | } |
| 45 | ~ScopedDeclarationState() { |
Manuel Klimek | 0a3a3c9 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 46 | Stack.pop_back(); |
Manuel Klimek | c1237a8 | 2013-01-23 14:08:21 +0000 | [diff] [blame] | 47 | if (!Stack.empty()) |
| 48 | Line.MustBeDeclaration = Stack.back(); |
| 49 | else |
| 50 | Line.MustBeDeclaration = true; |
Manuel Klimek | 0a3a3c9 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 51 | } |
Daniel Jasper | 393564f | 2013-05-31 14:56:29 +0000 | [diff] [blame] | 52 | |
Manuel Klimek | 0a3a3c9 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 53 | private: |
| 54 | UnwrappedLine &Line; |
| 55 | std::vector<bool> &Stack; |
| 56 | }; |
| 57 | |
Krasimir Georgiev | a1c3093 | 2017-05-19 10:34:57 +0000 | [diff] [blame] | 58 | static bool isLineComment(const FormatToken &FormatTok) { |
Manuel Klimek | 89628f6 | 2017-09-20 09:51:03 +0000 | [diff] [blame] | 59 | return FormatTok.is(tok::comment) && FormatTok.TokenText.startswith("//"); |
Krasimir Georgiev | a1c3093 | 2017-05-19 10:34:57 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Krasimir Georgiev | ea222a7 | 2017-05-22 10:07:56 +0000 | [diff] [blame] | 62 | // Checks if \p FormatTok is a line comment that continues the line comment |
| 63 | // \p Previous. The original column of \p MinColumnToken is used to determine |
| 64 | // whether \p FormatTok is indented enough to the right to continue \p Previous. |
| 65 | static bool continuesLineComment(const FormatToken &FormatTok, |
| 66 | const FormatToken *Previous, |
| 67 | const FormatToken *MinColumnToken) { |
| 68 | if (!Previous || !MinColumnToken) |
| 69 | return false; |
| 70 | unsigned MinContinueColumn = |
| 71 | MinColumnToken->OriginalColumn + (isLineComment(*MinColumnToken) ? 0 : 1); |
| 72 | return isLineComment(FormatTok) && FormatTok.NewlinesBefore == 1 && |
| 73 | isLineComment(*Previous) && |
| 74 | FormatTok.OriginalColumn >= MinContinueColumn; |
| 75 | } |
| 76 | |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 77 | class ScopedMacroState : public FormatTokenSource { |
| 78 | public: |
| 79 | ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource, |
Manuel Klimek | 20e0af6 | 2015-05-06 11:56:29 +0000 | [diff] [blame] | 80 | FormatToken *&ResetToken) |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 81 | : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken), |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 82 | PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource), |
Krasimir Georgiev | a1c3093 | 2017-05-19 10:34:57 +0000 | [diff] [blame] | 83 | Token(nullptr), PreviousToken(nullptr) { |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 84 | TokenSource = this; |
Manuel Klimek | ef2cfb1 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 85 | Line.Level = 0; |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 86 | Line.InPPDirective = true; |
| 87 | } |
| 88 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 89 | ~ScopedMacroState() override { |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 90 | TokenSource = PreviousTokenSource; |
| 91 | ResetToken = Token; |
| 92 | Line.InPPDirective = false; |
Manuel Klimek | ef2cfb1 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 93 | Line.Level = PreviousLineLevel; |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 96 | FormatToken *getNextToken() override { |
Manuel Klimek | 7872571 | 2013-01-07 10:03:37 +0000 | [diff] [blame] | 97 | // The \c UnwrappedLineParser guards against this by never calling |
| 98 | // \c getNextToken() after it has encountered the first eof token. |
| 99 | assert(!eof()); |
Krasimir Georgiev | a1c3093 | 2017-05-19 10:34:57 +0000 | [diff] [blame] | 100 | PreviousToken = Token; |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 101 | Token = PreviousTokenSource->getNextToken(); |
| 102 | if (eof()) |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 103 | return getFakeEOF(); |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 104 | return Token; |
| 105 | } |
| 106 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 107 | unsigned getPosition() override { return PreviousTokenSource->getPosition(); } |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 108 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 109 | FormatToken *setPosition(unsigned Position) override { |
Krasimir Georgiev | a1c3093 | 2017-05-19 10:34:57 +0000 | [diff] [blame] | 110 | PreviousToken = nullptr; |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 111 | Token = PreviousTokenSource->setPosition(Position); |
| 112 | return Token; |
| 113 | } |
| 114 | |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 115 | private: |
Krasimir Georgiev | a1c3093 | 2017-05-19 10:34:57 +0000 | [diff] [blame] | 116 | bool eof() { |
| 117 | return Token && Token->HasUnescapedNewline && |
Krasimir Georgiev | ea222a7 | 2017-05-22 10:07:56 +0000 | [diff] [blame] | 118 | !continuesLineComment(*Token, PreviousToken, |
| 119 | /*MinColumnToken=*/PreviousToken); |
Krasimir Georgiev | a1c3093 | 2017-05-19 10:34:57 +0000 | [diff] [blame] | 120 | } |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 121 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 122 | FormatToken *getFakeEOF() { |
| 123 | static bool EOFInitialized = false; |
| 124 | static FormatToken FormatTok; |
| 125 | if (!EOFInitialized) { |
| 126 | FormatTok.Tok.startToken(); |
| 127 | FormatTok.Tok.setKind(tok::eof); |
| 128 | EOFInitialized = true; |
| 129 | } |
| 130 | return &FormatTok; |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | UnwrappedLine &Line; |
| 134 | FormatTokenSource *&TokenSource; |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 135 | FormatToken *&ResetToken; |
Manuel Klimek | ef2cfb1 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 136 | unsigned PreviousLineLevel; |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 137 | FormatTokenSource *PreviousTokenSource; |
| 138 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 139 | FormatToken *Token; |
Krasimir Georgiev | a1c3093 | 2017-05-19 10:34:57 +0000 | [diff] [blame] | 140 | FormatToken *PreviousToken; |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 141 | }; |
| 142 | |
Craig Topper | 69665e1 | 2013-07-01 04:21:54 +0000 | [diff] [blame] | 143 | } // end anonymous namespace |
| 144 | |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 145 | class ScopedLineState { |
| 146 | public: |
Manuel Klimek | d3b92fa | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 147 | ScopedLineState(UnwrappedLineParser &Parser, |
| 148 | bool SwitchToPreprocessorLines = false) |
David Blaikie | efb6eb2 | 2014-08-09 20:02:07 +0000 | [diff] [blame] | 149 | : Parser(Parser), OriginalLines(Parser.CurrentLines) { |
Manuel Klimek | d3b92fa | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 150 | if (SwitchToPreprocessorLines) |
| 151 | Parser.CurrentLines = &Parser.PreprocessorDirectives; |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 152 | else if (!Parser.Line->Tokens.empty()) |
| 153 | Parser.CurrentLines = &Parser.Line->Tokens.back().Children; |
David Blaikie | efb6eb2 | 2014-08-09 20:02:07 +0000 | [diff] [blame] | 154 | PreBlockLine = std::move(Parser.Line); |
| 155 | Parser.Line = llvm::make_unique<UnwrappedLine>(); |
Daniel Jasper | daffc0d | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 156 | Parser.Line->Level = PreBlockLine->Level; |
| 157 | Parser.Line->InPPDirective = PreBlockLine->InPPDirective; |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | ~ScopedLineState() { |
Daniel Jasper | daffc0d | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 161 | if (!Parser.Line->Tokens.empty()) { |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 162 | Parser.addUnwrappedLine(); |
| 163 | } |
Daniel Jasper | daffc0d | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 164 | assert(Parser.Line->Tokens.empty()); |
David Blaikie | efb6eb2 | 2014-08-09 20:02:07 +0000 | [diff] [blame] | 165 | Parser.Line = std::move(PreBlockLine); |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 166 | if (Parser.CurrentLines == &Parser.PreprocessorDirectives) |
| 167 | Parser.MustBreakBeforeNextToken = true; |
| 168 | Parser.CurrentLines = OriginalLines; |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | private: |
| 172 | UnwrappedLineParser &Parser; |
| 173 | |
David Blaikie | efb6eb2 | 2014-08-09 20:02:07 +0000 | [diff] [blame] | 174 | std::unique_ptr<UnwrappedLine> PreBlockLine; |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 175 | SmallVectorImpl<UnwrappedLine> *OriginalLines; |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 176 | }; |
| 177 | |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 178 | class CompoundStatementIndenter { |
| 179 | public: |
| 180 | CompoundStatementIndenter(UnwrappedLineParser *Parser, |
| 181 | const FormatStyle &Style, unsigned &LineLevel) |
| 182 | : LineLevel(LineLevel), OldLineLevel(LineLevel) { |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 183 | if (Style.BraceWrapping.AfterControlStatement) |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 184 | Parser->addUnwrappedLine(); |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 185 | if (Style.BraceWrapping.IndentBraces) |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 186 | ++LineLevel; |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 187 | } |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 188 | ~CompoundStatementIndenter() { LineLevel = OldLineLevel; } |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 189 | |
| 190 | private: |
| 191 | unsigned &LineLevel; |
| 192 | unsigned OldLineLevel; |
| 193 | }; |
| 194 | |
Craig Topper | 69665e1 | 2013-07-01 04:21:54 +0000 | [diff] [blame] | 195 | namespace { |
| 196 | |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 197 | class IndexedTokenSource : public FormatTokenSource { |
| 198 | public: |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 199 | IndexedTokenSource(ArrayRef<FormatToken *> Tokens) |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 200 | : Tokens(Tokens), Position(-1) {} |
| 201 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 202 | FormatToken *getNextToken() override { |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 203 | ++Position; |
| 204 | return Tokens[Position]; |
| 205 | } |
| 206 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 207 | unsigned getPosition() override { |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 208 | assert(Position >= 0); |
| 209 | return Position; |
| 210 | } |
| 211 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 212 | FormatToken *setPosition(unsigned P) override { |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 213 | Position = P; |
| 214 | return Tokens[Position]; |
| 215 | } |
| 216 | |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 217 | void reset() { Position = -1; } |
| 218 | |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 219 | private: |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 220 | ArrayRef<FormatToken *> Tokens; |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 221 | int Position; |
| 222 | }; |
| 223 | |
Craig Topper | 69665e1 | 2013-07-01 04:21:54 +0000 | [diff] [blame] | 224 | } // end anonymous namespace |
| 225 | |
Daniel Jasper | d2ae41a | 2013-05-15 08:14:19 +0000 | [diff] [blame] | 226 | UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style, |
Daniel Jasper | d0ec0d6 | 2014-11-04 12:41:02 +0000 | [diff] [blame] | 227 | const AdditionalKeywords &Keywords, |
Krasimir Georgiev | 9ad83fe | 2017-10-30 14:01:50 +0000 | [diff] [blame] | 228 | unsigned FirstStartColumn, |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 229 | ArrayRef<FormatToken *> Tokens, |
Daniel Jasper | d2ae41a | 2013-05-15 08:14:19 +0000 | [diff] [blame] | 230 | UnwrappedLineConsumer &Callback) |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 231 | : Line(new UnwrappedLine), MustBreakBeforeNextToken(false), |
Krasimir Georgiev | 00c5c72 | 2017-02-02 15:32:19 +0000 | [diff] [blame] | 232 | CurrentLines(&Lines), Style(Style), Keywords(Keywords), |
| 233 | CommentPragmasRegex(Style.CommentPragmas), Tokens(nullptr), |
Krasimir Georgiev | ad47c90 | 2017-08-30 14:34:57 +0000 | [diff] [blame] | 234 | Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1), |
| 235 | IfNdefCondition(nullptr), FoundIncludeGuardStart(false), |
Krasimir Georgiev | 9ad83fe | 2017-10-30 14:01:50 +0000 | [diff] [blame] | 236 | IncludeGuardRejected(false), FirstStartColumn(FirstStartColumn) {} |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 237 | |
| 238 | void UnwrappedLineParser::reset() { |
| 239 | PPBranchLevel = -1; |
Krasimir Georgiev | ad47c90 | 2017-08-30 14:34:57 +0000 | [diff] [blame] | 240 | IfNdefCondition = nullptr; |
| 241 | FoundIncludeGuardStart = false; |
| 242 | IncludeGuardRejected = false; |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 243 | Line.reset(new UnwrappedLine); |
| 244 | CommentsBeforeNextToken.clear(); |
Craig Topper | 2145bc0 | 2014-05-09 08:15:10 +0000 | [diff] [blame] | 245 | FormatTok = nullptr; |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 246 | MustBreakBeforeNextToken = false; |
| 247 | PreprocessorDirectives.clear(); |
| 248 | CurrentLines = &Lines; |
| 249 | DeclarationScopeStack.clear(); |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 250 | PPStack.clear(); |
Krasimir Georgiev | 9ad83fe | 2017-10-30 14:01:50 +0000 | [diff] [blame] | 251 | Line->FirstStartColumn = FirstStartColumn; |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 252 | } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 253 | |
Manuel Klimek | 20e0af6 | 2015-05-06 11:56:29 +0000 | [diff] [blame] | 254 | void UnwrappedLineParser::parse() { |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 255 | IndexedTokenSource TokenSource(AllTokens); |
Krasimir Georgiev | 9ad83fe | 2017-10-30 14:01:50 +0000 | [diff] [blame] | 256 | Line->FirstStartColumn = FirstStartColumn; |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 257 | do { |
| 258 | DEBUG(llvm::dbgs() << "----\n"); |
| 259 | reset(); |
| 260 | Tokens = &TokenSource; |
| 261 | TokenSource.reset(); |
Daniel Jasper | a79064a | 2013-03-01 18:11:39 +0000 | [diff] [blame] | 262 | |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 263 | readToken(); |
| 264 | parseFile(); |
| 265 | // Create line with eof token. |
| 266 | pushToken(FormatTok); |
| 267 | addUnwrappedLine(); |
| 268 | |
| 269 | for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(), |
| 270 | E = Lines.end(); |
| 271 | I != E; ++I) { |
| 272 | Callback.consumeUnwrappedLine(*I); |
| 273 | } |
| 274 | Callback.finishRun(); |
| 275 | Lines.clear(); |
| 276 | while (!PPLevelBranchIndex.empty() && |
Daniel Jasper | 53bd167 | 2013-10-12 13:32:56 +0000 | [diff] [blame] | 277 | PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) { |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 278 | PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1); |
| 279 | PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1); |
| 280 | } |
| 281 | if (!PPLevelBranchIndex.empty()) { |
| 282 | ++PPLevelBranchIndex.back(); |
| 283 | assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size()); |
| 284 | assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back()); |
| 285 | } |
| 286 | } while (!PPLevelBranchIndex.empty()); |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 289 | void UnwrappedLineParser::parseFile() { |
Daniel Jasper | 9326f91 | 2015-05-05 08:40:32 +0000 | [diff] [blame] | 290 | // The top-level context in a file always has declarations, except for pre- |
| 291 | // processor directives and JavaScript files. |
| 292 | bool MustBeDeclaration = |
| 293 | !Line->InPPDirective && Style.Language != FormatStyle::LK_JavaScript; |
| 294 | ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, |
| 295 | MustBeDeclaration); |
Krasimir Georgiev | 26b144c | 2017-07-03 15:05:14 +0000 | [diff] [blame] | 296 | if (Style.Language == FormatStyle::LK_TextProto) |
| 297 | parseBracedList(); |
| 298 | else |
| 299 | parseLevel(/*HasOpeningBrace=*/false); |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 300 | // Make sure to format the remaining tokens. |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 301 | flushComments(true); |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 302 | addUnwrappedLine(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 305 | void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) { |
Daniel Jasper | 516d797 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 306 | bool SwitchLabelEncountered = false; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 307 | do { |
Birunthan Mohanathas | b001a0b | 2015-07-03 17:25:16 +0000 | [diff] [blame] | 308 | tok::TokenKind kind = FormatTok->Tok.getKind(); |
| 309 | if (FormatTok->Type == TT_MacroBlockBegin) { |
| 310 | kind = tok::l_brace; |
| 311 | } else if (FormatTok->Type == TT_MacroBlockEnd) { |
| 312 | kind = tok::r_brace; |
| 313 | } |
| 314 | |
| 315 | switch (kind) { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 316 | case tok::comment: |
Daniel Jasper | e25509f | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 317 | nextToken(); |
| 318 | addUnwrappedLine(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 319 | break; |
| 320 | case tok::l_brace: |
Manuel Klimek | 0a3a3c9 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 321 | // FIXME: Add parameter whether this can happen - if this happens, we must |
| 322 | // be in a non-declaration context. |
Daniel Jasper | b86e272 | 2015-08-24 13:23:37 +0000 | [diff] [blame] | 323 | if (!FormatTok->is(TT_MacroBlockBegin) && tryToParseBracedList()) |
| 324 | continue; |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 325 | parseBlock(/*MustBeDeclaration=*/false); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 326 | addUnwrappedLine(); |
| 327 | break; |
| 328 | case tok::r_brace: |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 329 | if (HasOpeningBrace) |
| 330 | return; |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 331 | nextToken(); |
| 332 | addUnwrappedLine(); |
Manuel Klimek | 1058d98 | 2013-01-06 20:07:31 +0000 | [diff] [blame] | 333 | break; |
Daniel Jasper | 516d797 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 334 | case tok::kw_default: |
| 335 | case tok::kw_case: |
Manuel Klimek | 89628f6 | 2017-09-20 09:51:03 +0000 | [diff] [blame] | 336 | if (Style.Language == FormatStyle::LK_JavaScript && |
| 337 | Line->MustBeDeclaration) { |
Martin Probst | f785fd9 | 2017-08-04 17:07:15 +0000 | [diff] [blame] | 338 | // A 'case: string' style field declaration. |
| 339 | parseStructuralElement(); |
| 340 | break; |
| 341 | } |
Daniel Jasper | 7240762 | 2013-09-02 08:26:29 +0000 | [diff] [blame] | 342 | if (!SwitchLabelEncountered && |
| 343 | (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1))) |
| 344 | ++Line->Level; |
Daniel Jasper | 516d797 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 345 | SwitchLabelEncountered = true; |
| 346 | parseStructuralElement(); |
| 347 | break; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 348 | default: |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 349 | parseStructuralElement(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 350 | break; |
| 351 | } |
| 352 | } while (!eof()); |
| 353 | } |
| 354 | |
Daniel Jasper | adba2aa | 2015-05-18 12:52:00 +0000 | [diff] [blame] | 355 | void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) { |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 356 | // We'll parse forward through the tokens until we hit |
| 357 | // a closing brace or eof - note that getNextToken() will |
| 358 | // parse macros, so this will magically work inside macro |
| 359 | // definitions, too. |
| 360 | unsigned StoredPosition = Tokens->getPosition(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 361 | FormatToken *Tok = FormatTok; |
Manuel Klimek | 89628f6 | 2017-09-20 09:51:03 +0000 | [diff] [blame] | 362 | const FormatToken *PrevTok = Tok->Previous; |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 363 | // Keep a stack of positions of lbrace tokens. We will |
| 364 | // update information about whether an lbrace starts a |
| 365 | // braced init list or a different block during the loop. |
Daniel Jasper | b1f74a8 | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 366 | SmallVector<FormatToken *, 8> LBraceStack; |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 367 | assert(Tok->Tok.is(tok::l_brace)); |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 368 | do { |
Daniel Jasper | eb65e91 | 2015-12-21 18:31:15 +0000 | [diff] [blame] | 369 | // Get next non-comment token. |
Daniel Jasper | 7f5d53e | 2013-07-01 09:15:46 +0000 | [diff] [blame] | 370 | FormatToken *NextTok; |
Daniel Jasper | ca7bd72 | 2013-07-01 16:43:38 +0000 | [diff] [blame] | 371 | unsigned ReadTokens = 0; |
Daniel Jasper | 7f5d53e | 2013-07-01 09:15:46 +0000 | [diff] [blame] | 372 | do { |
| 373 | NextTok = Tokens->getNextToken(); |
Daniel Jasper | ca7bd72 | 2013-07-01 16:43:38 +0000 | [diff] [blame] | 374 | ++ReadTokens; |
Daniel Jasper | 7f5d53e | 2013-07-01 09:15:46 +0000 | [diff] [blame] | 375 | } while (NextTok->is(tok::comment)); |
| 376 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 377 | switch (Tok->Tok.getKind()) { |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 378 | case tok::l_brace: |
Martin Probst | 95ed8e7 | 2017-05-31 09:29:40 +0000 | [diff] [blame] | 379 | if (Style.Language == FormatStyle::LK_JavaScript && PrevTok) { |
| 380 | if (PrevTok->is(tok::colon)) |
| 381 | // A colon indicates this code is in a type, or a braced list |
| 382 | // following a label in an object literal ({a: {b: 1}}). The code |
| 383 | // below could be confused by semicolons between the individual |
| 384 | // members in a type member list, which would normally trigger |
| 385 | // BK_Block. In both cases, this must be parsed as an inline braced |
| 386 | // init. |
| 387 | Tok->BlockKind = BK_BracedInit; |
| 388 | else if (PrevTok->is(tok::r_paren)) |
| 389 | // `) { }` can only occur in function or method declarations in JS. |
| 390 | Tok->BlockKind = BK_Block; |
| 391 | } else { |
Daniel Jasper | b9a4990 | 2016-01-09 15:56:28 +0000 | [diff] [blame] | 392 | Tok->BlockKind = BK_Unknown; |
Martin Probst | 95ed8e7 | 2017-05-31 09:29:40 +0000 | [diff] [blame] | 393 | } |
Daniel Jasper | b1f74a8 | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 394 | LBraceStack.push_back(Tok); |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 395 | break; |
| 396 | case tok::r_brace: |
Daniel Jasper | b9a4990 | 2016-01-09 15:56:28 +0000 | [diff] [blame] | 397 | if (LBraceStack.empty()) |
| 398 | break; |
| 399 | if (LBraceStack.back()->BlockKind == BK_Unknown) { |
| 400 | bool ProbablyBracedList = false; |
| 401 | if (Style.Language == FormatStyle::LK_Proto) { |
| 402 | ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square); |
| 403 | } else { |
| 404 | // Using OriginalColumn to distinguish between ObjC methods and |
| 405 | // binary operators is a bit hacky. |
| 406 | bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) && |
| 407 | NextTok->OriginalColumn == 0; |
Daniel Jasper | 91b032a | 2014-05-22 12:46:38 +0000 | [diff] [blame] | 408 | |
Daniel Jasper | b9a4990 | 2016-01-09 15:56:28 +0000 | [diff] [blame] | 409 | // If there is a comma, semicolon or right paren after the closing |
| 410 | // brace, we assume this is a braced initializer list. Note that |
| 411 | // regardless how we mark inner braces here, we will overwrite the |
| 412 | // BlockKind later if we parse a braced list (where all blocks |
| 413 | // inside are by default braced lists), or when we explicitly detect |
| 414 | // blocks (for example while parsing lambdas). |
Martin Probst | 95ed8e7 | 2017-05-31 09:29:40 +0000 | [diff] [blame] | 415 | // FIXME: Some of these do not apply to JS, e.g. "} {" can never be a |
| 416 | // braced list in JS. |
Daniel Jasper | b9a4990 | 2016-01-09 15:56:28 +0000 | [diff] [blame] | 417 | ProbablyBracedList = |
Daniel Jasper | acffeb8 | 2016-03-05 18:34:26 +0000 | [diff] [blame] | 418 | (Style.Language == FormatStyle::LK_JavaScript && |
Martin Probst | e1e12a7 | 2016-08-19 14:35:01 +0000 | [diff] [blame] | 419 | NextTok->isOneOf(Keywords.kw_of, Keywords.kw_in, |
| 420 | Keywords.kw_as)) || |
Martin Probst | b7fb267 | 2017-05-10 13:53:29 +0000 | [diff] [blame] | 421 | (Style.isCpp() && NextTok->is(tok::l_paren)) || |
Daniel Jasper | b9a4990 | 2016-01-09 15:56:28 +0000 | [diff] [blame] | 422 | NextTok->isOneOf(tok::comma, tok::period, tok::colon, |
| 423 | tok::r_paren, tok::r_square, tok::l_brace, |
Martin Probst | b7fb267 | 2017-05-10 13:53:29 +0000 | [diff] [blame] | 424 | tok::l_square, tok::ellipsis) || |
Daniel Jasper | e4ada02 | 2016-12-13 10:05:03 +0000 | [diff] [blame] | 425 | (NextTok->is(tok::identifier) && |
| 426 | !PrevTok->isOneOf(tok::semi, tok::r_brace, tok::l_brace)) || |
Daniel Jasper | b9a4990 | 2016-01-09 15:56:28 +0000 | [diff] [blame] | 427 | (NextTok->is(tok::semi) && |
| 428 | (!ExpectClassBody || LBraceStack.size() != 1)) || |
| 429 | (NextTok->isBinaryOperator() && !NextIsObjCMethod); |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 430 | } |
Daniel Jasper | b9a4990 | 2016-01-09 15:56:28 +0000 | [diff] [blame] | 431 | if (ProbablyBracedList) { |
| 432 | Tok->BlockKind = BK_BracedInit; |
| 433 | LBraceStack.back()->BlockKind = BK_BracedInit; |
| 434 | } else { |
| 435 | Tok->BlockKind = BK_Block; |
| 436 | LBraceStack.back()->BlockKind = BK_Block; |
| 437 | } |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 438 | } |
Daniel Jasper | b9a4990 | 2016-01-09 15:56:28 +0000 | [diff] [blame] | 439 | LBraceStack.pop_back(); |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 440 | break; |
Daniel Jasper | ac7e34e | 2014-03-13 10:11:17 +0000 | [diff] [blame] | 441 | case tok::at: |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 442 | case tok::semi: |
| 443 | case tok::kw_if: |
| 444 | case tok::kw_while: |
| 445 | case tok::kw_for: |
| 446 | case tok::kw_switch: |
| 447 | case tok::kw_try: |
Nico Weber | fac2371 | 2015-02-04 15:26:27 +0000 | [diff] [blame] | 448 | case tok::kw___try: |
Daniel Jasper | b9a4990 | 2016-01-09 15:56:28 +0000 | [diff] [blame] | 449 | if (!LBraceStack.empty() && LBraceStack.back()->BlockKind == BK_Unknown) |
Daniel Jasper | b1f74a8 | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 450 | LBraceStack.back()->BlockKind = BK_Block; |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 451 | break; |
| 452 | default: |
| 453 | break; |
| 454 | } |
Daniel Jasper | b9a4990 | 2016-01-09 15:56:28 +0000 | [diff] [blame] | 455 | PrevTok = Tok; |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 456 | Tok = NextTok; |
Manuel Klimek | bab25fd | 2013-09-04 08:20:47 +0000 | [diff] [blame] | 457 | } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty()); |
Daniel Jasper | b9a4990 | 2016-01-09 15:56:28 +0000 | [diff] [blame] | 458 | |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 459 | // Assume other blocks for all unclosed opening braces. |
| 460 | for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) { |
Daniel Jasper | b1f74a8 | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 461 | if (LBraceStack[i]->BlockKind == BK_Unknown) |
| 462 | LBraceStack[i]->BlockKind = BK_Block; |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 463 | } |
Manuel Klimek | bab25fd | 2013-09-04 08:20:47 +0000 | [diff] [blame] | 464 | |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 465 | FormatTok = Tokens->setPosition(StoredPosition); |
| 466 | } |
| 467 | |
Francois Ferrand | a98a95c | 2017-07-28 07:56:14 +0000 | [diff] [blame] | 468 | template <class T> |
| 469 | static inline void hash_combine(std::size_t &seed, const T &v) { |
| 470 | std::hash<T> hasher; |
| 471 | seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); |
| 472 | } |
| 473 | |
| 474 | size_t UnwrappedLineParser::computePPHash() const { |
| 475 | size_t h = 0; |
| 476 | for (const auto &i : PPStack) { |
| 477 | hash_combine(h, size_t(i.Kind)); |
| 478 | hash_combine(h, i.Line); |
| 479 | } |
| 480 | return h; |
| 481 | } |
| 482 | |
Manuel Klimek | b212f3b | 2013-10-12 22:46:56 +0000 | [diff] [blame] | 483 | void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel, |
| 484 | bool MunchSemi) { |
Birunthan Mohanathas | b001a0b | 2015-07-03 17:25:16 +0000 | [diff] [blame] | 485 | assert(FormatTok->isOneOf(tok::l_brace, TT_MacroBlockBegin) && |
| 486 | "'{' or macro block token expected"); |
| 487 | const bool MacroBlock = FormatTok->is(TT_MacroBlockBegin); |
Daniel Jasper | eb65e91 | 2015-12-21 18:31:15 +0000 | [diff] [blame] | 488 | FormatTok->BlockKind = BK_Block; |
Birunthan Mohanathas | b001a0b | 2015-07-03 17:25:16 +0000 | [diff] [blame] | 489 | |
Francois Ferrand | a98a95c | 2017-07-28 07:56:14 +0000 | [diff] [blame] | 490 | size_t PPStartHash = computePPHash(); |
| 491 | |
Daniel Jasper | 516d797 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 492 | unsigned InitialLevel = Line->Level; |
Krasimir Georgiev | 3e05105 | 2017-07-24 14:51:59 +0000 | [diff] [blame] | 493 | nextToken(/*LevelDifference=*/AddLevel ? 1 : 0); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 494 | |
Birunthan Mohanathas | b001a0b | 2015-07-03 17:25:16 +0000 | [diff] [blame] | 495 | if (MacroBlock && FormatTok->is(tok::l_paren)) |
| 496 | parseParens(); |
| 497 | |
Francois Ferrand | a98a95c | 2017-07-28 07:56:14 +0000 | [diff] [blame] | 498 | size_t NbPreprocessorDirectives = |
| 499 | CurrentLines == &Lines ? PreprocessorDirectives.size() : 0; |
Manuel Klimek | a4fe1c1 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 500 | addUnwrappedLine(); |
Francois Ferrand | a98a95c | 2017-07-28 07:56:14 +0000 | [diff] [blame] | 501 | size_t OpeningLineIndex = |
| 502 | CurrentLines->empty() |
| 503 | ? (UnwrappedLine::kInvalidIndex) |
| 504 | : (CurrentLines->size() - 1 - NbPreprocessorDirectives); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 505 | |
Manuel Klimek | 0a3a3c9 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 506 | ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, |
| 507 | MustBeDeclaration); |
Daniel Jasper | 65ee347 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 508 | if (AddLevel) |
| 509 | ++Line->Level; |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 510 | parseLevel(/*HasOpeningBrace=*/true); |
Alexander Kornienko | 578fdd8 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 511 | |
Marianne Mailhot-Sarrasin | 03137c6 | 2016-04-14 14:56:49 +0000 | [diff] [blame] | 512 | if (eof()) |
| 513 | return; |
| 514 | |
Birunthan Mohanathas | b001a0b | 2015-07-03 17:25:16 +0000 | [diff] [blame] | 515 | if (MacroBlock ? !FormatTok->is(TT_MacroBlockEnd) |
| 516 | : !FormatTok->is(tok::r_brace)) { |
Daniel Jasper | 516d797 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 517 | Line->Level = InitialLevel; |
Daniel Jasper | eb65e91 | 2015-12-21 18:31:15 +0000 | [diff] [blame] | 518 | FormatTok->BlockKind = BK_Block; |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 519 | return; |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 520 | } |
Alexander Kornienko | 0ea8e10 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 521 | |
Francois Ferrand | a98a95c | 2017-07-28 07:56:14 +0000 | [diff] [blame] | 522 | size_t PPEndHash = computePPHash(); |
| 523 | |
Krasimir Georgiev | 3e05105 | 2017-07-24 14:51:59 +0000 | [diff] [blame] | 524 | // Munch the closing brace. |
| 525 | nextToken(/*LevelDifference=*/AddLevel ? -1 : 0); |
Birunthan Mohanathas | b001a0b | 2015-07-03 17:25:16 +0000 | [diff] [blame] | 526 | |
| 527 | if (MacroBlock && FormatTok->is(tok::l_paren)) |
| 528 | parseParens(); |
| 529 | |
Manuel Klimek | b212f3b | 2013-10-12 22:46:56 +0000 | [diff] [blame] | 530 | if (MunchSemi && FormatTok->Tok.is(tok::semi)) |
| 531 | nextToken(); |
Krasimir Georgiev | 3e05105 | 2017-07-24 14:51:59 +0000 | [diff] [blame] | 532 | Line->Level = InitialLevel; |
Francois Ferrand | a98a95c | 2017-07-28 07:56:14 +0000 | [diff] [blame] | 533 | |
| 534 | if (PPStartHash == PPEndHash) { |
| 535 | Line->MatchingOpeningBlockLineIndex = OpeningLineIndex; |
| 536 | if (OpeningLineIndex != UnwrappedLine::kInvalidIndex) { |
| 537 | // Update the opening line to add the forward reference as well |
| 538 | (*CurrentLines)[OpeningLineIndex].MatchingOpeningBlockLineIndex = |
| 539 | CurrentLines->size() - 1; |
| 540 | } |
Francois Ferrand | e56a829 | 2017-06-14 12:29:47 +0000 | [diff] [blame] | 541 | } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Daniel Jasper | 02c7bca | 2015-03-30 09:56:50 +0000 | [diff] [blame] | 544 | static bool isGoogScope(const UnwrappedLine &Line) { |
Daniel Jasper | 616de864 | 2014-11-23 16:46:28 +0000 | [diff] [blame] | 545 | // FIXME: Closure-library specific stuff should not be hard-coded but be |
| 546 | // configurable. |
Daniel Jasper | 4a39c84 | 2014-05-06 13:54:10 +0000 | [diff] [blame] | 547 | if (Line.Tokens.size() < 4) |
| 548 | return false; |
| 549 | auto I = Line.Tokens.begin(); |
| 550 | if (I->Tok->TokenText != "goog") |
| 551 | return false; |
| 552 | ++I; |
| 553 | if (I->Tok->isNot(tok::period)) |
| 554 | return false; |
| 555 | ++I; |
| 556 | if (I->Tok->TokenText != "scope") |
| 557 | return false; |
| 558 | ++I; |
| 559 | return I->Tok->is(tok::l_paren); |
| 560 | } |
| 561 | |
Martin Probst | 101ec89 | 2017-05-09 20:04:09 +0000 | [diff] [blame] | 562 | static bool isIIFE(const UnwrappedLine &Line, |
| 563 | const AdditionalKeywords &Keywords) { |
| 564 | // Look for the start of an immediately invoked anonymous function. |
| 565 | // https://en.wikipedia.org/wiki/Immediately-invoked_function_expression |
| 566 | // This is commonly done in JavaScript to create a new, anonymous scope. |
| 567 | // Example: (function() { ... })() |
| 568 | if (Line.Tokens.size() < 3) |
| 569 | return false; |
| 570 | auto I = Line.Tokens.begin(); |
| 571 | if (I->Tok->isNot(tok::l_paren)) |
| 572 | return false; |
| 573 | ++I; |
| 574 | if (I->Tok->isNot(Keywords.kw_function)) |
| 575 | return false; |
| 576 | ++I; |
| 577 | return I->Tok->is(tok::l_paren); |
| 578 | } |
| 579 | |
Roman Kashitsyn | a043ced | 2014-08-11 12:18:01 +0000 | [diff] [blame] | 580 | static bool ShouldBreakBeforeBrace(const FormatStyle &Style, |
| 581 | const FormatToken &InitialToken) { |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 582 | if (InitialToken.is(tok::kw_namespace)) |
| 583 | return Style.BraceWrapping.AfterNamespace; |
| 584 | if (InitialToken.is(tok::kw_class)) |
| 585 | return Style.BraceWrapping.AfterClass; |
| 586 | if (InitialToken.is(tok::kw_union)) |
| 587 | return Style.BraceWrapping.AfterUnion; |
| 588 | if (InitialToken.is(tok::kw_struct)) |
| 589 | return Style.BraceWrapping.AfterStruct; |
| 590 | return false; |
Roman Kashitsyn | a043ced | 2014-08-11 12:18:01 +0000 | [diff] [blame] | 591 | } |
| 592 | |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 593 | void UnwrappedLineParser::parseChildBlock() { |
| 594 | FormatTok->BlockKind = BK_Block; |
| 595 | nextToken(); |
| 596 | { |
Manuel Klimek | 89628f6 | 2017-09-20 09:51:03 +0000 | [diff] [blame] | 597 | bool SkipIndent = (Style.Language == FormatStyle::LK_JavaScript && |
| 598 | (isGoogScope(*Line) || isIIFE(*Line, Keywords))); |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 599 | ScopedLineState LineState(*this); |
| 600 | ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, |
| 601 | /*MustBeDeclaration=*/false); |
Martin Probst | 101ec89 | 2017-05-09 20:04:09 +0000 | [diff] [blame] | 602 | Line->Level += SkipIndent ? 0 : 1; |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 603 | parseLevel(/*HasOpeningBrace=*/true); |
Daniel Jasper | 02c7bca | 2015-03-30 09:56:50 +0000 | [diff] [blame] | 604 | flushComments(isOnNewLine(*FormatTok)); |
Martin Probst | 101ec89 | 2017-05-09 20:04:09 +0000 | [diff] [blame] | 605 | Line->Level -= SkipIndent ? 0 : 1; |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 606 | } |
| 607 | nextToken(); |
| 608 | } |
| 609 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 610 | void UnwrappedLineParser::parsePPDirective() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 611 | assert(FormatTok->Tok.is(tok::hash) && "'#' expected"); |
Manuel Klimek | 20e0af6 | 2015-05-06 11:56:29 +0000 | [diff] [blame] | 612 | ScopedMacroState MacroState(*Line, Tokens, FormatTok); |
Manuel Klimek | a71e5d8 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 613 | nextToken(); |
| 614 | |
Craig Topper | 2145bc0 | 2014-05-09 08:15:10 +0000 | [diff] [blame] | 615 | if (!FormatTok->Tok.getIdentifierInfo()) { |
Manuel Klimek | 591b580 | 2013-01-31 15:58:48 +0000 | [diff] [blame] | 616 | parsePPUnknown(); |
Manuel Klimek | a71e5d8 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 617 | return; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 618 | } |
Manuel Klimek | a71e5d8 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 619 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 620 | switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) { |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 621 | case tok::pp_define: |
| 622 | parsePPDefine(); |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 623 | return; |
| 624 | case tok::pp_if: |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 625 | parsePPIf(/*IfDef=*/false); |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 626 | break; |
| 627 | case tok::pp_ifdef: |
| 628 | case tok::pp_ifndef: |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 629 | parsePPIf(/*IfDef=*/true); |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 630 | break; |
| 631 | case tok::pp_else: |
| 632 | parsePPElse(); |
| 633 | break; |
| 634 | case tok::pp_elif: |
| 635 | parsePPElIf(); |
| 636 | break; |
| 637 | case tok::pp_endif: |
| 638 | parsePPEndIf(); |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 639 | break; |
| 640 | default: |
| 641 | parsePPUnknown(); |
| 642 | break; |
| 643 | } |
| 644 | } |
| 645 | |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 646 | void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) { |
Francois Ferrand | a98a95c | 2017-07-28 07:56:14 +0000 | [diff] [blame] | 647 | size_t Line = CurrentLines->size(); |
| 648 | if (CurrentLines == &PreprocessorDirectives) |
| 649 | Line += Lines.size(); |
| 650 | |
| 651 | if (Unreachable || |
| 652 | (!PPStack.empty() && PPStack.back().Kind == PP_Unreachable)) |
| 653 | PPStack.push_back({PP_Unreachable, Line}); |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 654 | else |
Francois Ferrand | a98a95c | 2017-07-28 07:56:14 +0000 | [diff] [blame] | 655 | PPStack.push_back({PP_Conditional, Line}); |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 658 | void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) { |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 659 | ++PPBranchLevel; |
| 660 | assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size()); |
| 661 | if (PPBranchLevel == (int)PPLevelBranchIndex.size()) { |
| 662 | PPLevelBranchIndex.push_back(0); |
| 663 | PPLevelBranchCount.push_back(0); |
| 664 | } |
| 665 | PPChainBranchIndex.push(0); |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 666 | bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0; |
| 667 | conditionalCompilationCondition(Unreachable || Skip); |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 668 | } |
| 669 | |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 670 | void UnwrappedLineParser::conditionalCompilationAlternative() { |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 671 | if (!PPStack.empty()) |
| 672 | PPStack.pop_back(); |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 673 | assert(PPBranchLevel < (int)PPLevelBranchIndex.size()); |
| 674 | if (!PPChainBranchIndex.empty()) |
| 675 | ++PPChainBranchIndex.top(); |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 676 | conditionalCompilationCondition( |
| 677 | PPBranchLevel >= 0 && !PPChainBranchIndex.empty() && |
| 678 | PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top()); |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 679 | } |
| 680 | |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 681 | void UnwrappedLineParser::conditionalCompilationEnd() { |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 682 | assert(PPBranchLevel < (int)PPLevelBranchIndex.size()); |
| 683 | if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) { |
| 684 | if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) { |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 685 | PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1; |
| 686 | } |
| 687 | } |
Manuel Klimek | 14bd917 | 2014-01-29 08:49:02 +0000 | [diff] [blame] | 688 | // Guard against #endif's without #if. |
Krasimir Georgiev | ad47c90 | 2017-08-30 14:34:57 +0000 | [diff] [blame] | 689 | if (PPBranchLevel > -1) |
Manuel Klimek | 14bd917 | 2014-01-29 08:49:02 +0000 | [diff] [blame] | 690 | --PPBranchLevel; |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 691 | if (!PPChainBranchIndex.empty()) |
| 692 | PPChainBranchIndex.pop(); |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 693 | if (!PPStack.empty()) |
| 694 | PPStack.pop_back(); |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | void UnwrappedLineParser::parsePPIf(bool IfDef) { |
Daniel Jasper | 62703eb | 2017-03-01 11:10:11 +0000 | [diff] [blame] | 698 | bool IfNDef = FormatTok->is(tok::pp_ifndef); |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 699 | nextToken(); |
Daniel Jasper | eab6cd4 | 2017-03-01 10:47:52 +0000 | [diff] [blame] | 700 | bool Unreachable = false; |
| 701 | if (!IfDef && (FormatTok->is(tok::kw_false) || FormatTok->TokenText == "0")) |
| 702 | Unreachable = true; |
Daniel Jasper | 62703eb | 2017-03-01 11:10:11 +0000 | [diff] [blame] | 703 | if (IfDef && !IfNDef && FormatTok->TokenText == "SWIG") |
Daniel Jasper | eab6cd4 | 2017-03-01 10:47:52 +0000 | [diff] [blame] | 704 | Unreachable = true; |
| 705 | conditionalCompilationStart(Unreachable); |
Krasimir Georgiev | ad47c90 | 2017-08-30 14:34:57 +0000 | [diff] [blame] | 706 | FormatToken *IfCondition = FormatTok; |
| 707 | // If there's a #ifndef on the first line, and the only lines before it are |
| 708 | // comments, it could be an include guard. |
| 709 | bool MaybeIncludeGuard = IfNDef; |
| 710 | if (!IncludeGuardRejected && !FoundIncludeGuardStart && MaybeIncludeGuard) { |
| 711 | for (auto &Line : Lines) { |
| 712 | if (!Line.Tokens.front().Tok->is(tok::comment)) { |
| 713 | MaybeIncludeGuard = false; |
| 714 | IncludeGuardRejected = true; |
| 715 | break; |
| 716 | } |
| 717 | } |
| 718 | } |
| 719 | --PPBranchLevel; |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 720 | parsePPUnknown(); |
Krasimir Georgiev | ad47c90 | 2017-08-30 14:34:57 +0000 | [diff] [blame] | 721 | ++PPBranchLevel; |
| 722 | if (!IncludeGuardRejected && !FoundIncludeGuardStart && MaybeIncludeGuard) |
| 723 | IfNdefCondition = IfCondition; |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | void UnwrappedLineParser::parsePPElse() { |
Krasimir Georgiev | ad47c90 | 2017-08-30 14:34:57 +0000 | [diff] [blame] | 727 | // If a potential include guard has an #else, it's not an include guard. |
| 728 | if (FoundIncludeGuardStart && PPBranchLevel == 0) |
| 729 | FoundIncludeGuardStart = false; |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 730 | conditionalCompilationAlternative(); |
Krasimir Georgiev | ad47c90 | 2017-08-30 14:34:57 +0000 | [diff] [blame] | 731 | if (PPBranchLevel > -1) |
| 732 | --PPBranchLevel; |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 733 | parsePPUnknown(); |
Krasimir Georgiev | ad47c90 | 2017-08-30 14:34:57 +0000 | [diff] [blame] | 734 | ++PPBranchLevel; |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | void UnwrappedLineParser::parsePPElIf() { parsePPElse(); } |
| 738 | |
| 739 | void UnwrappedLineParser::parsePPEndIf() { |
| 740 | conditionalCompilationEnd(); |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 741 | parsePPUnknown(); |
Krasimir Georgiev | ad47c90 | 2017-08-30 14:34:57 +0000 | [diff] [blame] | 742 | // If the #endif of a potential include guard is the last thing in the file, |
| 743 | // then we count it as a real include guard and subtract one from every |
| 744 | // preprocessor indent. |
| 745 | unsigned TokenPosition = Tokens->getPosition(); |
| 746 | FormatToken *PeekNext = AllTokens[TokenPosition]; |
Daniel Jasper | 4df130f | 2017-09-04 13:33:52 +0000 | [diff] [blame] | 747 | if (FoundIncludeGuardStart && PPBranchLevel == -1 && PeekNext->is(tok::eof) && |
| 748 | Style.IndentPPDirectives != FormatStyle::PPDIS_None) |
| 749 | for (auto &Line : Lines) |
Krasimir Georgiev | ad47c90 | 2017-08-30 14:34:57 +0000 | [diff] [blame] | 750 | if (Line.InPPDirective && Line.Level > 0) |
| 751 | --Line.Level; |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 752 | } |
| 753 | |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 754 | void UnwrappedLineParser::parsePPDefine() { |
| 755 | nextToken(); |
| 756 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 757 | if (FormatTok->Tok.getKind() != tok::identifier) { |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 758 | parsePPUnknown(); |
| 759 | return; |
| 760 | } |
Krasimir Georgiev | ad47c90 | 2017-08-30 14:34:57 +0000 | [diff] [blame] | 761 | if (IfNdefCondition && IfNdefCondition->TokenText == FormatTok->TokenText) { |
| 762 | FoundIncludeGuardStart = true; |
| 763 | for (auto &Line : Lines) { |
| 764 | if (!Line.Tokens.front().Tok->isOneOf(tok::comment, tok::hash)) { |
| 765 | FoundIncludeGuardStart = false; |
| 766 | break; |
| 767 | } |
| 768 | } |
| 769 | } |
| 770 | IfNdefCondition = nullptr; |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 771 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 772 | if (FormatTok->Tok.getKind() == tok::l_paren && |
| 773 | FormatTok->WhitespaceRange.getBegin() == |
| 774 | FormatTok->WhitespaceRange.getEnd()) { |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 775 | parseParens(); |
| 776 | } |
Krasimir Georgiev | ad47c90 | 2017-08-30 14:34:57 +0000 | [diff] [blame] | 777 | if (Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash) |
| 778 | Line->Level += PPBranchLevel + 1; |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 779 | addUnwrappedLine(); |
Krasimir Georgiev | ad47c90 | 2017-08-30 14:34:57 +0000 | [diff] [blame] | 780 | ++Line->Level; |
Manuel Klimek | 1b89629 | 2013-01-07 09:34:28 +0000 | [diff] [blame] | 781 | |
| 782 | // Errors during a preprocessor directive can only affect the layout of the |
| 783 | // preprocessor directive, and thus we ignore them. An alternative approach |
| 784 | // would be to use the same approach we use on the file level (no |
| 785 | // re-indentation if there was a structural error) within the macro |
| 786 | // definition. |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 787 | parseFile(); |
| 788 | } |
| 789 | |
| 790 | void UnwrappedLineParser::parsePPUnknown() { |
Manuel Klimek | a71e5d8 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 791 | do { |
Manuel Klimek | a71e5d8 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 792 | nextToken(); |
| 793 | } while (!eof()); |
Krasimir Georgiev | ad47c90 | 2017-08-30 14:34:57 +0000 | [diff] [blame] | 794 | if (Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash) |
| 795 | Line->Level += PPBranchLevel + 1; |
Manuel Klimek | a71e5d8 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 796 | addUnwrappedLine(); |
Krasimir Georgiev | ad47c90 | 2017-08-30 14:34:57 +0000 | [diff] [blame] | 797 | IfNdefCondition = nullptr; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 798 | } |
| 799 | |
Alexander Kornienko | a04e5e2 | 2013-04-09 16:15:19 +0000 | [diff] [blame] | 800 | // Here we blacklist certain tokens that are not usually the first token in an |
| 801 | // unwrapped line. This is used in attempt to distinguish macro calls without |
| 802 | // trailing semicolons from other constructs split to several lines. |
Benjamin Kramer | 8407df7 | 2015-03-09 16:47:52 +0000 | [diff] [blame] | 803 | static bool tokenCanStartNewLine(const clang::Token &Tok) { |
Alexander Kornienko | a04e5e2 | 2013-04-09 16:15:19 +0000 | [diff] [blame] | 804 | // Semicolon can be a null-statement, l_square can be a start of a macro or |
| 805 | // a C++11 attribute, but this doesn't seem to be common. |
| 806 | return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) && |
| 807 | Tok.isNot(tok::l_square) && |
| 808 | // Tokens that can only be used as binary operators and a part of |
| 809 | // overloaded operator names. |
| 810 | Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) && |
| 811 | Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) && |
| 812 | Tok.isNot(tok::less) && Tok.isNot(tok::greater) && |
| 813 | Tok.isNot(tok::slash) && Tok.isNot(tok::percent) && |
| 814 | Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) && |
| 815 | Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) && |
| 816 | Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) && |
| 817 | Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) && |
| 818 | Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) && |
| 819 | Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) && |
| 820 | Tok.isNot(tok::lesslessequal) && |
| 821 | // Colon is used in labels, base class lists, initializer lists, |
| 822 | // range-based for loops, ternary operator, but should never be the |
| 823 | // first token in an unwrapped line. |
Daniel Jasper | 5ebb2f3 | 2014-05-21 13:08:17 +0000 | [diff] [blame] | 824 | Tok.isNot(tok::colon) && |
| 825 | // 'noexcept' is a trailing annotation. |
| 826 | Tok.isNot(tok::kw_noexcept); |
Alexander Kornienko | a04e5e2 | 2013-04-09 16:15:19 +0000 | [diff] [blame] | 827 | } |
| 828 | |
Martin Probst | 533965c | 2016-04-19 18:19:06 +0000 | [diff] [blame] | 829 | static bool mustBeJSIdent(const AdditionalKeywords &Keywords, |
| 830 | const FormatToken *FormatTok) { |
Daniel Jasper | 1dcbbcfc | 2016-03-14 19:21:36 +0000 | [diff] [blame] | 831 | // FIXME: This returns true for C/C++ keywords like 'struct'. |
| 832 | return FormatTok->is(tok::identifier) && |
| 833 | (FormatTok->Tok.getIdentifierInfo() == nullptr || |
Martin Probst | 3dbbefa | 2016-11-10 16:21:02 +0000 | [diff] [blame] | 834 | !FormatTok->isOneOf( |
| 835 | Keywords.kw_in, Keywords.kw_of, Keywords.kw_as, Keywords.kw_async, |
| 836 | Keywords.kw_await, Keywords.kw_yield, Keywords.kw_finally, |
| 837 | Keywords.kw_function, Keywords.kw_import, Keywords.kw_is, |
| 838 | Keywords.kw_let, Keywords.kw_var, tok::kw_const, |
| 839 | Keywords.kw_abstract, Keywords.kw_extends, Keywords.kw_implements, |
Manuel Klimek | 89628f6 | 2017-09-20 09:51:03 +0000 | [diff] [blame] | 840 | Keywords.kw_instanceof, Keywords.kw_interface, Keywords.kw_throws, |
| 841 | Keywords.kw_from)); |
Daniel Jasper | 1dcbbcfc | 2016-03-14 19:21:36 +0000 | [diff] [blame] | 842 | } |
| 843 | |
Martin Probst | 533965c | 2016-04-19 18:19:06 +0000 | [diff] [blame] | 844 | static bool mustBeJSIdentOrValue(const AdditionalKeywords &Keywords, |
| 845 | const FormatToken *FormatTok) { |
Martin Probst | b9316ff | 2016-09-18 17:21:52 +0000 | [diff] [blame] | 846 | return FormatTok->Tok.isLiteral() || |
| 847 | FormatTok->isOneOf(tok::kw_true, tok::kw_false) || |
| 848 | mustBeJSIdent(Keywords, FormatTok); |
Martin Probst | 533965c | 2016-04-19 18:19:06 +0000 | [diff] [blame] | 849 | } |
| 850 | |
Daniel Jasper | 1dcbbcfc | 2016-03-14 19:21:36 +0000 | [diff] [blame] | 851 | // isJSDeclOrStmt returns true if |FormatTok| starts a declaration or statement |
| 852 | // when encountered after a value (see mustBeJSIdentOrValue). |
| 853 | static bool isJSDeclOrStmt(const AdditionalKeywords &Keywords, |
| 854 | const FormatToken *FormatTok) { |
| 855 | return FormatTok->isOneOf( |
Martin Probst | 5f8445b | 2016-04-24 22:05:09 +0000 | [diff] [blame] | 856 | tok::kw_return, Keywords.kw_yield, |
Daniel Jasper | 1dcbbcfc | 2016-03-14 19:21:36 +0000 | [diff] [blame] | 857 | // conditionals |
| 858 | tok::kw_if, tok::kw_else, |
| 859 | // loops |
| 860 | tok::kw_for, tok::kw_while, tok::kw_do, tok::kw_continue, tok::kw_break, |
| 861 | // switch/case |
| 862 | tok::kw_switch, tok::kw_case, |
| 863 | // exceptions |
| 864 | tok::kw_throw, tok::kw_try, tok::kw_catch, Keywords.kw_finally, |
| 865 | // declaration |
| 866 | tok::kw_const, tok::kw_class, Keywords.kw_var, Keywords.kw_let, |
Martin Probst | 5f8445b | 2016-04-24 22:05:09 +0000 | [diff] [blame] | 867 | Keywords.kw_async, Keywords.kw_function, |
| 868 | // import/export |
| 869 | Keywords.kw_import, tok::kw_export); |
Daniel Jasper | 1dcbbcfc | 2016-03-14 19:21:36 +0000 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | // readTokenWithJavaScriptASI reads the next token and terminates the current |
| 873 | // line if JavaScript Automatic Semicolon Insertion must |
| 874 | // happen between the current token and the next token. |
| 875 | // |
| 876 | // This method is conservative - it cannot cover all edge cases of JavaScript, |
| 877 | // but only aims to correctly handle certain well known cases. It *must not* |
| 878 | // return true in speculative cases. |
| 879 | void UnwrappedLineParser::readTokenWithJavaScriptASI() { |
| 880 | FormatToken *Previous = FormatTok; |
| 881 | readToken(); |
| 882 | FormatToken *Next = FormatTok; |
| 883 | |
| 884 | bool IsOnSameLine = |
| 885 | CommentsBeforeNextToken.empty() |
| 886 | ? Next->NewlinesBefore == 0 |
| 887 | : CommentsBeforeNextToken.front()->NewlinesBefore == 0; |
| 888 | if (IsOnSameLine) |
| 889 | return; |
| 890 | |
| 891 | bool PreviousMustBeValue = mustBeJSIdentOrValue(Keywords, Previous); |
Martin Probst | 717f6dc | 2016-10-21 05:11:38 +0000 | [diff] [blame] | 892 | bool PreviousStartsTemplateExpr = |
| 893 | Previous->is(TT_TemplateString) && Previous->TokenText.endswith("${"); |
Martin Probst | bbffeac | 2016-04-11 07:35:57 +0000 | [diff] [blame] | 894 | if (PreviousMustBeValue && Line && Line->Tokens.size() > 1) { |
| 895 | // If the token before the previous one is an '@', the previous token is an |
| 896 | // annotation and can precede another identifier/value. |
Benjamin Kramer | 5ffc24e | 2016-04-11 12:19:19 +0000 | [diff] [blame] | 897 | const FormatToken *PrePrevious = std::prev(Line->Tokens.end(), 2)->Tok; |
Martin Probst | bbffeac | 2016-04-11 07:35:57 +0000 | [diff] [blame] | 898 | if (PrePrevious->is(tok::at)) |
| 899 | return; |
| 900 | } |
Daniel Jasper | 1dcbbcfc | 2016-03-14 19:21:36 +0000 | [diff] [blame] | 901 | if (Next->is(tok::exclaim) && PreviousMustBeValue) |
Martin Probst | d40bca4 | 2017-01-09 08:56:36 +0000 | [diff] [blame] | 902 | return addUnwrappedLine(); |
Daniel Jasper | 1dcbbcfc | 2016-03-14 19:21:36 +0000 | [diff] [blame] | 903 | bool NextMustBeValue = mustBeJSIdentOrValue(Keywords, Next); |
Martin Probst | 717f6dc | 2016-10-21 05:11:38 +0000 | [diff] [blame] | 904 | bool NextEndsTemplateExpr = |
| 905 | Next->is(TT_TemplateString) && Next->TokenText.startswith("}"); |
| 906 | if (NextMustBeValue && !NextEndsTemplateExpr && !PreviousStartsTemplateExpr && |
| 907 | (PreviousMustBeValue || |
| 908 | Previous->isOneOf(tok::r_square, tok::r_paren, tok::plusplus, |
| 909 | tok::minusminus))) |
Martin Probst | d40bca4 | 2017-01-09 08:56:36 +0000 | [diff] [blame] | 910 | return addUnwrappedLine(); |
Martin Probst | 0a19d43 | 2017-08-09 15:19:16 +0000 | [diff] [blame] | 911 | if ((PreviousMustBeValue || Previous->is(tok::r_paren)) && |
| 912 | isJSDeclOrStmt(Keywords, Next)) |
Martin Probst | d40bca4 | 2017-01-09 08:56:36 +0000 | [diff] [blame] | 913 | return addUnwrappedLine(); |
Daniel Jasper | 1dcbbcfc | 2016-03-14 19:21:36 +0000 | [diff] [blame] | 914 | } |
| 915 | |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 916 | void UnwrappedLineParser::parseStructuralElement() { |
Daniel Jasper | 498f558 | 2015-12-25 08:53:31 +0000 | [diff] [blame] | 917 | assert(!FormatTok->is(tok::l_brace)); |
| 918 | if (Style.Language == FormatStyle::LK_TableGen && |
| 919 | FormatTok->is(tok::pp_include)) { |
| 920 | nextToken(); |
| 921 | if (FormatTok->is(tok::string_literal)) |
| 922 | nextToken(); |
| 923 | addUnwrappedLine(); |
| 924 | return; |
| 925 | } |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 926 | switch (FormatTok->Tok.getKind()) { |
Nico Weber | 04e9f1a | 2013-01-07 19:05:19 +0000 | [diff] [blame] | 927 | case tok::at: |
| 928 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 929 | if (FormatTok->Tok.is(tok::l_brace)) { |
Krasimir Georgiev | 26b144c | 2017-07-03 15:05:14 +0000 | [diff] [blame] | 930 | nextToken(); |
Nico Weber | 372d8dc | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 931 | parseBracedList(); |
| 932 | break; |
| 933 | } |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 934 | switch (FormatTok->Tok.getObjCKeywordID()) { |
Nico Weber | 04e9f1a | 2013-01-07 19:05:19 +0000 | [diff] [blame] | 935 | case tok::objc_public: |
| 936 | case tok::objc_protected: |
| 937 | case tok::objc_package: |
| 938 | case tok::objc_private: |
| 939 | return parseAccessSpecifier(); |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 940 | case tok::objc_interface: |
Nico Weber | 2ce0ac5 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 941 | case tok::objc_implementation: |
| 942 | return parseObjCInterfaceOrImplementation(); |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 943 | case tok::objc_protocol: |
| 944 | return parseObjCProtocol(); |
Nico Weber | d8ffe75 | 2013-01-09 21:42:32 +0000 | [diff] [blame] | 945 | case tok::objc_end: |
| 946 | return; // Handled by the caller. |
Nico Weber | 51306d2 | 2013-01-10 00:25:19 +0000 | [diff] [blame] | 947 | case tok::objc_optional: |
| 948 | case tok::objc_required: |
| 949 | nextToken(); |
| 950 | addUnwrappedLine(); |
| 951 | return; |
Nico Weber | 45c4812 | 2015-06-28 01:06:16 +0000 | [diff] [blame] | 952 | case tok::objc_autoreleasepool: |
| 953 | nextToken(); |
| 954 | if (FormatTok->Tok.is(tok::l_brace)) { |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 955 | if (Style.BraceWrapping.AfterObjCDeclaration) |
Nico Weber | 45c4812 | 2015-06-28 01:06:16 +0000 | [diff] [blame] | 956 | addUnwrappedLine(); |
| 957 | parseBlock(/*MustBeDeclaration=*/false); |
| 958 | } |
| 959 | addUnwrappedLine(); |
| 960 | return; |
Nico Weber | 33381f5 | 2015-02-07 01:57:32 +0000 | [diff] [blame] | 961 | case tok::objc_try: |
| 962 | // This branch isn't strictly necessary (the kw_try case below would |
| 963 | // do this too after the tok::at is parsed above). But be explicit. |
| 964 | parseTryCatch(); |
| 965 | return; |
Nico Weber | 04e9f1a | 2013-01-07 19:05:19 +0000 | [diff] [blame] | 966 | default: |
| 967 | break; |
| 968 | } |
| 969 | break; |
Daniel Jasper | 8f46365 | 2014-08-26 23:15:12 +0000 | [diff] [blame] | 970 | case tok::kw_asm: |
Daniel Jasper | 8f46365 | 2014-08-26 23:15:12 +0000 | [diff] [blame] | 971 | nextToken(); |
| 972 | if (FormatTok->is(tok::l_brace)) { |
Daniel Jasper | c636607 | 2015-05-10 08:42:04 +0000 | [diff] [blame] | 973 | FormatTok->Type = TT_InlineASMBrace; |
Daniel Jasper | 2337f28 | 2015-01-12 10:14:56 +0000 | [diff] [blame] | 974 | nextToken(); |
Daniel Jasper | 4429f14 | 2014-08-27 17:16:46 +0000 | [diff] [blame] | 975 | while (FormatTok && FormatTok->isNot(tok::eof)) { |
Daniel Jasper | 8f46365 | 2014-08-26 23:15:12 +0000 | [diff] [blame] | 976 | if (FormatTok->is(tok::r_brace)) { |
Daniel Jasper | c636607 | 2015-05-10 08:42:04 +0000 | [diff] [blame] | 977 | FormatTok->Type = TT_InlineASMBrace; |
Daniel Jasper | 8f46365 | 2014-08-26 23:15:12 +0000 | [diff] [blame] | 978 | nextToken(); |
Daniel Jasper | 790d4f9 | 2015-05-11 11:59:46 +0000 | [diff] [blame] | 979 | addUnwrappedLine(); |
Daniel Jasper | 8f46365 | 2014-08-26 23:15:12 +0000 | [diff] [blame] | 980 | break; |
| 981 | } |
Daniel Jasper | 2337f28 | 2015-01-12 10:14:56 +0000 | [diff] [blame] | 982 | FormatTok->Finalized = true; |
Daniel Jasper | 8f46365 | 2014-08-26 23:15:12 +0000 | [diff] [blame] | 983 | nextToken(); |
| 984 | } |
| 985 | } |
| 986 | break; |
Alexander Kornienko | 578fdd8 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 987 | case tok::kw_namespace: |
| 988 | parseNamespace(); |
| 989 | return; |
Dmitri Gribenko | 58d64e2 | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 990 | case tok::kw_inline: |
| 991 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 992 | if (FormatTok->Tok.is(tok::kw_namespace)) { |
Dmitri Gribenko | 58d64e2 | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 993 | parseNamespace(); |
| 994 | return; |
| 995 | } |
| 996 | break; |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 997 | case tok::kw_public: |
| 998 | case tok::kw_protected: |
| 999 | case tok::kw_private: |
Daniel Jasper | 8370908 | 2015-02-18 17:14:05 +0000 | [diff] [blame] | 1000 | if (Style.Language == FormatStyle::LK_Java || |
| 1001 | Style.Language == FormatStyle::LK_JavaScript) |
Daniel Jasper | c58c70e | 2014-09-15 11:21:46 +0000 | [diff] [blame] | 1002 | nextToken(); |
| 1003 | else |
| 1004 | parseAccessSpecifier(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1005 | return; |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 1006 | case tok::kw_if: |
| 1007 | parseIfThenElse(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1008 | return; |
Alexander Kornienko | 37d6c94 | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 1009 | case tok::kw_for: |
| 1010 | case tok::kw_while: |
| 1011 | parseForOrWhileLoop(); |
| 1012 | return; |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 1013 | case tok::kw_do: |
| 1014 | parseDoWhile(); |
| 1015 | return; |
| 1016 | case tok::kw_switch: |
Martin Probst | f785fd9 | 2017-08-04 17:07:15 +0000 | [diff] [blame] | 1017 | if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration) |
| 1018 | // 'switch: string' field declaration. |
| 1019 | break; |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 1020 | parseSwitch(); |
| 1021 | return; |
| 1022 | case tok::kw_default: |
Martin Probst | f785fd9 | 2017-08-04 17:07:15 +0000 | [diff] [blame] | 1023 | if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration) |
| 1024 | // 'default: string' field declaration. |
| 1025 | break; |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 1026 | nextToken(); |
| 1027 | parseLabel(); |
| 1028 | return; |
| 1029 | case tok::kw_case: |
Martin Probst | f785fd9 | 2017-08-04 17:07:15 +0000 | [diff] [blame] | 1030 | if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration) |
| 1031 | // 'case: string' field declaration. |
| 1032 | break; |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 1033 | parseCaseLabel(); |
| 1034 | return; |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 1035 | case tok::kw_try: |
Nico Weber | fac2371 | 2015-02-04 15:26:27 +0000 | [diff] [blame] | 1036 | case tok::kw___try: |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 1037 | parseTryCatch(); |
| 1038 | return; |
Manuel Klimek | ae610d1 | 2013-01-21 14:32:05 +0000 | [diff] [blame] | 1039 | case tok::kw_extern: |
| 1040 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1041 | if (FormatTok->Tok.is(tok::string_literal)) { |
Manuel Klimek | ae610d1 | 2013-01-21 14:32:05 +0000 | [diff] [blame] | 1042 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1043 | if (FormatTok->Tok.is(tok::l_brace)) { |
Krasimir Georgiev | d6ce937 | 2017-09-15 11:23:50 +0000 | [diff] [blame] | 1044 | if (Style.BraceWrapping.AfterExternBlock) { |
| 1045 | addUnwrappedLine(); |
| 1046 | parseBlock(/*MustBeDeclaration=*/true); |
| 1047 | } else { |
| 1048 | parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false); |
| 1049 | } |
Manuel Klimek | ae610d1 | 2013-01-21 14:32:05 +0000 | [diff] [blame] | 1050 | addUnwrappedLine(); |
| 1051 | return; |
| 1052 | } |
| 1053 | } |
Daniel Jasper | e1e4319 | 2014-04-01 12:55:11 +0000 | [diff] [blame] | 1054 | break; |
Daniel Jasper | fca735c | 2015-02-19 16:14:18 +0000 | [diff] [blame] | 1055 | case tok::kw_export: |
| 1056 | if (Style.Language == FormatStyle::LK_JavaScript) { |
| 1057 | parseJavaScriptEs6ImportExport(); |
| 1058 | return; |
| 1059 | } |
| 1060 | break; |
Daniel Jasper | e1e4319 | 2014-04-01 12:55:11 +0000 | [diff] [blame] | 1061 | case tok::identifier: |
Daniel Jasper | 66cb8c5 | 2015-05-04 09:22:29 +0000 | [diff] [blame] | 1062 | if (FormatTok->is(TT_ForEachMacro)) { |
Daniel Jasper | e1e4319 | 2014-04-01 12:55:11 +0000 | [diff] [blame] | 1063 | parseForOrWhileLoop(); |
| 1064 | return; |
| 1065 | } |
Birunthan Mohanathas | b001a0b | 2015-07-03 17:25:16 +0000 | [diff] [blame] | 1066 | if (FormatTok->is(TT_MacroBlockBegin)) { |
| 1067 | parseBlock(/*MustBeDeclaration=*/false, /*AddLevel=*/true, |
| 1068 | /*MunchSemi=*/false); |
| 1069 | return; |
| 1070 | } |
Daniel Jasper | 3d5a7d6 | 2016-06-20 18:20:38 +0000 | [diff] [blame] | 1071 | if (FormatTok->is(Keywords.kw_import)) { |
| 1072 | if (Style.Language == FormatStyle::LK_JavaScript) { |
| 1073 | parseJavaScriptEs6ImportExport(); |
| 1074 | return; |
| 1075 | } |
| 1076 | if (Style.Language == FormatStyle::LK_Proto) { |
| 1077 | nextToken(); |
Daniel Jasper | 8b61d14 | 2016-06-20 20:39:53 +0000 | [diff] [blame] | 1078 | if (FormatTok->is(tok::kw_public)) |
| 1079 | nextToken(); |
Daniel Jasper | 3d5a7d6 | 2016-06-20 18:20:38 +0000 | [diff] [blame] | 1080 | if (!FormatTok->is(tok::string_literal)) |
| 1081 | return; |
| 1082 | nextToken(); |
| 1083 | if (FormatTok->is(tok::semi)) |
| 1084 | nextToken(); |
| 1085 | addUnwrappedLine(); |
| 1086 | return; |
| 1087 | } |
Daniel Jasper | 354aa51 | 2015-02-19 16:07:32 +0000 | [diff] [blame] | 1088 | } |
Daniel Jasper | 1dbc210 | 2017-03-31 13:30:24 +0000 | [diff] [blame] | 1089 | if (Style.isCpp() && |
Daniel Jasper | 72b3357 | 2017-03-31 12:04:37 +0000 | [diff] [blame] | 1090 | FormatTok->isOneOf(Keywords.kw_signals, Keywords.kw_qsignals, |
Daniel Jasper | a00de63 | 2015-12-01 12:05:04 +0000 | [diff] [blame] | 1091 | Keywords.kw_slots, Keywords.kw_qslots)) { |
Daniel Jasper | de0d1f3 | 2015-04-24 07:50:34 +0000 | [diff] [blame] | 1092 | nextToken(); |
| 1093 | if (FormatTok->is(tok::colon)) { |
| 1094 | nextToken(); |
| 1095 | addUnwrappedLine(); |
Daniel Jasper | 3134383 | 2016-07-27 10:13:24 +0000 | [diff] [blame] | 1096 | return; |
Daniel Jasper | de0d1f3 | 2015-04-24 07:50:34 +0000 | [diff] [blame] | 1097 | } |
Daniel Jasper | 5339540 | 2015-04-07 15:04:40 +0000 | [diff] [blame] | 1098 | } |
Manuel Klimek | ae610d1 | 2013-01-21 14:32:05 +0000 | [diff] [blame] | 1099 | // In all other cases, parse the declaration. |
| 1100 | break; |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 1101 | default: |
| 1102 | break; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1103 | } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1104 | do { |
Manuel Klimek | e411aa8 | 2017-09-20 09:29:37 +0000 | [diff] [blame] | 1105 | const FormatToken *Previous = FormatTok->Previous; |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1106 | switch (FormatTok->Tok.getKind()) { |
Nico Weber | 372d8dc | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 1107 | case tok::at: |
| 1108 | nextToken(); |
Krasimir Georgiev | 26b144c | 2017-07-03 15:05:14 +0000 | [diff] [blame] | 1109 | if (FormatTok->Tok.is(tok::l_brace)) { |
| 1110 | nextToken(); |
Nico Weber | 372d8dc | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 1111 | parseBracedList(); |
Krasimir Georgiev | 26b144c | 2017-07-03 15:05:14 +0000 | [diff] [blame] | 1112 | } |
Nico Weber | 372d8dc | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 1113 | break; |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 1114 | case tok::kw_enum: |
Daniel Jasper | a7900ad | 2016-05-08 18:12:22 +0000 | [diff] [blame] | 1115 | // Ignore if this is part of "template <enum ...". |
| 1116 | if (Previous && Previous->is(tok::less)) { |
| 1117 | nextToken(); |
| 1118 | break; |
| 1119 | } |
| 1120 | |
Daniel Jasper | 90cf380 | 2015-06-17 09:44:02 +0000 | [diff] [blame] | 1121 | // parseEnum falls through and does not yet add an unwrapped line as an |
| 1122 | // enum definition can start a structural element. |
Daniel Jasper | 6f5a193 | 2015-12-29 08:54:23 +0000 | [diff] [blame] | 1123 | if (!parseEnum()) |
| 1124 | break; |
Daniel Jasper | c6dd273 | 2015-07-16 14:25:43 +0000 | [diff] [blame] | 1125 | // This only applies for C++. |
Daniel Jasper | 1dbc210 | 2017-03-31 13:30:24 +0000 | [diff] [blame] | 1126 | if (!Style.isCpp()) { |
Daniel Jasper | 90cf380 | 2015-06-17 09:44:02 +0000 | [diff] [blame] | 1127 | addUnwrappedLine(); |
| 1128 | return; |
| 1129 | } |
Manuel Klimek | 2cec019 | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 1130 | break; |
Daniel Jasper | a88f80a | 2014-01-30 14:38:37 +0000 | [diff] [blame] | 1131 | case tok::kw_typedef: |
| 1132 | nextToken(); |
Daniel Jasper | 31f6c54 | 2014-12-05 10:42:21 +0000 | [diff] [blame] | 1133 | if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS, |
| 1134 | Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS)) |
Daniel Jasper | a88f80a | 2014-01-30 14:38:37 +0000 | [diff] [blame] | 1135 | parseEnum(); |
| 1136 | break; |
Alexander Kornienko | 1231e06 | 2013-01-16 11:43:46 +0000 | [diff] [blame] | 1137 | case tok::kw_struct: |
| 1138 | case tok::kw_union: |
Manuel Klimek | 28cacc7 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 1139 | case tok::kw_class: |
Daniel Jasper | 910807d | 2015-06-12 04:52:02 +0000 | [diff] [blame] | 1140 | // parseRecord falls through and does not yet add an unwrapped line as a |
| 1141 | // record declaration or definition can start a structural element. |
Manuel Klimek | e01bab5 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 1142 | parseRecord(); |
Daniel Jasper | 910807d | 2015-06-12 04:52:02 +0000 | [diff] [blame] | 1143 | // This does not apply for Java and JavaScript. |
| 1144 | if (Style.Language == FormatStyle::LK_Java || |
| 1145 | Style.Language == FormatStyle::LK_JavaScript) { |
Daniel Jasper | d5ec65b | 2016-01-08 07:06:07 +0000 | [diff] [blame] | 1146 | if (FormatTok->is(tok::semi)) |
| 1147 | nextToken(); |
Daniel Jasper | 910807d | 2015-06-12 04:52:02 +0000 | [diff] [blame] | 1148 | addUnwrappedLine(); |
| 1149 | return; |
| 1150 | } |
Manuel Klimek | e01bab5 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 1151 | break; |
Daniel Jasper | e5d7486 | 2014-11-26 08:17:08 +0000 | [diff] [blame] | 1152 | case tok::period: |
| 1153 | nextToken(); |
| 1154 | // In Java, classes have an implicit static member "class". |
| 1155 | if (Style.Language == FormatStyle::LK_Java && FormatTok && |
| 1156 | FormatTok->is(tok::kw_class)) |
| 1157 | nextToken(); |
Daniel Jasper | ba52fcb | 2015-09-28 14:29:45 +0000 | [diff] [blame] | 1158 | if (Style.Language == FormatStyle::LK_JavaScript && FormatTok && |
| 1159 | FormatTok->Tok.getIdentifierInfo()) |
| 1160 | // JavaScript only has pseudo keywords, all keywords are allowed to |
| 1161 | // appear in "IdentifierName" positions. See http://es5.github.io/#x7.6 |
| 1162 | nextToken(); |
Daniel Jasper | e5d7486 | 2014-11-26 08:17:08 +0000 | [diff] [blame] | 1163 | break; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1164 | case tok::semi: |
| 1165 | nextToken(); |
| 1166 | addUnwrappedLine(); |
| 1167 | return; |
Alexander Kornienko | 1231e06 | 2013-01-16 11:43:46 +0000 | [diff] [blame] | 1168 | case tok::r_brace: |
| 1169 | addUnwrappedLine(); |
| 1170 | return; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1171 | case tok::l_paren: |
| 1172 | parseParens(); |
| 1173 | break; |
Daniel Jasper | 5af04a4 | 2015-10-07 03:43:10 +0000 | [diff] [blame] | 1174 | case tok::kw_operator: |
| 1175 | nextToken(); |
| 1176 | if (FormatTok->isBinaryOperator()) |
| 1177 | nextToken(); |
| 1178 | break; |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 1179 | case tok::caret: |
| 1180 | nextToken(); |
Daniel Jasper | 395193c | 2014-03-28 07:48:59 +0000 | [diff] [blame] | 1181 | if (FormatTok->Tok.isAnyIdentifier() || |
| 1182 | FormatTok->isSimpleTypeSpecifier()) |
| 1183 | nextToken(); |
| 1184 | if (FormatTok->is(tok::l_paren)) |
| 1185 | parseParens(); |
| 1186 | if (FormatTok->is(tok::l_brace)) |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 1187 | parseChildBlock(); |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 1188 | break; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1189 | case tok::l_brace: |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 1190 | if (!tryToParseBracedList()) { |
| 1191 | // A block outside of parentheses must be the last part of a |
| 1192 | // structural element. |
| 1193 | // FIXME: Figure out cases where this is not true, and add projections |
| 1194 | // for them (the one we know is missing are lambdas). |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 1195 | if (Style.BraceWrapping.AfterFunction) |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 1196 | addUnwrappedLine(); |
Alexander Kornienko | 3cfa973 | 2013-11-20 16:33:05 +0000 | [diff] [blame] | 1197 | FormatTok->Type = TT_FunctionLBrace; |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 1198 | parseBlock(/*MustBeDeclaration=*/false); |
Manuel Klimek | a8eb914 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 1199 | addUnwrappedLine(); |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 1200 | return; |
| 1201 | } |
| 1202 | // Otherwise this was a braced init list, and the structural |
| 1203 | // element continues. |
| 1204 | break; |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 1205 | case tok::kw_try: |
| 1206 | // We arrive here when parsing function-try blocks. |
| 1207 | parseTryCatch(); |
| 1208 | return; |
Daniel Jasper | 40e1921 | 2013-05-29 13:16:10 +0000 | [diff] [blame] | 1209 | case tok::identifier: { |
Birunthan Mohanathas | b001a0b | 2015-07-03 17:25:16 +0000 | [diff] [blame] | 1210 | if (FormatTok->is(TT_MacroBlockEnd)) { |
| 1211 | addUnwrappedLine(); |
| 1212 | return; |
| 1213 | } |
| 1214 | |
Martin Probst | 973ff79 | 2017-04-27 13:07:24 +0000 | [diff] [blame] | 1215 | // Function declarations (as opposed to function expressions) are parsed |
| 1216 | // on their own unwrapped line by continuing this loop. Function |
| 1217 | // expressions (functions that are not on their own line) must not create |
| 1218 | // a new unwrapped line, so they are special cased below. |
| 1219 | size_t TokenCount = Line->Tokens.size(); |
Daniel Jasper | 9326f91 | 2015-05-05 08:40:32 +0000 | [diff] [blame] | 1220 | if (Style.Language == FormatStyle::LK_JavaScript && |
Martin Probst | 973ff79 | 2017-04-27 13:07:24 +0000 | [diff] [blame] | 1221 | FormatTok->is(Keywords.kw_function) && |
| 1222 | (TokenCount > 1 || (TokenCount == 1 && !Line->Tokens.front().Tok->is( |
| 1223 | Keywords.kw_async)))) { |
Daniel Jasper | 069e5f4 | 2014-05-20 11:14:57 +0000 | [diff] [blame] | 1224 | tryToParseJSFunction(); |
| 1225 | break; |
| 1226 | } |
Daniel Jasper | 9326f91 | 2015-05-05 08:40:32 +0000 | [diff] [blame] | 1227 | if ((Style.Language == FormatStyle::LK_JavaScript || |
| 1228 | Style.Language == FormatStyle::LK_Java) && |
| 1229 | FormatTok->is(Keywords.kw_interface)) { |
Martin Probst | 1e8261e | 2016-04-19 18:18:59 +0000 | [diff] [blame] | 1230 | if (Style.Language == FormatStyle::LK_JavaScript) { |
| 1231 | // In JavaScript/TypeScript, "interface" can be used as a standalone |
| 1232 | // identifier, e.g. in `var interface = 1;`. If "interface" is |
| 1233 | // followed by another identifier, it is very like to be an actual |
| 1234 | // interface declaration. |
| 1235 | unsigned StoredPosition = Tokens->getPosition(); |
| 1236 | FormatToken *Next = Tokens->getNextToken(); |
| 1237 | FormatTok = Tokens->setPosition(StoredPosition); |
Martin Probst | 533965c | 2016-04-19 18:19:06 +0000 | [diff] [blame] | 1238 | if (Next && !mustBeJSIdent(Keywords, Next)) { |
Martin Probst | 1e8261e | 2016-04-19 18:18:59 +0000 | [diff] [blame] | 1239 | nextToken(); |
| 1240 | break; |
| 1241 | } |
| 1242 | } |
Daniel Jasper | 9326f91 | 2015-05-05 08:40:32 +0000 | [diff] [blame] | 1243 | parseRecord(); |
Daniel Jasper | 259188b | 2015-06-12 04:56:34 +0000 | [diff] [blame] | 1244 | addUnwrappedLine(); |
Daniel Jasper | 5c235c0 | 2015-07-06 14:26:04 +0000 | [diff] [blame] | 1245 | return; |
Daniel Jasper | 9326f91 | 2015-05-05 08:40:32 +0000 | [diff] [blame] | 1246 | } |
| 1247 | |
Daniel Jasper | 1dcbbcfc | 2016-03-14 19:21:36 +0000 | [diff] [blame] | 1248 | // See if the following token should start a new unwrapped line. |
Daniel Jasper | 9326f91 | 2015-05-05 08:40:32 +0000 | [diff] [blame] | 1249 | StringRef Text = FormatTok->TokenText; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1250 | nextToken(); |
Daniel Jasper | 8370908 | 2015-02-18 17:14:05 +0000 | [diff] [blame] | 1251 | if (Line->Tokens.size() == 1 && |
| 1252 | // JS doesn't have macros, and within classes colons indicate fields, |
| 1253 | // not labels. |
Daniel Jasper | 676e516 | 2015-04-07 14:36:33 +0000 | [diff] [blame] | 1254 | Style.Language != FormatStyle::LK_JavaScript) { |
| 1255 | if (FormatTok->Tok.is(tok::colon) && !Line->MustBeDeclaration) { |
Daniel Jasper | 4060947 | 2016-04-06 15:02:46 +0000 | [diff] [blame] | 1256 | Line->Tokens.begin()->Tok->MustBreakBefore = true; |
Alexander Kornienko | de64427 | 2013-04-08 22:16:06 +0000 | [diff] [blame] | 1257 | parseLabel(); |
| 1258 | return; |
| 1259 | } |
Daniel Jasper | 680b09b | 2014-11-05 10:48:04 +0000 | [diff] [blame] | 1260 | // Recognize function-like macro usages without trailing semicolon as |
Daniel Jasper | 8370908 | 2015-02-18 17:14:05 +0000 | [diff] [blame] | 1261 | // well as free-standing macros like Q_OBJECT. |
Daniel Jasper | 680b09b | 2014-11-05 10:48:04 +0000 | [diff] [blame] | 1262 | bool FunctionLike = FormatTok->is(tok::l_paren); |
| 1263 | if (FunctionLike) |
Alexander Kornienko | de64427 | 2013-04-08 22:16:06 +0000 | [diff] [blame] | 1264 | parseParens(); |
Daniel Jasper | e60cba1 | 2015-05-13 11:35:53 +0000 | [diff] [blame] | 1265 | |
| 1266 | bool FollowedByNewline = |
| 1267 | CommentsBeforeNextToken.empty() |
| 1268 | ? FormatTok->NewlinesBefore > 0 |
| 1269 | : CommentsBeforeNextToken.front()->NewlinesBefore > 0; |
| 1270 | |
Daniel Jasper | e6fcf7d | 2015-06-17 13:08:06 +0000 | [diff] [blame] | 1271 | if (FollowedByNewline && (Text.size() >= 5 || FunctionLike) && |
Daniel Jasper | 680b09b | 2014-11-05 10:48:04 +0000 | [diff] [blame] | 1272 | tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) { |
Daniel Jasper | 40e1921 | 2013-05-29 13:16:10 +0000 | [diff] [blame] | 1273 | addUnwrappedLine(); |
Daniel Jasper | 41a0f78 | 2013-05-29 14:09:17 +0000 | [diff] [blame] | 1274 | return; |
Alexander Kornienko | de64427 | 2013-04-08 22:16:06 +0000 | [diff] [blame] | 1275 | } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1276 | } |
| 1277 | break; |
Daniel Jasper | 40e1921 | 2013-05-29 13:16:10 +0000 | [diff] [blame] | 1278 | } |
Daniel Jasper | e25509f | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 1279 | case tok::equal: |
Manuel Klimek | 79e0608 | 2015-05-21 12:23:34 +0000 | [diff] [blame] | 1280 | // Fat arrows (=>) have tok::TokenKind tok::equal but TokenType |
| 1281 | // TT_JsFatArrow. The always start an expression or a child block if |
| 1282 | // followed by a curly. |
| 1283 | if (FormatTok->is(TT_JsFatArrow)) { |
| 1284 | nextToken(); |
Daniel Jasper | be520bd | 2015-05-31 08:51:54 +0000 | [diff] [blame] | 1285 | if (FormatTok->is(tok::l_brace)) |
Manuel Klimek | 79e0608 | 2015-05-21 12:23:34 +0000 | [diff] [blame] | 1286 | parseChildBlock(); |
Manuel Klimek | 79e0608 | 2015-05-21 12:23:34 +0000 | [diff] [blame] | 1287 | break; |
| 1288 | } |
| 1289 | |
Daniel Jasper | e25509f | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 1290 | nextToken(); |
Krasimir Georgiev | 26b144c | 2017-07-03 15:05:14 +0000 | [diff] [blame] | 1291 | if (FormatTok->Tok.is(tok::l_brace)) { |
| 1292 | nextToken(); |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 1293 | parseBracedList(); |
Krasimir Georgiev | 26b144c | 2017-07-03 15:05:14 +0000 | [diff] [blame] | 1294 | } else if (Style.Language == FormatStyle::LK_Proto && |
Manuel Klimek | 89628f6 | 2017-09-20 09:51:03 +0000 | [diff] [blame] | 1295 | FormatTok->Tok.is(tok::less)) { |
Krasimir Georgiev | 26b144c | 2017-07-03 15:05:14 +0000 | [diff] [blame] | 1296 | nextToken(); |
Krasimir Georgiev | 0b41fcb | 2017-06-27 13:58:41 +0000 | [diff] [blame] | 1297 | parseBracedList(/*ContinueOnSemicolons=*/false, |
| 1298 | /*ClosingBraceKind=*/tok::greater); |
Krasimir Georgiev | 26b144c | 2017-07-03 15:05:14 +0000 | [diff] [blame] | 1299 | } |
Daniel Jasper | e25509f | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 1300 | break; |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 1301 | case tok::l_square: |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 1302 | parseSquare(); |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 1303 | break; |
Daniel Jasper | 6acf513 | 2015-03-12 14:44:29 +0000 | [diff] [blame] | 1304 | case tok::kw_new: |
| 1305 | parseNew(); |
| 1306 | break; |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 1307 | default: |
| 1308 | nextToken(); |
| 1309 | break; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1310 | } |
| 1311 | } while (!eof()); |
| 1312 | } |
| 1313 | |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 1314 | bool UnwrappedLineParser::tryToParseLambda() { |
Daniel Jasper | 1dbc210 | 2017-03-31 13:30:24 +0000 | [diff] [blame] | 1315 | if (!Style.isCpp()) { |
Daniel Jasper | 1feab0f | 2015-06-02 15:31:37 +0000 | [diff] [blame] | 1316 | nextToken(); |
| 1317 | return false; |
| 1318 | } |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1319 | assert(FormatTok->is(tok::l_square)); |
| 1320 | FormatToken &LSquare = *FormatTok; |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 1321 | if (!tryToParseLambdaIntroducer()) |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 1322 | return false; |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 1323 | |
Alexander Kornienko | c2ee9cf | 2014-03-13 13:59:48 +0000 | [diff] [blame] | 1324 | while (FormatTok->isNot(tok::l_brace)) { |
Daniel Jasper | cb51cf4 | 2014-01-16 09:11:55 +0000 | [diff] [blame] | 1325 | if (FormatTok->isSimpleTypeSpecifier()) { |
| 1326 | nextToken(); |
| 1327 | continue; |
| 1328 | } |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 1329 | switch (FormatTok->Tok.getKind()) { |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 1330 | case tok::l_brace: |
| 1331 | break; |
| 1332 | case tok::l_paren: |
| 1333 | parseParens(); |
| 1334 | break; |
Daniel Jasper | bcb55ee | 2014-11-21 14:08:38 +0000 | [diff] [blame] | 1335 | case tok::amp: |
| 1336 | case tok::star: |
| 1337 | case tok::kw_const: |
Daniel Jasper | 3431b75 | 2014-12-08 13:22:37 +0000 | [diff] [blame] | 1338 | case tok::comma: |
Daniel Jasper | cb51cf4 | 2014-01-16 09:11:55 +0000 | [diff] [blame] | 1339 | case tok::less: |
| 1340 | case tok::greater: |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 1341 | case tok::identifier: |
Daniel Jasper | 5eaa009 | 2015-08-13 13:37:08 +0000 | [diff] [blame] | 1342 | case tok::numeric_constant: |
Daniel Jasper | 1067ab0 | 2014-02-11 10:16:55 +0000 | [diff] [blame] | 1343 | case tok::coloncolon: |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 1344 | case tok::kw_mutable: |
Daniel Jasper | 81a2078 | 2014-03-10 10:02:02 +0000 | [diff] [blame] | 1345 | nextToken(); |
| 1346 | break; |
Daniel Jasper | cb51cf4 | 2014-01-16 09:11:55 +0000 | [diff] [blame] | 1347 | case tok::arrow: |
Daniel Jasper | 6f2b88a | 2015-06-05 13:18:09 +0000 | [diff] [blame] | 1348 | FormatTok->Type = TT_LambdaArrow; |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 1349 | nextToken(); |
| 1350 | break; |
| 1351 | default: |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 1352 | return true; |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 1353 | } |
| 1354 | } |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1355 | LSquare.Type = TT_LambdaLSquare; |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 1356 | parseChildBlock(); |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 1357 | return true; |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
| 1360 | bool UnwrappedLineParser::tryToParseLambdaIntroducer() { |
Manuel Klimek | 89628f6 | 2017-09-20 09:51:03 +0000 | [diff] [blame] | 1361 | const FormatToken *Previous = FormatTok->Previous; |
Manuel Klimek | 9f0a4e5 | 2017-09-19 09:59:30 +0000 | [diff] [blame] | 1362 | if (Previous && |
| 1363 | (Previous->isOneOf(tok::identifier, tok::kw_operator, tok::kw_new, |
| 1364 | tok::kw_delete) || |
Manuel Klimek | 89628f6 | 2017-09-20 09:51:03 +0000 | [diff] [blame] | 1365 | FormatTok->isCppStructuredBinding(Style) || Previous->closesScope() || |
| 1366 | Previous->isSimpleTypeSpecifier())) { |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 1367 | nextToken(); |
Manuel Klimek | 9f0a4e5 | 2017-09-19 09:59:30 +0000 | [diff] [blame] | 1368 | return false; |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 1369 | } |
Manuel Klimek | 9f0a4e5 | 2017-09-19 09:59:30 +0000 | [diff] [blame] | 1370 | nextToken(); |
| 1371 | parseSquare(/*LambdaIntroducer=*/true); |
| 1372 | return true; |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
Daniel Jasper | c03e16a | 2014-05-08 09:25:39 +0000 | [diff] [blame] | 1375 | void UnwrappedLineParser::tryToParseJSFunction() { |
Martin Probst | 409697e | 2016-05-29 14:41:07 +0000 | [diff] [blame] | 1376 | assert(FormatTok->is(Keywords.kw_function) || |
| 1377 | FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)); |
Martin Probst | 5f8445b | 2016-04-24 22:05:09 +0000 | [diff] [blame] | 1378 | if (FormatTok->is(Keywords.kw_async)) |
| 1379 | nextToken(); |
| 1380 | // Consume "function". |
Daniel Jasper | c03e16a | 2014-05-08 09:25:39 +0000 | [diff] [blame] | 1381 | nextToken(); |
Daniel Jasper | 5217a8b | 2014-06-13 07:02:04 +0000 | [diff] [blame] | 1382 | |
Daniel Jasper | 71e50af | 2016-11-01 06:22:59 +0000 | [diff] [blame] | 1383 | // Consume * (generator function). Treat it like C++'s overloaded operators. |
| 1384 | if (FormatTok->is(tok::star)) { |
| 1385 | FormatTok->Type = TT_OverloadedOperator; |
Martin Probst | 5f8445b | 2016-04-24 22:05:09 +0000 | [diff] [blame] | 1386 | nextToken(); |
Daniel Jasper | 71e50af | 2016-11-01 06:22:59 +0000 | [diff] [blame] | 1387 | } |
Martin Probst | 5f8445b | 2016-04-24 22:05:09 +0000 | [diff] [blame] | 1388 | |
Daniel Jasper | 5217a8b | 2014-06-13 07:02:04 +0000 | [diff] [blame] | 1389 | // Consume function name. |
| 1390 | if (FormatTok->is(tok::identifier)) |
Daniel Jasper | fca735c | 2015-02-19 16:14:18 +0000 | [diff] [blame] | 1391 | nextToken(); |
Daniel Jasper | 5217a8b | 2014-06-13 07:02:04 +0000 | [diff] [blame] | 1392 | |
Daniel Jasper | c03e16a | 2014-05-08 09:25:39 +0000 | [diff] [blame] | 1393 | if (FormatTok->isNot(tok::l_paren)) |
| 1394 | return; |
Manuel Klimek | 79e0608 | 2015-05-21 12:23:34 +0000 | [diff] [blame] | 1395 | |
| 1396 | // Parse formal parameter list. |
Daniel Jasper | be520bd | 2015-05-31 08:51:54 +0000 | [diff] [blame] | 1397 | parseParens(); |
Manuel Klimek | 79e0608 | 2015-05-21 12:23:34 +0000 | [diff] [blame] | 1398 | |
| 1399 | if (FormatTok->is(tok::colon)) { |
| 1400 | // Parse a type definition. |
| 1401 | nextToken(); |
| 1402 | |
| 1403 | // Eat the type declaration. For braced inline object types, balance braces, |
| 1404 | // otherwise just parse until finding an l_brace for the function body. |
Daniel Jasper | be520bd | 2015-05-31 08:51:54 +0000 | [diff] [blame] | 1405 | if (FormatTok->is(tok::l_brace)) |
| 1406 | tryToParseBracedList(); |
| 1407 | else |
Martin Probst | af16c50 | 2017-01-04 13:36:43 +0000 | [diff] [blame] | 1408 | while (!FormatTok->isOneOf(tok::l_brace, tok::semi) && !eof()) |
Manuel Klimek | 79e0608 | 2015-05-21 12:23:34 +0000 | [diff] [blame] | 1409 | nextToken(); |
Manuel Klimek | 79e0608 | 2015-05-21 12:23:34 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
Martin Probst | af16c50 | 2017-01-04 13:36:43 +0000 | [diff] [blame] | 1412 | if (FormatTok->is(tok::semi)) |
| 1413 | return; |
| 1414 | |
Manuel Klimek | 79e0608 | 2015-05-21 12:23:34 +0000 | [diff] [blame] | 1415 | parseChildBlock(); |
| 1416 | } |
| 1417 | |
Daniel Jasper | 3c883d1 | 2015-05-18 14:49:19 +0000 | [diff] [blame] | 1418 | bool UnwrappedLineParser::tryToParseBracedList() { |
Daniel Jasper | b1f74a8 | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 1419 | if (FormatTok->BlockKind == BK_Unknown) |
Daniel Jasper | 3c883d1 | 2015-05-18 14:49:19 +0000 | [diff] [blame] | 1420 | calculateBraceTypes(); |
Daniel Jasper | b1f74a8 | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 1421 | assert(FormatTok->BlockKind != BK_Unknown); |
| 1422 | if (FormatTok->BlockKind == BK_Block) |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 1423 | return false; |
Krasimir Georgiev | 26b144c | 2017-07-03 15:05:14 +0000 | [diff] [blame] | 1424 | nextToken(); |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 1425 | parseBracedList(); |
| 1426 | return true; |
| 1427 | } |
| 1428 | |
Krasimir Georgiev | ff747be | 2017-06-27 13:43:07 +0000 | [diff] [blame] | 1429 | bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons, |
| 1430 | tok::TokenKind ClosingBraceKind) { |
Daniel Jasper | 015ed02 | 2013-09-13 09:20:45 +0000 | [diff] [blame] | 1431 | bool HasError = false; |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 1432 | |
Manuel Klimek | a3ff45e | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 1433 | // FIXME: Once we have an expression parser in the UnwrappedLineParser, |
| 1434 | // replace this by using parseAssigmentExpression() inside. |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 1435 | do { |
Manuel Klimek | 79e0608 | 2015-05-21 12:23:34 +0000 | [diff] [blame] | 1436 | if (Style.Language == FormatStyle::LK_JavaScript) { |
Martin Probst | 409697e | 2016-05-29 14:41:07 +0000 | [diff] [blame] | 1437 | if (FormatTok->is(Keywords.kw_function) || |
| 1438 | FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)) { |
Manuel Klimek | 79e0608 | 2015-05-21 12:23:34 +0000 | [diff] [blame] | 1439 | tryToParseJSFunction(); |
| 1440 | continue; |
Daniel Jasper | be520bd | 2015-05-31 08:51:54 +0000 | [diff] [blame] | 1441 | } |
| 1442 | if (FormatTok->is(TT_JsFatArrow)) { |
Manuel Klimek | 79e0608 | 2015-05-21 12:23:34 +0000 | [diff] [blame] | 1443 | nextToken(); |
| 1444 | // Fat arrows can be followed by simple expressions or by child blocks |
| 1445 | // in curly braces. |
Daniel Jasper | e6fcf7d | 2015-06-17 13:08:06 +0000 | [diff] [blame] | 1446 | if (FormatTok->is(tok::l_brace)) { |
Manuel Klimek | 79e0608 | 2015-05-21 12:23:34 +0000 | [diff] [blame] | 1447 | parseChildBlock(); |
| 1448 | continue; |
| 1449 | } |
| 1450 | } |
Martin Probst | 8e3eba0 | 2017-02-07 16:33:13 +0000 | [diff] [blame] | 1451 | if (FormatTok->is(tok::l_brace)) { |
| 1452 | // Could be a method inside of a braced list `{a() { return 1; }}`. |
| 1453 | if (tryToParseBracedList()) |
| 1454 | continue; |
| 1455 | parseChildBlock(); |
| 1456 | } |
Daniel Jasper | c03e16a | 2014-05-08 09:25:39 +0000 | [diff] [blame] | 1457 | } |
Krasimir Georgiev | ff747be | 2017-06-27 13:43:07 +0000 | [diff] [blame] | 1458 | if (FormatTok->Tok.getKind() == ClosingBraceKind) { |
| 1459 | nextToken(); |
| 1460 | return !HasError; |
| 1461 | } |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1462 | switch (FormatTok->Tok.getKind()) { |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 1463 | case tok::caret: |
| 1464 | nextToken(); |
| 1465 | if (FormatTok->is(tok::l_brace)) { |
| 1466 | parseChildBlock(); |
| 1467 | } |
| 1468 | break; |
| 1469 | case tok::l_square: |
| 1470 | tryToParseLambda(); |
| 1471 | break; |
Daniel Jasper | a87af7a | 2015-06-30 11:32:22 +0000 | [diff] [blame] | 1472 | case tok::l_paren: |
| 1473 | parseParens(); |
Daniel Jasper | f46dec8 | 2015-03-31 14:34:15 +0000 | [diff] [blame] | 1474 | // JavaScript can just have free standing methods and getters/setters in |
| 1475 | // object literals. Detect them by a "{" following ")". |
| 1476 | if (Style.Language == FormatStyle::LK_JavaScript) { |
Daniel Jasper | f46dec8 | 2015-03-31 14:34:15 +0000 | [diff] [blame] | 1477 | if (FormatTok->is(tok::l_brace)) |
| 1478 | parseChildBlock(); |
| 1479 | break; |
| 1480 | } |
Daniel Jasper | f46dec8 | 2015-03-31 14:34:15 +0000 | [diff] [blame] | 1481 | break; |
Martin Probst | 8e3eba0 | 2017-02-07 16:33:13 +0000 | [diff] [blame] | 1482 | case tok::l_brace: |
| 1483 | // Assume there are no blocks inside a braced init list apart |
| 1484 | // from the ones we explicitly parse out (like lambdas). |
| 1485 | FormatTok->BlockKind = BK_BracedInit; |
Krasimir Georgiev | 26b144c | 2017-07-03 15:05:14 +0000 | [diff] [blame] | 1486 | nextToken(); |
Martin Probst | 8e3eba0 | 2017-02-07 16:33:13 +0000 | [diff] [blame] | 1487 | parseBracedList(); |
| 1488 | break; |
Krasimir Georgiev | fa4dbb6 | 2017-08-03 13:43:45 +0000 | [diff] [blame] | 1489 | case tok::less: |
| 1490 | if (Style.Language == FormatStyle::LK_Proto) { |
| 1491 | nextToken(); |
| 1492 | parseBracedList(/*ContinueOnSemicolons=*/false, |
| 1493 | /*ClosingBraceKind=*/tok::greater); |
| 1494 | } else { |
| 1495 | nextToken(); |
| 1496 | } |
| 1497 | break; |
Manuel Klimek | a3ff45e | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 1498 | case tok::semi: |
Daniel Jasper | b9a4990 | 2016-01-09 15:56:28 +0000 | [diff] [blame] | 1499 | // JavaScript (or more precisely TypeScript) can have semicolons in braced |
| 1500 | // lists (in so-called TypeMemberLists). Thus, the semicolon cannot be |
| 1501 | // used for error recovery if we have otherwise determined that this is |
| 1502 | // a braced list. |
| 1503 | if (Style.Language == FormatStyle::LK_JavaScript) { |
| 1504 | nextToken(); |
| 1505 | break; |
| 1506 | } |
Daniel Jasper | 015ed02 | 2013-09-13 09:20:45 +0000 | [diff] [blame] | 1507 | HasError = true; |
| 1508 | if (!ContinueOnSemicolons) |
| 1509 | return !HasError; |
| 1510 | nextToken(); |
| 1511 | break; |
Manuel Klimek | a3ff45e | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 1512 | case tok::comma: |
| 1513 | nextToken(); |
Manuel Klimek | a3ff45e | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 1514 | break; |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 1515 | default: |
| 1516 | nextToken(); |
| 1517 | break; |
| 1518 | } |
| 1519 | } while (!eof()); |
Daniel Jasper | 015ed02 | 2013-09-13 09:20:45 +0000 | [diff] [blame] | 1520 | return false; |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1523 | void UnwrappedLineParser::parseParens() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1524 | assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected."); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1525 | nextToken(); |
| 1526 | do { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1527 | switch (FormatTok->Tok.getKind()) { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1528 | case tok::l_paren: |
| 1529 | parseParens(); |
Daniel Jasper | 5f1fa85 | 2015-01-04 20:40:51 +0000 | [diff] [blame] | 1530 | if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace)) |
| 1531 | parseChildBlock(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1532 | break; |
| 1533 | case tok::r_paren: |
| 1534 | nextToken(); |
| 1535 | return; |
Daniel Jasper | 393564f | 2013-05-31 14:56:29 +0000 | [diff] [blame] | 1536 | case tok::r_brace: |
| 1537 | // A "}" inside parenthesis is an error if there wasn't a matching "{". |
| 1538 | return; |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 1539 | case tok::l_square: |
| 1540 | tryToParseLambda(); |
| 1541 | break; |
Daniel Jasper | 5f1fa85 | 2015-01-04 20:40:51 +0000 | [diff] [blame] | 1542 | case tok::l_brace: |
Daniel Jasper | adba2aa | 2015-05-18 12:52:00 +0000 | [diff] [blame] | 1543 | if (!tryToParseBracedList()) |
Manuel Klimek | f017dc0 | 2013-09-04 13:34:14 +0000 | [diff] [blame] | 1544 | parseChildBlock(); |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 1545 | break; |
Nico Weber | 372d8dc | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 1546 | case tok::at: |
| 1547 | nextToken(); |
Krasimir Georgiev | 26b144c | 2017-07-03 15:05:14 +0000 | [diff] [blame] | 1548 | if (FormatTok->Tok.is(tok::l_brace)) { |
| 1549 | nextToken(); |
Nico Weber | 372d8dc | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 1550 | parseBracedList(); |
Krasimir Georgiev | 26b144c | 2017-07-03 15:05:14 +0000 | [diff] [blame] | 1551 | } |
Nico Weber | 372d8dc | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 1552 | break; |
Martin Probst | 1027fb8 | 2017-02-07 14:05:30 +0000 | [diff] [blame] | 1553 | case tok::kw_class: |
| 1554 | if (Style.Language == FormatStyle::LK_JavaScript) |
| 1555 | parseRecord(/*ParseAsExpr=*/true); |
| 1556 | else |
| 1557 | nextToken(); |
| 1558 | break; |
Daniel Jasper | 3f69ba1 | 2014-09-05 08:42:27 +0000 | [diff] [blame] | 1559 | case tok::identifier: |
| 1560 | if (Style.Language == FormatStyle::LK_JavaScript && |
Martin Probst | 409697e | 2016-05-29 14:41:07 +0000 | [diff] [blame] | 1561 | (FormatTok->is(Keywords.kw_function) || |
| 1562 | FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function))) |
Daniel Jasper | 3f69ba1 | 2014-09-05 08:42:27 +0000 | [diff] [blame] | 1563 | tryToParseJSFunction(); |
| 1564 | else |
| 1565 | nextToken(); |
| 1566 | break; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1567 | default: |
| 1568 | nextToken(); |
| 1569 | break; |
| 1570 | } |
| 1571 | } while (!eof()); |
| 1572 | } |
| 1573 | |
Manuel Klimek | 9f0a4e5 | 2017-09-19 09:59:30 +0000 | [diff] [blame] | 1574 | void UnwrappedLineParser::parseSquare(bool LambdaIntroducer) { |
| 1575 | if (!LambdaIntroducer) { |
| 1576 | assert(FormatTok->Tok.is(tok::l_square) && "'[' expected."); |
| 1577 | if (tryToParseLambda()) |
| 1578 | return; |
| 1579 | } |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 1580 | do { |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 1581 | switch (FormatTok->Tok.getKind()) { |
| 1582 | case tok::l_paren: |
| 1583 | parseParens(); |
| 1584 | break; |
| 1585 | case tok::r_square: |
| 1586 | nextToken(); |
| 1587 | return; |
| 1588 | case tok::r_brace: |
| 1589 | // A "}" inside parenthesis is an error if there wasn't a matching "{". |
| 1590 | return; |
| 1591 | case tok::l_square: |
| 1592 | parseSquare(); |
| 1593 | break; |
| 1594 | case tok::l_brace: { |
Daniel Jasper | adba2aa | 2015-05-18 12:52:00 +0000 | [diff] [blame] | 1595 | if (!tryToParseBracedList()) |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 1596 | parseChildBlock(); |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 1597 | break; |
| 1598 | } |
| 1599 | case tok::at: |
| 1600 | nextToken(); |
Krasimir Georgiev | 26b144c | 2017-07-03 15:05:14 +0000 | [diff] [blame] | 1601 | if (FormatTok->Tok.is(tok::l_brace)) { |
| 1602 | nextToken(); |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 1603 | parseBracedList(); |
Krasimir Georgiev | 26b144c | 2017-07-03 15:05:14 +0000 | [diff] [blame] | 1604 | } |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 1605 | break; |
| 1606 | default: |
| 1607 | nextToken(); |
| 1608 | break; |
| 1609 | } |
| 1610 | } while (!eof()); |
| 1611 | } |
| 1612 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1613 | void UnwrappedLineParser::parseIfThenElse() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1614 | assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected"); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1615 | nextToken(); |
Daniel Jasper | 6a7d5a7 | 2017-06-19 07:40:49 +0000 | [diff] [blame] | 1616 | if (FormatTok->Tok.is(tok::kw_constexpr)) |
| 1617 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1618 | if (FormatTok->Tok.is(tok::l_paren)) |
Manuel Klimek | adededf | 2013-01-11 18:28:36 +0000 | [diff] [blame] | 1619 | parseParens(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1620 | bool NeedsUnwrappedLine = false; |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1621 | if (FormatTok->Tok.is(tok::l_brace)) { |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1622 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 1623 | parseBlock(/*MustBeDeclaration=*/false); |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 1624 | if (Style.BraceWrapping.BeforeElse) |
Manuel Klimek | d3ed59a | 2013-08-02 21:31:59 +0000 | [diff] [blame] | 1625 | addUnwrappedLine(); |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 1626 | else |
Manuel Klimek | d3ed59a | 2013-08-02 21:31:59 +0000 | [diff] [blame] | 1627 | NeedsUnwrappedLine = true; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1628 | } else { |
| 1629 | addUnwrappedLine(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1630 | ++Line->Level; |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 1631 | parseStructuralElement(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1632 | --Line->Level; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1633 | } |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1634 | if (FormatTok->Tok.is(tok::kw_else)) { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1635 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1636 | if (FormatTok->Tok.is(tok::l_brace)) { |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1637 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 1638 | parseBlock(/*MustBeDeclaration=*/false); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1639 | addUnwrappedLine(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1640 | } else if (FormatTok->Tok.is(tok::kw_if)) { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1641 | parseIfThenElse(); |
| 1642 | } else { |
| 1643 | addUnwrappedLine(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1644 | ++Line->Level; |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 1645 | parseStructuralElement(); |
Daniel Jasper | 451544a | 2016-05-19 06:30:48 +0000 | [diff] [blame] | 1646 | if (FormatTok->is(tok::eof)) |
| 1647 | addUnwrappedLine(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1648 | --Line->Level; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1649 | } |
| 1650 | } else if (NeedsUnwrappedLine) { |
| 1651 | addUnwrappedLine(); |
| 1652 | } |
| 1653 | } |
| 1654 | |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 1655 | void UnwrappedLineParser::parseTryCatch() { |
Nico Weber | fac2371 | 2015-02-04 15:26:27 +0000 | [diff] [blame] | 1656 | assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected"); |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 1657 | nextToken(); |
| 1658 | bool NeedsUnwrappedLine = false; |
| 1659 | if (FormatTok->is(tok::colon)) { |
| 1660 | // We are in a function try block, what comes is an initializer list. |
| 1661 | nextToken(); |
| 1662 | while (FormatTok->is(tok::identifier)) { |
| 1663 | nextToken(); |
| 1664 | if (FormatTok->is(tok::l_paren)) |
| 1665 | parseParens(); |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 1666 | if (FormatTok->is(tok::comma)) |
| 1667 | nextToken(); |
| 1668 | } |
| 1669 | } |
Daniel Jasper | e189d46 | 2015-01-14 10:48:41 +0000 | [diff] [blame] | 1670 | // Parse try with resource. |
| 1671 | if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) { |
| 1672 | parseParens(); |
| 1673 | } |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 1674 | if (FormatTok->is(tok::l_brace)) { |
| 1675 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
| 1676 | parseBlock(/*MustBeDeclaration=*/false); |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 1677 | if (Style.BraceWrapping.BeforeCatch) { |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 1678 | addUnwrappedLine(); |
| 1679 | } else { |
| 1680 | NeedsUnwrappedLine = true; |
| 1681 | } |
| 1682 | } else if (!FormatTok->is(tok::kw_catch)) { |
| 1683 | // The C++ standard requires a compound-statement after a try. |
| 1684 | // If there's none, we try to assume there's a structuralElement |
| 1685 | // and try to continue. |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 1686 | addUnwrappedLine(); |
| 1687 | ++Line->Level; |
| 1688 | parseStructuralElement(); |
| 1689 | --Line->Level; |
| 1690 | } |
Nico Weber | 33381f5 | 2015-02-07 01:57:32 +0000 | [diff] [blame] | 1691 | while (1) { |
| 1692 | if (FormatTok->is(tok::at)) |
| 1693 | nextToken(); |
| 1694 | if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except, |
| 1695 | tok::kw___finally) || |
| 1696 | ((Style.Language == FormatStyle::LK_Java || |
| 1697 | Style.Language == FormatStyle::LK_JavaScript) && |
| 1698 | FormatTok->is(Keywords.kw_finally)) || |
| 1699 | (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) || |
| 1700 | FormatTok->Tok.isObjCAtKeyword(tok::objc_finally)))) |
| 1701 | break; |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 1702 | nextToken(); |
| 1703 | while (FormatTok->isNot(tok::l_brace)) { |
| 1704 | if (FormatTok->is(tok::l_paren)) { |
| 1705 | parseParens(); |
| 1706 | continue; |
| 1707 | } |
Daniel Jasper | 2bd7a64 | 2015-01-19 10:50:51 +0000 | [diff] [blame] | 1708 | if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof)) |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 1709 | return; |
| 1710 | nextToken(); |
| 1711 | } |
| 1712 | NeedsUnwrappedLine = false; |
| 1713 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
| 1714 | parseBlock(/*MustBeDeclaration=*/false); |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 1715 | if (Style.BraceWrapping.BeforeCatch) |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 1716 | addUnwrappedLine(); |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 1717 | else |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 1718 | NeedsUnwrappedLine = true; |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 1719 | } |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 1720 | if (NeedsUnwrappedLine) |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 1721 | addUnwrappedLine(); |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 1722 | } |
| 1723 | |
Alexander Kornienko | 578fdd8 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1724 | void UnwrappedLineParser::parseNamespace() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1725 | assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected"); |
Roman Kashitsyn | a043ced | 2014-08-11 12:18:01 +0000 | [diff] [blame] | 1726 | |
| 1727 | const FormatToken &InitialToken = *FormatTok; |
Alexander Kornienko | 578fdd8 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1728 | nextToken(); |
Saleem Abdulrasool | 328085f | 2015-10-30 05:07:56 +0000 | [diff] [blame] | 1729 | while (FormatTok->isOneOf(tok::identifier, tok::coloncolon)) |
Alexander Kornienko | 578fdd8 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1730 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1731 | if (FormatTok->Tok.is(tok::l_brace)) { |
Roman Kashitsyn | a043ced | 2014-08-11 12:18:01 +0000 | [diff] [blame] | 1732 | if (ShouldBreakBeforeBrace(Style, InitialToken)) |
Manuel Klimek | a8eb914 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 1733 | addUnwrappedLine(); |
| 1734 | |
Daniel Jasper | 65ee347 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 1735 | bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All || |
| 1736 | (Style.NamespaceIndentation == FormatStyle::NI_Inner && |
| 1737 | DeclarationScopeStack.size() > 1); |
| 1738 | parseBlock(/*MustBeDeclaration=*/true, AddLevel); |
Manuel Klimek | 046b930 | 2013-02-06 16:08:09 +0000 | [diff] [blame] | 1739 | // Munch the semicolon after a namespace. This is more common than one would |
| 1740 | // think. Puttin the semicolon into its own line is very ugly. |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1741 | if (FormatTok->Tok.is(tok::semi)) |
Manuel Klimek | 046b930 | 2013-02-06 16:08:09 +0000 | [diff] [blame] | 1742 | nextToken(); |
Alexander Kornienko | 578fdd8 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1743 | addUnwrappedLine(); |
| 1744 | } |
| 1745 | // FIXME: Add error handling. |
| 1746 | } |
| 1747 | |
Daniel Jasper | 6acf513 | 2015-03-12 14:44:29 +0000 | [diff] [blame] | 1748 | void UnwrappedLineParser::parseNew() { |
| 1749 | assert(FormatTok->is(tok::kw_new) && "'new' expected"); |
| 1750 | nextToken(); |
| 1751 | if (Style.Language != FormatStyle::LK_Java) |
| 1752 | return; |
| 1753 | |
| 1754 | // In Java, we can parse everything up to the parens, which aren't optional. |
| 1755 | do { |
| 1756 | // There should not be a ;, { or } before the new's open paren. |
| 1757 | if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace)) |
| 1758 | return; |
| 1759 | |
| 1760 | // Consume the parens. |
| 1761 | if (FormatTok->is(tok::l_paren)) { |
| 1762 | parseParens(); |
| 1763 | |
| 1764 | // If there is a class body of an anonymous class, consume that as child. |
| 1765 | if (FormatTok->is(tok::l_brace)) |
| 1766 | parseChildBlock(); |
| 1767 | return; |
| 1768 | } |
| 1769 | nextToken(); |
| 1770 | } while (!eof()); |
| 1771 | } |
| 1772 | |
Alexander Kornienko | 37d6c94 | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 1773 | void UnwrappedLineParser::parseForOrWhileLoop() { |
Daniel Jasper | 66cb8c5 | 2015-05-04 09:22:29 +0000 | [diff] [blame] | 1774 | assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) && |
Daniel Jasper | e1e4319 | 2014-04-01 12:55:11 +0000 | [diff] [blame] | 1775 | "'for', 'while' or foreach macro expected"); |
Alexander Kornienko | 37d6c94 | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 1776 | nextToken(); |
Martin Probst | a050f41 | 2017-05-18 21:19:29 +0000 | [diff] [blame] | 1777 | // JS' for await ( ... |
Martin Probst | bd49e32 | 2017-05-15 19:33:20 +0000 | [diff] [blame] | 1778 | if (Style.Language == FormatStyle::LK_JavaScript && |
Martin Probst | a050f41 | 2017-05-18 21:19:29 +0000 | [diff] [blame] | 1779 | FormatTok->is(Keywords.kw_await)) |
Martin Probst | bd49e32 | 2017-05-15 19:33:20 +0000 | [diff] [blame] | 1780 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1781 | if (FormatTok->Tok.is(tok::l_paren)) |
Manuel Klimek | 9fa8d55 | 2013-01-11 19:23:05 +0000 | [diff] [blame] | 1782 | parseParens(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1783 | if (FormatTok->Tok.is(tok::l_brace)) { |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1784 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 1785 | parseBlock(/*MustBeDeclaration=*/false); |
Alexander Kornienko | 37d6c94 | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 1786 | addUnwrappedLine(); |
| 1787 | } else { |
| 1788 | addUnwrappedLine(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1789 | ++Line->Level; |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 1790 | parseStructuralElement(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1791 | --Line->Level; |
Alexander Kornienko | 37d6c94 | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 1792 | } |
| 1793 | } |
| 1794 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1795 | void UnwrappedLineParser::parseDoWhile() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1796 | assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected"); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1797 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1798 | if (FormatTok->Tok.is(tok::l_brace)) { |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1799 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 1800 | parseBlock(/*MustBeDeclaration=*/false); |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 1801 | if (Style.BraceWrapping.IndentBraces) |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1802 | addUnwrappedLine(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1803 | } else { |
| 1804 | addUnwrappedLine(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1805 | ++Line->Level; |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 1806 | parseStructuralElement(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1807 | --Line->Level; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1808 | } |
| 1809 | |
Alexander Kornienko | 0ea8e10 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 1810 | // FIXME: Add error handling. |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1811 | if (!FormatTok->Tok.is(tok::kw_while)) { |
Alexander Kornienko | 0ea8e10 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 1812 | addUnwrappedLine(); |
| 1813 | return; |
| 1814 | } |
| 1815 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1816 | nextToken(); |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 1817 | parseStructuralElement(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1818 | } |
| 1819 | |
| 1820 | void UnwrappedLineParser::parseLabel() { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1821 | nextToken(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1822 | unsigned OldLineLevel = Line->Level; |
Daniel Jasper | a127512 | 2013-03-20 10:23:53 +0000 | [diff] [blame] | 1823 | if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0)) |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1824 | --Line->Level; |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1825 | if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) { |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1826 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 1827 | parseBlock(/*MustBeDeclaration=*/false); |
Manuel Klimek | d3ed59a | 2013-08-02 21:31:59 +0000 | [diff] [blame] | 1828 | if (FormatTok->Tok.is(tok::kw_break)) { |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 1829 | if (Style.BraceWrapping.AfterControlStatement) |
Manuel Klimek | d3ed59a | 2013-08-02 21:31:59 +0000 | [diff] [blame] | 1830 | addUnwrappedLine(); |
| 1831 | parseStructuralElement(); |
| 1832 | } |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1833 | addUnwrappedLine(); |
| 1834 | } else { |
Daniel Jasper | 1fe0d5c | 2015-05-06 15:19:47 +0000 | [diff] [blame] | 1835 | if (FormatTok->is(tok::semi)) |
| 1836 | nextToken(); |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1837 | addUnwrappedLine(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1838 | } |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1839 | Line->Level = OldLineLevel; |
Daniel Jasper | 2cce7b7 | 2016-04-06 16:41:39 +0000 | [diff] [blame] | 1840 | if (FormatTok->isNot(tok::l_brace)) { |
Daniel Jasper | 4060947 | 2016-04-06 15:02:46 +0000 | [diff] [blame] | 1841 | parseStructuralElement(); |
Daniel Jasper | 2cce7b7 | 2016-04-06 16:41:39 +0000 | [diff] [blame] | 1842 | addUnwrappedLine(); |
| 1843 | } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1844 | } |
| 1845 | |
| 1846 | void UnwrappedLineParser::parseCaseLabel() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1847 | assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected"); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1848 | // FIXME: fix handling of complex expressions here. |
| 1849 | do { |
| 1850 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1851 | } while (!eof() && !FormatTok->Tok.is(tok::colon)); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1852 | parseLabel(); |
| 1853 | } |
| 1854 | |
| 1855 | void UnwrappedLineParser::parseSwitch() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1856 | assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected"); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1857 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1858 | if (FormatTok->Tok.is(tok::l_paren)) |
Manuel Klimek | 9fa8d55 | 2013-01-11 19:23:05 +0000 | [diff] [blame] | 1859 | parseParens(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1860 | if (FormatTok->Tok.is(tok::l_brace)) { |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 1861 | CompoundStatementIndenter Indenter(this, Style, Line->Level); |
Daniel Jasper | 65ee347 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 1862 | parseBlock(/*MustBeDeclaration=*/false); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1863 | addUnwrappedLine(); |
| 1864 | } else { |
| 1865 | addUnwrappedLine(); |
Daniel Jasper | 516d797 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 1866 | ++Line->Level; |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 1867 | parseStructuralElement(); |
Daniel Jasper | 516d797 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 1868 | --Line->Level; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1869 | } |
| 1870 | } |
| 1871 | |
| 1872 | void UnwrappedLineParser::parseAccessSpecifier() { |
| 1873 | nextToken(); |
Daniel Jasper | 84c47a1 | 2013-11-23 17:53:41 +0000 | [diff] [blame] | 1874 | // Understand Qt's slots. |
Daniel Jasper | 5339540 | 2015-04-07 15:04:40 +0000 | [diff] [blame] | 1875 | if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots)) |
Daniel Jasper | 84c47a1 | 2013-11-23 17:53:41 +0000 | [diff] [blame] | 1876 | nextToken(); |
Alexander Kornienko | 2ca766f | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1877 | // Otherwise, we don't know what it is, and we'd better keep the next token. |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1878 | if (FormatTok->Tok.is(tok::colon)) |
Alexander Kornienko | 2ca766f | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1879 | nextToken(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1880 | addUnwrappedLine(); |
| 1881 | } |
| 1882 | |
Daniel Jasper | 6f5a193 | 2015-12-29 08:54:23 +0000 | [diff] [blame] | 1883 | bool UnwrappedLineParser::parseEnum() { |
Daniel Jasper | 6be0f55 | 2014-11-13 15:56:28 +0000 | [diff] [blame] | 1884 | // Won't be 'enum' for NS_ENUMs. |
| 1885 | if (FormatTok->Tok.is(tok::kw_enum)) |
Daniel Jasper | ccb68b4 | 2014-11-19 22:38:18 +0000 | [diff] [blame] | 1886 | nextToken(); |
Daniel Jasper | 6be0f55 | 2014-11-13 15:56:28 +0000 | [diff] [blame] | 1887 | |
Daniel Jasper | 6f5a193 | 2015-12-29 08:54:23 +0000 | [diff] [blame] | 1888 | // In TypeScript, "enum" can also be used as property name, e.g. in interface |
| 1889 | // declarations. An "enum" keyword followed by a colon would be a syntax |
| 1890 | // error and thus assume it is just an identifier. |
Daniel Jasper | 8737930 | 2016-02-03 05:33:44 +0000 | [diff] [blame] | 1891 | if (Style.Language == FormatStyle::LK_JavaScript && |
| 1892 | FormatTok->isOneOf(tok::colon, tok::question)) |
Daniel Jasper | 6f5a193 | 2015-12-29 08:54:23 +0000 | [diff] [blame] | 1893 | return false; |
| 1894 | |
Daniel Jasper | 2b41a82 | 2013-08-20 12:42:50 +0000 | [diff] [blame] | 1895 | // Eat up enum class ... |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 1896 | if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct)) |
| 1897 | nextToken(); |
Daniel Jasper | b5a0b85 | 2015-06-19 08:17:32 +0000 | [diff] [blame] | 1898 | |
Daniel Jasper | 786a550 | 2013-09-06 21:32:35 +0000 | [diff] [blame] | 1899 | while (FormatTok->Tok.getIdentifierInfo() || |
Daniel Jasper | ccb68b4 | 2014-11-19 22:38:18 +0000 | [diff] [blame] | 1900 | FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less, |
| 1901 | tok::greater, tok::comma, tok::question)) { |
Manuel Klimek | 2cec019 | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 1902 | nextToken(); |
| 1903 | // We can have macros or attributes in between 'enum' and the enum name. |
Daniel Jasper | ccb68b4 | 2014-11-19 22:38:18 +0000 | [diff] [blame] | 1904 | if (FormatTok->is(tok::l_paren)) |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 1905 | parseParens(); |
Daniel Jasper | b5a0b85 | 2015-06-19 08:17:32 +0000 | [diff] [blame] | 1906 | if (FormatTok->is(tok::identifier)) { |
Manuel Klimek | 2cec019 | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 1907 | nextToken(); |
Daniel Jasper | b5a0b85 | 2015-06-19 08:17:32 +0000 | [diff] [blame] | 1908 | // If there are two identifiers in a row, this is likely an elaborate |
| 1909 | // return type. In Java, this can be "implements", etc. |
Daniel Jasper | 1dbc210 | 2017-03-31 13:30:24 +0000 | [diff] [blame] | 1910 | if (Style.isCpp() && FormatTok->is(tok::identifier)) |
Daniel Jasper | 6f5a193 | 2015-12-29 08:54:23 +0000 | [diff] [blame] | 1911 | return false; |
Daniel Jasper | b5a0b85 | 2015-06-19 08:17:32 +0000 | [diff] [blame] | 1912 | } |
Manuel Klimek | 2cec019 | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 1913 | } |
Daniel Jasper | 6be0f55 | 2014-11-13 15:56:28 +0000 | [diff] [blame] | 1914 | |
| 1915 | // Just a declaration or something is wrong. |
Daniel Jasper | ccb68b4 | 2014-11-19 22:38:18 +0000 | [diff] [blame] | 1916 | if (FormatTok->isNot(tok::l_brace)) |
Daniel Jasper | 6f5a193 | 2015-12-29 08:54:23 +0000 | [diff] [blame] | 1917 | return true; |
Daniel Jasper | 6be0f55 | 2014-11-13 15:56:28 +0000 | [diff] [blame] | 1918 | FormatTok->BlockKind = BK_Block; |
| 1919 | |
| 1920 | if (Style.Language == FormatStyle::LK_Java) { |
| 1921 | // Java enums are different. |
| 1922 | parseJavaEnumBody(); |
Daniel Jasper | 6f5a193 | 2015-12-29 08:54:23 +0000 | [diff] [blame] | 1923 | return true; |
| 1924 | } |
| 1925 | if (Style.Language == FormatStyle::LK_Proto) { |
Daniel Jasper | c6dd273 | 2015-07-16 14:25:43 +0000 | [diff] [blame] | 1926 | parseBlock(/*MustBeDeclaration=*/true); |
Daniel Jasper | 6f5a193 | 2015-12-29 08:54:23 +0000 | [diff] [blame] | 1927 | return true; |
Manuel Klimek | 2cec019 | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 1928 | } |
Daniel Jasper | 6be0f55 | 2014-11-13 15:56:28 +0000 | [diff] [blame] | 1929 | |
| 1930 | // Parse enum body. |
Krasimir Georgiev | 26b144c | 2017-07-03 15:05:14 +0000 | [diff] [blame] | 1931 | nextToken(); |
Daniel Jasper | 6be0f55 | 2014-11-13 15:56:28 +0000 | [diff] [blame] | 1932 | bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true); |
| 1933 | if (HasError) { |
| 1934 | if (FormatTok->is(tok::semi)) |
| 1935 | nextToken(); |
| 1936 | addUnwrappedLine(); |
| 1937 | } |
Daniel Jasper | 6f5a193 | 2015-12-29 08:54:23 +0000 | [diff] [blame] | 1938 | return true; |
Daniel Jasper | 6be0f55 | 2014-11-13 15:56:28 +0000 | [diff] [blame] | 1939 | |
Daniel Jasper | 90cf380 | 2015-06-17 09:44:02 +0000 | [diff] [blame] | 1940 | // There is no addUnwrappedLine() here so that we fall through to parsing a |
| 1941 | // structural element afterwards. Thus, in "enum A {} n, m;", |
Manuel Klimek | 2cec019 | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 1942 | // "} n, m;" will end up in one unwrapped line. |
Daniel Jasper | 6be0f55 | 2014-11-13 15:56:28 +0000 | [diff] [blame] | 1943 | } |
| 1944 | |
| 1945 | void UnwrappedLineParser::parseJavaEnumBody() { |
| 1946 | // Determine whether the enum is simple, i.e. does not have a semicolon or |
| 1947 | // constants with class bodies. Simple enums can be formatted like braced |
| 1948 | // lists, contracted to a single line, etc. |
| 1949 | unsigned StoredPosition = Tokens->getPosition(); |
| 1950 | bool IsSimple = true; |
| 1951 | FormatToken *Tok = Tokens->getNextToken(); |
| 1952 | while (Tok) { |
| 1953 | if (Tok->is(tok::r_brace)) |
| 1954 | break; |
| 1955 | if (Tok->isOneOf(tok::l_brace, tok::semi)) { |
| 1956 | IsSimple = false; |
| 1957 | break; |
| 1958 | } |
| 1959 | // FIXME: This will also mark enums with braces in the arguments to enum |
| 1960 | // constants as "not simple". This is probably fine in practice, though. |
| 1961 | Tok = Tokens->getNextToken(); |
| 1962 | } |
| 1963 | FormatTok = Tokens->setPosition(StoredPosition); |
| 1964 | |
| 1965 | if (IsSimple) { |
Krasimir Georgiev | 26b144c | 2017-07-03 15:05:14 +0000 | [diff] [blame] | 1966 | nextToken(); |
Daniel Jasper | 6be0f55 | 2014-11-13 15:56:28 +0000 | [diff] [blame] | 1967 | parseBracedList(); |
Daniel Jasper | df2ff00 | 2014-11-02 22:31:39 +0000 | [diff] [blame] | 1968 | addUnwrappedLine(); |
Daniel Jasper | 6be0f55 | 2014-11-13 15:56:28 +0000 | [diff] [blame] | 1969 | return; |
| 1970 | } |
| 1971 | |
| 1972 | // Parse the body of a more complex enum. |
| 1973 | // First add a line for everything up to the "{". |
| 1974 | nextToken(); |
| 1975 | addUnwrappedLine(); |
| 1976 | ++Line->Level; |
| 1977 | |
| 1978 | // Parse the enum constants. |
| 1979 | while (FormatTok) { |
| 1980 | if (FormatTok->is(tok::l_brace)) { |
| 1981 | // Parse the constant's class body. |
| 1982 | parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true, |
| 1983 | /*MunchSemi=*/false); |
| 1984 | } else if (FormatTok->is(tok::l_paren)) { |
| 1985 | parseParens(); |
| 1986 | } else if (FormatTok->is(tok::comma)) { |
| 1987 | nextToken(); |
| 1988 | addUnwrappedLine(); |
| 1989 | } else if (FormatTok->is(tok::semi)) { |
| 1990 | nextToken(); |
| 1991 | addUnwrappedLine(); |
| 1992 | break; |
| 1993 | } else if (FormatTok->is(tok::r_brace)) { |
| 1994 | addUnwrappedLine(); |
| 1995 | break; |
| 1996 | } else { |
| 1997 | nextToken(); |
| 1998 | } |
| 1999 | } |
| 2000 | |
| 2001 | // Parse the class body after the enum's ";" if any. |
| 2002 | parseLevel(/*HasOpeningBrace=*/true); |
| 2003 | nextToken(); |
| 2004 | --Line->Level; |
| 2005 | addUnwrappedLine(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 2006 | } |
| 2007 | |
Martin Probst | 1027fb8 | 2017-02-07 14:05:30 +0000 | [diff] [blame] | 2008 | void UnwrappedLineParser::parseRecord(bool ParseAsExpr) { |
Roman Kashitsyn | a043ced | 2014-08-11 12:18:01 +0000 | [diff] [blame] | 2009 | const FormatToken &InitialToken = *FormatTok; |
Manuel Klimek | 28cacc7 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 2010 | nextToken(); |
Daniel Jasper | 04785d0 | 2015-05-06 14:03:02 +0000 | [diff] [blame] | 2011 | |
Daniel Jasper | 04785d0 | 2015-05-06 14:03:02 +0000 | [diff] [blame] | 2012 | // The actual identifier can be a nested name specifier, and in macros |
| 2013 | // it is often token-pasted. |
| 2014 | while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash, |
| 2015 | tok::kw___attribute, tok::kw___declspec, |
| 2016 | tok::kw_alignas) || |
| 2017 | ((Style.Language == FormatStyle::LK_Java || |
| 2018 | Style.Language == FormatStyle::LK_JavaScript) && |
| 2019 | FormatTok->isOneOf(tok::period, tok::comma))) { |
Martin Probst | cb870c5 | 2017-08-01 15:46:10 +0000 | [diff] [blame] | 2020 | if (Style.Language == FormatStyle::LK_JavaScript && |
| 2021 | FormatTok->isOneOf(Keywords.kw_extends, Keywords.kw_implements)) { |
| 2022 | // JavaScript/TypeScript supports inline object types in |
| 2023 | // extends/implements positions: |
| 2024 | // class Foo implements {bar: number} { } |
| 2025 | nextToken(); |
| 2026 | if (FormatTok->is(tok::l_brace)) { |
| 2027 | tryToParseBracedList(); |
| 2028 | continue; |
| 2029 | } |
| 2030 | } |
Daniel Jasper | 04785d0 | 2015-05-06 14:03:02 +0000 | [diff] [blame] | 2031 | bool IsNonMacroIdentifier = |
| 2032 | FormatTok->is(tok::identifier) && |
| 2033 | FormatTok->TokenText != FormatTok->TokenText.upper(); |
Manuel Klimek | e01bab5 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 2034 | nextToken(); |
| 2035 | // We can have macros or attributes in between 'class' and the class name. |
Daniel Jasper | 04785d0 | 2015-05-06 14:03:02 +0000 | [diff] [blame] | 2036 | if (!IsNonMacroIdentifier && FormatTok->Tok.is(tok::l_paren)) |
Manuel Klimek | e01bab5 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 2037 | parseParens(); |
Daniel Jasper | 04785d0 | 2015-05-06 14:03:02 +0000 | [diff] [blame] | 2038 | } |
Manuel Klimek | e01bab5 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 2039 | |
Daniel Jasper | 04785d0 | 2015-05-06 14:03:02 +0000 | [diff] [blame] | 2040 | // Note that parsing away template declarations here leads to incorrectly |
| 2041 | // accepting function declarations as record declarations. |
| 2042 | // In general, we cannot solve this problem. Consider: |
| 2043 | // class A<int> B() {} |
| 2044 | // which can be a function definition or a class definition when B() is a |
| 2045 | // macro. If we find enough real-world cases where this is a problem, we |
| 2046 | // can parse for the 'template' keyword in the beginning of the statement, |
| 2047 | // and thus rule out the record production in case there is no template |
| 2048 | // (this would still leave us with an ambiguity between template function |
| 2049 | // and class declarations). |
Daniel Jasper | adba2aa | 2015-05-18 12:52:00 +0000 | [diff] [blame] | 2050 | if (FormatTok->isOneOf(tok::colon, tok::less)) { |
| 2051 | while (!eof()) { |
Daniel Jasper | 3c883d1 | 2015-05-18 14:49:19 +0000 | [diff] [blame] | 2052 | if (FormatTok->is(tok::l_brace)) { |
| 2053 | calculateBraceTypes(/*ExpectClassBody=*/true); |
| 2054 | if (!tryToParseBracedList()) |
| 2055 | break; |
| 2056 | } |
Daniel Jasper | 04785d0 | 2015-05-06 14:03:02 +0000 | [diff] [blame] | 2057 | if (FormatTok->Tok.is(tok::semi)) |
| 2058 | return; |
| 2059 | nextToken(); |
Manuel Klimek | e01bab5 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 2060 | } |
| 2061 | } |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 2062 | if (FormatTok->Tok.is(tok::l_brace)) { |
Martin Probst | 1027fb8 | 2017-02-07 14:05:30 +0000 | [diff] [blame] | 2063 | if (ParseAsExpr) { |
| 2064 | parseChildBlock(); |
| 2065 | } else { |
| 2066 | if (ShouldBreakBeforeBrace(Style, InitialToken)) |
| 2067 | addUnwrappedLine(); |
Manuel Klimek | a8eb914 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 2068 | |
Martin Probst | 1027fb8 | 2017-02-07 14:05:30 +0000 | [diff] [blame] | 2069 | parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true, |
| 2070 | /*MunchSemi=*/false); |
| 2071 | } |
Manuel Klimek | a8eb914 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 2072 | } |
Daniel Jasper | 90cf380 | 2015-06-17 09:44:02 +0000 | [diff] [blame] | 2073 | // There is no addUnwrappedLine() here so that we fall through to parsing a |
| 2074 | // structural element afterwards. Thus, in "class A {} n, m;", |
| 2075 | // "} n, m;" will end up in one unwrapped line. |
Manuel Klimek | 28cacc7 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 2076 | } |
| 2077 | |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 2078 | void UnwrappedLineParser::parseObjCProtocolList() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 2079 | assert(FormatTok->Tok.is(tok::less) && "'<' expected."); |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 2080 | do |
| 2081 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 2082 | while (!eof() && FormatTok->Tok.isNot(tok::greater)); |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 2083 | nextToken(); // Skip '>'. |
| 2084 | } |
| 2085 | |
| 2086 | void UnwrappedLineParser::parseObjCUntilAtEnd() { |
| 2087 | do { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 2088 | if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) { |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 2089 | nextToken(); |
| 2090 | addUnwrappedLine(); |
| 2091 | break; |
| 2092 | } |
Daniel Jasper | a15da30 | 2013-08-28 08:04:23 +0000 | [diff] [blame] | 2093 | if (FormatTok->is(tok::l_brace)) { |
| 2094 | parseBlock(/*MustBeDeclaration=*/false); |
| 2095 | // In ObjC interfaces, nothing should be following the "}". |
| 2096 | addUnwrappedLine(); |
Benjamin Kramer | e21cb74 | 2014-01-08 15:59:42 +0000 | [diff] [blame] | 2097 | } else if (FormatTok->is(tok::r_brace)) { |
| 2098 | // Ignore stray "}". parseStructuralElement doesn't consume them. |
| 2099 | nextToken(); |
| 2100 | addUnwrappedLine(); |
Daniel Jasper | a15da30 | 2013-08-28 08:04:23 +0000 | [diff] [blame] | 2101 | } else { |
| 2102 | parseStructuralElement(); |
| 2103 | } |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 2104 | } while (!eof()); |
| 2105 | } |
| 2106 | |
Nico Weber | 2ce0ac5 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 2107 | void UnwrappedLineParser::parseObjCInterfaceOrImplementation() { |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2108 | nextToken(); |
Daniel Jasper | d1ae358 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 2109 | nextToken(); // interface name |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2110 | |
| 2111 | // @interface can be followed by either a base class, or a category. |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 2112 | if (FormatTok->Tok.is(tok::colon)) { |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2113 | nextToken(); |
Daniel Jasper | d1ae358 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 2114 | nextToken(); // base class name |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 2115 | } else if (FormatTok->Tok.is(tok::l_paren)) |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2116 | // Skip category, if present. |
| 2117 | parseParens(); |
| 2118 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 2119 | if (FormatTok->Tok.is(tok::less)) |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 2120 | parseObjCProtocolList(); |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2121 | |
Dinesh Dwivedi | ea3aca8 | 2014-05-02 17:01:46 +0000 | [diff] [blame] | 2122 | if (FormatTok->Tok.is(tok::l_brace)) { |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 2123 | if (Style.BraceWrapping.AfterObjCDeclaration) |
Dinesh Dwivedi | ea3aca8 | 2014-05-02 17:01:46 +0000 | [diff] [blame] | 2124 | addUnwrappedLine(); |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 2125 | parseBlock(/*MustBeDeclaration=*/true); |
Dinesh Dwivedi | ea3aca8 | 2014-05-02 17:01:46 +0000 | [diff] [blame] | 2126 | } |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2127 | |
| 2128 | // With instance variables, this puts '}' on its own line. Without instance |
| 2129 | // variables, this ends the @interface line. |
| 2130 | addUnwrappedLine(); |
| 2131 | |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 2132 | parseObjCUntilAtEnd(); |
| 2133 | } |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2134 | |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 2135 | void UnwrappedLineParser::parseObjCProtocol() { |
| 2136 | nextToken(); |
Daniel Jasper | d1ae358 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 2137 | nextToken(); // protocol name |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 2138 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 2139 | if (FormatTok->Tok.is(tok::less)) |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 2140 | parseObjCProtocolList(); |
| 2141 | |
| 2142 | // Check for protocol declaration. |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 2143 | if (FormatTok->Tok.is(tok::semi)) { |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 2144 | nextToken(); |
| 2145 | return addUnwrappedLine(); |
| 2146 | } |
| 2147 | |
| 2148 | addUnwrappedLine(); |
| 2149 | parseObjCUntilAtEnd(); |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2150 | } |
| 2151 | |
Daniel Jasper | fca735c | 2015-02-19 16:14:18 +0000 | [diff] [blame] | 2152 | void UnwrappedLineParser::parseJavaScriptEs6ImportExport() { |
Martin Probst | 053f1aa | 2016-04-19 14:55:37 +0000 | [diff] [blame] | 2153 | bool IsImport = FormatTok->is(Keywords.kw_import); |
| 2154 | assert(IsImport || FormatTok->is(tok::kw_export)); |
Daniel Jasper | 354aa51 | 2015-02-19 16:07:32 +0000 | [diff] [blame] | 2155 | nextToken(); |
Daniel Jasper | fca735c | 2015-02-19 16:14:18 +0000 | [diff] [blame] | 2156 | |
Daniel Jasper | ec05fc7 | 2015-05-11 09:14:50 +0000 | [diff] [blame] | 2157 | // Consume the "default" in "export default class/function". |
Daniel Jasper | 668c7bb | 2015-05-11 09:03:10 +0000 | [diff] [blame] | 2158 | if (FormatTok->is(tok::kw_default)) |
| 2159 | nextToken(); |
Daniel Jasper | ec05fc7 | 2015-05-11 09:14:50 +0000 | [diff] [blame] | 2160 | |
Martin Probst | 5f8445b | 2016-04-24 22:05:09 +0000 | [diff] [blame] | 2161 | // Consume "async function", "function" and "default function", so that these |
| 2162 | // get parsed as free-standing JS functions, i.e. do not require a trailing |
| 2163 | // semicolon. |
| 2164 | if (FormatTok->is(Keywords.kw_async)) |
| 2165 | nextToken(); |
Daniel Jasper | 668c7bb | 2015-05-11 09:03:10 +0000 | [diff] [blame] | 2166 | if (FormatTok->is(Keywords.kw_function)) { |
| 2167 | nextToken(); |
| 2168 | return; |
| 2169 | } |
| 2170 | |
Martin Probst | 053f1aa | 2016-04-19 14:55:37 +0000 | [diff] [blame] | 2171 | // For imports, `export *`, `export {...}`, consume the rest of the line up |
| 2172 | // to the terminating `;`. For everything else, just return and continue |
| 2173 | // parsing the structural element, i.e. the declaration or expression for |
| 2174 | // `export default`. |
| 2175 | if (!IsImport && !FormatTok->isOneOf(tok::l_brace, tok::star) && |
| 2176 | !FormatTok->isStringLiteral()) |
| 2177 | return; |
Daniel Jasper | fca735c | 2015-02-19 16:14:18 +0000 | [diff] [blame] | 2178 | |
Martin Probst | d40bca4 | 2017-01-09 08:56:36 +0000 | [diff] [blame] | 2179 | while (!eof()) { |
| 2180 | if (FormatTok->is(tok::semi)) |
| 2181 | return; |
| 2182 | if (Line->Tokens.size() == 0) { |
| 2183 | // Common issue: Automatic Semicolon Insertion wrapped the line, so the |
| 2184 | // import statement should terminate. |
| 2185 | return; |
| 2186 | } |
Daniel Jasper | efc1a83 | 2016-01-07 08:53:35 +0000 | [diff] [blame] | 2187 | if (FormatTok->is(tok::l_brace)) { |
| 2188 | FormatTok->BlockKind = BK_Block; |
Krasimir Georgiev | 26b144c | 2017-07-03 15:05:14 +0000 | [diff] [blame] | 2189 | nextToken(); |
Daniel Jasper | efc1a83 | 2016-01-07 08:53:35 +0000 | [diff] [blame] | 2190 | parseBracedList(); |
| 2191 | } else { |
| 2192 | nextToken(); |
| 2193 | } |
Daniel Jasper | 354aa51 | 2015-02-19 16:07:32 +0000 | [diff] [blame] | 2194 | } |
| 2195 | } |
| 2196 | |
Daniel Jasper | 3b203a6 | 2013-09-05 16:05:56 +0000 | [diff] [blame] | 2197 | LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line, |
| 2198 | StringRef Prefix = "") { |
Krasimir Georgiev | 9ad83fe | 2017-10-30 14:01:50 +0000 | [diff] [blame] | 2199 | llvm::dbgs() << Prefix << "Line(" << Line.Level |
| 2200 | << ", FSC=" << Line.FirstStartColumn << ")" |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 2201 | << (Line.InPPDirective ? " MACRO" : "") << ": "; |
| 2202 | for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(), |
| 2203 | E = Line.Tokens.end(); |
| 2204 | I != E; ++I) { |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 2205 | llvm::dbgs() << I->Tok->Tok.getName() << "[" |
Manuel Klimek | 89628f6 | 2017-09-20 09:51:03 +0000 | [diff] [blame] | 2206 | << "T=" << I->Tok->Type << ", OC=" << I->Tok->OriginalColumn |
| 2207 | << "] "; |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 2208 | } |
| 2209 | for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(), |
| 2210 | E = Line.Tokens.end(); |
| 2211 | I != E; ++I) { |
| 2212 | const UnwrappedLineNode &Node = *I; |
| 2213 | for (SmallVectorImpl<UnwrappedLine>::const_iterator |
| 2214 | I = Node.Children.begin(), |
| 2215 | E = Node.Children.end(); |
| 2216 | I != E; ++I) { |
| 2217 | printDebugInfo(*I, "\nChild: "); |
| 2218 | } |
| 2219 | } |
| 2220 | llvm::dbgs() << "\n"; |
| 2221 | } |
| 2222 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 2223 | void UnwrappedLineParser::addUnwrappedLine() { |
Daniel Jasper | daffc0d | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 2224 | if (Line->Tokens.empty()) |
Daniel Jasper | 7c85fde | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 2225 | return; |
Manuel Klimek | ab3dc00 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 2226 | DEBUG({ |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 2227 | if (CurrentLines == &Lines) |
| 2228 | printDebugInfo(*Line); |
Manuel Klimek | ab3dc00 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 2229 | }); |
Benjamin Kramer | c7551a4 | 2015-05-31 11:18:05 +0000 | [diff] [blame] | 2230 | CurrentLines->push_back(std::move(*Line)); |
Daniel Jasper | daffc0d | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 2231 | Line->Tokens.clear(); |
Krasimir Georgiev | 85c3704 | 2017-03-01 16:38:08 +0000 | [diff] [blame] | 2232 | Line->MatchingOpeningBlockLineIndex = UnwrappedLine::kInvalidIndex; |
Krasimir Georgiev | 9ad83fe | 2017-10-30 14:01:50 +0000 | [diff] [blame] | 2233 | Line->FirstStartColumn = 0; |
Manuel Klimek | d3b92fa | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 2234 | if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) { |
Benjamin Kramer | c7551a4 | 2015-05-31 11:18:05 +0000 | [diff] [blame] | 2235 | CurrentLines->append( |
| 2236 | std::make_move_iterator(PreprocessorDirectives.begin()), |
| 2237 | std::make_move_iterator(PreprocessorDirectives.end())); |
Manuel Klimek | d3b92fa | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 2238 | PreprocessorDirectives.clear(); |
| 2239 | } |
Manuel Klimek | e411aa8 | 2017-09-20 09:29:37 +0000 | [diff] [blame] | 2240 | // Disconnect the current token from the last token on the previous line. |
| 2241 | FormatTok->Previous = nullptr; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 2242 | } |
| 2243 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 2244 | bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 2245 | |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 2246 | bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) { |
Manuel Klimek | 1fcbe67 | 2014-04-11 12:27:47 +0000 | [diff] [blame] | 2247 | return (Line->InPPDirective || FormatTok.HasUnescapedNewline) && |
| 2248 | FormatTok.NewlinesBefore > 0; |
| 2249 | } |
| 2250 | |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 2251 | // Checks if \p FormatTok is a line comment that continues the line comment |
| 2252 | // section on \p Line. |
Krasimir Georgiev | ea222a7 | 2017-05-22 10:07:56 +0000 | [diff] [blame] | 2253 | static bool continuesLineCommentSection(const FormatToken &FormatTok, |
| 2254 | const UnwrappedLine &Line, |
| 2255 | llvm::Regex &CommentPragmasRegex) { |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 2256 | if (Line.Tokens.empty()) |
| 2257 | return false; |
Krasimir Georgiev | 8432161 | 2017-01-30 19:18:55 +0000 | [diff] [blame] | 2258 | |
Krasimir Georgiev | 00c5c72 | 2017-02-02 15:32:19 +0000 | [diff] [blame] | 2259 | StringRef IndentContent = FormatTok.TokenText; |
| 2260 | if (FormatTok.TokenText.startswith("//") || |
| 2261 | FormatTok.TokenText.startswith("/*")) |
| 2262 | IndentContent = FormatTok.TokenText.substr(2); |
| 2263 | if (CommentPragmasRegex.match(IndentContent)) |
| 2264 | return false; |
| 2265 | |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 2266 | // If Line starts with a line comment, then FormatTok continues the comment |
Krasimir Georgiev | 8432161 | 2017-01-30 19:18:55 +0000 | [diff] [blame] | 2267 | // section if its original column is greater or equal to the original start |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 2268 | // column of the line. |
| 2269 | // |
Krasimir Georgiev | 8432161 | 2017-01-30 19:18:55 +0000 | [diff] [blame] | 2270 | // Define the min column token of a line as follows: if a line ends in '{' or |
| 2271 | // contains a '{' followed by a line comment, then the min column token is |
| 2272 | // that '{'. Otherwise, the min column token of the line is the first token of |
| 2273 | // the line. |
| 2274 | // |
| 2275 | // If Line starts with a token other than a line comment, then FormatTok |
| 2276 | // continues the comment section if its original column is greater than the |
| 2277 | // original start column of the min column token of the line. |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 2278 | // |
| 2279 | // For example, the second line comment continues the first in these cases: |
Krasimir Georgiev | b6ccd38 | 2017-02-02 14:36:50 +0000 | [diff] [blame] | 2280 | // |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 2281 | // // first line |
| 2282 | // // second line |
Krasimir Georgiev | b6ccd38 | 2017-02-02 14:36:50 +0000 | [diff] [blame] | 2283 | // |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 2284 | // and: |
Krasimir Georgiev | b6ccd38 | 2017-02-02 14:36:50 +0000 | [diff] [blame] | 2285 | // |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 2286 | // // first line |
| 2287 | // // second line |
Krasimir Georgiev | b6ccd38 | 2017-02-02 14:36:50 +0000 | [diff] [blame] | 2288 | // |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 2289 | // and: |
Krasimir Georgiev | b6ccd38 | 2017-02-02 14:36:50 +0000 | [diff] [blame] | 2290 | // |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 2291 | // int i; // first line |
| 2292 | // // second line |
Krasimir Georgiev | b6ccd38 | 2017-02-02 14:36:50 +0000 | [diff] [blame] | 2293 | // |
Krasimir Georgiev | 8432161 | 2017-01-30 19:18:55 +0000 | [diff] [blame] | 2294 | // and: |
Krasimir Georgiev | b6ccd38 | 2017-02-02 14:36:50 +0000 | [diff] [blame] | 2295 | // |
Krasimir Georgiev | 8432161 | 2017-01-30 19:18:55 +0000 | [diff] [blame] | 2296 | // do { // first line |
| 2297 | // // second line |
| 2298 | // int i; |
| 2299 | // } while (true); |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 2300 | // |
Krasimir Georgiev | b6ccd38 | 2017-02-02 14:36:50 +0000 | [diff] [blame] | 2301 | // and: |
| 2302 | // |
| 2303 | // enum { |
| 2304 | // a, // first line |
| 2305 | // // second line |
| 2306 | // b |
| 2307 | // }; |
| 2308 | // |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 2309 | // The second line comment doesn't continue the first in these cases: |
Krasimir Georgiev | b6ccd38 | 2017-02-02 14:36:50 +0000 | [diff] [blame] | 2310 | // |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 2311 | // // first line |
| 2312 | // // second line |
Krasimir Georgiev | b6ccd38 | 2017-02-02 14:36:50 +0000 | [diff] [blame] | 2313 | // |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 2314 | // and: |
Krasimir Georgiev | b6ccd38 | 2017-02-02 14:36:50 +0000 | [diff] [blame] | 2315 | // |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 2316 | // int i; // first line |
| 2317 | // // second line |
Krasimir Georgiev | b6ccd38 | 2017-02-02 14:36:50 +0000 | [diff] [blame] | 2318 | // |
Krasimir Georgiev | 8432161 | 2017-01-30 19:18:55 +0000 | [diff] [blame] | 2319 | // and: |
Krasimir Georgiev | b6ccd38 | 2017-02-02 14:36:50 +0000 | [diff] [blame] | 2320 | // |
Krasimir Georgiev | 8432161 | 2017-01-30 19:18:55 +0000 | [diff] [blame] | 2321 | // do { // first line |
| 2322 | // // second line |
| 2323 | // int i; |
| 2324 | // } while (true); |
Krasimir Georgiev | b6ccd38 | 2017-02-02 14:36:50 +0000 | [diff] [blame] | 2325 | // |
| 2326 | // and: |
| 2327 | // |
| 2328 | // enum { |
| 2329 | // a, // first line |
| 2330 | // // second line |
| 2331 | // }; |
Krasimir Georgiev | 8432161 | 2017-01-30 19:18:55 +0000 | [diff] [blame] | 2332 | const FormatToken *MinColumnToken = Line.Tokens.front().Tok; |
| 2333 | |
| 2334 | // Scan for '{//'. If found, use the column of '{' as a min column for line |
| 2335 | // comment section continuation. |
| 2336 | const FormatToken *PreviousToken = nullptr; |
Krasimir Georgiev | d86c25d | 2017-03-10 13:09:29 +0000 | [diff] [blame] | 2337 | for (const UnwrappedLineNode &Node : Line.Tokens) { |
Krasimir Georgiev | 8432161 | 2017-01-30 19:18:55 +0000 | [diff] [blame] | 2338 | if (PreviousToken && PreviousToken->is(tok::l_brace) && |
| 2339 | isLineComment(*Node.Tok)) { |
| 2340 | MinColumnToken = PreviousToken; |
| 2341 | break; |
| 2342 | } |
| 2343 | PreviousToken = Node.Tok; |
Krasimir Georgiev | b6ccd38 | 2017-02-02 14:36:50 +0000 | [diff] [blame] | 2344 | |
| 2345 | // Grab the last newline preceding a token in this unwrapped line. |
| 2346 | if (Node.Tok->NewlinesBefore > 0) { |
| 2347 | MinColumnToken = Node.Tok; |
| 2348 | } |
Krasimir Georgiev | 8432161 | 2017-01-30 19:18:55 +0000 | [diff] [blame] | 2349 | } |
| 2350 | if (PreviousToken && PreviousToken->is(tok::l_brace)) { |
| 2351 | MinColumnToken = PreviousToken; |
| 2352 | } |
| 2353 | |
Krasimir Georgiev | ea222a7 | 2017-05-22 10:07:56 +0000 | [diff] [blame] | 2354 | return continuesLineComment(FormatTok, /*Previous=*/Line.Tokens.back().Tok, |
| 2355 | MinColumnToken); |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 2356 | } |
| 2357 | |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2358 | void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) { |
| 2359 | bool JustComments = Line->Tokens.empty(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 2360 | for (SmallVectorImpl<FormatToken *>::const_iterator |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2361 | I = CommentsBeforeNextToken.begin(), |
| 2362 | E = CommentsBeforeNextToken.end(); |
| 2363 | I != E; ++I) { |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 2364 | // Line comments that belong to the same line comment section are put on the |
| 2365 | // same line since later we might want to reflow content between them. |
Krasimir Georgiev | 753625b | 2017-01-31 13:32:38 +0000 | [diff] [blame] | 2366 | // Additional fine-grained breaking of line comment sections is controlled |
| 2367 | // by the class BreakableLineCommentSection in case it is desirable to keep |
| 2368 | // several line comment sections in the same unwrapped line. |
| 2369 | // |
| 2370 | // FIXME: Consider putting separate line comment sections as children to the |
| 2371 | // unwrapped line instead. |
Krasimir Georgiev | 00c5c72 | 2017-02-02 15:32:19 +0000 | [diff] [blame] | 2372 | (*I)->ContinuesLineCommentSection = |
Krasimir Georgiev | ea222a7 | 2017-05-22 10:07:56 +0000 | [diff] [blame] | 2373 | continuesLineCommentSection(**I, *Line, CommentPragmasRegex); |
Krasimir Georgiev | b6ccd38 | 2017-02-02 14:36:50 +0000 | [diff] [blame] | 2374 | if (isOnNewLine(**I) && JustComments && !(*I)->ContinuesLineCommentSection) |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2375 | addUnwrappedLine(); |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2376 | pushToken(*I); |
| 2377 | } |
Daniel Jasper | e60cba1 | 2015-05-13 11:35:53 +0000 | [diff] [blame] | 2378 | if (NewlineBeforeNext && JustComments) |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2379 | addUnwrappedLine(); |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2380 | CommentsBeforeNextToken.clear(); |
| 2381 | } |
| 2382 | |
Krasimir Georgiev | 3e05105 | 2017-07-24 14:51:59 +0000 | [diff] [blame] | 2383 | void UnwrappedLineParser::nextToken(int LevelDifference) { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 2384 | if (eof()) |
| 2385 | return; |
Manuel Klimek | 1fcbe67 | 2014-04-11 12:27:47 +0000 | [diff] [blame] | 2386 | flushComments(isOnNewLine(*FormatTok)); |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2387 | pushToken(FormatTok); |
Manuel Klimek | 89628f6 | 2017-09-20 09:51:03 +0000 | [diff] [blame] | 2388 | FormatToken *Previous = FormatTok; |
Daniel Jasper | 1dcbbcfc | 2016-03-14 19:21:36 +0000 | [diff] [blame] | 2389 | if (Style.Language != FormatStyle::LK_JavaScript) |
Krasimir Georgiev | 3e05105 | 2017-07-24 14:51:59 +0000 | [diff] [blame] | 2390 | readToken(LevelDifference); |
Daniel Jasper | 1dcbbcfc | 2016-03-14 19:21:36 +0000 | [diff] [blame] | 2391 | else |
| 2392 | readTokenWithJavaScriptASI(); |
Manuel Klimek | e411aa8 | 2017-09-20 09:29:37 +0000 | [diff] [blame] | 2393 | FormatTok->Previous = Previous; |
Daniel Jasper | b9a4990 | 2016-01-09 15:56:28 +0000 | [diff] [blame] | 2394 | } |
| 2395 | |
Krasimir Georgiev | f62f958 | 2017-02-08 10:30:44 +0000 | [diff] [blame] | 2396 | void UnwrappedLineParser::distributeComments( |
| 2397 | const SmallVectorImpl<FormatToken *> &Comments, |
| 2398 | const FormatToken *NextTok) { |
| 2399 | // Whether or not a line comment token continues a line is controlled by |
Krasimir Georgiev | ea222a7 | 2017-05-22 10:07:56 +0000 | [diff] [blame] | 2400 | // the method continuesLineCommentSection, with the following caveat: |
Krasimir Georgiev | f62f958 | 2017-02-08 10:30:44 +0000 | [diff] [blame] | 2401 | // |
| 2402 | // Define a trail of Comments to be a nonempty proper postfix of Comments such |
| 2403 | // that each comment line from the trail is aligned with the next token, if |
| 2404 | // the next token exists. If a trail exists, the beginning of the maximal |
| 2405 | // trail is marked as a start of a new comment section. |
| 2406 | // |
| 2407 | // For example in this code: |
| 2408 | // |
| 2409 | // int a; // line about a |
| 2410 | // // line 1 about b |
| 2411 | // // line 2 about b |
| 2412 | // int b; |
| 2413 | // |
| 2414 | // the two lines about b form a maximal trail, so there are two sections, the |
| 2415 | // first one consisting of the single comment "// line about a" and the |
| 2416 | // second one consisting of the next two comments. |
| 2417 | if (Comments.empty()) |
| 2418 | return; |
| 2419 | bool ShouldPushCommentsInCurrentLine = true; |
| 2420 | bool HasTrailAlignedWithNextToken = false; |
| 2421 | unsigned StartOfTrailAlignedWithNextToken = 0; |
| 2422 | if (NextTok) { |
| 2423 | // We are skipping the first element intentionally. |
| 2424 | for (unsigned i = Comments.size() - 1; i > 0; --i) { |
| 2425 | if (Comments[i]->OriginalColumn == NextTok->OriginalColumn) { |
| 2426 | HasTrailAlignedWithNextToken = true; |
| 2427 | StartOfTrailAlignedWithNextToken = i; |
| 2428 | } |
| 2429 | } |
| 2430 | } |
| 2431 | for (unsigned i = 0, e = Comments.size(); i < e; ++i) { |
| 2432 | FormatToken *FormatTok = Comments[i]; |
Manuel Klimek | 89628f6 | 2017-09-20 09:51:03 +0000 | [diff] [blame] | 2433 | if (HasTrailAlignedWithNextToken && i == StartOfTrailAlignedWithNextToken) { |
Krasimir Georgiev | f62f958 | 2017-02-08 10:30:44 +0000 | [diff] [blame] | 2434 | FormatTok->ContinuesLineCommentSection = false; |
| 2435 | } else { |
| 2436 | FormatTok->ContinuesLineCommentSection = |
Krasimir Georgiev | ea222a7 | 2017-05-22 10:07:56 +0000 | [diff] [blame] | 2437 | continuesLineCommentSection(*FormatTok, *Line, CommentPragmasRegex); |
Krasimir Georgiev | f62f958 | 2017-02-08 10:30:44 +0000 | [diff] [blame] | 2438 | } |
| 2439 | if (!FormatTok->ContinuesLineCommentSection && |
| 2440 | (isOnNewLine(*FormatTok) || FormatTok->IsFirst)) { |
| 2441 | ShouldPushCommentsInCurrentLine = false; |
| 2442 | } |
| 2443 | if (ShouldPushCommentsInCurrentLine) { |
| 2444 | pushToken(FormatTok); |
| 2445 | } else { |
| 2446 | CommentsBeforeNextToken.push_back(FormatTok); |
| 2447 | } |
| 2448 | } |
| 2449 | } |
| 2450 | |
Krasimir Georgiev | 3e05105 | 2017-07-24 14:51:59 +0000 | [diff] [blame] | 2451 | void UnwrappedLineParser::readToken(int LevelDifference) { |
Krasimir Georgiev | f62f958 | 2017-02-08 10:30:44 +0000 | [diff] [blame] | 2452 | SmallVector<FormatToken *, 1> Comments; |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2453 | do { |
| 2454 | FormatTok = Tokens->getNextToken(); |
Alexander Kornienko | c2ee9cf | 2014-03-13 13:59:48 +0000 | [diff] [blame] | 2455 | assert(FormatTok); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 2456 | while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) && |
| 2457 | (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) { |
Krasimir Georgiev | f62f958 | 2017-02-08 10:30:44 +0000 | [diff] [blame] | 2458 | distributeComments(Comments, FormatTok); |
| 2459 | Comments.clear(); |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2460 | // If there is an unfinished unwrapped line, we flush the preprocessor |
| 2461 | // directives only after that unwrapped line was finished later. |
Daniel Jasper | 29d39d5 | 2015-02-08 09:34:49 +0000 | [diff] [blame] | 2462 | bool SwitchToPreprocessorLines = !Line->Tokens.empty(); |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2463 | ScopedLineState BlockState(*this, SwitchToPreprocessorLines); |
Krasimir Georgiev | 3e05105 | 2017-07-24 14:51:59 +0000 | [diff] [blame] | 2464 | assert((LevelDifference >= 0 || |
| 2465 | static_cast<unsigned>(-LevelDifference) <= Line->Level) && |
| 2466 | "LevelDifference makes Line->Level negative"); |
| 2467 | Line->Level += LevelDifference; |
Alexander Kornienko | b1be9d6 | 2013-04-03 12:38:53 +0000 | [diff] [blame] | 2468 | // Comments stored before the preprocessor directive need to be output |
| 2469 | // before the preprocessor directive, at the same level as the |
| 2470 | // preprocessor directive, as we consider them to apply to the directive. |
Manuel Klimek | 1fcbe67 | 2014-04-11 12:27:47 +0000 | [diff] [blame] | 2471 | flushComments(isOnNewLine(*FormatTok)); |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2472 | parsePPDirective(); |
| 2473 | } |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 2474 | while (FormatTok->Type == TT_ConflictStart || |
| 2475 | FormatTok->Type == TT_ConflictEnd || |
| 2476 | FormatTok->Type == TT_ConflictAlternative) { |
| 2477 | if (FormatTok->Type == TT_ConflictStart) { |
| 2478 | conditionalCompilationStart(/*Unreachable=*/false); |
| 2479 | } else if (FormatTok->Type == TT_ConflictAlternative) { |
| 2480 | conditionalCompilationAlternative(); |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 2481 | } else if (FormatTok->Type == TT_ConflictEnd) { |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 2482 | conditionalCompilationEnd(); |
| 2483 | } |
| 2484 | FormatTok = Tokens->getNextToken(); |
| 2485 | FormatTok->MustBreakBefore = true; |
| 2486 | } |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 2487 | |
Francois Ferrand | a98a95c | 2017-07-28 07:56:14 +0000 | [diff] [blame] | 2488 | if (!PPStack.empty() && (PPStack.back().Kind == PP_Unreachable) && |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 2489 | !Line->InPPDirective) { |
| 2490 | continue; |
| 2491 | } |
| 2492 | |
Krasimir Georgiev | f62f958 | 2017-02-08 10:30:44 +0000 | [diff] [blame] | 2493 | if (!FormatTok->Tok.is(tok::comment)) { |
| 2494 | distributeComments(Comments, FormatTok); |
| 2495 | Comments.clear(); |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2496 | return; |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2497 | } |
Krasimir Georgiev | f62f958 | 2017-02-08 10:30:44 +0000 | [diff] [blame] | 2498 | |
| 2499 | Comments.push_back(FormatTok); |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2500 | } while (!eof()); |
Krasimir Georgiev | f62f958 | 2017-02-08 10:30:44 +0000 | [diff] [blame] | 2501 | |
| 2502 | distributeComments(Comments, nullptr); |
| 2503 | Comments.clear(); |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2504 | } |
| 2505 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 2506 | void UnwrappedLineParser::pushToken(FormatToken *Tok) { |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 2507 | Line->Tokens.push_back(UnwrappedLineNode(Tok)); |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2508 | if (MustBreakBeforeNextToken) { |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 2509 | Line->Tokens.back().Tok->MustBreakBefore = true; |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2510 | MustBreakBeforeNextToken = false; |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 2511 | } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 2512 | } |
| 2513 | |
Daniel Jasper | 8d1832e | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 2514 | } // end namespace format |
| 2515 | } // end namespace clang |