1 //===--- FormatTokenLexer.cpp - Lex FormatTokens -------------*- C++ ----*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief This file implements FormatTokenLexer, which tokenizes a source file
12 /// into a FormatToken stream suitable for ClangFormat.
14 //===----------------------------------------------------------------------===//
16 #include "FormatTokenLexer.h"
17 #include "FormatToken.h"
18 #include "clang/Basic/SourceLocation.h"
19 #include "clang/Basic/SourceManager.h"
20 #include "clang/Format/Format.h"
21 #include "llvm/Support/Regex.h"
26 FormatTokenLexer::FormatTokenLexer(const SourceManager &SourceMgr, FileID ID,
27 const FormatStyle &Style,
28 encoding::Encoding Encoding)
29 : FormatTok(nullptr), IsFirstToken(true), GreaterStashed(false),
30 LessStashed(false), Column(0), TrailingWhitespace(0),
31 SourceMgr(SourceMgr), ID(ID), Style(Style),
32 IdentTable(getFormattingLangOpts(Style)), Keywords(IdentTable),
33 Encoding(Encoding), FirstInLineIndex(0), FormattingDisabled(false),
34 MacroBlockBeginRegex(Style.MacroBlockBegin),
35 MacroBlockEndRegex(Style.MacroBlockEnd) {
36 Lex.reset(new Lexer(ID, SourceMgr.getBuffer(ID), SourceMgr,
37 getFormattingLangOpts(Style)));
38 Lex->SetKeepWhitespaceMode(true);
40 for (const std::string &ForEachMacro : Style.ForEachMacros)
41 ForEachMacros.push_back(&IdentTable.get(ForEachMacro));
42 std::sort(ForEachMacros.begin(), ForEachMacros.end());
45 ArrayRef<FormatToken *> FormatTokenLexer::lex() {
46 assert(Tokens.empty());
47 assert(FirstInLineIndex == 0);
49 Tokens.push_back(getNextToken());
50 if (Style.Language == FormatStyle::LK_JavaScript) {
51 tryParseJSRegexLiteral();
52 tryParseTemplateString();
54 tryMergePreviousTokens();
55 if (Tokens.back()->NewlinesBefore > 0 || Tokens.back()->IsMultiline)
56 FirstInLineIndex = Tokens.size() - 1;
57 } while (Tokens.back()->Tok.isNot(tok::eof));
61 void FormatTokenLexer::tryMergePreviousTokens() {
62 if (tryMerge_TMacro())
64 if (tryMergeConflictMarkers())
66 if (tryMergeLessLess())
69 if (Style.Language == FormatStyle::LK_JavaScript) {
70 static const tok::TokenKind JSIdentity[] = {tok::equalequal, tok::equal};
71 static const tok::TokenKind JSNotIdentity[] = {tok::exclaimequal,
73 static const tok::TokenKind JSShiftEqual[] = {tok::greater, tok::greater,
75 static const tok::TokenKind JSRightArrow[] = {tok::equal, tok::greater};
76 // FIXME: Investigate what token type gives the correct operator priority.
77 if (tryMergeTokens(JSIdentity, TT_BinaryOperator))
79 if (tryMergeTokens(JSNotIdentity, TT_BinaryOperator))
81 if (tryMergeTokens(JSShiftEqual, TT_BinaryOperator))
83 if (tryMergeTokens(JSRightArrow, TT_JsFatArrow))
88 bool FormatTokenLexer::tryMergeLessLess() {
89 // Merge X,less,less,Y into X,lessless,Y unless X or Y is less.
90 if (Tokens.size() < 3)
93 bool FourthTokenIsLess = false;
94 if (Tokens.size() > 3)
95 FourthTokenIsLess = (Tokens.end() - 4)[0]->is(tok::less);
97 auto First = Tokens.end() - 3;
98 if (First[2]->is(tok::less) || First[1]->isNot(tok::less) ||
99 First[0]->isNot(tok::less) || FourthTokenIsLess)
102 // Only merge if there currently is no whitespace between the two "<".
103 if (First[1]->WhitespaceRange.getBegin() !=
104 First[1]->WhitespaceRange.getEnd())
107 First[0]->Tok.setKind(tok::lessless);
108 First[0]->TokenText = "<<";
109 First[0]->ColumnWidth += 1;
110 Tokens.erase(Tokens.end() - 2);
114 bool FormatTokenLexer::tryMergeTokens(ArrayRef<tok::TokenKind> Kinds,
116 if (Tokens.size() < Kinds.size())
119 SmallVectorImpl<FormatToken *>::const_iterator First =
120 Tokens.end() - Kinds.size();
121 if (!First[0]->is(Kinds[0]))
123 unsigned AddLength = 0;
124 for (unsigned i = 1; i < Kinds.size(); ++i) {
125 if (!First[i]->is(Kinds[i]) ||
126 First[i]->WhitespaceRange.getBegin() !=
127 First[i]->WhitespaceRange.getEnd())
129 AddLength += First[i]->TokenText.size();
131 Tokens.resize(Tokens.size() - Kinds.size() + 1);
132 First[0]->TokenText = StringRef(First[0]->TokenText.data(),
133 First[0]->TokenText.size() + AddLength);
134 First[0]->ColumnWidth += AddLength;
135 First[0]->Type = NewType;
139 // Returns \c true if \p Tok can only be followed by an operand in JavaScript.
140 bool FormatTokenLexer::precedesOperand(FormatToken *Tok) {
141 // NB: This is not entirely correct, as an r_paren can introduce an operand
142 // location in e.g. `if (foo) /bar/.exec(...);`. That is a rare enough
143 // corner case to not matter in practice, though.
144 return Tok->isOneOf(tok::period, tok::l_paren, tok::comma, tok::l_brace,
145 tok::r_brace, tok::l_square, tok::semi, tok::exclaim,
146 tok::colon, tok::question, tok::tilde) ||
147 Tok->isOneOf(tok::kw_return, tok::kw_do, tok::kw_case, tok::kw_throw,
148 tok::kw_else, tok::kw_new, tok::kw_delete, tok::kw_void,
149 tok::kw_typeof, Keywords.kw_instanceof, Keywords.kw_in) ||
150 Tok->isBinaryOperator();
153 bool FormatTokenLexer::canPrecedeRegexLiteral(FormatToken *Prev) {
157 // Regex literals can only follow after prefix unary operators, not after
158 // postfix unary operators. If the '++' is followed by a non-operand
159 // introducing token, the slash here is the operand and not the start of a
161 if (Prev->isOneOf(tok::plusplus, tok::minusminus))
162 return (Tokens.size() < 3 || precedesOperand(Tokens[Tokens.size() - 3]));
164 // The previous token must introduce an operand location where regex
165 // literals can occur.
166 if (!precedesOperand(Prev))
172 // Tries to parse a JavaScript Regex literal starting at the current token,
173 // if that begins with a slash and is in a location where JavaScript allows
174 // regex literals. Changes the current token to a regex literal and updates
175 // its text if successful.
176 void FormatTokenLexer::tryParseJSRegexLiteral() {
177 FormatToken *RegexToken = Tokens.back();
178 if (!RegexToken->isOneOf(tok::slash, tok::slashequal))
181 FormatToken *Prev = nullptr;
182 for (auto I = Tokens.rbegin() + 1, E = Tokens.rend(); I != E; ++I) {
183 // NB: Because previous pointers are not initialized yet, this cannot use
184 // Token.getPreviousNonComment.
185 if ((*I)->isNot(tok::comment)) {
191 if (!canPrecedeRegexLiteral(Prev))
194 // 'Manually' lex ahead in the current file buffer.
195 const char *Offset = Lex->getBufferLocation();
196 const char *RegexBegin = Offset - RegexToken->TokenText.size();
197 StringRef Buffer = Lex->getBuffer();
198 bool InCharacterClass = false;
199 bool HaveClosingSlash = false;
200 for (; !HaveClosingSlash && Offset != Buffer.end(); ++Offset) {
201 // Regular expressions are terminated with a '/', which can only be
202 // escaped using '\' or a character class between '[' and ']'.
203 // See http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.5.
206 // Skip the escaped character.
210 InCharacterClass = true;
213 InCharacterClass = false;
216 if (!InCharacterClass)
217 HaveClosingSlash = true;
222 RegexToken->Type = TT_RegexLiteral;
223 // Treat regex literals like other string_literals.
224 RegexToken->Tok.setKind(tok::string_literal);
225 RegexToken->TokenText = StringRef(RegexBegin, Offset - RegexBegin);
226 RegexToken->ColumnWidth = RegexToken->TokenText.size();
228 resetLexer(SourceMgr.getFileOffset(Lex->getSourceLocation(Offset)));
231 void FormatTokenLexer::tryParseTemplateString() {
232 FormatToken *BacktickToken = Tokens.back();
233 if (!BacktickToken->is(tok::unknown) || BacktickToken->TokenText != "`")
236 // 'Manually' lex ahead in the current file buffer.
237 const char *Offset = Lex->getBufferLocation();
238 const char *TmplBegin = Offset - BacktickToken->TokenText.size(); // at "`"
239 for (; Offset != Lex->getBuffer().end() && *Offset != '`'; ++Offset) {
241 ++Offset; // Skip the escaped character.
244 StringRef LiteralText(TmplBegin, Offset - TmplBegin + 1);
245 BacktickToken->Type = TT_TemplateString;
246 BacktickToken->Tok.setKind(tok::string_literal);
247 BacktickToken->TokenText = LiteralText;
249 // Adjust width for potentially multiline string literals.
250 size_t FirstBreak = LiteralText.find('\n');
251 StringRef FirstLineText = FirstBreak == StringRef::npos
253 : LiteralText.substr(0, FirstBreak);
254 BacktickToken->ColumnWidth = encoding::columnWidthWithTabs(
255 FirstLineText, BacktickToken->OriginalColumn, Style.TabWidth, Encoding);
256 size_t LastBreak = LiteralText.rfind('\n');
257 if (LastBreak != StringRef::npos) {
258 BacktickToken->IsMultiline = true;
259 unsigned StartColumn = 0; // The template tail spans the entire line.
260 BacktickToken->LastLineColumnWidth = encoding::columnWidthWithTabs(
261 LiteralText.substr(LastBreak + 1, LiteralText.size()), StartColumn,
262 Style.TabWidth, Encoding);
265 resetLexer(SourceMgr.getFileOffset(Lex->getSourceLocation(Offset + 1)));
268 bool FormatTokenLexer::tryMerge_TMacro() {
269 if (Tokens.size() < 4)
271 FormatToken *Last = Tokens.back();
272 if (!Last->is(tok::r_paren))
275 FormatToken *String = Tokens[Tokens.size() - 2];
276 if (!String->is(tok::string_literal) || String->IsMultiline)
279 if (!Tokens[Tokens.size() - 3]->is(tok::l_paren))
282 FormatToken *Macro = Tokens[Tokens.size() - 4];
283 if (Macro->TokenText != "_T")
286 const char *Start = Macro->TokenText.data();
287 const char *End = Last->TokenText.data() + Last->TokenText.size();
288 String->TokenText = StringRef(Start, End - Start);
289 String->IsFirst = Macro->IsFirst;
290 String->LastNewlineOffset = Macro->LastNewlineOffset;
291 String->WhitespaceRange = Macro->WhitespaceRange;
292 String->OriginalColumn = Macro->OriginalColumn;
293 String->ColumnWidth = encoding::columnWidthWithTabs(
294 String->TokenText, String->OriginalColumn, Style.TabWidth, Encoding);
295 String->NewlinesBefore = Macro->NewlinesBefore;
296 String->HasUnescapedNewline = Macro->HasUnescapedNewline;
301 Tokens.back() = String;
305 bool FormatTokenLexer::tryMergeConflictMarkers() {
306 if (Tokens.back()->NewlinesBefore == 0 && Tokens.back()->isNot(tok::eof))
309 // Conflict lines look like:
310 // <marker> <text from the vcs>
312 // >>>>>>> /file/in/file/system at revision 1234
314 // We merge all tokens in a line that starts with a conflict marker
315 // into a single token with a special token type that the unwrapped line
316 // parser will use to correctly rebuild the underlying code.
319 // Get the position of the first token in the line.
320 unsigned FirstInLineOffset;
321 std::tie(ID, FirstInLineOffset) = SourceMgr.getDecomposedLoc(
322 Tokens[FirstInLineIndex]->getStartOfNonWhitespace());
323 StringRef Buffer = SourceMgr.getBuffer(ID)->getBuffer();
324 // Calculate the offset of the start of the current line.
325 auto LineOffset = Buffer.rfind('\n', FirstInLineOffset);
326 if (LineOffset == StringRef::npos) {
332 auto FirstSpace = Buffer.find_first_of(" \n", LineOffset);
334 if (FirstSpace == StringRef::npos) {
335 LineStart = Buffer.substr(LineOffset);
337 LineStart = Buffer.substr(LineOffset, FirstSpace - LineOffset);
340 TokenType Type = TT_Unknown;
341 if (LineStart == "<<<<<<<" || LineStart == ">>>>") {
342 Type = TT_ConflictStart;
343 } else if (LineStart == "|||||||" || LineStart == "=======" ||
344 LineStart == "====") {
345 Type = TT_ConflictAlternative;
346 } else if (LineStart == ">>>>>>>" || LineStart == "<<<<") {
347 Type = TT_ConflictEnd;
350 if (Type != TT_Unknown) {
351 FormatToken *Next = Tokens.back();
353 Tokens.resize(FirstInLineIndex + 1);
354 // We do not need to build a complete token here, as we will skip it
355 // during parsing anyway (as we must not touch whitespace around conflict
357 Tokens.back()->Type = Type;
358 Tokens.back()->Tok.setKind(tok::kw___unknown_anytype);
360 Tokens.push_back(Next);
367 FormatToken *FormatTokenLexer::getStashedToken() {
368 // Create a synthesized second '>' or '<' token.
369 Token Tok = FormatTok->Tok;
370 StringRef TokenText = FormatTok->TokenText;
372 unsigned OriginalColumn = FormatTok->OriginalColumn;
373 FormatTok = new (Allocator.Allocate()) FormatToken;
374 FormatTok->Tok = Tok;
375 SourceLocation TokLocation =
376 FormatTok->Tok.getLocation().getLocWithOffset(Tok.getLength() - 1);
377 FormatTok->Tok.setLocation(TokLocation);
378 FormatTok->WhitespaceRange = SourceRange(TokLocation, TokLocation);
379 FormatTok->TokenText = TokenText;
380 FormatTok->ColumnWidth = 1;
381 FormatTok->OriginalColumn = OriginalColumn + 1;
386 FormatToken *FormatTokenLexer::getNextToken() {
387 if (GreaterStashed) {
388 GreaterStashed = false;
389 return getStashedToken();
393 return getStashedToken();
396 FormatTok = new (Allocator.Allocate()) FormatToken;
397 readRawToken(*FormatTok);
398 SourceLocation WhitespaceStart =
399 FormatTok->Tok.getLocation().getLocWithOffset(-TrailingWhitespace);
400 FormatTok->IsFirst = IsFirstToken;
401 IsFirstToken = false;
403 // Consume and record whitespace until we find a significant token.
404 unsigned WhitespaceLength = TrailingWhitespace;
405 while (FormatTok->Tok.is(tok::unknown)) {
406 StringRef Text = FormatTok->TokenText;
407 auto EscapesNewline = [&](int pos) {
408 // A '\r' here is just part of '\r\n'. Skip it.
409 if (pos >= 0 && Text[pos] == '\r')
411 // See whether there is an odd number of '\' before this.
413 for (; pos >= 0; --pos, ++count)
414 if (Text[pos] != '\\')
418 // FIXME: This miscounts tok:unknown tokens that are not just
419 // whitespace, e.g. a '`' character.
420 for (int i = 0, e = Text.size(); i != e; ++i) {
423 ++FormatTok->NewlinesBefore;
424 FormatTok->HasUnescapedNewline = !EscapesNewline(i - 1);
425 FormatTok->LastNewlineOffset = WhitespaceLength + i + 1;
429 FormatTok->LastNewlineOffset = WhitespaceLength + i + 1;
440 Column += Style.TabWidth - Column % Style.TabWidth;
443 if (i + 1 == e || (Text[i + 1] != '\r' && Text[i + 1] != '\n'))
444 FormatTok->Type = TT_ImplicitStringLiteral;
447 FormatTok->Type = TT_ImplicitStringLiteral;
450 if (FormatTok->Type == TT_ImplicitStringLiteral)
454 if (FormatTok->is(TT_ImplicitStringLiteral))
456 WhitespaceLength += FormatTok->Tok.getLength();
458 readRawToken(*FormatTok);
461 // In case the token starts with escaped newlines, we want to
462 // take them into account as whitespace - this pattern is quite frequent
463 // in macro definitions.
464 // FIXME: Add a more explicit test.
465 while (FormatTok->TokenText.size() > 1 && FormatTok->TokenText[0] == '\\' &&
466 FormatTok->TokenText[1] == '\n') {
467 ++FormatTok->NewlinesBefore;
468 WhitespaceLength += 2;
469 FormatTok->LastNewlineOffset = 2;
471 FormatTok->TokenText = FormatTok->TokenText.substr(2);
474 FormatTok->WhitespaceRange = SourceRange(
475 WhitespaceStart, WhitespaceStart.getLocWithOffset(WhitespaceLength));
477 FormatTok->OriginalColumn = Column;
479 TrailingWhitespace = 0;
480 if (FormatTok->Tok.is(tok::comment)) {
481 // FIXME: Add the trimmed whitespace to Column.
482 StringRef UntrimmedText = FormatTok->TokenText;
483 FormatTok->TokenText = FormatTok->TokenText.rtrim(" \t\v\f");
484 TrailingWhitespace = UntrimmedText.size() - FormatTok->TokenText.size();
485 } else if (FormatTok->Tok.is(tok::raw_identifier)) {
486 IdentifierInfo &Info = IdentTable.get(FormatTok->TokenText);
487 FormatTok->Tok.setIdentifierInfo(&Info);
488 FormatTok->Tok.setKind(Info.getTokenID());
489 if (Style.Language == FormatStyle::LK_Java &&
490 FormatTok->isOneOf(tok::kw_struct, tok::kw_union, tok::kw_delete,
492 FormatTok->Tok.setKind(tok::identifier);
493 FormatTok->Tok.setIdentifierInfo(nullptr);
494 } else if (Style.Language == FormatStyle::LK_JavaScript &&
495 FormatTok->isOneOf(tok::kw_struct, tok::kw_union,
497 FormatTok->Tok.setKind(tok::identifier);
498 FormatTok->Tok.setIdentifierInfo(nullptr);
500 } else if (FormatTok->Tok.is(tok::greatergreater)) {
501 FormatTok->Tok.setKind(tok::greater);
502 FormatTok->TokenText = FormatTok->TokenText.substr(0, 1);
503 GreaterStashed = true;
504 } else if (FormatTok->Tok.is(tok::lessless)) {
505 FormatTok->Tok.setKind(tok::less);
506 FormatTok->TokenText = FormatTok->TokenText.substr(0, 1);
510 // Now FormatTok is the next non-whitespace token.
512 StringRef Text = FormatTok->TokenText;
513 size_t FirstNewlinePos = Text.find('\n');
514 if (FirstNewlinePos == StringRef::npos) {
515 // FIXME: ColumnWidth actually depends on the start column, we need to
516 // take this into account when the token is moved.
517 FormatTok->ColumnWidth =
518 encoding::columnWidthWithTabs(Text, Column, Style.TabWidth, Encoding);
519 Column += FormatTok->ColumnWidth;
521 FormatTok->IsMultiline = true;
522 // FIXME: ColumnWidth actually depends on the start column, we need to
523 // take this into account when the token is moved.
524 FormatTok->ColumnWidth = encoding::columnWidthWithTabs(
525 Text.substr(0, FirstNewlinePos), Column, Style.TabWidth, Encoding);
527 // The last line of the token always starts in column 0.
528 // Thus, the length can be precomputed even in the presence of tabs.
529 FormatTok->LastLineColumnWidth = encoding::columnWidthWithTabs(
530 Text.substr(Text.find_last_of('\n') + 1), 0, Style.TabWidth, Encoding);
531 Column = FormatTok->LastLineColumnWidth;
534 if (Style.Language == FormatStyle::LK_Cpp) {
535 if (!(Tokens.size() > 0 && Tokens.back()->Tok.getIdentifierInfo() &&
536 Tokens.back()->Tok.getIdentifierInfo()->getPPKeywordID() ==
538 std::find(ForEachMacros.begin(), ForEachMacros.end(),
539 FormatTok->Tok.getIdentifierInfo()) != ForEachMacros.end()) {
540 FormatTok->Type = TT_ForEachMacro;
541 } else if (FormatTok->is(tok::identifier)) {
542 if (MacroBlockBeginRegex.match(Text)) {
543 FormatTok->Type = TT_MacroBlockBegin;
544 } else if (MacroBlockEndRegex.match(Text)) {
545 FormatTok->Type = TT_MacroBlockEnd;
553 void FormatTokenLexer::readRawToken(FormatToken &Tok) {
554 Lex->LexFromRawLexer(Tok.Tok);
555 Tok.TokenText = StringRef(SourceMgr.getCharacterData(Tok.Tok.getLocation()),
556 Tok.Tok.getLength());
557 // For formatting, treat unterminated string literals like normal string
559 if (Tok.is(tok::unknown)) {
560 if (!Tok.TokenText.empty() && Tok.TokenText[0] == '"') {
561 Tok.Tok.setKind(tok::string_literal);
562 Tok.IsUnterminatedLiteral = true;
563 } else if (Style.Language == FormatStyle::LK_JavaScript &&
564 Tok.TokenText == "''") {
565 Tok.Tok.setKind(tok::string_literal);
569 if (Style.Language == FormatStyle::LK_JavaScript &&
570 Tok.is(tok::char_constant)) {
571 Tok.Tok.setKind(tok::string_literal);
574 if (Tok.is(tok::comment) && (Tok.TokenText == "// clang-format on" ||
575 Tok.TokenText == "/* clang-format on */")) {
576 FormattingDisabled = false;
579 Tok.Finalized = FormattingDisabled;
581 if (Tok.is(tok::comment) && (Tok.TokenText == "// clang-format off" ||
582 Tok.TokenText == "/* clang-format off */")) {
583 FormattingDisabled = true;
587 void FormatTokenLexer::resetLexer(unsigned Offset) {
588 StringRef Buffer = SourceMgr.getBufferData(ID);
589 Lex.reset(new Lexer(SourceMgr.getLocForStartOfFile(ID),
590 getFormattingLangOpts(Style), Buffer.begin(),
591 Buffer.begin() + Offset, Buffer.end()));
592 Lex->SetKeepWhitespaceMode(true);
593 TrailingWhitespace = 0;
596 } // namespace format