Skip to content

Commit 46a9c10

Browse files
ZhongRuoyuLukacma
authored andcommitted
[clang-format] Fix repeated backslash insertion in macro line comments (llvm#164300)
Line comments in preprocessor directives were incorrectly marked as continuing the directive, causing clang-format to add backslashes after them on repeated runs. Backslashes appended after line comments in this way do not continue the PP directive because the following line would also become part of the comment. 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 PP directives and trailing line comments after macro bodies. Fixes llvm#164282. Signed-off-by: Ruoyu Zhong <[email protected]>
1 parent 4e2b35d commit 46a9c10

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 7 additions & 4 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+
const 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
@@ -1176,10 +1178,11 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
11761178
// about removing empty lines on closing blocks. Special case them here.
11771179
MaxEmptyLinesToKeep = 1;
11781180
}
1179-
unsigned Newlines =
1181+
const unsigned Newlines =
11801182
std::max(1u, std::min(Current.NewlinesBefore, MaxEmptyLinesToKeep));
1181-
bool ContinuePPDirective =
1182-
State.Line->InPPDirective && State.Line->Type != LT_ImportStatement;
1183+
const 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+
verifyFormat("#define SOME_MACRO(x) x\n"
851+
"#define FOO \\\n"
852+
" SOME_MACRO(1) + \\\n"
853+
" SOME_MACRO(2) // comment\n"
854+
" // comment",
855+
"#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)