Skip to content

[clang-format] Crash when using AllowShortNamespacesOnASingleLine with AlignAfterOpenBracket: BlockIndent #163355

@marco-antognini-sonarsource

Description

The following code crashes:

namespace {
void xxxxxxxxxxxxxxxxxxxxx(nnnnn::TTTTTTTTTTTTT const *mmmm,
                           YYYYYYYYYYYYYYYYY &yyyyyyyyyyyyyy);
} //
clang-format \
  --style="{ColumnLimit: 120, AllowShortNamespacesOnASingleLine: true, AlignAfterOpenBracket: BlockIndent}" \
  reproducer.cpp

In release with assertion, I get this assertion violation

Assertion failed: (LBrace && LBrace->is(tok::l_brace)), function isBlockIndentedInitRBrace, file FormatToken.cpp, line 74.
Stacktrace

0  libLLVMSupport.dylib     0x00000001012cf7e0 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  libLLVMSupport.dylib     0x00000001012cd290 llvm::sys::RunSignalHandlers() + 172
2  libLLVMSupport.dylib     0x00000001012d02c8 SignalHandler(int, __siginfo*, void*) + 344
3  libsystem_platform.dylib 0x000000018f7a96a4 _sigtramp + 56
4  libsystem_pthread.dylib  0x000000018f76f848 pthread_kill + 296
5  libsystem_c.dylib        0x000000018f6789e4 abort + 124
6  libsystem_c.dylib        0x000000018f677c18 err + 0
7  libclangFormat.dylib     0x000000010081fd20 clang::format::CommaSeparatedList::formatAfterToken(clang::format::LineState&, clang::format::ContinuationIndenter*, bool) (.cold.1) + 0
8  libclangFormat.dylib     0x00000001007beb14 clang::format::FormatToken::opensBlockOrBlockTypeList(clang::format::FormatStyle const&) const + 0
9  libclangFormat.dylib     0x000000010078b644 clang::format::ContinuationIndenter::mustBreak(clang::format::LineState const&) + 580
10 libclangFormat.dylib     0x00000001007fb428 clang::format::(anonymous namespace)::OptimizingLineFormatter::addNextStateToQueue(unsigned int, clang::format::(anonymous namespace)::OptimizingLineFormatter::StateNode*, bool, unsigned int*, std::__1::priority_queue<std::__1::pair<std::__1::pair<unsigned int, unsigned int>, clang::format::(anonymous namespace)::OptimizingLineFormatter::StateNode*>, llvm::SmallVector<std::__1::pair<std::__1::pair<unsigned int, unsigned int>, clang::format::(anonymous namespace)::OptimizingLineFormatter::StateNode*>, 3u>, std::__1::greater<std::__1::pair<std::__1::pair<unsigned int, unsigned int>, clang::format::(anonymous namespace)::OptimizingLineFormatter::StateNode*>>>*) + 96
11 libclangFormat.dylib     0x00000001007fa948 clang::format::(anonymous namespace)::OptimizingLineFormatter::analyzeSolutionSpace(clang::format::LineState&, bool) + 1472
12 libclangFormat.dylib     0x00000001007f7024 clang::format::UnwrappedLineFormatter::format(llvm::SmallVectorImpl<clang::format::AnnotatedLine*> const&, bool, int, bool, unsigned int, unsigned int, unsigned int) + 1728
13 libclangFormat.dylib     0x00000001007a7558 clang::format::(anonymous namespace)::Formatter::analyze(clang::format::TokenAnnotator&, llvm::SmallVectorImpl<clang::format::AnnotatedLine*>&, clang::format::FormatTokenLexer&) + 556
14 libclangFormat.dylib     0x00000001007e13c0 clang::format::TokenAnalyzer::process(bool) + 1376
15 libclangFormat.dylib     0x00000001007bddc0 std::__1::__function::__func<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*)::$_12, std::__1::allocator<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*)::$_12>, std::__1::pair<clang::tooling::Replacements, unsigned int> (clang::format::Environment const&)>::operator()(clang::format::Environment const&) + 112
16 libclangFormat.dylib     0x000000010079fc4c 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*) + 1948
17 libclangFormat.dylib     0x00000001007a0bcc clang::format::reformat(clang::format::FormatStyle const&, llvm::StringRef, llvm::ArrayRef<clang::tooling::Range>, llvm::StringRef, clang::format::FormattingAttemptStatus*) + 52
18 clang-format             0x000000010062d360 clang::format::format(llvm::StringRef, bool) + 5172
19 clang-format             0x000000010062b1f8 main + 2024
20 dyld                     0x000000018f3ceb98 start + 6076

Tested with current main (3793e75).
Bisection shows the bug was introduced in 083f099, i.e., the commit that introduced AllowShortNamespacesOnASingleLine from #123010.

Metadata

Metadata

Assignees

Type

Projects

Status

Done

Relationships

None yet

Development

No branches or pull requests

Issue actions