@@ -4484,7 +4484,7 @@ TEST_F(FormatTest, FormatsCompactNamespaces) {
4484
4484
"} // namespace A",
4485
4485
Style);
4486
4486
4487
- Style.ColumnLimit = 40 ;
4487
+ Style.ColumnLimit = 41 ;
4488
4488
verifyFormat("namespace aaaaaaaaaa {\n"
4489
4489
"namespace bbbbbbbbbb {\n"
4490
4490
"}} // namespace aaaaaaaaaa::bbbbbbbbbb",
@@ -4504,6 +4504,16 @@ TEST_F(FormatTest, FormatsCompactNamespaces) {
4504
4504
"} // namespace bbbbbb\n"
4505
4505
"} // namespace aaaaaa",
4506
4506
Style);
4507
+
4508
+ verifyFormat("namespace a { namespace b { namespace c {\n"
4509
+ "}}} // namespace a::b::c",
4510
+ Style);
4511
+
4512
+ verifyFormat("namespace a { namespace b {\n"
4513
+ "namespace cc {\n"
4514
+ "}}} // namespace a::b::cc",
4515
+ Style);
4516
+
4507
4517
Style.ColumnLimit = 80;
4508
4518
4509
4519
// Extra semicolon after 'inner' closing brace prevents merging
@@ -28318,6 +28328,7 @@ TEST_F(FormatTest, ShortNamespacesOption) {
28318
28328
auto BaseStyle = getLLVMStyle();
28319
28329
BaseStyle.AllowShortNamespacesOnASingleLine = true;
28320
28330
BaseStyle.FixNamespaceComments = false;
28331
+ BaseStyle.CompactNamespaces = true;
28321
28332
28322
28333
auto Style = BaseStyle;
28323
28334
@@ -28332,8 +28343,9 @@ TEST_F(FormatTest, ShortNamespacesOption) {
28332
28343
Style);
28333
28344
28334
28345
// Trailing comments prevent merging.
28335
- verifyFormat("namespace foo {\n"
28336
- "namespace baz { class qux; } // comment\n"
28346
+ verifyFormat("namespace foo { namespace baz {\n"
28347
+ "class qux;\n"
28348
+ "} // comment\n"
28337
28349
"}",
28338
28350
Style);
28339
28351
@@ -28348,12 +28360,23 @@ TEST_F(FormatTest, ShortNamespacesOption) {
28348
28360
28349
28361
// Nested namespaces.
28350
28362
verifyFormat("namespace foo { namespace bar { class baz; } }", Style);
28363
+
28364
+ // Without CompactNamespaces, we won't merge consecutive namespace
28365
+ // declarations
28366
+ Style.CompactNamespaces = false;
28367
+ verifyFormat("namespace foo {\n"
28368
+ "namespace bar { class baz; }\n"
28369
+ "}",
28370
+ Style);
28371
+
28351
28372
verifyFormat("namespace foo {\n"
28352
28373
"namespace bar { class baz; }\n"
28353
28374
"namespace qux { class quux; }\n"
28354
28375
"}",
28355
28376
Style);
28356
28377
28378
+ Style = BaseStyle;
28379
+
28357
28380
// Varying inner content.
28358
28381
verifyFormat("namespace foo {\n"
28359
28382
"int f() { return 5; }\n"
@@ -28362,7 +28385,7 @@ TEST_F(FormatTest, ShortNamespacesOption) {
28362
28385
verifyFormat("namespace foo { template <T> struct bar; }", Style);
28363
28386
verifyFormat("namespace foo { constexpr int num = 42; }", Style);
28364
28387
28365
- // Validate wrapping scenarios around the ColumnLimit.
28388
+ // Validate nested namespace wrapping scenarios around the ColumnLimit.
28366
28389
Style.ColumnLimit = 64;
28367
28390
28368
28391
// Validate just under the ColumnLimit.
@@ -28371,22 +28394,24 @@ TEST_F(FormatTest, ShortNamespacesOption) {
28371
28394
Style);
28372
28395
28373
28396
// Validate just over the ColumnLimit.
28374
- verifyFormat("namespace foo {\n"
28375
- "namespace bar { namespace baz { class quux; } } \n"
28376
- "}",
28397
+ verifyFormat("namespace foo { namespace baar { namespace baaz { \n"
28398
+ "class quux;\n"
28399
+ "}}} ",
28377
28400
Style);
28378
28401
28379
- verifyFormat("namespace foo {\n"
28380
- "namespace bar {\n"
28381
- "namespace baz { namespace qux { class quux; } }\n"
28382
- "}\n"
28383
- "}",
28384
- Style);
28402
+ verifyFormat(
28403
+ "namespace foo { namespace bar { namespace baz { namespace qux {\n"
28404
+ "class quux;\n"
28405
+ "}}}}",
28406
+ Style);
28385
28407
28386
28408
// Validate that the ColumnLimit logic accounts for trailing content as well.
28387
- verifyFormat("namespace foo {\n"
28388
- "namespace bar { namespace baz { class qux; } }\n"
28389
- "} // extra",
28409
+ verifyFormat("namespace foo { namespace bar { class qux; } } // extra",
28410
+ Style);
28411
+
28412
+ verifyFormat("namespace foo { namespace bar { namespace baz {\n"
28413
+ "class qux;\n"
28414
+ "}}} // extra",
28390
28415
Style);
28391
28416
28392
28417
// No ColumnLimit, allows long nested one-liners, but also leaves multi-line
@@ -28401,41 +28426,24 @@ TEST_F(FormatTest, ShortNamespacesOption) {
28401
28426
"}",
28402
28427
Style);
28403
28428
28404
- Style = BaseStyle;
28405
- Style.CompactNamespaces = true;
28406
28429
verifyFormat("namespace foo { namespace bar { class baz; } }", Style);
28407
28430
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
-
28417
- // This option doesn't really work with FixNamespaceComments and nested
28418
- // namespaces. Code should use the concatenated namespace syntax. e.g.
28419
- // 'namespace foo::bar'.
28431
+ // FIXME: Ideally AllowShortNamespacesOnASingleLine would disable the trailing
28432
+ // namespace comment from 'FixNamespaceComments', as it's not really necessary
28433
+ // in this scenario, but the two options work at very different layers of the
28434
+ // formatter, so I'm not sure how to make them interact.
28435
+ //
28436
+ // As it stands, the trailing comment will be added and likely make the line
28437
+ // too long to fit within the ColumnLimit, reducing the how likely the line
28438
+ // will still fit on a single line. The recommendation for now is to use the
28439
+ // concatenated namespace syntax instead. e.g. 'namespace foo::bar'
28420
28440
Style = BaseStyle;
28421
28441
Style.FixNamespaceComments = true;
28422
28442
28423
28443
verifyFormat(
28424
- "namespace foo {\n"
28425
- "namespace bar { namespace baz { class qux; } } // namespace bar\n"
28426
- "} // namespace foo",
28427
- "namespace foo { namespace bar { namespace baz { class qux; } } }",
28428
- Style);
28429
-
28430
- // This option doesn't really make any sense with ShortNamespaceLines = 0.
28431
- Style.ShortNamespaceLines = 0;
28432
-
28433
- verifyFormat(
28434
- "namespace foo {\n"
28435
- "namespace bar {\n"
28436
- "namespace baz { class qux; } // namespace baz\n"
28437
- "} // namespace bar\n"
28438
- "} // namespace foo",
28444
+ "namespace foo { namespace bar { namespace baz {\n"
28445
+ "class qux;\n"
28446
+ "}}} // namespace foo::bar::baz",
28439
28447
"namespace foo { namespace bar { namespace baz { class qux; } } }",
28440
28448
Style);
28441
28449
}
0 commit comments