[clang] [clang-format][NFC] Clean up FormatTokenLexer (PR #203825)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Jun 19 01:16:52 PDT 2026
https://github.com/owenca updated https://github.com/llvm/llvm-project/pull/203825
>From 67c171e6a54bebf747b43db6fe190dd0f653c6f6 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Sun, 14 Jun 2026 22:07:31 -0700
Subject: [PATCH] [clang-format][NFC] Clean up FormatTokenLexer
---
clang/lib/Format/FormatTokenLexer.cpp | 15 +++++++++++----
clang/lib/Format/FormatTokenLexer.h | 1 -
2 files changed, 11 insertions(+), 5 deletions(-)
diff --git a/clang/lib/Format/FormatTokenLexer.cpp b/clang/lib/Format/FormatTokenLexer.cpp
index 92571c012bdb2..0cf01875af833 100644
--- a/clang/lib/Format/FormatTokenLexer.cpp
+++ b/clang/lib/Format/FormatTokenLexer.cpp
@@ -33,8 +33,7 @@ FormatTokenLexer::FormatTokenLexer(
LangOpts(getFormattingLangOpts(Style)), SourceMgr(SourceMgr), ID(ID),
Style(Style), IdentTable(IdentTable), Keywords(IdentTable),
Encoding(Encoding), Allocator(Allocator), FirstInLineIndex(0),
- FormattingDisabled(false), FormatOffRegex(Style.OneLineFormatOffRegex),
- MacroBlockBeginRegex(Style.MacroBlockBegin),
+ FormattingDisabled(false), MacroBlockBeginRegex(Style.MacroBlockBegin),
MacroBlockEndRegex(Style.MacroBlockEnd), VerilogProtectedBlock(false) {
Lex = std::make_unique<Lexer>(ID, SourceMgr.getBufferOrFake(ID), SourceMgr,
LangOpts);
@@ -88,12 +87,14 @@ FormatTokenLexer::FormatTokenLexer(
ArrayRef<FormatToken *> FormatTokenLexer::lex() {
assert(Tokens.empty());
assert(FirstInLineIndex == 0);
+
enum { FO_None, FO_CurrentLine, FO_NextLine } FormatOff = FO_None;
+ llvm::Regex FormatOffRegex(Style.OneLineFormatOffRegex);
do {
Tokens.push_back(getNextToken());
+
auto &Tok = *Tokens.back();
- const auto NewlinesBefore = Tok.NewlinesBefore;
- switch (FormatOff) {
+ switch (const auto NewlinesBefore = Tok.NewlinesBefore; FormatOff) {
case FO_NextLine:
if (NewlinesBefore > 1) {
FormatOff = FO_None;
@@ -125,13 +126,16 @@ ArrayRef<FormatToken *> FormatTokenLexer::lex() {
}
}
}
+
if (Style.isJavaScript()) {
tryParseJSRegexLiteral();
handleTemplateStrings();
} else if (Style.isTextProto()) {
tryParsePythonComment();
}
+
tryMergePreviousTokens();
+
if (Style.isCSharp()) {
// This needs to come after tokens have been merged so that C#
// string literals are correctly identified.
@@ -140,9 +144,11 @@ ArrayRef<FormatToken *> FormatTokenLexer::lex() {
handleTableGenMultilineString();
handleTableGenNumericLikeIdentifier();
}
+
if (Tokens.back()->NewlinesBefore > 0 || Tokens.back()->IsMultiline)
FirstInLineIndex = Tokens.size() - 1;
} while (Tokens.back()->isNot(tok::eof));
+
if (Style.InsertNewlineAtEOF) {
auto &TokEOF = *Tokens.back();
if (TokEOF.NewlinesBefore == 0) {
@@ -150,6 +156,7 @@ ArrayRef<FormatToken *> FormatTokenLexer::lex() {
TokEOF.OriginalColumn = 0;
}
}
+
return Tokens;
}
diff --git a/clang/lib/Format/FormatTokenLexer.h b/clang/lib/Format/FormatTokenLexer.h
index 9f5b735efe1d0..3f8c6ba15173d 100644
--- a/clang/lib/Format/FormatTokenLexer.h
+++ b/clang/lib/Format/FormatTokenLexer.h
@@ -135,7 +135,6 @@ class FormatTokenLexer {
TemplateNames, TypeNames, VariableTemplates;
bool FormattingDisabled;
- llvm::Regex FormatOffRegex; // For one line.
llvm::Regex MacroBlockBeginRegex;
llvm::Regex MacroBlockEndRegex;
More information about the cfe-commits
mailing list