[clang] clang-format: Add "AllowShortNamespacesOnASingleLine" option (PR #105597)

Owen Pan via cfe-commits cfe-commits at lists.llvm.org
Sun Sep 22 17:59:22 PDT 2024


================
@@ -27981,6 +27981,132 @@ TEST_F(FormatTest, BreakBinaryOperations) {
                Style);
 }
 
+TEST_F(FormatTest, ShortNamespacesOption) {
+  auto BaseStyle = getLLVMStyle();
+  BaseStyle.AllowShortNamespacesOnASingleLine = true;
+  BaseStyle.FixNamespaceComments = false;
+
+  auto Style = BaseStyle;
+
+  // Basic functionality.
+  verifyFormat("namespace foo { class bar; }", Style);
+  verifyFormat("namespace foo::bar { class baz; }", Style);
+  verifyFormat("namespace { class bar; }", Style);
+  verifyFormat("namespace foo {\n"
+               "class bar;\n"
+               "class baz;\n"
+               "}",
+               Style);
+
+  // Trailing comments prevent merging.
+  verifyFormat("namespace foo {\n"
+               "namespace baz { class qux; } // comment\n"
+               "}",
+               Style);
+
+  // Make sure code doesn't walk too far on unbalanced code.
+  verifyFormat("namespace foo {", Style);
+  verifyFormat("namespace foo {\n"
+               "class baz;",
+               Style);
+  verifyFormat("namespace foo {\n"
+               "namespace bar { class baz; }",
+               Style);
+
+  // Nested namespaces.
+  verifyFormat("namespace foo { namespace bar { class baz; } }", Style);
+  verifyFormat("namespace foo {\n"
+               "namespace bar { class baz; }\n"
+               "namespace qux { class quux; }\n"
+               "}",
+               Style);
+
+  // Varying inner content.
+  verifyFormat("namespace foo {\n"
+               "int f() { return 5; }\n"
+               "}",
+               Style);
+  verifyFormat("namespace foo { template <T> struct bar; }", Style);
+  verifyFormat("namespace foo { constexpr int num = 42; }", Style);
+
+  // Validate wrapping scenarios around the ColumnLimit.
+  Style.ColumnLimit = 64;
+
+  // Validate just under the ColumnLimit.
+  verifyFormat(
+      "namespace foo { namespace bar { namespace baz { class qux; } } }",
+      Style);
+
+  // Validate just over the ColumnLimit.
+  verifyFormat("namespace foo {\n"
+               "namespace bar { namespace baz { class quux; } }\n"
+               "}",
+               Style);
+
+  verifyFormat("namespace foo {\n"
+               "namespace bar {\n"
+               "namespace baz { namespace qux { class quux; } }\n"
+               "}\n"
+               "}",
+               Style);
+
+  // Validate that the ColumnLimit logic accounts for trailing content as well.
+  verifyFormat("namespace foo {\n"
+               "namespace bar { namespace baz { class qux; } }\n"
+               "} // extra",
+               Style);
+
+  // No ColumnLimit, allows long nested one-liners, but also leaves multi-line
+  // instances alone.
+  Style.ColumnLimit = 0;
+  verifyFormat(
+      "namespace foo { namespace bar { namespace baz { class qux; } } }",
+      Style);
+
+  verifyNoChange("namespace foo {\n"
+                 "namespace bar { namespace baz { class qux; } }\n"
+                 "}",
+                 Style);
----------------
owenca wrote:

```suggestion
// FIXME
#if 0
  verifyNoChange("namespace foo {\n"
                 "namespace bar { namespace baz { class qux; } }\n"
                 "}",
                 Style);
#endif
```
Shouldn't this be merged as well? See this potential [bug](https://discourse.llvm.org/t/clang-format-compactnamespaces-in-combination-with-columnlimit-0/78808).

https://github.com/llvm/llvm-project/pull/105597


More information about the cfe-commits mailing list