Skip to content

Commit 4f19c01

Browse files
committed
[clang-format] Fix repeated backslash insertion in macro line comments
Line comments in preprocessor directives were incorrectly marked as continuing the directive, causing clang-format to add backslashes after them on repeated runs. Line comments cannot span multiple lines with backslashes, so they should not be treated as PP directive continuations. Fix by unsetting InPPDirective in WhitespaceManager::replaceWhitespace for line comments in two places: when breaking lines and when formatting tokens on the same line. This stops the spurious backslash insertion for both standalone line comments after preprocessor directives and trailing line comments after macro bodies. Fixes #164282.
1 parent b2574c9 commit 4f19c01

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -798,9 +798,11 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
798798
}
799799

800800
if (!DryRun) {
801+
bool ContinuePPDirective =
802+
State.Line->InMacroBody && Current.isNot(TT_LineComment);
801803
Whitespaces.replaceWhitespace(Current, /*Newlines=*/0, Spaces,
802804
State.Column + Spaces + PPColumnCorrection,
803-
/*IsAligned=*/false, State.Line->InMacroBody);
805+
/*IsAligned=*/false, ContinuePPDirective);
804806
}
805807

806808
// If "BreakBeforeInheritanceComma" mode, don't break within the inheritance
@@ -1178,8 +1180,9 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
11781180
}
11791181
unsigned Newlines =
11801182
std::max(1u, std::min(Current.NewlinesBefore, MaxEmptyLinesToKeep));
1181-
bool ContinuePPDirective =
1182-
State.Line->InPPDirective && State.Line->Type != LT_ImportStatement;
1183+
bool ContinuePPDirective = State.Line->InPPDirective &&
1184+
State.Line->Type != LT_ImportStatement &&
1185+
Current.isNot(TT_LineComment);
11831186
Whitespaces.replaceWhitespace(Current, Newlines, State.Column, State.Column,
11841187
CurrentState.IsAligned, ContinuePPDirective);
11851188
}

clang/unittests/Format/FormatTestComments.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,25 @@ TEST_F(FormatTestComments, MultiLineCommentsInDefines) {
839839
getLLVMStyleWithColumns(17)));
840840
}
841841

842+
TEST_F(FormatTestComments, LineCommentsInMacrosDoNotGetEscapedNewlines) {
843+
FormatStyle Style = getLLVMStyleWithColumns(0);
844+
Style.ReflowComments = FormatStyle::RCS_Never;
845+
verifyFormat("#define FOO (1U) // comment\n"
846+
" // comment",
847+
Style);
848+
849+
Style.ColumnLimit = 32;
850+
EXPECT_EQ("#define SOME_MACRO(x) x\n"
851+
"#define FOO \\\n"
852+
" SOME_MACRO(1) + \\\n"
853+
" SOME_MACRO(2) // comment\n"
854+
" // comment",
855+
format("#define SOME_MACRO(x) x\n"
856+
"#define FOO SOME_MACRO(1) + SOME_MACRO(2) // comment\n"
857+
" // comment",
858+
Style));
859+
}
860+
842861
TEST_F(FormatTestComments, ParsesCommentsAdjacentToPPDirectives) {
843862
EXPECT_EQ("namespace {}\n// Test\n#define A",
844863
format("namespace {}\n // Test\n#define A"));

0 commit comments

Comments
 (0)