Skip to content

Commit 22967da

Browse files
author
Galen Elias
committed
Fix interaction with CompactNamespaces
1 parent a246202 commit 22967da

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

clang/lib/Format/UnwrappedLineFormatter.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -361,9 +361,18 @@ class LineJoiner {
361361
const auto *FirstNonComment = TheLine->getFirstNonComment();
362362
if (!FirstNonComment)
363363
return 0;
364+
364365
// FIXME: There are probably cases where we should use FirstNonComment
365366
// instead of TheLine->First.
366367

368+
if (TheLine->Last->is(tok::l_brace)) {
369+
if (Style.AllowShortNamespacesOnASingleLine &&
370+
TheLine->First->is(tok::kw_namespace)) {
371+
if (unsigned result = tryMergeNamespace(I, E, Limit))
372+
return result;
373+
}
374+
}
375+
367376
if (Style.CompactNamespaces) {
368377
if (const auto *NSToken = TheLine->First->getNamespaceToken()) {
369378
int J = 1;
@@ -421,14 +430,6 @@ class LineJoiner {
421430
return MergeShortFunctions ? tryMergeSimpleBlock(I, E, Limit) : 0;
422431
}
423432

424-
if (TheLine->Last->is(tok::l_brace)) {
425-
if (Style.AllowShortNamespacesOnASingleLine &&
426-
TheLine->First->is(tok::kw_namespace)) {
427-
if (unsigned result = tryMergeNamespace(I, E, Limit))
428-
return result;
429-
}
430-
}
431-
432433
// Try to merge a control statement block with left brace unwrapped.
433434
if (TheLine->Last->is(tok::l_brace) && FirstNonComment != TheLine->Last &&
434435
FirstNonComment->isOneOf(tok::kw_if, tok::kw_while, tok::kw_for,

clang/unittests/Format/FormatTest.cpp

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28315,9 +28315,11 @@ TEST_F(FormatTest, KeepFormFeed) {
2831528315
}
2831628316

2831728317
TEST_F(FormatTest, ShortNamespacesOption) {
28318-
FormatStyle Style = getLLVMStyle();
28319-
Style.AllowShortNamespacesOnASingleLine = true;
28320-
Style.FixNamespaceComments = false;
28318+
auto BaseStyle = getLLVMStyle();
28319+
BaseStyle.AllowShortNamespacesOnASingleLine = true;
28320+
BaseStyle.FixNamespaceComments = false;
28321+
28322+
auto Style = BaseStyle;
2832128323

2832228324
// Basic functionality.
2832328325
verifyFormat("namespace foo { class bar; }", Style);
@@ -28335,7 +28337,7 @@ TEST_F(FormatTest, ShortNamespacesOption) {
2833528337
"}",
2833628338
Style);
2833728339

28338-
// Make sure code doesn't walk to far on unbalanced code.
28340+
// Make sure code doesn't walk too far on unbalanced code.
2833928341
verifyFormat("namespace foo {", Style);
2834028342
verifyFormat("namespace foo {\n"
2834128343
"class baz;",
@@ -28348,7 +28350,7 @@ TEST_F(FormatTest, ShortNamespacesOption) {
2834828350
verifyFormat("namespace foo { namespace bar { class baz; } }", Style);
2834928351
verifyFormat("namespace foo {\n"
2835028352
"namespace bar { class baz; }\n"
28351-
"namespace quar { class quaz; }\n"
28353+
"namespace qux { class quux; }\n"
2835228354
"}",
2835328355
Style);
2835428356

@@ -28390,20 +28392,32 @@ TEST_F(FormatTest, ShortNamespacesOption) {
2839028392
// No ColumnLimit, allows long nested one-liners, but also leaves multi-line
2839128393
// instances alone.
2839228394
Style.ColumnLimit = 0;
28393-
verifyFormat("namespace foo { namespace bar { namespace baz { namespace qux "
28394-
"{ class quux; } } } }",
28395-
Style);
28395+
verifyFormat(
28396+
"namespace foo { namespace bar { namespace baz { class qux; } } }",
28397+
Style);
2839628398

2839728399
verifyNoChange("namespace foo {\n"
28398-
"namespace bar {\n"
28399-
"namespace baz { namespace qux { class quux; } }\n"
28400-
"}\n"
28400+
"namespace bar { namespace baz { class qux; } }\n"
2840128401
"}",
2840228402
Style);
2840328403

28404+
Style = BaseStyle;
28405+
Style.CompactNamespaces = true;
28406+
verifyFormat("namespace foo { namespace bar { class baz; } }", Style);
28407+
28408+
// If we can't merge an outer nested namespaces, but can merge an inner
28409+
// nested namespace, then CompactNamespaces will merge the outer namespace
28410+
// first, preventing the merging of the inner namespace
28411+
verifyFormat("namespace foo { namespace baz {\n"
28412+
"class qux;\n"
28413+
"} // comment\n"
28414+
"}",
28415+
Style);
28416+
2840428417
// This option doesn't really work with FixNamespaceComments and nested
2840528418
// namespaces. Code should use the concatenated namespace syntax. e.g.
2840628419
// 'namespace foo::bar'.
28420+
Style = BaseStyle;
2840728421
Style.FixNamespaceComments = true;
2840828422

2840928423
verifyFormat(

0 commit comments

Comments
 (0)