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

    <tr>
        <th>Summary</th>
        <td>
            clang-format crashes on with macro wrapping struct
        </td>
    </tr>

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

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

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

<pre>
    Here is my simplified source code:
```c++
static bool foo;

//! \brief Blah
PACK(struct Foo {
   uint16_t a;
});

PACK(struct Bar {
   uint32_t a;
});

PACK(struct Baz {
   uint16_t a;
});
```

Here is my simplified `.clang-format`:
```
BreakBeforeBraces: Custom
BraceWrapping:
 AfterStruct: true
SeparateDefinitionBlocks: Always
Macros:
- PACK(x)=x
```

If I run clang-format on this file, I get this crash:
```
clang-format: /home/user/code/llvm-project/clang/lib/Format/WhitespaceManager.cpp:135: void clang::format::WhitespaceManager::calculateLineBreakInformation(): Assertion `PreviousOriginalWhitespaceEndOffset <= OriginalWhitespaceStartOffset' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: /home/user/clang-format main.cpp
 #0 0x0000559557ec5a2e llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /home/user/code/llvm-project/llvm/lib/Support/Unix/Signals.inc:723:22
 #1 0x0000559557ec5e76 PrintStackTraceSignalHandler(void*) /home/user/code/llvm-project/llvm/lib/Support/Unix/Signals.inc:798:1
 #2 0x0000559557ec3712 llvm::sys::RunSignalHandlers() /home/user/code/llvm-project/llvm/lib/Support/Signals.cpp:105:20
 #3 0x0000559557ec5403 SignalHandler(int) /home/user/code/llvm-project/llvm/lib/Support/Unix/Signals.inc:413:1
 #4 0x00007f68a685afd0 (/lib/x86_64-linux-gnu/libc.so.6+0x3bfd0)
 #5 0x00007f68a68a9d3c (/lib/x86_64-linux-gnu/libc.so.6+0x8ad3c)
 #6 0x00007f68a685af32 raise (/lib/x86_64-linux-gnu/libc.so.6+0x3bf32)
 #7 0x00007f68a6845472 abort (/lib/x86_64-linux-gnu/libc.so.6+0x26472)
 #8 0x00007f68a6845395 (/lib/x86_64-linux-gnu/libc.so.6+0x26395)
 #9 0x00007f68a6853e32 (/lib/x86_64-linux-gnu/libc.so.6+0x34e32)
#10 0x0000559558027de3 clang::format::WhitespaceManager::calculateLineBreakInformation() /home/user/code/llvm-project/clang/lib/Format/WhitespaceManager.cpp:138:9
#11 0x0000559558027c2a clang::format::WhitespaceManager::generateReplacements() /home/user/code/llvm-project/clang/lib/Format/WhitespaceManager.cpp:109:25
#12 0x0000559558049fb0 clang::format::DefinitionBlockSeparator::separateBlocks(llvm::SmallVectorImpl<clang::format::AnnotatedLine*>&, clang::tooling::Replacements&, clang::format::FormatTokenLexer&) /home/user/code/llvm-project/clang/lib/Format/DefinitionBlockSeparator.cpp:252:56
#13 0x0000559558048dc7 clang::format::DefinitionBlockSeparator::analyze(clang::format::TokenAnnotator&, llvm::SmallVectorImpl<clang::format::AnnotatedLine*>&, clang::format::FormatTokenLexer&) /home/user/code/llvm-project/clang/lib/Format/DefinitionBlockSeparator.cpp:29:19
#14 0x0000559557fcb480 clang::format::TokenAnalyzer::process(bool) /home/user/code/llvm-project/clang/lib/Format/TokenAnalyzer.cpp:132:5
#15 0x0000559557f79ff1 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*)::'lambda6'(clang::format::Environment const&)::operator()(clang::format::Environment const&) const /home/user/code/llvm-project/clang/lib/Format/Format.cpp:3711:14
#16 0x0000559557f85516 std::pair<clang::tooling::Replacements, unsigned int> std::__invoke_impl<std::pair<clang::tooling::Replacements, unsigned int>, 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*)::'lambda6'(clang::format::Environment const&)&, clang::format::Environment const&>(std::__invoke_other, 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*)::'lambda6'(clang::format::Environment const&)&, clang::format::Environment const&) /usr/include/c++/12/bits/invoke.h:61:70
#17 0x0000559557f83706 std::enable_if<is_invocable_r_v<std::pair<clang::tooling::Replacements, unsigned int>, 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*)::'lambda6'(clang::format::Environment const&)&, clang::format::Environment const&>, std::pair<clang::tooling::Replacements, unsigned int>>::type std::__invoke_r<std::pair<clang::tooling::Replacements, unsigned int>, 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*)::'lambda6'(clang::format::Environment const&)&, 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*)::'lambda6'(clang::format::Environment const&)&, clang::format::Environment const&) /usr/include/c++/12/bits/invoke.h:117:5
#18 0x0000559557f81e6e 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*)::'lambda6'(clang::format::Environment const&)>::_M_invoke(std::_Any_data const&, clang::format::Environment const&) /usr/include/c++/12/bits/std_function.h:292:7
#19 0x0000559557f99257 std::function<std::pair<clang::tooling::Replacements, unsigned int> (clang::format::Environment const&)>::operator()(clang::format::Environment const&) const /usr/include/c++/12/bits/std_function.h:592:7
#20 0x0000559557f7ad4d 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/user/code/llvm-project/clang/lib/Format/Format.cpp:3746:9
#21 0x0000559557f7b2c7 clang::format::reformat(clang::format::FormatStyle const&, llvm::StringRef, llvm::ArrayRef<clang::tooling::Range>, llvm::StringRef, clang::format::FormattingAttemptStatus*) /home/user/code/llvm-project/clang/lib/Format/Format.cpp:3788:28
#22 0x0000559557de9342 clang::format::format(llvm::StringRef, bool) /home/user/code/llvm-project/clang/tools/clang-format/ClangFormat.cpp:499:76
#23 0x0000559557deaa6c main /home/user/code/llvm-project/clang/tools/clang-format/ClangFormat.cpp:717:11
#24 0x00007f68a68461ca (/lib/x86_64-linux-gnu/libc.so.6+0x271ca)
#25 0x00007f68a6846285 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x27285)
#26 0x0000559557de71b1 _start (/home/user/clang-format+0x3471b1)
zsh: IOT instruction  ~/clang-format main.cpp
```

I've reduced this example to the bear minimum. It doesn't crash if:
- I remove the comment
- I remove `static bool foo;`
- I remove one of the 3 structs
- I remove any line from `.clang-format`

I am using `clang-format` build from source based on a6b5d6dab0544892fb6afc46f71677969285c5a8

Even if it does not crash, it does not format the document how I would expect.

What I get:
```c++
PACK(
    struct Foo
    {
    uint16_t a;
 });

PACK(
    struct Bar
    {
    uint32_t a;
 });

PACK(
    struct Baz
    {
    uint16_t a;
 });
```

What I expect (similar to without the macro):
```c++
PACK(struct Foo
{
   uint16_t a;
});

PACK(struct Bar
{
   uint32_t a;
});

PACK(struct Baz
{
   uint16_t a;
});
```

What I get without the macro:
```c++
struct Foo
{
  uint16_t a;
};

struct Bar
{
  uint32_t a;
};

struct Baz
{
  uint16_t a;
};
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWktv4zgS_jXMhWhDIvU85KA8jAl2GtPozG4fDYoq2dxIpEFSefRhf_uCEhNb8qPX6WSngUlgBDZFfqz66mORkooZI5YS4BzFFyi-OmOdXSl9_plpDs1XVQrLRNPAWamqp_PfQAMWBrdP2Ih23YhaQIWN6jQHzFUFiBYouEJBgZJg-HBELtynbzWWWcFxqVSDa6UQ9e3-P5n3nxCj-LLUAmp80bDVcPFLcfkPRDJjdcctniuFUepHY4w7IW2YLCxmG8z0CpF8MsUY5YLpHRRKTkf5fqotz-Rsg-7nFiXBjDdMLj_VSrfMukE7HA8_LzSwuwuolYYLzTgYRAt82Rmr2ucOjMM3zdZrIZcvKLioLejb3hc3wuoOhiu3sGaaWbiCWkhhhZIXjeJ3PW7RPLAnM_T7zLhW5gXwE_b8PPY-Xz0ecfqmxjdYdxJv-4iVxHYlDK5FA4hc4hu8BDs0cc3M6hADI6JogRGZr1QLiMw7AxqReS9RMm-a-_bTWqt_A7eu1Q1zzaJEZD4fhpP5t5WwYNaMw2cm2RL0jK_XiBYhjR34vRLVYLYzhxYv0yJa7AwdmjlreNcwC78LCX24buQwTCiJSNbzVeDCGNCuyUX_i4Z7oTrzhxZLIVmzgb6W1R91bcBiRC8RvcK7XW4t03bohEiKayYaqGZexL9fF7fX2HRlKyxmuOyWWMNaaYutwitr131I-zW5FHbVlTOuWs_eLonCmA4MInPMZIWF5E1XAbYrGEKGS8bvrBOgn_7WMn6Hq65dv0QzmOH-74tWS81azPSya0Fasz-W24ppmZB9eAZJI0IDHDwGQRDEcR7HKfCYEcC96X0ozJMZvnzRQtremD-ddYhkm06aPSyUsRpYi0jilCikRST_X4X1zFSvq9tu7chFZP5PKR5dg1hK1piZkBzRIiUU0YKQjQfh1ANIEzwxd8D4jcmqcXZkTpSIFO9lY545_W9MJBMTaRqSfSR_7eTIUjOI_SdtfDbOL8vALUsSbK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