Index: llvm/tools/clang/lib/CodeGen/CGDebugInfo.h =================================================================== --- llvm/tools/clang/lib/CodeGen/CGDebugInfo.h +++ llvm/tools/clang/lib/CodeGen/CGDebugInfo.h @@ -283,6 +283,9 @@ /// \brief - Emit C++ using directive. void EmitUsingDirective(const UsingDirectiveDecl &UD); + /// EmitExplicitCastType - Emit the type explicitly casted to. + void EmitExplicitCastType(QualType Ty); + /// \brief - Emit C++ using declaration. void EmitUsingDecl(const UsingDecl &UD); Index: llvm/tools/clang/lib/CodeGen/CGDebugInfo.cpp =================================================================== --- llvm/tools/clang/lib/CodeGen/CGDebugInfo.cpp +++ llvm/tools/clang/lib/CodeGen/CGDebugInfo.cpp @@ -3347,3 +3347,11 @@ DBuilder.finalize(); } + +void CGDebugInfo::EmitExplicitCastType(QualType Ty) { + if (CGM.getCodeGenOpts().getDebugInfo() < CodeGenOptions::LimitedDebugInfo) + return; + llvm::DIType DieTy = getOrCreateType(Ty, getOrCreateMainFile()); + // Don't ignore in case of explicit cast where it is referenced indirectly. + DBuilder.retainType(DieTy); +} Index: llvm/tools/clang/lib/CodeGen/CGExprScalar.cpp =================================================================== --- llvm/tools/clang/lib/CodeGen/CGExprScalar.cpp +++ llvm/tools/clang/lib/CodeGen/CGExprScalar.cpp @@ -274,6 +274,10 @@ Value *VisitExplicitCastExpr(ExplicitCastExpr *E) { if (E->getType()->isVariablyModifiedType()) CGF.EmitVariablyModifiedType(E->getType()); + + if (CGDebugInfo *DI = CGF.getDebugInfo()) + DI->EmitExplicitCastType(E->getType()); + return VisitCastExpr(E); } Value *VisitCastExpr(CastExpr *E); Index: llvm/tools/clang/test/CodeGen/Debug-info-explicitcast.c =================================================================== --- llvm/tools/clang/test/CodeGen/Debug-info-explicitcast.c +++ llvm/tools/clang/test/CodeGen/Debug-info-explicitcast.c @@ -0,0 +1,24 @@ +// RUN: %clang_cc1 -emit-llvm -g %s -o - | FileCheck %s + +struct S2 { + int i; +}; +void foo2(void *p) { ((struct S2 *)p)->i++; } + + + +struct S3 { + int i; +}; +void foo3(struct S3 *p) { p->i++; } + +union U4 { + int i; + char c; +}; +void foo4(void *p) { ((union U4 *)p)->i++; } + +// CHECK: DW_TAG_structure_type ] [S2] +// CHECK: DW_TAG_union_type ] [U4] +// CHECK: DW_TAG_structure_type ] [S3] +