[clang] [clang-format] Add `AllowShortNamespacesOnASingleLine` option (PR #105597)
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Mon Dec 30 00:54:23 PST 2024
================
@@ -28314,6 +28320,116 @@ TEST_F(FormatTest, KeepFormFeed) {
Style);
}
+TEST_F(FormatTest, ShortNamespacesOption) {
+ auto BaseStyle = getLLVMStyle();
+ BaseStyle.AllowShortNamespacesOnASingleLine = true;
+ BaseStyle.FixNamespaceComments = false;
+ BaseStyle.CompactNamespaces = true;
+
+ 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 { namespace baz {\n"
+ "class qux;\n"
+ "} // 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);
+
+ // Without CompactNamespaces, we won't merge consecutive namespace
+ // declarations
+ Style.CompactNamespaces = false;
+ verifyFormat("namespace foo {\n"
+ "namespace bar { class baz; }\n"
+ "}",
+ Style);
+
+ verifyFormat("namespace foo {\n"
+ "namespace bar { class baz; }\n"
+ "namespace qux { class quux; }\n"
+ "}",
+ Style);
+
+ Style = BaseStyle;
+
+ // 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 nested namespace 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 { namespace baar { namespace baaz {\n"
+ "class quux;\n"
+ "}}}",
+ Style);
+
+ verifyFormat(
+ "namespace foo { namespace bar { namespace baz { namespace qux {\n"
+ "class quux;\n"
+ "}}}}",
+ Style);
+
+ // Validate that the ColumnLimit logic accounts for trailing content as well.
+ verifyFormat("namespace foo { namespace bar { class qux; } } // extra",
+ Style);
+
+ verifyFormat("namespace foo { namespace bar { namespace baz {\n"
+ "class qux;\n"
+ "}}} // extra",
+ Style);
+
+ // FIXME: Ideally AllowShortNamespacesOnASingleLine would disable the trailing
+ // namespace comment from 'FixNamespaceComments', as it's not really necessary
+ // in this scenario, but the two options work at very different layers of the
+ // formatter, so I'm not sure how to make them interact.
+ //
+ // As it stands, the trailing comment will be added and likely make the line
+ // too long to fit within the ColumnLimit, reducing the how likely the line
+ // will still fit on a single line. The recommendation for now is to use the
+ // concatenated namespace syntax instead. e.g. 'namespace foo::bar'
+ Style = BaseStyle;
+ Style.FixNamespaceComments = true;
+
----------------
owenca wrote:
```suggestion
```
https://github.com/llvm/llvm-project/pull/105597
More information about the cfe-commits
mailing list