Issue 239327
Summary [clang-format] InsertBraces crashes with a configured function-like macro
Labels clang-format
Assignees
Reporter and2049
    With `InsertBraces: true`, using a configured macro as an unbraced body triggers an assertion in `BracesInserter`.

### Configuration (`.clang-format`)

```yaml
BasedOnStyle: LLVM
InsertBraces: true
Macros:
  - CALL(x)=f(x)
```

### Input (`test.cpp`)

```cpp
if (a) CALL(x);
```


```sh
clang-format test.cpp
```

### Actual behavior

An assertion-enabled build of main aborts with:

```text
clang/lib/Format/Format.cpp:2776:
void clang::format::(anonymous namespace)::BracesInserter::insertBraces(...):
Assertion `OpeningBraceSurplus == 0' failed.
```

clang-format 17.0.1 and Ubuntu clang-format 21.1.8 instead exit successfully but produce an unmatched opening brace:

```cpp
if (a) {
  CALL(x);
```

### Expected behavior

Insert a complete brace pair while retaining the macro invocation:

```cpp
if (a) {
  CALL(x);
}
```

The failure does not occur for every configured macro. For example, replacing the configuration entry with `ID(x)=x` and the input with `if (a) ID(f(x));` produces a complete brace pair on main and 21.1.8. 

Both macros expand to `f(x)` in these examples, but `ID` passes that _expression_ through as its argument, whereas `CALL` supplies the function call in its replacement text.

The macros here are defined through clang-format's `Macros` option, not through `#define` directives in the source.

Reproduced on main at `71926eeb7dc3` (`clang-format version 24.0.0git`, assertions enabled)
Also reproduced as malformed output on clang-format 17.0.1 and Ubuntu clang-format 21.1.8.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to