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

Owen Pan via cfe-commits cfe-commits at lists.llvm.org
Sun Dec 29 22:08:55 PST 2024


================
@@ -4504,6 +4504,16 @@ TEST_F(FormatTest, FormatsCompactNamespaces) {
                "} // namespace bbbbbb\n"
                "} // namespace aaaaaa",
                Style);
+
+  verifyFormat("namespace a { namespace b { namespace c {\n"
+               "}}} // namespace a::b::c",
+               Style);
+
+  verifyFormat("namespace a { namespace b {\n"
+               "namespace cc {\n"
+               "}}} // namespace a::b::cc",
----------------
owenca wrote:

I agree, but in this particular case the bug occurs when the length is over the column limit, not when it's within the limit?

The following test case for `ColumnLimit: 41`:
```
namespace a { namespace b { namespace cc {
}}} // namespace a::b::cc
```
is equivalent to the test case below with `ColumnLimit: 40`:
```
namespace a { namespace b { namespace c {
}}} // namespace a::b::c
```

If you want to test that your fix doesn't break the existing behavior when the length is at the limit, I suggest that you reuse the existing test case:
```cpp
   verifyFormat("namespace out {\n"
                "int i;\n"
                "namespace in {\n"
                "int j;\n"
                "} // namespace in\n"
                "int k;\n"
                "} // namespace out",
                "namespace out { int i;\n"
                "namespace in { int j; } // namespace in\n"
                "int k; } // namespace out",
                Style);
 
+  Style.ColumnLimit = 41;
   verifyFormat("namespace A { namespace B { namespace C {\n"
                "}}} // namespace A::B::C",
                "namespace A { namespace B {\n"
                "namespace C {\n"
                "}} // namespace B::C\n"
                "} // namespace A",
                Style);
 
   Style.ColumnLimit = 40;
   verifyFormat("namespace aaaaaaaaaa {\n"
                "namespace bbbbbbbbbb {\n"
                "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
                "namespace aaaaaaaaaa {\n"
                "namespace bbbbbbbbbb {\n"
                "} // namespace bbbbbbbbbb\n"
                "} // namespace aaaaaaaaaa",
                Style);
```
FormatTest.cpp is 28K+ lines long and still growing. It should be broken up into multiple files. Until then, I prefer that we don't add nice-to-have new tests that aren't absolutely related to the pull request.

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


More information about the cfe-commits mailing list