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