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