aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Format/FormatTokenLexer.cpp
diff options
context:
space:
mode:
authorOwen Pan <owenpiano@gmail.com>2025-04-30 19:56:22 -0700
committerOwen Pan <owenpiano@gmail.com>2025-04-30 19:58:59 -0700
commit8effc8da292bfacb823a7e3c4134296da481fedc (patch)
tree6877bf8d5ec5c48ca744e0bdb20cb6074d2985a1 /clang/lib/Format/FormatTokenLexer.cpp
parentc588224ca797886064a7a79f6c0114a6963c325e (diff)
downloadllvm-8effc8da292bfacb823a7e3c4134296da481fedc.zip
llvm-8effc8da292bfacb823a7e3c4134296da481fedc.tar.gz
llvm-8effc8da292bfacb823a7e3c4134296da481fedc.tar.bz2
Reland [clang-format] Add OneLineFormatOffRegex option (#137577)
Diffstat (limited to 'clang/lib/Format/FormatTokenLexer.cpp')
-rw-r--r--clang/lib/Format/FormatTokenLexer.cpp37
1 files changed, 36 insertions, 1 deletions
diff --git a/clang/lib/Format/FormatTokenLexer.cpp b/clang/lib/Format/FormatTokenLexer.cpp
index a4c94ac..944d30b 100644
--- a/clang/lib/Format/FormatTokenLexer.cpp
+++ b/clang/lib/Format/FormatTokenLexer.cpp
@@ -32,7 +32,8 @@ FormatTokenLexer::FormatTokenLexer(
LangOpts(getFormattingLangOpts(Style)), SourceMgr(SourceMgr), ID(ID),
Style(Style), IdentTable(IdentTable), Keywords(IdentTable),
Encoding(Encoding), Allocator(Allocator), FirstInLineIndex(0),
- FormattingDisabled(false), MacroBlockBeginRegex(Style.MacroBlockBegin),
+ FormattingDisabled(false), FormatOffRegex(Style.OneLineFormatOffRegex),
+ MacroBlockBeginRegex(Style.MacroBlockBegin),
MacroBlockEndRegex(Style.MacroBlockEnd) {
Lex.reset(new Lexer(ID, SourceMgr.getBufferOrFake(ID), SourceMgr, LangOpts));
Lex->SetKeepWhitespaceMode(true);
@@ -83,8 +84,42 @@ FormatTokenLexer::FormatTokenLexer(
ArrayRef<FormatToken *> FormatTokenLexer::lex() {
assert(Tokens.empty());
assert(FirstInLineIndex == 0);
+ enum { FO_None, FO_CurrentLine, FO_NextLine } FormatOff = FO_None;
do {
Tokens.push_back(getNextToken());
+ auto &Tok = *Tokens.back();
+ const auto NewlinesBefore = Tok.NewlinesBefore;
+ switch (FormatOff) {
+ case FO_CurrentLine:
+ if (NewlinesBefore == 0)
+ Tok.Finalized = true;
+ else
+ FormatOff = FO_None;
+ break;
+ case FO_NextLine:
+ if (NewlinesBefore > 1) {
+ FormatOff = FO_None;
+ } else {
+ Tok.Finalized = true;
+ FormatOff = FO_CurrentLine;
+ }
+ break;
+ default:
+ if (!FormattingDisabled && FormatOffRegex.match(Tok.TokenText)) {
+ if (Tok.is(tok::comment) &&
+ (NewlinesBefore > 0 || Tokens.size() == 1)) {
+ Tok.Finalized = true;
+ FormatOff = FO_NextLine;
+ } else {
+ for (auto *Token : reverse(Tokens)) {
+ Token->Finalized = true;
+ if (Token->NewlinesBefore > 0)
+ break;
+ }
+ FormatOff = FO_CurrentLine;
+ }
+ }
+ }
if (Style.isJavaScript()) {
tryParseJSRegexLiteral();
handleTemplateStrings();