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

Owen Pan via cfe-commits cfe-commits at lists.llvm.org
Sat Oct 12 23:38:54 PDT 2024


owenca wrote:

> > The following are missing:
> > 
> > * Run `dump_format_style.py`.
> > * Add a `ConfigParseTest` for the new option.
> > 
> > How does the new option interact with `CompactNamespaces`? For example:
> > 
> > * `AllowShortNamespacesOnASingleLine: true` and `CompactNamespaces: false`
> > 
> > ```
> > namespace a {
> > namespace b { class c; }
> > } // namespace a
> > ```
> > 
> > * Both `true`
> >   `namespace a { namespace b { class c; } }`
> 
> Aah, I hadn't considered the interaction of this AllowShortNamespacesOnASingleLine and CompactNamespaces, as AllowShortNamespacesOnASingleLine effectively forces CompactNamespaces behavior when the block can all fit on a single line. However, it does appear that they were in conflict where the CompactNamespaces code was running first and then short circuiting out of the AllowShortNamespacesOnASingleLine logic.
> 
> Moved the logic around a bit, as AllowShortNamespacesOnASingleLine is mostly a superset of the CompactNamespaces logic. There are still some ambiguous situations around nested namespaces with a single statement inside that could be merged with both these two rules. I am just letting those get merged with CompactNamespaces first if the full expression can't be pulled onto a single line.

IMO `AllowShortNamespacesOnASingleLine` shouldn't imply `CompactNamespaces` so that we can get any of the formats below:

- `AllowShortNamespacesOnASingleLine: false`, `CompactNamespaces: false`
```
namespace a {
namespace b {
class c;
}
}
```

- `AllowShortNamespacesOnASingleLine: false`, `CompactNamespaces: true`
```
namespace a { namespace b {
class c;
}}
```

- `AllowShortNamespacesOnASingleLine: true`, `CompactNamespaces: false`
```
namespace a {
namespace b { class c; }
}
```

- `AllowShortNamespacesOnASingleLine: true`, `CompactNamespaces: true`
```
namespace a { namespace b { class c; } }
```

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


More information about the cfe-commits mailing list