| Issue |
240514
|
| Summary |
[clang-format] `FixNamespaceComments` puts the namespace end comment on a function's closing brace after a `Macros`-expanded `default:` label
|
| Labels |
clang-format
|
| Assignees |
|
| Reporter |
SCFrench
|
When a function-like macro listed in the `Macros` option expands to a `default:` label and is called inside a `switch` with an argument containing `;`, clang-format puts the namespace end comment in the wrong place for a later namespace. The comment is added to the closing brace of the last function inside the namespace, as well as to the namespace's own closing brace. When the namespace closer already has its comment, the result is a duplicate.
Reproduces with clang-format 21.1.1, 22.1.4 and 23.1.2 on Linux x86_64.
## Steps to reproduce
`test.cpp` (valid C++; compiles cleanly with `clang++ -std=c++20 -Wall -Wextra`):
```cpp
#define XXXX(action) \
default: \
action
void f(int x)
{
switch (x) {
XXXX(
(void)x;
break);
}
}
namespace n {
void g()
{
}
} // namespace n
```
`.clang-format`:
```yaml
BreakBeforeBraces: Linux
Macros: ['XXXX(action)=default: action']
```
Command:
```
clang-format test.cpp
```
The same result with no config file:
```
clang-format --style="{BreakBeforeBraces: Linux, Macros: ['XXXX(action)=default: action']}" test.cpp
```
## Actual output
```cpp
#define XXXX(action) \
default: \
action
void f(int x)
{
switch (x) {
XXXX(
(void)x;
break);
}
}
namespace n
{
void g() {} // namespace n
} // namespace n
```
`void g() {}` has been given a `// namespace n` comment, although the brace it follows closes the function `g`, not the namespace. Running clang-format again on this output changes nothing, so the stray comment is not removed later.
## Expected output
The same, except for the line with `g`:
```cpp
void g() {}
```
## Observations
Each of the following changes makes the bug disappear:
- **No `Macros` entry.** Without `Macros`, clang-format treats `XXXX(...)` as an ordinary call and the output is correct.
- **No `default:` in the macro definition.** `Macros: ['XXXX(action)=action']` is correct.
- **No `;` in the macro argument.** Calling `XXXX(break);` instead is correct.
- **No brace wrapping after functions.** `BreakBeforeBraces: Attach` is correct. Every preset that wraps after function definitions (`Linux`, `Allman`, `Stroustrup`, `Mozilla`, `GNU`, `WebKit`, `Whitesmiths`) shows the bug. So does `BreakBeforeBraces: Custom` with `BraceWrapping: {AfterFunction: true}`.
- **No namespace after the `switch`.** Putting the namespace before `f`, or putting `f` and `g` in the same namespace, is correct.
These changes do not affect the bug:
- **Keeping the macro argument on one line,** for example `XXXX((void)x; break);`.
- **Setting `ShortNamespaceLines: 0`.**
The trigger looks like the `;` inside the macro argument combined with the `default:` label from the `Macros` expansion. Together they seem to throw off the brace matching that `FixNamespaceComments` relies on, so a later function's closing brace is taken for the namespace's closing brace.
---
*This report was written with the help of Claude (Opus 5.5), an AI assistant from Anthropic. Claude reduced the original source file and `.clang-format` configuration to the minimal reproduction above and drafted this report. Every result shown here was checked by running clang-format.*
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs