Skip to content

Commit cddb9ce

Browse files
authored
[clang-format] Don't over-indent comment below unbraced body (#95354)
Fixes #45002.
1 parent 904c53d commit cddb9ce

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ class ScopedLineState {
112112
Parser.Line->PPLevel = PreBlockLine->PPLevel;
113113
Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
114114
Parser.Line->InMacroBody = PreBlockLine->InMacroBody;
115+
Parser.Line->UnbracedBodyLevel = PreBlockLine->UnbracedBodyLevel;
115116
}
116117

117118
~ScopedLineState() {
@@ -2708,7 +2709,9 @@ void UnwrappedLineParser::parseUnbracedBody(bool CheckEOF) {
27082709

27092710
addUnwrappedLine();
27102711
++Line->Level;
2712+
++Line->UnbracedBodyLevel;
27112713
parseStructuralElement();
2714+
--Line->UnbracedBodyLevel;
27122715

27132716
if (Tok) {
27142717
assert(!Line->InPPDirective);
@@ -4836,6 +4839,8 @@ void UnwrappedLineParser::readToken(int LevelDifference) {
48364839
PPBranchLevel > 0) {
48374840
Line->Level += PPBranchLevel;
48384841
}
4842+
assert(Line->Level >= Line->UnbracedBodyLevel);
4843+
Line->Level -= Line->UnbracedBodyLevel;
48394844
flushComments(isOnNewLine(*FormatTok));
48404845
parsePPDirective();
48414846
PreviousWasComment = FormatTok->is(tok::comment);

clang/lib/Format/UnwrappedLineParser.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ struct UnwrappedLine {
4949
/// Whether it is part of a macro body.
5050
bool InMacroBody = false;
5151

52+
/// Nesting level of unbraced body of a control statement.
53+
unsigned UnbracedBodyLevel = 0;
54+
5255
bool MustBeDeclaration = false;
5356

5457
/// Whether the parser has seen \c decltype(auto) in this line.

clang/unittests/Format/FormatTestComments.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,36 @@ TEST_F(FormatTestComments, KeepsLevelOfCommentBeforePPDirective) {
10871087
Style);
10881088
}
10891089

1090+
TEST_F(FormatTestComments, CommentsBetweenUnbracedBodyAndPPDirective) {
1091+
verifyFormat("{\n"
1092+
" if (a)\n"
1093+
" f(); // comment\n"
1094+
"#define A\n"
1095+
"}");
1096+
1097+
verifyFormat("{\n"
1098+
" while (a)\n"
1099+
" f();\n"
1100+
"// comment\n"
1101+
"#define A\n"
1102+
"}");
1103+
1104+
verifyNoChange("{\n"
1105+
" if (a)\n"
1106+
" f();\n"
1107+
" // comment\n"
1108+
"#define A\n"
1109+
"}");
1110+
1111+
verifyNoChange("{\n"
1112+
" while (a)\n"
1113+
" if (b)\n"
1114+
" f();\n"
1115+
" // comment\n"
1116+
"#define A\n"
1117+
"}");
1118+
}
1119+
10901120
TEST_F(FormatTestComments, SplitsLongLinesInComments) {
10911121
// FIXME: Do we need to fix up the " */" at the end?
10921122
// It doesn't look like any of our current logic triggers this.

0 commit comments

Comments
 (0)