diff --git a/clang/lib/CodeGen/CGDebugInfo.h b/clang/lib/CodeGen/CGDebugInfo.h --- a/clang/lib/CodeGen/CGDebugInfo.h +++ b/clang/lib/CodeGen/CGDebugInfo.h @@ -493,6 +493,9 @@ /// Emit information about an external variable. void EmitExternalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl); + /// Emit information about global alias. + void EmitGlobalAlias(const VarDecl *Decl); + /// Emit C++ using directive. void EmitUsingDirective(const UsingDirectiveDecl &UD); diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp --- a/clang/lib/CodeGen/CGDebugInfo.cpp +++ b/clang/lib/CodeGen/CGDebugInfo.cpp @@ -4945,6 +4945,20 @@ Var->addDebugInfo(GVE); } +void CGDebugInfo::EmitGlobalAlias(const VarDecl *D) { + if (!CGM.getCodeGenOpts().hasReducedDebugInfo()) + return; + + llvm::DIFile *Unit = getOrCreateFile(D->getLocation()); + StringRef Name = D->getName(); + llvm::DIType *Ty = getOrCreateType(D->getType(), Unit); + llvm::DIScope *DContext = getDeclContextDescriptor(D); + auto Loc = getLineNumber(D->getLocation()); + DBuilder.createGlobalVariableExpression( + DContext, Name, StringRef(), Unit, Loc, Ty, + !D->hasExternalFormalLinkage()); +} + llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) { if (!LexicalBlockStack.empty()) return LexicalBlockStack.back(); diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -4927,6 +4927,12 @@ setTLSMode(GA, *VD); SetCommonAttributes(GD, GA); + + // Emit global alias debug information. + if (const auto *VD = dyn_cast(D)) { + if (CGDebugInfo *DI = getModuleDebugInfo()) + DI->EmitGlobalAlias(VD); + } } void CodeGenModule::emitIFuncDefinition(GlobalDecl GD) { diff --git a/clang/test/CodeGen/debug-info-alias.c b/clang/test/CodeGen/debug-info-alias.c new file mode 100644 --- /dev/null +++ b/clang/test/CodeGen/debug-info-alias.c @@ -0,0 +1,6 @@ +// RUN: %clang_cc1 -emit-llvm -debug-info-kind=limited -triple x86_64-linux-unknown %s -o - | FileCheck %s + +// CHECK: !DIGlobalVariable(name: "newname" + +int oldname = 1; +extern int newname __attribute__((alias("oldname")));