[clang] ff4cccb - [clang-format] Fix a bug in indenting lambda comments with only tabs (#186862)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Mar 16 13:07:45 PDT 2026
Author: owenca
Date: 2026-03-16T13:07:40-07:00
New Revision: ff4cccb678397f2ce37496aa80cccb85e148b3c5
URL: https://github.com/llvm/llvm-project/commit/ff4cccb678397f2ce37496aa80cccb85e148b3c5
DIFF: https://github.com/llvm/llvm-project/commit/ff4cccb678397f2ce37496aa80cccb85e148b3c5.diff
LOG: [clang-format] Fix a bug in indenting lambda comments with only tabs (#186862)
Fixes #175151
Added:
Modified:
clang/lib/Format/BreakableToken.cpp
clang/unittests/Format/FormatTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/BreakableToken.cpp b/clang/lib/Format/BreakableToken.cpp
index 994a427517ffc..b60daffc0eb1c 100644
--- a/clang/lib/Format/BreakableToken.cpp
+++ b/clang/lib/Format/BreakableToken.cpp
@@ -1042,38 +1042,40 @@ BreakableComment::Split BreakableLineCommentSection::getReflowSplit(
void BreakableLineCommentSection::reflow(unsigned LineIndex,
WhitespaceManager &Whitespaces) const {
- if (LineIndex > 0 && Tokens[LineIndex] != Tokens[LineIndex - 1]) {
- // Reflow happens between tokens. Replace the whitespace between the
- // tokens by the empty string.
- Whitespaces.replaceWhitespace(
- *Tokens[LineIndex], /*Newlines=*/0, /*Spaces=*/0,
- /*StartOfTokenColumn=*/StartColumn, /*IsAligned=*/true,
- /*InPPDirective=*/false);
- } else if (LineIndex > 0) {
- // In case we're reflowing after the '\' in:
- //
- // // line comment \
- // // line 2
- //
- // the reflow happens inside the single comment token (it is a single line
- // comment with an unescaped newline).
- // Replace the whitespace between the '\' and '//' with the empty string.
- //
- // Offset points to after the '\' relative to start of the token.
- unsigned Offset = Lines[LineIndex - 1].data() +
- Lines[LineIndex - 1].size() -
- tokenAt(LineIndex - 1).TokenText.data();
- // WhitespaceLength is the number of chars between the '\' and the '//' on
- // the next line.
- unsigned WhitespaceLength =
- Lines[LineIndex].data() - tokenAt(LineIndex).TokenText.data() - Offset;
- Whitespaces.replaceWhitespaceInToken(*Tokens[LineIndex], Offset,
- /*ReplaceChars=*/WhitespaceLength,
- /*PreviousPostfix=*/"",
- /*CurrentPrefix=*/"",
- /*InPPDirective=*/false,
- /*Newlines=*/0,
- /*Spaces=*/0);
+ if (LineIndex > 0) {
+ if (Tokens[LineIndex] != Tokens[LineIndex - 1]) {
+ // Reflow happens between tokens. Replace the whitespace between the
+ // tokens by the empty string.
+ Whitespaces.replaceWhitespace(
+ *Tokens[LineIndex], /*Newlines=*/0, /*Spaces=*/0,
+ /*StartOfTokenColumn=*/StartColumn, /*IsAligned=*/true,
+ /*InPPDirective=*/false);
+ } else {
+ // In case we're reflowing after the '\' in:
+ //
+ // // line comment \
+ // // line 2
+ //
+ // the reflow happens inside the single comment token (it is a single line
+ // comment with an unescaped newline).
+ // Replace the whitespace between the '\' and '//' with the empty string.
+ //
+ // Offset points to after the '\' relative to start of the token.
+ unsigned Offset = Lines[LineIndex - 1].data() +
+ Lines[LineIndex - 1].size() -
+ tokenAt(LineIndex - 1).TokenText.data();
+ // WhitespaceLength is the number of chars between the '\' and the '//' on
+ // the next line.
+ unsigned WhitespaceLength = Lines[LineIndex].data() -
+ tokenAt(LineIndex).TokenText.data() - Offset;
+ Whitespaces.replaceWhitespaceInToken(*Tokens[LineIndex], Offset,
+ /*ReplaceChars=*/WhitespaceLength,
+ /*PreviousPostfix=*/"",
+ /*CurrentPrefix=*/"",
+ /*InPPDirective=*/false,
+ /*Newlines=*/0,
+ /*Spaces=*/0);
+ }
}
// Replace the indent and prefix of the token with the reflow prefix.
unsigned Offset =
@@ -1116,7 +1118,7 @@ void BreakableLineCommentSection::adaptStartOfLine(
/*Newlines=*/1,
/*Spaces=*/LineColumn,
/*StartOfTokenColumn=*/LineColumn,
- /*IsAligned=*/true,
+ /*IsAligned=*/tokenAt(0).NewlinesBefore == 0,
/*InPPDirective=*/false);
}
if (OriginalPrefix[LineIndex] != Prefix[LineIndex]) {
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 77676184f0d6a..4be9b3ea42930 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -17350,6 +17350,16 @@ TEST_F(FormatTest, ConfigurableUseOfTab) {
verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n"
" + cccccccccccccccccccc;",
Tab);
+
+ Tab.BreakBeforeBraces = FormatStyle::BS_Custom;
+ Tab.BraceWrapping.BeforeLambdaBody = true;
+ verifyNoChange("example(\n"
+ "\t[]\n"
+ "\t{\n"
+ "\t\t// foo\n"
+ "\t\t// bar\n"
+ "\t});",
+ Tab);
}
TEST_F(FormatTest, ZeroTabWidth) {
More information about the cfe-commits
mailing list