From 43ecd7c71d690a18e655d6c88d6a6333cf750e7f Mon Sep 17 00:00:00 2001 From: Reid Kleckner Date: Fri, 20 Nov 2015 17:41:12 +0000 Subject: [PATCH] [DebugInfo] Look through type sugar on union types when casting Fixes PR25584. llvm-svn: 253680 --- clang/lib/CodeGen/CGDebugInfo.cpp | 2 +- .../CodeGenCXX/debug-info-anon-union-vars.cpp | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp index 582fb7307795..4305280a0c87 100644 --- a/clang/lib/CodeGen/CGDebugInfo.cpp +++ b/clang/lib/CodeGen/CGDebugInfo.cpp @@ -3329,7 +3329,7 @@ void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var, // variable for each member of the anonymous union so that it's possible // to find the name of any field in the union. if (T->isUnionType() && DeclName.empty()) { - const RecordDecl *RD = cast(T)->getDecl(); + const RecordDecl *RD = T->castAs()->getDecl(); assert(RD->isAnonymousStructOrUnion() && "unnamed non-anonymous struct or union?"); GV = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext); diff --git a/clang/test/CodeGenCXX/debug-info-anon-union-vars.cpp b/clang/test/CodeGenCXX/debug-info-anon-union-vars.cpp index 3d3dff7a5ed5..5b0370eb749d 100644 --- a/clang/test/CodeGenCXX/debug-info-anon-union-vars.cpp +++ b/clang/test/CodeGenCXX/debug-info-anon-union-vars.cpp @@ -29,11 +29,28 @@ void foo() { i = 8; } +// A funky reinterpret cast idiom that we used to crash on. +template +unsigned char *buildBytes(const T v) { + static union { + unsigned char result[sizeof(T)]; + T value; + }; + value = v; + return result; +} + +void instantiate(int x) { + buildBytes(x); +} + // CHECK: [[FILE:.*]] = !DIFile(filename: "{{.*}}debug-info-anon-union-vars.cpp", // CHECK: !DIGlobalVariable(name: "c",{{.*}} file: [[FILE]], line: 6,{{.*}} isLocal: true, isDefinition: true // CHECK: !DIGlobalVariable(name: "d",{{.*}} file: [[FILE]], line: 6,{{.*}} isLocal: true, isDefinition: true // CHECK: !DIGlobalVariable(name: "a",{{.*}} file: [[FILE]], line: 6,{{.*}} isLocal: true, isDefinition: true // CHECK: !DIGlobalVariable(name: "b",{{.*}} file: [[FILE]], line: 6,{{.*}} isLocal: true, isDefinition: true +// CHECK: !DIGlobalVariable(name: "result", {{.*}} isLocal: false, isDefinition: true +// CHECK: !DIGlobalVariable(name: "value", {{.*}} isLocal: false, isDefinition: true // CHECK: !DILocalVariable(name: "i", {{.*}}, flags: DIFlagArtificial // CHECK: !DILocalVariable(name: "c", {{.*}}, flags: DIFlagArtificial // CHECK: !DILocalVariable(