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), StateStack({LexerState::NORMAL}),
30 Column(0), TrailingWhitespace(0), SourceMgr(SourceMgr), ID(ID),
31 Style(Style), IdentTable(getFormattingLangOpts(Style)),
32 Keywords(IdentTable), Encoding(Encoding), FirstInLineIndex(0),
33 FormattingDisabled(false), MacroBlockBeginRegex(Style.MacroBlockBegin),
34 MacroBlockEndRegex(Style.MacroBlockEnd) {
35 Lex.reset(new Lexer(ID, SourceMgr.getBuffer(ID), SourceMgr,
36 getFormattingLangOpts(Style)));
37 Lex->SetKeepWhitespaceMode(true);
39 for (const std::string &ForEachMacro : Style.ForEachMacros)
40 ForEachMacros.push_back(&IdentTable.get(ForEachMacro));
41 std::sort(ForEachMacros.begin(), ForEachMacros.end());
44 ArrayRef<FormatToken *> FormatTokenLexer::lex() {
45 assert(Tokens.empty());
46 assert(FirstInLineIndex == 0);
48 Tokens.push_back(getNextToken());
49 if (Style.Language == FormatStyle::LK_JavaScript) {
50 tryParseJSRegexLiteral();
51 handleTemplateStrings();
53 tryMergePreviousTokens();
54 if (Tokens.back()->NewlinesBefore > 0 || Tokens.back()->IsMultiline)
55 FirstInLineIndex = Tokens.size() - 1;
56 } while (Tokens.back()->Tok.isNot(tok::eof));
60 void FormatTokenLexer::tryMergePreviousTokens() {
61 if (tryMerge_TMacro())
63 if (tryMergeConflictMarkers())
65 if (tryMergeLessLess())
67 if (tryMergeNSStringLiteral())
70 if (Style.Language == FormatStyle::LK_JavaScript) {
71 static const tok::TokenKind JSIdentity[] = {tok::equalequal, tok::equal};
72 static const tok::TokenKind JSNotIdentity[] = {tok::exclaimequal,
74 static const tok::TokenKind JSShiftEqual[] = {tok::greater, tok::greater,
76 static const tok::TokenKind JSRightArrow[] = {tok::equal, tok::greater};
77 static const tok::TokenKind JSExponentiation[] = {tok::star, tok::star};
78 static const tok::TokenKind JSExponentiationEqual[] = {tok::star,
81 // FIXME: Investigate what token type gives the correct operator priority.
82 if (tryMergeTokens(JSIdentity, TT_BinaryOperator))
84 if (tryMergeTokens(JSNotIdentity, TT_BinaryOperator))
86 if (tryMergeTokens(JSShiftEqual, TT_BinaryOperator))
88 if (tryMergeTokens(JSRightArrow, TT_JsFatArrow))
90 if (tryMergeTokens(JSExponentiation, TT_JsExponentiation))
92 if (tryMergeTokens(JSExponentiationEqual, TT_JsExponentiationEqual)) {
93 Tokens.back()->Tok.setKind(tok::starequal);
98 if (Style.Language == FormatStyle::LK_Java) {
99 static const tok::TokenKind JavaRightLogicalShift[] = {tok::greater,
102 static const tok::TokenKind JavaRightLogicalShiftAssign[] = {tok::greater,
105 if (tryMergeTokens(JavaRightLogicalShift, TT_BinaryOperator))
107 if (tryMergeTokens(JavaRightLogicalShiftAssign, TT_BinaryOperator))
112 bool FormatTokenLexer::tryMergeNSStringLiteral() {
113 if (Tokens.size() < 2)
115 auto &At = *(Tokens.end() - 2);
116 auto &String = *(Tokens.end() - 1);
117 if (!At->is(tok::at) || !String->is(tok::string_literal))
119 At->Tok.setKind(tok::string_literal);
120 At->TokenText = StringRef(At->TokenText.begin(),
121 String->TokenText.end() - At->TokenText.begin());
122 At->ColumnWidth += String->ColumnWidth;
123 At->Type = TT_ObjCStringLiteral;
124 Tokens.erase(Tokens.end() - 1);
128 bool FormatTokenLexer::tryMergeLessLess() {
129 // Merge X,less,less,Y into X,lessless,Y unless X or Y is less.
130 if (Tokens.size() < 3)
133 bool FourthTokenIsLess = false;
134 if (Tokens.size() > 3)
135 FourthTokenIsLess = (Tokens.end() - 4)[0]->is(tok::less);
137 auto First = Tokens.end() - 3;
138 if (First[2]->is(tok::less) || First[1]->isNot(tok::less) ||
139 First[0]->isNot(tok::less) || FourthTokenIsLess)
142 // Only merge if there currently is no whitespace between the two "<".
143 if (First[1]->WhitespaceRange.getBegin() !=
144 First[1]->WhitespaceRange.getEnd())
147 First[0]->Tok.setKind(tok::lessless);
148 First[0]->TokenText = "<<";
149 First[0]->ColumnWidth += 1;
150 Tokens.erase(Tokens.end() - 2);
154 bool FormatTokenLexer::tryMergeTokens(ArrayRef<tok::TokenKind> Kinds,
156 if (Tokens.size() < Kinds.size())
159 SmallVectorImpl<FormatToken *>::const_iterator First =
160 Tokens.end() - Kinds.size();
161 if (!First[0]->is(Kinds[0]))
163 unsigned AddLength = 0;
164 for (unsigned i = 1; i < Kinds.size(); ++i) {
165 if (!First[i]->is(Kinds[i]) ||
166 First[i]->WhitespaceRange.getBegin() !=
167 First[i]->WhitespaceRange.getEnd())
169 AddLength += First[i]->TokenText.size();
171 Tokens.resize(Tokens.size() - Kinds.size() + 1);
172 First[0]->TokenText = StringRef(First[0]->TokenText.data(),
173 First[0]->TokenText.size() + AddLength);
174 First[0]->ColumnWidth += AddLength;
175 First[0]->Type = NewType;
179 // Returns \c true if \p Tok can only be followed by an operand in JavaScript.
180 bool FormatTokenLexer::precedesOperand(FormatToken *Tok) {
181 // NB: This is not entirely correct, as an r_paren can introduce an operand
182 // location in e.g. `if (foo) /bar/.exec(...);`. That is a rare enough
183 // corner case to not matter in practice, though.
184 return Tok->isOneOf(tok::period, tok::l_paren, tok::comma, tok::l_brace,
185 tok::r_brace, tok::l_square, tok::semi, tok::exclaim,
186 tok::colon, tok::question, tok::tilde) ||
187 Tok->isOneOf(tok::kw_return, tok::kw_do, tok::kw_case, tok::kw_throw,
188 tok::kw_else, tok::kw_new, tok::kw_delete, tok::kw_void,
189 tok::kw_typeof, Keywords.kw_instanceof, Keywords.kw_in) ||
190 Tok->isBinaryOperator();
193 bool FormatTokenLexer::canPrecedeRegexLiteral(FormatToken *Prev) {
197 // Regex literals can only follow after prefix unary operators, not after
198 // postfix unary operators. If the '++' is followed by a non-operand
199 // introducing token, the slash here is the operand and not the start of a
201 // `!` is an unary prefix operator, but also a post-fix operator that casts
202 // away nullability, so the same check applies.
203 if (Prev->isOneOf(tok::plusplus, tok::minusminus, tok::exclaim))
204 return (Tokens.size() < 3 || precedesOperand(Tokens[Tokens.size() - 3]));
206 // The previous token must introduce an operand location where regex
207 // literals can occur.
208 if (!precedesOperand(Prev))
214 // Tries to parse a JavaScript Regex literal starting at the current token,
215 // if that begins with a slash and is in a location where JavaScript allows
216 // regex literals. Changes the current token to a regex literal and updates
217 // its text if successful.
218 void FormatTokenLexer::tryParseJSRegexLiteral() {
219 FormatToken *RegexToken = Tokens.back();
220 if (!RegexToken->isOneOf(tok::slash, tok::slashequal))
223 FormatToken *Prev = nullptr;
224 for (auto I = Tokens.rbegin() + 1, E = Tokens.rend(); I != E; ++I) {
225 // NB: Because previous pointers are not initialized yet, this cannot use
226 // Token.getPreviousNonComment.
227 if ((*I)->isNot(tok::comment)) {
233 if (!canPrecedeRegexLiteral(Prev))
236 // 'Manually' lex ahead in the current file buffer.
237 const char *Offset = Lex->getBufferLocation();
238 const char *RegexBegin = Offset - RegexToken->TokenText.size();
239 StringRef Buffer = Lex->getBuffer();
240 bool InCharacterClass = false;
241 bool HaveClosingSlash = false;
242 for (; !HaveClosingSlash && Offset != Buffer.end(); ++Offset) {
243 // Regular expressions are terminated with a '/', which can only be
244 // escaped using '\' or a character class between '[' and ']'.
245 // See http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.5.
248 // Skip the escaped character.
252 InCharacterClass = true;
255 InCharacterClass = false;
258 if (!InCharacterClass)
259 HaveClosingSlash = true;
264 RegexToken->Type = TT_RegexLiteral;
265 // Treat regex literals like other string_literals.
266 RegexToken->Tok.setKind(tok::string_literal);
267 RegexToken->TokenText = StringRef(RegexBegin, Offset - RegexBegin);
268 RegexToken->ColumnWidth = RegexToken->TokenText.size();
270 resetLexer(SourceMgr.getFileOffset(Lex->getSourceLocation(Offset)));
273 void FormatTokenLexer::handleTemplateStrings() {
274 FormatToken *BacktickToken = Tokens.back();
276 if (BacktickToken->is(tok::l_brace)) {
277 StateStack.push(LexerState::NORMAL);
280 if (BacktickToken->is(tok::r_brace)) {
281 if (StateStack.size() == 1)
284 if (StateStack.top() != LexerState::TEMPLATE_STRING)
286 // If back in TEMPLATE_STRING, fallthrough and continue parsing the
287 } else if (BacktickToken->is(tok::unknown) &&
288 BacktickToken->TokenText == "`") {
289 StateStack.push(LexerState::TEMPLATE_STRING);
291 return; // Not actually a template
294 // 'Manually' lex ahead in the current file buffer.
295 const char *Offset = Lex->getBufferLocation();
296 const char *TmplBegin = Offset - BacktickToken->TokenText.size(); // at "`"
297 for (; Offset != Lex->getBuffer().end(); ++Offset) {
298 if (Offset[0] == '`') {
302 if (Offset[0] == '\\') {
303 ++Offset; // Skip the escaped character.
304 } else if (Offset + 1 < Lex->getBuffer().end() && Offset[0] == '$' &&
306 // '${' introduces an expression interpolation in the template string.
307 StateStack.push(LexerState::NORMAL);
313 StringRef LiteralText(TmplBegin, Offset - TmplBegin + 1);
314 BacktickToken->Type = TT_TemplateString;
315 BacktickToken->Tok.setKind(tok::string_literal);
316 BacktickToken->TokenText = LiteralText;
318 // Adjust width for potentially multiline string literals.
319 size_t FirstBreak = LiteralText.find('\n');
320 StringRef FirstLineText = FirstBreak == StringRef::npos
322 : LiteralText.substr(0, FirstBreak);
323 BacktickToken->ColumnWidth = encoding::columnWidthWithTabs(
324 FirstLineText, BacktickToken->OriginalColumn, Style.TabWidth, Encoding);
325 size_t LastBreak = LiteralText.rfind('\n');
326 if (LastBreak != StringRef::npos) {
327 BacktickToken->IsMultiline = true;
328 unsigned StartColumn = 0; // The template tail spans the entire line.
329 BacktickToken->LastLineColumnWidth = encoding::columnWidthWithTabs(
330 LiteralText.substr(LastBreak + 1, LiteralText.size()), StartColumn,
331 Style.TabWidth, Encoding);
334 SourceLocation loc = Offset < Lex->getBuffer().end()
335 ? Lex->getSourceLocation(Offset + 1)
336 : SourceMgr.getLocForEndOfFile(ID);
337 resetLexer(SourceMgr.getFileOffset(loc));
340 bool FormatTokenLexer::tryMerge_TMacro() {
341 if (Tokens.size() < 4)
343 FormatToken *Last = Tokens.back();
344 if (!Last->is(tok::r_paren))
347 FormatToken *String = Tokens[Tokens.size() - 2];
348 if (!String->is(tok::string_literal) || String->IsMultiline)
351 if (!Tokens[Tokens.size() - 3]->is(tok::l_paren))
354 FormatToken *Macro = Tokens[Tokens.size() - 4];
355 if (Macro->TokenText != "_T")
358 const char *Start = Macro->TokenText.data();
359 const char *End = Last->TokenText.data() + Last->TokenText.size();
360 String->TokenText = StringRef(Start, End - Start);
361 String->IsFirst = Macro->IsFirst;
362 String->LastNewlineOffset = Macro->LastNewlineOffset;
363 String->WhitespaceRange = Macro->WhitespaceRange;
364 String->OriginalColumn = Macro->OriginalColumn;
365 String->ColumnWidth = encoding::columnWidthWithTabs(
366 String->TokenText, String->OriginalColumn, Style.TabWidth, Encoding);
367 String->NewlinesBefore = Macro->NewlinesBefore;
368 String->HasUnescapedNewline = Macro->HasUnescapedNewline;
373 Tokens.back() = String;
377 bool FormatTokenLexer::tryMergeConflictMarkers() {
378 if (Tokens.back()->NewlinesBefore == 0 && Tokens.back()->isNot(tok::eof))
381 // Conflict lines look like:
382 // <marker> <text from the vcs>
384 // >>>>>>> /file/in/file/system at revision 1234
386 // We merge all tokens in a line that starts with a conflict marker
387 // into a single token with a special token type that the unwrapped line
388 // parser will use to correctly rebuild the underlying code.
391 // Get the position of the first token in the line.
392 unsigned FirstInLineOffset;
393 std::tie(ID, FirstInLineOffset) = SourceMgr.getDecomposedLoc(
394 Tokens[FirstInLineIndex]->getStartOfNonWhitespace());
395 StringRef Buffer = SourceMgr.getBuffer(ID)->getBuffer();
396 // Calculate the offset of the start of the current line.
397 auto LineOffset = Buffer.rfind('\n', FirstInLineOffset);
398 if (LineOffset == StringRef::npos) {
404 auto FirstSpace = Buffer.find_first_of(" \n", LineOffset);
406 if (FirstSpace == StringRef::npos) {
407 LineStart = Buffer.substr(LineOffset);
409 LineStart = Buffer.substr(LineOffset, FirstSpace - LineOffset);
412 TokenType Type = TT_Unknown;
413 if (LineStart == "<<<<<<<" || LineStart == ">>>>") {
414 Type = TT_ConflictStart;
415 } else if (LineStart == "|||||||" || LineStart == "=======" ||
416 LineStart == "====") {
417 Type = TT_ConflictAlternative;
418 } else if (LineStart == ">>>>>>>" || LineStart == "<<<<") {
419 Type = TT_ConflictEnd;
422 if (Type != TT_Unknown) {
423 FormatToken *Next = Tokens.back();
425 Tokens.resize(FirstInLineIndex + 1);
426 // We do not need to build a complete token here, as we will skip it
427 // during parsing anyway (as we must not touch whitespace around conflict
429 Tokens.back()->Type = Type;
430 Tokens.back()->Tok.setKind(tok::kw___unknown_anytype);
432 Tokens.push_back(Next);
439 FormatToken *FormatTokenLexer::getStashedToken() {
440 // Create a synthesized second '>' or '<' token.
441 Token Tok = FormatTok->Tok;
442 StringRef TokenText = FormatTok->TokenText;
444 unsigned OriginalColumn = FormatTok->OriginalColumn;
445 FormatTok = new (Allocator.Allocate()) FormatToken;
446 FormatTok->Tok = Tok;
447 SourceLocation TokLocation =
448 FormatTok->Tok.getLocation().getLocWithOffset(Tok.getLength() - 1);
449 FormatTok->Tok.setLocation(TokLocation);
450 FormatTok->WhitespaceRange = SourceRange(TokLocation, TokLocation);
451 FormatTok->TokenText = TokenText;
452 FormatTok->ColumnWidth = 1;
453 FormatTok->OriginalColumn = OriginalColumn + 1;
458 FormatToken *FormatTokenLexer::getNextToken() {
459 if (StateStack.top() == LexerState::TOKEN_STASHED) {
461 return getStashedToken();
464 FormatTok = new (Allocator.Allocate()) FormatToken;
465 readRawToken(*FormatTok);
466 SourceLocation WhitespaceStart =
467 FormatTok->Tok.getLocation().getLocWithOffset(-TrailingWhitespace);
468 FormatTok->IsFirst = IsFirstToken;
469 IsFirstToken = false;
471 // Consume and record whitespace until we find a significant token.
472 unsigned WhitespaceLength = TrailingWhitespace;
473 while (FormatTok->Tok.is(tok::unknown)) {
474 StringRef Text = FormatTok->TokenText;
475 auto EscapesNewline = [&](int pos) {
476 // A '\r' here is just part of '\r\n'. Skip it.
477 if (pos >= 0 && Text[pos] == '\r')
479 // See whether there is an odd number of '\' before this.
480 // FIXME: This is wrong. A '\' followed by a newline is always removed,
481 // regardless of whether there is another '\' before it.
482 // FIXME: Newlines can also be escaped by a '?' '?' '/' trigraph.
484 for (; pos >= 0; --pos, ++count)
485 if (Text[pos] != '\\')
489 // FIXME: This miscounts tok:unknown tokens that are not just
490 // whitespace, e.g. a '`' character.
491 for (int i = 0, e = Text.size(); i != e; ++i) {
494 ++FormatTok->NewlinesBefore;
495 FormatTok->HasUnescapedNewline = !EscapesNewline(i - 1);
496 FormatTok->LastNewlineOffset = WhitespaceLength + i + 1;
500 FormatTok->LastNewlineOffset = WhitespaceLength + i + 1;
511 Column += Style.TabWidth - Column % Style.TabWidth;
514 if (i + 1 == e || (Text[i + 1] != '\r' && Text[i + 1] != '\n'))
515 FormatTok->Type = TT_ImplicitStringLiteral;
518 FormatTok->Type = TT_ImplicitStringLiteral;
521 if (FormatTok->Type == TT_ImplicitStringLiteral)
525 if (FormatTok->is(TT_ImplicitStringLiteral))
527 WhitespaceLength += FormatTok->Tok.getLength();
529 readRawToken(*FormatTok);
532 // In case the token starts with escaped newlines, we want to
533 // take them into account as whitespace - this pattern is quite frequent
534 // in macro definitions.
535 // FIXME: Add a more explicit test.
536 while (FormatTok->TokenText.size() > 1 && FormatTok->TokenText[0] == '\\' &&
537 FormatTok->TokenText[1] == '\n') {
538 ++FormatTok->NewlinesBefore;
539 WhitespaceLength += 2;
540 FormatTok->LastNewlineOffset = 2;
542 FormatTok->TokenText = FormatTok->TokenText.substr(2);
545 FormatTok->WhitespaceRange = SourceRange(
546 WhitespaceStart, WhitespaceStart.getLocWithOffset(WhitespaceLength));
548 FormatTok->OriginalColumn = Column;
550 TrailingWhitespace = 0;
551 if (FormatTok->Tok.is(tok::comment)) {
552 // FIXME: Add the trimmed whitespace to Column.
553 StringRef UntrimmedText = FormatTok->TokenText;
554 FormatTok->TokenText = FormatTok->TokenText.rtrim(" \t\v\f");
555 TrailingWhitespace = UntrimmedText.size() - FormatTok->TokenText.size();
556 } else if (FormatTok->Tok.is(tok::raw_identifier)) {
557 IdentifierInfo &Info = IdentTable.get(FormatTok->TokenText);
558 FormatTok->Tok.setIdentifierInfo(&Info);
559 FormatTok->Tok.setKind(Info.getTokenID());
560 if (Style.Language == FormatStyle::LK_Java &&
561 FormatTok->isOneOf(tok::kw_struct, tok::kw_union, tok::kw_delete,
563 FormatTok->Tok.setKind(tok::identifier);
564 FormatTok->Tok.setIdentifierInfo(nullptr);
565 } else if (Style.Language == FormatStyle::LK_JavaScript &&
566 FormatTok->isOneOf(tok::kw_struct, tok::kw_union,
568 FormatTok->Tok.setKind(tok::identifier);
569 FormatTok->Tok.setIdentifierInfo(nullptr);
571 } else if (FormatTok->Tok.is(tok::greatergreater)) {
572 FormatTok->Tok.setKind(tok::greater);
573 FormatTok->TokenText = FormatTok->TokenText.substr(0, 1);
575 StateStack.push(LexerState::TOKEN_STASHED);
576 } else if (FormatTok->Tok.is(tok::lessless)) {
577 FormatTok->Tok.setKind(tok::less);
578 FormatTok->TokenText = FormatTok->TokenText.substr(0, 1);
580 StateStack.push(LexerState::TOKEN_STASHED);
583 // Now FormatTok is the next non-whitespace token.
585 StringRef Text = FormatTok->TokenText;
586 size_t FirstNewlinePos = Text.find('\n');
587 if (FirstNewlinePos == StringRef::npos) {
588 // FIXME: ColumnWidth actually depends on the start column, we need to
589 // take this into account when the token is moved.
590 FormatTok->ColumnWidth =
591 encoding::columnWidthWithTabs(Text, Column, Style.TabWidth, Encoding);
592 Column += FormatTok->ColumnWidth;
594 FormatTok->IsMultiline = true;
595 // FIXME: ColumnWidth actually depends on the start column, we need to
596 // take this into account when the token is moved.
597 FormatTok->ColumnWidth = encoding::columnWidthWithTabs(
598 Text.substr(0, FirstNewlinePos), Column, Style.TabWidth, Encoding);
600 // The last line of the token always starts in column 0.
601 // Thus, the length can be precomputed even in the presence of tabs.
602 FormatTok->LastLineColumnWidth = encoding::columnWidthWithTabs(
603 Text.substr(Text.find_last_of('\n') + 1), 0, Style.TabWidth, Encoding);
604 Column = FormatTok->LastLineColumnWidth;
608 if (!(Tokens.size() > 0 && Tokens.back()->Tok.getIdentifierInfo() &&
609 Tokens.back()->Tok.getIdentifierInfo()->getPPKeywordID() ==
611 std::find(ForEachMacros.begin(), ForEachMacros.end(),
612 FormatTok->Tok.getIdentifierInfo()) != ForEachMacros.end()) {
613 FormatTok->Type = TT_ForEachMacro;
614 } else if (FormatTok->is(tok::identifier)) {
615 if (MacroBlockBeginRegex.match(Text)) {
616 FormatTok->Type = TT_MacroBlockBegin;
617 } else if (MacroBlockEndRegex.match(Text)) {
618 FormatTok->Type = TT_MacroBlockEnd;
626 void FormatTokenLexer::readRawToken(FormatToken &Tok) {
627 Lex->LexFromRawLexer(Tok.Tok);
628 Tok.TokenText = StringRef(SourceMgr.getCharacterData(Tok.Tok.getLocation()),
629 Tok.Tok.getLength());
630 // For formatting, treat unterminated string literals like normal string
632 if (Tok.is(tok::unknown)) {
633 if (!Tok.TokenText.empty() && Tok.TokenText[0] == '"') {
634 Tok.Tok.setKind(tok::string_literal);
635 Tok.IsUnterminatedLiteral = true;
636 } else if (Style.Language == FormatStyle::LK_JavaScript &&
637 Tok.TokenText == "''") {
638 Tok.Tok.setKind(tok::string_literal);
642 if (Style.Language == FormatStyle::LK_JavaScript &&
643 Tok.is(tok::char_constant)) {
644 Tok.Tok.setKind(tok::string_literal);
647 if (Tok.is(tok::comment) && (Tok.TokenText == "// clang-format on" ||
648 Tok.TokenText == "/* clang-format on */")) {
649 FormattingDisabled = false;
652 Tok.Finalized = FormattingDisabled;
654 if (Tok.is(tok::comment) && (Tok.TokenText == "// clang-format off" ||
655 Tok.TokenText == "/* clang-format off */")) {
656 FormattingDisabled = true;
660 void FormatTokenLexer::resetLexer(unsigned Offset) {
661 StringRef Buffer = SourceMgr.getBufferData(ID);
662 Lex.reset(new Lexer(SourceMgr.getLocForStartOfFile(ID),
663 getFormattingLangOpts(Style), Buffer.begin(),
664 Buffer.begin() + Offset, Buffer.end()));
665 Lex->SetKeepWhitespaceMode(true);
666 TrailingWhitespace = 0;
669 } // namespace format