<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/130859>130859</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            clang-format (LLVM 19) SIGSEGV when formatting macro that generates function declaration
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang-format
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          eliminmax
      </td>
    </tr>
</table>

<pre>
    Defining the following macro then adding it to the `Macros` section of `.clang-format` causes both the Debian bookworm-updates build of `clang-format-19` and the `clang-format` from the Linux x64 tarball in the GitHub release for LLVM 19.1.7 to segfault.

```c
#define UNIT_TEST(foo) static void foo(void)
```

The most bare-bones files that reproduce it I've found is as follows:

**src.c**:
```c
#define UNIT_TEST(name) static void name(void)

UNIT_TEST(test_fn) {
}
```

**.clang-format**
```yaml
---
Macros:
  - UNIT_TEST
```

`clang-format` (LLVM 19.1.7) output:

```
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: /home/eliminmax/Downloads/clang-format src.c
 #0 0x000055a0c49bcc91 PrintStackTraceSignalHandler(void*) (.llvm.12627951935020062282) Signals.cpp:0:0
 #1 0x000055a0c49bd856 SignalHandler(int) Signals.cpp:0:0
 #2 0x00007fe05085b050 (/lib/x86_64-linux-gnu/libc.so.6+0x3c050)
 #3 0x000055a0c4a442f0 clang::format::TokenAnalyzer::process(bool) (/home/eliminmax/Downloads/clang-format+0x21f2f0)
 #4 0x000055a0c4a10bc1 std::_Function_handler<std::pair<clang::tooling::Replacements, unsigned int> (clang::format::Environment const&), clang::format::internal::reformat(clang::format::FormatStyle const&, llvm::StringRef, llvm::ArrayRef<clang::tooling::Range>, unsigned int, unsigned int, unsigned int, llvm::StringRef, clang::format::FormattingAttemptStatus*)::$_5>::_M_invoke(std::_Any_data const&, clang::format::Environment const&) Format.cpp:0:0
 #5 0x000055a0c49f4b87 clang::format::internal::reformat(clang::format::FormatStyle const&, llvm::StringRef, llvm::ArrayRef<clang::tooling::Range>, unsigned int, unsigned int, unsigned int, llvm::StringRef, clang::format::FormattingAttemptStatus*) (/home/eliminmax/Downloads/clang-format+0x1cfb87)
 #6 0x000055a0c49f5693 clang::format::reformat(clang::format::FormatStyle const&, llvm::StringRef, llvm::ArrayRef<clang::tooling::Range>, llvm::StringRef, clang::format::FormattingAttemptStatus*) (/home/eliminmax/Downloads/clang-format+0x1d0693)
 #7 0x000055a0c4964be8 clang::format::format(llvm::StringRef, bool) ClangFormat.cpp:0:0
 #8 0x000055a0c4961ac8 main (/home/eliminmax/Downloads/clang-format+0x13cac8)
 #9 0x00007fe05084624a __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:74:3
#10 0x00007fe050846305 call_init ./csu/../csu/libc-start.c:128:20
#11 0x00007fe050846305 __libc_start_main ./csu/../csu/libc-start.c:347:5
#12 0x000055a0c4960905 _start (/home/eliminmax/Downloads/clang-format+0x13b905)
Segmentation fault
```

`clang-format-19` (Debian `clang-format-19` package):
```
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: clang-format-19 src.c
 #0 0x00007f710deb72c6 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/usr/lib/llvm-19/bin/../lib/libLLVM.so.19.1+0xeb72c6)
 #1 0x00007f710deb4f70 llvm::sys::RunSignalHandlers() (/usr/lib/llvm-19/bin/../lib/libLLVM.so.19.1+0xeb4f70)
 #2 0x00007f710deb798b (/usr/lib/llvm-19/bin/../lib/libLLVM.so.19.1+0xeb798b)
 #3 0x00007f710ce5b050 (/lib/x86_64-linux-gnu/libc.so.6+0x3c050)
 #4 0x00007f711809a6d2 (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0x349a6d2)
 #5 0x00007f711809a266 clang::format::TokenAnalyzer::process(bool) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0x349a266)
 #6 0x00007f7118079515 (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0x3479515)
 #7 0x00007f7118061d65 clang::format::internal::reformat(clang::format::FormatStyle const&, llvm::StringRef, llvm::ArrayRef<clang::tooling::Range>, unsigned int, unsigned int, unsigned int, llvm::StringRef, clang::format::FormattingAttemptStatus*) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0x3461d65)
 #8 0x00007f7118062be1 clang::format::reformat(clang::format::FormatStyle const&, llvm::StringRef, llvm::ArrayRef<clang::tooling::Range>, llvm::StringRef, clang::format::FormattingAttemptStatus*) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.1+0x3462be1)
 #9 0x0000561961931fb1 (/usr/lib/llvm-19/bin/clang-format+0xcfb1)
#10 0x000056196192f66b (/usr/lib/llvm-19/bin/clang-format+0xa66b)
#11 0x00007f710ce4624a __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:74:3
#12 0x00007f710ce46305 call_init ./csu/../csu/libc-start.c:128:20
#13 0x00007f710ce46305 __libc_start_main ./csu/../csu/libc-start.c:347:5
#14 0x000056196192c471 (/usr/lib/llvm-19/bin/clang-format+0x7471)
```

The binary from the latest release does not have this issue, but I could not find any issue reporting anything similar, and no commits since that come up when running `git log --since=2025-01-01 --grep='\[clang-format\]' main` mention anything related to memory, segmentation faults, use-after-free, out of bounds, or the like.

`clang-format` (LLVM 20.1.0) output:
```
#define UNIT_TEST(name) static void name(void)

UNIT_TEST(test_fn) {
}
```
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzsWVtv46oW_jXkBdkCfH_IQ6Zt9h5p5mg07dmvEcbY4RRDBLhtzq8_ArtN4jRz6VRbI509yqiYy1rfugCfl6m1olOcL0H2AWTXCzq4rTZLLkUvVE-fFrVu9str3golVAfdlsNWS6kf_VNPmdG-T0HaNL5HOOhCDwQ5-uyHLcgRtJw5oRXUre-Pma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