llvm / llvm/llvm-project

Clang crash in CodeGen with templated member enum and dependent underlying type

Open
#176,752 1 comment 0 reactions 0 assignees View on GitHub
clang:codegen crash
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

**What code / commands /steps will reproduce the problem?**

Compile the sample code:

```c++
template
struct bar { enum city : int; };

template
enum bar::city : T { london };

int main() {
bar::city e = bar::london;
return e == london;
}
```

Compiler Explorer link: https://godbolt.org/z/4W4z8sv61

**What is the expected result?**
Compilation should fail, and there should be clear diagnostic about the member enum declaration/definition.

**What happens instead?**
**g++ (GCC) compilation fails.**

```
:5:21: error: different underlying type in enum 'enum bar::city' [-Wtemplate-body]
5 | enum bar::city : T { london };
| ^
:2:26: note: previous definition here
2 | struct bar { enum city : int; };
| ^~~
: In function 'int main()':
:9:17: error: 'london' was not declared in this scope
9 | return e == london;
| ^~~~~~
Compiler returned: 1
```

**Clang frontend segfaults in CodeGen (`clang::CodeGenFunction::EmitLoadOfScalar`).**

```
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: /opt/compiler-explorer/clang-trunk/bin/clang++ -g -o /app/output.s -mllvm --x86-asm-syntax=intel -fno-verbose-asm -S --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics -fno-crash-diagnostics -std=c++17
1. parser at end of file
2. :7:5: LLVM IR generation of declaration 'main'
3. :7:5: Generating code for declaration 'main'
4. :5:25: Mangling declaration 'bar::london'
#0 0x0000000003d563b8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3d563b8)
#1 0x0000000003d53dac llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3d53dac)
#2 0x0000000003c99588 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
#3 0x0000768fb5842520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
#4 0x00000000077d5019 (anonymous namespace)::CXXNameMangler::mangle(clang::GlobalDecl) ItaniumMangle.cpp:0:0
#5 0x00000000077d99c7 (anonymous namespace)::ItaniumMangleContextImpl::mangleCXXName(clang::GlobalDecl, llvm::raw_ostream&) ItaniumMangle.cpp:0:0
#6 0x000000000428fe1b getMangledNameImpl(clang::CodeGen::CodeGenModule&, clang::GlobalDecl, clang::NamedDecl const*, bool) CodeGenModule.cpp:0:0
#7 0x00000000042a07b3 clang::CodeGen::CodeGenModule::getMangledName(clang::GlobalDecl) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x42a07b3)
#8 0x00000000042a1247 clang::CodeGen::CodeGenModule::GetAddrOfTemplateParamObject(clang::TemplateParamObjectDecl const*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x42a1247)
#9 0x0000000004089dd5 clang::CodeGen::CodeGenFunction::EmitDeclRefLValue(clang::DeclRefExpr const*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4089dd5)
#10 0x000000000409384b clang::CodeGen::CodeGenFunction::EmitLValueHelper(clang::Expr const*, clang::CodeGen::KnownNonNull_t) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x409384b)
#11 0x0000000004094257 void llvm::function_ref::callback_fn(long) CGExpr.cpp:0:0
#12 0x0000000007c6ca11 clang::StackExhaustionHandler::runWithSufficientStackSpace(clang::SourceLocation, llvm::function_ref) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x7c6ca11)
#13 0x000000000408fbbb clang::CodeGen::CodeGenFunction::EmitCheckedLValue(clang::Expr const*, clang::CodeGen::CodeGenFunction::TypeCheckKind) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x408fbbb)
#14 0x00000000040f25a0 (anonymous namespace)::ScalarExprEmitter::EmitLoadOfLValue(clang::Expr const*) CGExprScalar.cpp:0:0
#15 0x00000000040ed7d1 clang::StmtVisitorBase::Visit(clang::Stmt*) CGExprScalar.cpp:0:0
#16 0x00000000040fbcd6 (anonymous namespace)::ScalarExprEmitter::EmitBinOps(clang::BinaryOperator const*, clang::QualType) CGExprScalar.cpp:0:0
#17 0x00000000040fc095 (anonymous namespace)::ScalarExprEmitter::EmitCompare(clang::BinaryOperator const*, llvm::CmpInst::Predicate, llvm::CmpInst::Predicate, llvm::CmpInst::Predicate, bool) CGExprScalar.cpp:0:0
#18 0x00000000040ecca5 clang::StmtVisitorBase::Visit(clang::Stmt*) CGExprScalar.cpp:0:0
#19 0x00000000040f0b42 clang::CodeGen::CodeGenFunction::EmitScalarExpr(clang::Expr const*, bool) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x40f0b42)
#20 0x00000000041f777b clang::CodeGen::CodeGenFunction::EmitReturnStmt(clang::ReturnStmt const&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x41f777b)
#21 0x0000000004200a33 clang::CodeGen::CodeGenFunction::EmitStmt(clang::Stmt const*, llvm::ArrayRef) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4200a33)
#22 0x0000000004207b0b clang::CodeGen::CodeGenFunction::EmitCompoundStmtWithoutScope(clang::CompoundStmt const&, bool, clang::CodeGen::AggValueSlot) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4207b0b)
#23 0x0000000004267ced clang::CodeGen::CodeGenFunction::EmitFunctionBody(clang::Stmt const*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4267ced)
#24 0x0000000004278963 clang::CodeGen::CodeGenFunction::GenerateCode(clang::GlobalDecl, llvm::Function*, clang::CodeGen::CGFunctionInfo const&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4278963)
#25 0x00000000042d5e0b clang::CodeGen::CodeGenModule::EmitGlobalFunctionDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x42d5e0b)
#26 0x00000000042d1404 clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x42d1404)
#27 0x00000000042d2030 clang::CodeGen::CodeGenModule::EmitGlobal(clang::GlobalDecl) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x42d2030)
#28 0x00000000042dca5f clang::CodeGen::CodeGenModule::EmitTopLevelDecl(clang::Decl*) (.part.0) CodeGenModule.cpp:0:0
#29 0x000000000462a3b1 (anonymous namespace)::CodeGeneratorImpl::HandleTopLevelDecl(clang::DeclGroupRef) ModuleBuilder.cpp:0:0
#30 0x00000000046152e9 clang::BackendConsumer::HandleTopLevelDecl(clang::DeclGroupRef) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x46152e9)
#31 0x0000000006294294 clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x6294294)
#32 0x0000000004628655 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4628655)
#33 0x000000000493c63a clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-trunk/bin/clang+++0x493c63a)
#34 0x00000000048ba29b clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x48ba29b)
#35 0x0000000004a328cb clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4a328cb)
#36 0x0000000000de5fcd cc1_main(llvm::ArrayRef, char const*, void*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0xde5fcd)
#37 0x0000000000ddddfb ExecuteCC1Tool(llvm::SmallVectorImpl&, llvm::ToolContext const&, llvm::IntrusiveRefCntPtr) driver.cpp:0:0
#38 0x0000000000ddde9d int llvm::function_ref&)>::callback_fn&)>(long, llvm::SmallVectorImpl&) driver.cpp:0:0
#39 0x00000000046a30b9 void llvm::function_ref::callback_fn>, std::__cxx11::basic_string, std::allocator>*, bool*) const::'lambda'()>(long) Job.cpp:0:0
#40 0x0000000003c999a3 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3c999a3)
#41 0x00000000046a32d9 clang::driver::CC1Command::Execute(llvm::ArrayRef>, std::__cxx11::basic_string, std::allocator>*, bool*) const (.part.0) Job.cpp:0:0
#42 0x00000000046663c2 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-trunk/bin/clang+++0x46663c2)
#43 0x00000000046672a1 clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl>&, bool) const (/opt/compiler-explorer/clang-trunk/bin/clang+++0x46672a1)
#44 0x000000000466fd9c clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl>&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x466fd9c)
#45 0x0000000000de28c9 clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0xde28c9)
#46 0x0000000000c97e54 main (/opt/compiler-explorer/clang-trunk/bin/clang+++0xc97e54)
#47 0x0000768fb5829d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#48 0x0000768fb5829e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#49 0x0000000000ddd895 _start (/opt/compiler-explorer/clang-trunk/bin/clang+++0xddd895)
clang++: error: clang frontend command failed with exit code 139 (use -v to see invocation)
Compiler returned: 139
```

Contributor guide

Open the contributing guide

Research direction

Start by compiling the provided C++ reproducer with Clang in C++17 mode and compare the failure with GCC's diagnostic. The stack points to CodeGenFunction::EmitLoadOfScalar, ItaniumNameMangler::mangle, and CodeGenModule::GetAddrOfTemplateParamObject; inspect those entry points and verify that the sample reports a clear diagnostic instead of crashing.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.