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