Skip to content

Commit 77a38f4

Browse files
[clang-format] Supress aligning of trailing namespace comments
Fixes llvm#57504. Differential Revision: https://reviews.llvm.org/D138263
1 parent 8acb8a1 commit 77a38f4

File tree

5 files changed

+125
-7
lines changed

5 files changed

+125
-7
lines changed

clang/lib/Format/FormatToken.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ namespace format {
102102
TYPE(MacroBlockBegin) \
103103
TYPE(MacroBlockEnd) \
104104
TYPE(ModulePartitionColon) \
105+
TYPE(NamespaceLBrace) \
105106
TYPE(NamespaceMacro) \
107+
TYPE(NamespaceRBrace) \
106108
TYPE(NonNullAssertion) \
107109
TYPE(NullCoalescingEqual) \
108110
TYPE(NullCoalescingOperator) \

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,9 @@ FormatToken *UnwrappedLineParser::parseBlock(
806806
return IfLBrace;
807807
}
808808

809+
if (FormatTok->is(tok::r_brace) && Tok->is(TT_NamespaceLBrace))
810+
FormatTok->setFinalizedType(TT_NamespaceRBrace);
811+
809812
const bool IsFunctionRBrace =
810813
FormatTok->is(tok::r_brace) && Tok->is(TT_FunctionLBrace);
811814

@@ -2959,6 +2962,8 @@ void UnwrappedLineParser::parseNamespace() {
29592962
}
29602963
}
29612964
if (FormatTok->is(tok::l_brace)) {
2965+
FormatTok->setFinalizedType(TT_NamespaceLBrace);
2966+
29622967
if (ShouldBreakBeforeBrace(Style, InitialToken))
29632968
addUnwrappedLine();
29642969

clang/lib/Format/WhitespaceManager.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,11 +1081,10 @@ void WhitespaceManager::alignTrailingComments() {
10811081

10821082
if (i + 1 != e && Changes[i + 1].ContinuesPPDirective)
10831083
ChangeMaxColumn -= 2;
1084-
// If this comment follows an } in column 0, it probably documents the
1085-
// closing of a namespace and we don't want to align it.
1086-
bool FollowsRBraceInColumn0 = i > 0 && Changes[i].NewlinesBefore == 0 &&
1087-
Changes[i - 1].Tok->is(tok::r_brace) &&
1088-
Changes[i - 1].StartOfTokenColumn == 0;
1084+
1085+
// We don't want to align namespace end comments.
1086+
bool DontAlignThisComment = i > 0 && Changes[i].NewlinesBefore == 0 &&
1087+
Changes[i - 1].Tok->is(TT_NamespaceRBrace);
10891088
bool WasAlignedWithStartOfNextLine = false;
10901089
if (Changes[i].NewlinesBefore >= 1) { // A comment on its own line.
10911090
unsigned CommentColumn = SourceMgr.getSpellingColumnNumber(
@@ -1105,7 +1104,7 @@ void WhitespaceManager::alignTrailingComments() {
11051104
}
11061105
}
11071106
if (Style.AlignTrailingComments.Kind == FormatStyle::TCAS_Never ||
1108-
FollowsRBraceInColumn0) {
1107+
DontAlignThisComment) {
11091108
alignTrailingComments(StartOfSequence, i, MinColumn);
11101109
MinColumn = ChangeMinColumn;
11111110
MaxColumn = ChangeMinColumn;

clang/unittests/Format/FormatTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20316,7 +20316,7 @@ TEST_F(FormatTest, WhitesmithsBraceBreaking) {
2031620316
" int x;\n"
2031720317
" };\n"
2031820318
" } // namespace b\n"
20319-
" } // namespace a",
20319+
" } // namespace a",
2032020320
WhitesmithsBraceStyle);
2032120321

2032220322
verifyFormat("void f()\n"

clang/unittests/Format/FormatTestComments.cpp

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3087,6 +3087,118 @@ TEST_F(FormatTestComments, AlignTrailingCommentsLeave) {
30873087
Style));
30883088
}
30893089

3090+
TEST_F(FormatTestComments, DontAlignNamespaceComments) {
3091+
FormatStyle Style = getLLVMStyle();
3092+
Style.NamespaceIndentation = FormatStyle::NI_All;
3093+
Style.NamespaceMacros.push_back("TESTSUITE");
3094+
Style.ShortNamespaceLines = 0;
3095+
3096+
StringRef Input = "namespace A {\n"
3097+
" TESTSUITE(B) {\n"
3098+
" namespace C {\n"
3099+
" namespace D {} // namespace D\n"
3100+
" std::string Foo = Bar; // Comment\n"
3101+
" std::string BazString = Baz; // C2\n"
3102+
" } // namespace C\n"
3103+
" }\n"
3104+
"} // NaMeSpAcE A";
3105+
3106+
EXPECT_TRUE(Style.FixNamespaceComments);
3107+
EXPECT_EQ(Style.AlignTrailingComments.Kind, FormatStyle::TCAS_Always);
3108+
verifyFormat("namespace A {\n"
3109+
" TESTSUITE(B) {\n"
3110+
" namespace C {\n"
3111+
" namespace D {} // namespace D\n"
3112+
" std::string Foo = Bar; // Comment\n"
3113+
" std::string BazString = Baz; // C2\n"
3114+
" } // namespace C\n"
3115+
" } // TESTSUITE(B)\n"
3116+
"} // NaMeSpAcE A",
3117+
Input, Style);
3118+
3119+
Style.AlignTrailingComments.Kind = FormatStyle::TCAS_Never;
3120+
verifyFormat("namespace A {\n"
3121+
" TESTSUITE(B) {\n"
3122+
" namespace C {\n"
3123+
" namespace D {} // namespace D\n"
3124+
" std::string Foo = Bar; // Comment\n"
3125+
" std::string BazString = Baz; // C2\n"
3126+
" } // namespace C\n"
3127+
" } // TESTSUITE(B)\n"
3128+
"} // NaMeSpAcE A",
3129+
Input, Style);
3130+
3131+
Style.AlignTrailingComments.Kind = FormatStyle::TCAS_Leave;
3132+
verifyFormat("namespace A {\n"
3133+
" TESTSUITE(B) {\n"
3134+
" namespace C {\n"
3135+
" namespace D {} // namespace D\n"
3136+
" std::string Foo = Bar; // Comment\n"
3137+
" std::string BazString = Baz; // C2\n"
3138+
" } // namespace C\n"
3139+
" } // TESTSUITE(B)\n"
3140+
"} // NaMeSpAcE A",
3141+
Input, Style);
3142+
3143+
Style.FixNamespaceComments = false;
3144+
Style.AlignTrailingComments.Kind = FormatStyle::TCAS_Always;
3145+
verifyFormat("namespace A {\n"
3146+
" TESTSUITE(B) {\n"
3147+
" namespace C {\n"
3148+
" namespace D {} // namespace D\n"
3149+
" std::string Foo = Bar; // Comment\n"
3150+
" std::string BazString = Baz; // C2\n"
3151+
" } // namespace C\n"
3152+
" }\n"
3153+
"} // NaMeSpAcE A",
3154+
Input, Style);
3155+
3156+
Style.AlignTrailingComments.Kind = FormatStyle::TCAS_Never;
3157+
verifyFormat("namespace A {\n"
3158+
" TESTSUITE(B) {\n"
3159+
" namespace C {\n"
3160+
" namespace D {} // namespace D\n"
3161+
" std::string Foo = Bar; // Comment\n"
3162+
" std::string BazString = Baz; // C2\n"
3163+
" } // namespace C\n"
3164+
" }\n"
3165+
"} // NaMeSpAcE A",
3166+
Input, Style);
3167+
3168+
Style.AlignTrailingComments.Kind = FormatStyle::TCAS_Leave;
3169+
verifyFormat("namespace A {\n"
3170+
" TESTSUITE(B) {\n"
3171+
" namespace C {\n"
3172+
" namespace D {} // namespace D\n"
3173+
" std::string Foo = Bar; // Comment\n"
3174+
" std::string BazString = Baz; // C2\n"
3175+
" } // namespace C\n"
3176+
" }\n"
3177+
"} // NaMeSpAcE A",
3178+
Input, Style);
3179+
3180+
Style.AlignTrailingComments.Kind = FormatStyle::TCAS_Always;
3181+
Style.FixNamespaceComments = true;
3182+
Input = "namespace A {\n"
3183+
" int Foo;\n"
3184+
" int Bar;\n"
3185+
"}\n"
3186+
"// Comment";
3187+
3188+
#if 0
3189+
// FIXME: The following comment is aligned with the namespace comment.
3190+
verifyFormat("namespace A {\n"
3191+
" int Foo;\n"
3192+
" int Bar;\n"
3193+
"} // namespace A\n"
3194+
" // Comment",
3195+
Input, Style);
3196+
#endif
3197+
3198+
Style.FixNamespaceComments = false;
3199+
verifyFormat(Input, Style);
3200+
}
3201+
30903202
TEST_F(FormatTestComments, AlignsBlockCommentDecorations) {
30913203
EXPECT_EQ("/*\n"
30923204
" */",

0 commit comments

Comments
 (0)