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

    <tr>
        <th>Summary</th>
        <td>
            Crash in clang-format (17.x) with AlignArrayOfStructures set to Left
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang-format,
            crash-on-valid
      </td>
    </tr>

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

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

<pre>
    I am seeing a crash in clang-format on the following reduced code:

```
static const Test::Bar a[1] = {
    #define buf(a, b) \
      { #a, #b },

    { Test, bar },
};
```

With the following `.clang-format` 
```
BasedOnStyle : Microsoft 
AlignArrayOfStructures: Left
```

The crash happens here:

<details>
<summary>
crash backlog
</summary>

```
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: c:\\code\\llvm\\llvm-project\\out\\debug\\bin\\clang-format.exe reduced.cpp
Exception Code: 0xC0000005
 #0 0x00007ff67f23bd01 clang::Token::is(enum clang::tok::TokenKind) const C:\code\llvm\llvm-project\clang\include\clang\Lex\Token.h:98:0
 #1 0x00007ff67f23bbed clang::format::FormatToken::is(enum clang::tok::TokenKind) const C:\code\llvm\llvm-project\clang\lib\Format\FormatToken.h:560:0
 #2 0x00007ff67f3943cd clang::format::WhitespaceManager::getCells(unsigned int, unsigned int) C:\code\llvm\llvm-project\clang\lib\Format\WhitespaceManager.cpp:1402:0
 #3 0x00007ff67f3936d7 clang::format::WhitespaceManager::alignArrayInitializers(unsigned int, unsigned int) C:\code\llvm\llvm-project\clang\lib\Format\WhitespaceManager.cpp:1222:0
 #4 0x00007ff67f390427 clang::format::WhitespaceManager::alignArrayInitializers(void) C:\code\llvm\llvm-project\clang\lib\Format\WhitespaceManager.cpp:1206:0
 #5 0x00007ff67f38e592 clang::format::WhitespaceManager::generateReplacements(void) C:\code\llvm\llvm-project\clang\lib\Format\WhitespaceManager.cpp:112:0
 #6 0x00007ff67f226dca clang::format::`anonymous namespace'::Formatter::analyze C:\code\llvm\llvm-project\clang\lib\Format\Format.cpp:2391:0
 #7 0x00007ff67f34f6c7 clang::format::TokenAnalyzer::process(bool) C:\code\llvm\llvm-project\clang\lib\Format\TokenAnalyzer.cpp:132:0
 #8 0x00007ff67f22f4c5 clang::format::internal::reformat::<lambda_12>::operator() C:\code\llvm\llvm-project\clang\lib\Format\Format.cpp:3673:0
 #9 0x00007ff67f22f450 std::invoke<`lambda at C:/code/llvm/llvm-project/clang/lib/Format/Format.cpp:3672:23' &,const clang::format::Environment &> C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.37.32822\include\type_traits:1762:0
#10 0x00007ff67f22f35b std::_Func_impl_no_alloc<`lambda at C:/code/llvm/llvm-project/clang/lib/Format/Format.cpp:3672:23',std::pair<clang::tooling::Replacements,unsigned int>,const clang::format::Environment &>::_Do_call C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.37.32822\include\functional:841:0
#11 0x00007ff67f2322e5 std::_Func_class<struct std::pair<class clang::tooling::Replacements, unsigned int>, class clang::format::Environment const &>::operator()(class clang::format::Environment const &) const C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.37.32822\include\functional:883:0
#12 0x00007ff67f21d722 clang::format::internal::reformat(struct clang::format::FormatStyle const &, class llvm::StringRef, class llvm::ArrayRef<class clang::tooling::Range>, unsigned int, unsigned int, unsigned int, class llvm::StringRef, struct clang::format::FormattingAttemptStatus *) C:\code\llvm\llvm-project\clang\lib\Format\Format.cpp:3688:0
#13 0x00007ff67f21e5e0 clang::format::reformat(struct clang::format::FormatStyle const &, class llvm::StringRef, class llvm::ArrayRef<class clang::tooling::Range>, class llvm::StringRef, struct clang::format::FormattingAttemptStatus *) C:\code\llvm\llvm-project\clang\lib\Format\Format.cpp:3731:0
#14 0x00007ff67f113d1d clang::format::format C:\code\llvm\llvm-project\clang\tools\clang-format\ClangFormat.cpp:499:0
#15 0x00007ff67f1128c7 main C:\code\llvm\llvm-project\clang\tools\clang-format\ClangFormat.cpp:631:0
#16 0x00007ff67f6c001c invoke_main D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:78:0
#17 0x00007ff67f6c001c __scrt_common_main_seh D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288:0
#18 0x00007ff8395f257d (C:\WINDOWS\System32\KERNEL32.DLL+0x1257d)
#19 0x00007ff83a34aa78 (C:\WINDOWS\SYSTEM32\ntdll.dll+0x5aa78)
```

</details>

The actual crash is because `getNextNonComment()` returns nullptr inside `WhitespaceManager::getCells()`

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzUWN-P26gT_2vYF9QIQ7CdhzxknUSqbtuemuqq71OEMUn4LgYL8Hb3_voT2Plhb9K7Xq9XXVVlgQzD5zPDDJNhzsm9FmIO6D2gyzvW-oOxc2_bu9JUL_O3kNXQCSH1HjLILXMHKDXkiun9m52xNfPQaOgPAu6MUuZLELSiarmoIDeVAGQB0BKg42eK-v9x6jzzkkNutPPwk3A-iJPFPbOQAXqfALqEgCwhyO67DRBCCDCpxE5qAct2B3DOAC5gCfAMAlqcxWDYFYTj9wCTEoJsCXBxiecoFs8OapgdSmVLQO6vYu8-P0t_GNEHKZpcGgikCF7VcM-cqD7ojX9RAgKygO8kt8aZne_lF0ru9cJa9vJht_G25b61wgXJB7HzX0H16SB6Zx1Y0wjt4EHYV74gRSU8k8oBsjotubaumX05LXVqSsYfldmfpABejwWvwvn1YbXYrKBry1p6yGDZhuvRGOuhN_DgfRPoALwGeL2X_tCWE25qgNdKPR3_vGms-b_gHuC1dK4VDuA1ZLqCUnPVViKa_wzTW8bFpDt-4xl_hFVbNyfuaNJdjl-t2VtWQ2b3bS20j2blQYwWgBbx7sZRRHIancDEFdP2g0qU7b4bllL3Ki7uwEQ8i2NcTHjTdFhWz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