llvm / llvm/llvm-project

[clang] Crash when compiling a "modularized" project generated using CMake 4.3.0

Open
#189,256 8 comments 0 reactions 0 assignees View on GitHub
clang:frontend clang:modules crash
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Clang crashes when compiling a CMake 4.3.0 project containing C++20 modules.

This is a reduced project to reproduce the problem: [clang-bug.zip](https://github.com/user-attachments/files/26332588/clang-bug.zip)

Configure using CMake 4.3.0 and build:

```bash
cmake -S . -B build -DCMAKE_CXX_COMPILER=clang++ -GNinja
cmake --build build
```

The configure step should succeed and the build fails with the following output:

```
[2/28] Scanning /tmp/clang-bug/Modules/Core/Types.ixx for CXX dependencies
warning: argument unused during compilation: '-c'
Diagnostics while scanning dependencies for '/tmp/clang-bug/Modules/Core/Types.ixx':
warning: argument unused during compilation: '-c'
[5/28] Scanning /tmp/clang-bug/Modules/HalfTrace/Interface.ixx for CXX dependencies
warning: argument unused during compilation: '-c'
Diagnostics while scanning dependencies for '/tmp/clang-bug/Modules/HalfTrace/Interface.ixx':
warning: argument unused during compilation: '-c'
[6/28] Scanning /tmp/clang-bug/Modules/HalfTrace/Sink.ixx for CXX dependencies
warning: argument unused during compilation: '-c'
Diagnostics while scanning dependencies for '/tmp/clang-bug/Modules/HalfTrace/Sink.ixx':
warning: argument unused during compilation: '-c'
[10/28] Scanning /tmp/clang-bug/Modules/HalfTrace/Event.ixx for CXX dependencies
warning: argument unused during compilation: '-c'
Diagnostics while scanning dependencies for '/tmp/clang-bug/Modules/HalfTrace/Event.ixx':
warning: argument unused during compilation: '-c'
[17/29] Building CXX object 'Modules/Core/CMakeFiles/Core@synth_3767efb207b2.dir/f57ebaeff54a.bmi'
clang++: warning: argument unused during compilation: '-c' [-Wunused-command-line-argument]
[22/32] Building CXX object 'Modules/HalfTrace/CMakeFiles/HalfTrace@synth_fe17f1b9ba3a.dir/e942e34f4349.bmi'
clang++: warning: argument unused during compilation: '-c' [-Wunused-command-line-argument]
[25/32] Building CXX object 'Modules/HalfTrace/CMakeFiles/HalfTrace@synth_fe17f1b9ba3a.dir/a36a1e28ad01.bmi'
clang++: warning: argument unused during compilation: '-c' [-Wunused-command-line-argument]
[26/32] Building CXX object 'Modules/HalfTrace/CMakeFiles/HalfTrace@synth_fe17f1b9ba3a.dir/5ee785cdb3fd.bmi'
clang++: warning: argument unused during compilation: '-c' [-Wunused-command-line-argument]
[31/32] Building CXX object Net/CMakeFiles/Net.dir/__/Modules/HalfTrace/Sink.cpp.o
FAILED: [code=1] Net/CMakeFiles/Net.dir/__/Modules/HalfTrace/Sink.cpp.o
/usr/bin/clang++ -std=gnu++20 -MD -MT Net/CMakeFiles/Net.dir/__/Modules/HalfTrace/Sink.cpp.o -MF Net/CMakeFiles/Net.dir/__/Modules/HalfTrace/Sink.cpp.o.d @Net/CMakeFiles/Net.dir/__/Modules/HalfTrace/Sink.cpp.o.modmap -o Net/CMakeFiles/Net.dir/__/Modules/HalfTrace/Sink.cpp.o -c /tmp/clang-bug/Modules/HalfTrace/Sink.cpp
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0. Program arguments: /usr/bin/clang++ -std=gnu++20 -MD -MT Net/CMakeFiles/Net.dir/__/Modules/HalfTrace/Sink.cpp.o -MF Net/CMakeFiles/Net.dir/__/Modules/HalfTrace/Sink.cpp.o.d @Net/CMakeFiles/Net.dir/__/Modules/HalfTrace/Sink.cpp.o.modmap -o Net/CMakeFiles/Net.dir/__/Modules/HalfTrace/Sink.cpp.o -c /tmp/clang-bug/Modules/HalfTrace/Sink.cpp
1. /tmp/clang-bug/Modules/HalfTrace/Sink.cpp:8:1: current parser token 'import'
#0 0x00007f2de4f4c99e llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/usr/lib/libLLVM.so.22.1+0x94c99e)
#1 0x00007f2de4f4a4a4 llvm::sys::CleanupOnSignal(unsigned long) (/usr/lib/libLLVM.so.22.1+0x94a4a4)
#2 0x00007f2de4e18859 (/usr/lib/libLLVM.so.22.1+0x818859)
#3 0x00007f2de404ff80 (/usr/lib/libc.so.6+0x3df80)
#4 0x00007f2df1562088 clang::ASTReader::DeclCursorForID(clang::GlobalDeclID, clang::SourceLocation&) (/usr/lib/libclang-cpp.so.22.1+0x2d62088)
#5 0x00007f2df157ac40 clang::ASTReader::ReadDeclRecord(clang::GlobalDeclID) (/usr/lib/libclang-cpp.so.22.1+0x2d7ac40)
#6 0x00007f2df15175e6 clang::ASTReader::GetDecl(clang::GlobalDeclID) (/usr/lib/libclang-cpp.so.22.1+0x2d175e6)
#7 0x00007f2df1588989 clang::ASTDeclReader::VisitNamespaceDecl(clang::NamespaceDecl*) (/usr/lib/libclang-cpp.so.22.1+0x2d88989)
#8 0x00007f2df159bb83 clang::ASTDeclReader::Visit(clang::Decl*) (/usr/lib/libclang-cpp.so.22.1+0x2d9bb83)
#9 0x00007f2deeec9b95 clang::StackExhaustionHandler::runWithSufficientStackSpace(clang::SourceLocation, llvm::function_ref) (/usr/lib/libclang-cpp.so.22.1+0x6c9b95)
#10 0x00007f2df157b57b clang::ASTReader::ReadDeclRecord(clang::GlobalDeclID) (/usr/lib/libclang-cpp.so.22.1+0x2d7b57b)
#11 0x00007f2df15175e6 clang::ASTReader::GetDecl(clang::GlobalDeclID) (/usr/lib/libclang-cpp.so.22.1+0x2d175e6)
#12 0x00007f2df1588cd3 clang::ASTDeclReader::VisitNamespaceDecl(clang::NamespaceDecl*) (/usr/lib/libclang-cpp.so.22.1+0x2d88cd3)
#13 0x00007f2df159bb83 clang::ASTDeclReader::Visit(clang::Decl*) (/usr/lib/libclang-cpp.so.22.1+0x2d9bb83)
#14 0x00007f2deeec9b95 clang::StackExhaustionHandler::runWithSufficientStackSpace(clang::SourceLocation, llvm::function_ref) (/usr/lib/libclang-cpp.so.22.1+0x6c9b95)
#15 0x00007f2df157b57b clang::ASTReader::ReadDeclRecord(clang::GlobalDeclID) (/usr/lib/libclang-cpp.so.22.1+0x2d7b57b)
#16 0x00007f2df15175e6 clang::ASTReader::GetDecl(clang::GlobalDeclID) (/usr/lib/libclang-cpp.so.22.1+0x2d175e6)
#17 0x00007f2df154fa34 clang::ASTReader::FindExternalVisibleDeclsByName(clang::DeclContext const*, clang::DeclarationName, clang::DeclContext const*) (/usr/lib/libclang-cpp.so.22.1+0x2d4fa34)
#18 0x00007f2def4959ca clang::DeclContext::lookupImpl(clang::DeclarationName, clang::DeclContext const*) const (/usr/lib/libclang-cpp.so.22.1+0xc959ca)
#19 0x00007f2df14d4a2d clang::ASTReader::CompleteRedeclChain(clang::Decl const*) (/usr/lib/libclang-cpp.so.22.1+0x2cd4a2d)
#20 0x00007f2def4a87c2 clang::NamespaceDecl::getMostRecentDeclImpl() (/usr/lib/libclang-cpp.so.22.1+0xca87c2)
#21 0x00007f2def495940 clang::DeclContext::lookupImpl(clang::DeclarationName, clang::DeclContext const*) const (/usr/lib/libclang-cpp.so.22.1+0xc95940)
#22 0x00007f2df14d4a2d clang::ASTReader::CompleteRedeclChain(clang::Decl const*) (/usr/lib/libclang-cpp.so.22.1+0x2cd4a2d)
#23 0x00007f2def2757bf (/usr/lib/libclang-cpp.so.22.1+0xa757bf)
#24 0x00007f2def495940 clang::DeclContext::lookupImpl(clang::DeclarationName, clang::DeclContext const*) const (/usr/lib/libclang-cpp.so.22.1+0xc95940)
#25 0x00007f2df14d4a2d clang::ASTReader::CompleteRedeclChain(clang::Decl const*) (/usr/lib/libclang-cpp.so.22.1+0x2cd4a2d)
#26 0x00007f2def47f893 clang::FunctionDecl::isDefined(clang::FunctionDecl const*&, bool) const (/usr/lib/libclang-cpp.so.22.1+0xc7f893)
#27 0x00007f2df085cb54 clang::Sema::InstantiateFunctionDefinition(clang::SourceLocation, clang::FunctionDecl*, bool, bool, bool) (/usr/lib/libclang-cpp.so.22.1+0x205cb54)
#28 0x00007f2df085bca4 clang::Sema::PerformPendingInstantiations(bool, bool) (/usr/lib/libclang-cpp.so.22.1+0x205bca4)
#29 0x00007f2defe426f1 clang::Sema::ActOnEndOfTranslationUnitFragment(clang::TUFragmentKind) (/usr/lib/libclang-cpp.so.22.1+0x16426f1)
#30 0x00007f2df03cdb31 clang::Sema::ActOnModuleDecl(clang::SourceLocation, clang::SourceLocation, clang::Sema::ModuleDeclKind, llvm::ArrayRef, llvm::ArrayRef, clang::Sema::ModuleImportState&, bool) (/usr/lib/libclang-cpp.so.22.1+0x1bcdb31)
#31 0x00007f2def219692 clang::Parser::ParseModuleDecl(clang::Sema::ModuleImportState&) (/usr/lib/libclang-cpp.so.22.1+0xa19692)
#32 0x00007f2def21c664 clang::Parser::ParseTopLevelDecl(clang::OpaquePtr&, clang::Sema::ModuleImportState&) (/usr/lib/libclang-cpp.so.22.1+0xa1c664)
#33 0x00007f2def0d144d clang::ParseAST(clang::Sema&, bool, bool) (/usr/lib/libclang-cpp.so.22.1+0x8d144d)
#34 0x00007f2df1744d28 clang::FrontendAction::Execute() (/usr/lib/libclang-cpp.so.22.1+0x2f44d28)
#35 0x00007f2df16b699e clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/usr/lib/libclang-cpp.so.22.1+0x2eb699e)
#36 0x00007f2df17fbb52 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/usr/lib/libclang-cpp.so.22.1+0x2ffbb52)
#37 0x000055cea7f2cd24 cc1_main(llvm::ArrayRef, char const*, void*) (/usr/bin/clang+++0x15d24)
#38 0x000055cea7f23828 (/usr/bin/clang+++0xc828)
#39 0x000055cea7f239cc (/usr/bin/clang+++0xc9cc)
#40 0x00007f2df1274a9d (/usr/lib/libclang-cpp.so.22.1+0x2a74a9d)
#41 0x00007f2de4e18c88 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref) (/usr/lib/libLLVM.so.22.1+0x818c88)
#42 0x00007f2df1275484 (/usr/lib/libclang-cpp.so.22.1+0x2a75484)
#43 0x00007f2df1228d7d clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/usr/lib/libclang-cpp.so.22.1+0x2a28d7d)
#44 0x00007f2df1229cef clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl>&, bool) const (/usr/lib/libclang-cpp.so.22.1+0x2a29cef)
#45 0x00007f2df1241e76 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl>&) (/usr/lib/libclang-cpp.so.22.1+0x2a41e76)
#46 0x000055cea7f28aa4 clang_main(int, char**, llvm::ToolContext const&) (/usr/bin/clang+++0x11aa4)
#47 0x000055cea7f23096 main (/usr/bin/clang+++0xc096)
#48 0x00007f2de403977e (/usr/lib/libc.so.6+0x2777e)
#49 0x00007f2de40398bc __libc_start_main (/usr/lib/libc.so.6+0x278bc)
#50 0x000055cea7f230f5 _start (/usr/bin/clang+++0xc0f5)
clang++: error: clang frontend command failed with exit code 139 (use -v to see invocation)
clang version 22.1.2
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
clang++: note: diagnostic msg:
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang++: note: diagnostic msg: /tmp/Sink-6c35f0.cpp
clang++: note: diagnostic msg: /tmp/Sink-6c35f0.sh
clang++: note: diagnostic msg:

********************
ninja: build stopped: subcommand failed.
```

Note that the failure only happens when using CMake 4.3.0; earlier or later (4.3.1) versions work fine.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.