Issue 56682
Summary Issue with the `AlignArrayOfStructures` option in clang-format
Labels
Assignees
Reporter navneethsrsh
    When setting the `AlignArrayOfStructures` option to either `Right` or `Left` clang-format crashes.

This is what I've managed to narrow it down to:

```c
static constexpr Option_table options{
                Option_record{
                              "-c", "--colour",
                              "Show coloured output; off by default", },
                Option_record{"-h",   "--help",      "Show this menu"}, // No trailing comma after "menu"
                Option_record{
                              "-b", "--backup",
                              "Backup file before formatting; on by "
                              "default" }, // No trailing comma after "default"
};
```
When running clang-format with code having the above snippet, it crashes if `AlignArrayOfStructures` is not set to `None`. Screenshot of failure message on Emacs along with the .clang-format file below.

__Fails when set to Left__
![01-fail-AlignArrayOfStructures-Left](https://user-images.githubusercontent.com/75144673/180594924-2bf57149-c2c0-453f-934b-e9503cc585c1.png)

__Fails when set to Right__
![02-fail-AlignArrayOfStructures-Right](https://user-images.githubusercontent.com/75144673/180594927-b593d061-e3ed-41d0-905e-f8b828475c64.png)

__Suceeds when set to None__
![03-success-AlignArrayOfStructures-None](https://user-images.githubusercontent.com/75144673/180594929-de21b047-7acd-4b2c-a43f-aeb9c9b818b8.png)

```c
static constexpr Option_table options{
                Option_record{
                              "-c", "--colour",
                              "Show coloured output; off by default", },
                Option_record{"-h",   "--help",      "Show this menu"}, // No trailing comma after "menu"
                Option_record{
                              "-b", "--backup",
                              "Backup file before formatting; on by "
                              "default", }, // Trailing comma after "default"
};
```
clang-format did not crash for any setting of `AlignArrayOfStructures` after adding a trailing comma to the last member (code above).


__Succeeds when set to Right (after adding trailing comma)__
> Same result when set to Left


![04-success-with-trailing-comma](https://user-images.githubusercontent.com/75144673/180594932-9f1b5831-4e76-4e0b-b8c0-ef6893c5b577.png)

It did not matter if the first two had the trailing comma, only the last one had to have it or it would crash.

Attaching the source and .clang-format file from the screenshots if that helps.
[src-and-clang-fmt.tar.gz](https://github.com/llvm/llvm-project/files/9173288/src-and-clang-fmt.tar.gz)

__LLVM/Clang Source__: Gentoo Portage

__Version Info__:
```
clang version 14.0.4
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/lib/llvm/14/bin
```
__Stack Backtrace__:
```
Stack dump:
0.	Program arguments: clang-format src/arg_parser.cpp
 #0 0x00007f623b2563a2 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/usr/lib/llvm/14/bin/../lib64/libLLVM-14.so+0xe493a2)
 #1 0x00007f623b253f74 llvm::sys::RunSignalHandlers() (/usr/lib/llvm/14/bin/../lib64/libLLVM-14.so+0xe46f74)
 #2 0x00007f623b2540f6 (/usr/lib/llvm/14/bin/../lib64/libLLVM-14.so+0xe470f6)
 #3 0x00007f623a0177c0 (/lib64/libc.so.6+0x3d7c0)
 #4 0x00007f6243af07ca clang::format::WhitespaceManager::alignArrayInitializersRightJustified(clang::format::WhitespaceManager::CellDescriptions&&) (/usr/lib/llvm/14/bin/../lib64/libclang-cpp.so.14+0x2bba7ca)
 #5 0x00007f6243af1a6b clang::format::WhitespaceManager::alignArrayInitializers(unsigned int, unsigned int) (/usr/lib/llvm/14/bin/../lib64/libclang-cpp.so.14+0x2bbba6b)
 #6 0x00007f6243af1b1b clang::format::WhitespaceManager::alignArrayInitializers() (/usr/lib/llvm/14/bin/../lib64/libclang-cpp.so.14+0x2bbbb1b)
 #7 0x00007f6243af41a6 clang::format::WhitespaceManager::generateReplacements() (/usr/lib/llvm/14/bin/../lib64/libclang-cpp.so.14+0x2bbe1a6)
 #8 0x00007f6243a8d081 (/usr/lib/llvm/14/bin/../lib64/libclang-cpp.so.14+0x2b57081)
 #9 0x00007f6243abab54 clang::format::TokenAnalyzer::process() (/usr/lib/llvm/14/bin/../lib64/libclang-cpp.so.14+0x2b84b54)
#10 0x00007f6243a89e48 (/usr/lib/llvm/14/bin/../lib64/libclang-cpp.so.14+0x2b53e48)
#11 0x00007f6243a89eb8 (/usr/lib/llvm/14/bin/../lib64/libclang-cpp.so.14+0x2b53eb8)
#12 0x00007f6243a911c8 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/14/bin/../lib64/libclang-cpp.so.14+0x2b5b1c8)
#13 0x00007f6243a9233e clang::format::reformat(clang::format::FormatStyle const&, llvm::StringRef, llvm::ArrayRef<clang::tooling::Range>, llvm::StringRef, clang::format::FormattingAttemptStatus*) (/usr/lib/llvm/14/bin/../lib64/libclang-cpp.so.14+0x2b5c33e)
#14 0x0000564f08163f09 (/usr/lib/llvm/14/bin/clang-format+0x11f09)
#15 0x0000564f0815c5ca (/usr/lib/llvm/14/bin/clang-format+0xa5ca)
#16 0x00007f623a00331a (/lib64/libc.so.6+0x2931a)
#17 0x00007f623a0033cc __libc_start_main (/lib64/libc.so.6+0x293cc)
#18 0x0000564f0815ca41 (/usr/lib/llvm/14/bin/clang-format+0xaa41)
Segmentation fault
``
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to