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 @@ -533,6 +533,10 @@ /// Emit an @import declaration. void EmitImportDecl(const ImportDecl &ID); + /// Create and attach debuginfo to a the provided string literal GV. + void AddStringLiteralDebugInfo(llvm::GlobalVariable *GV, StringRef Name, + const StringLiteral *S); + /// Emit C++ namespace alias. llvm::DIImportedEntity *EmitNamespaceAlias(const NamespaceAliasDecl &NA); 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 @@ -5131,7 +5131,7 @@ return Name; codegenoptions::DebugTemplateNamesKind TemplateNamesKind = CGM.getCodeGenOpts().getDebugSimpleTemplateNames(); - + if (!CGM.getCodeGenOpts().hasReducedDebugInfo()) TemplateNamesKind = codegenoptions::DebugTemplateNamesKind::Full; @@ -5458,6 +5458,22 @@ ImportedDeclCache[GD.getCanonicalDecl().getDecl()].reset(ImportDI); } +void CGDebugInfo::AddStringLiteralDebugInfo(llvm::GlobalVariable *GV, + StringRef Name, + const StringLiteral *S) { + SourceLocation Loc = S->getStrTokenLoc(0); + PresumedLoc PLoc = CGM.getContext().getSourceManager().getPresumedLoc(Loc); + if (!PLoc.isValid()) + return; + + llvm::DIFile *File = getOrCreateFile(Loc); + llvm::DIGlobalVariableExpression *Debug = + DBuilder.createGlobalVariableExpression( + nullptr, StringRef(), StringRef(), getOrCreateFile(Loc), + getLineNumber(Loc), getOrCreateType(S->getType(), File), true); + GV->addDebugInfo(Debug); +} + 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 @@ -5659,6 +5659,11 @@ } auto GV = GenerateStringLiteral(C, LT, *this, GlobalVariableName, Alignment); + + CGDebugInfo *DI = getModuleDebugInfo(); + if (DI && getCodeGenOpts().hasReducedDebugInfo()) + DI->AddStringLiteralDebugInfo(GV, GlobalVariableName, S); + if (Entry) *Entry = GV; diff --git a/clang/test/CodeGen/debug-info-variables.c b/clang/test/CodeGen/debug-info-variables.c new file mode 100644 --- /dev/null +++ b/clang/test/CodeGen/debug-info-variables.c @@ -0,0 +1,17 @@ +// RUN: %clang_cc1 %s -debug-info-kind=standalone -S -emit-llvm -o - | FileCheck %s + +// CHECK: DIGlobalVariable(name: "global" +int global = 42; + +// CHECK: DIGlobalVariable({{.*}}line: [[@LINE+2]] +const char* s() { + return "1234567890"; +} + +// CHECK: DILocalVariable(name: "p" +// CHECK: DILocalVariable(name: "q" +// CHECK: DILocalVariable(name: "r" +int sum(int p, int q) { + int r = p + q; + return r; +} diff --git a/clang/test/VFS/external-names.c b/clang/test/VFS/external-names.c --- a/clang/test/VFS/external-names.c +++ b/clang/test/VFS/external-names.c @@ -30,8 +30,8 @@ // Debug info // RUN: %clang_cc1 -I %t -ivfsoverlay %t.external.yaml -triple %itanium_abi_triple -debug-info-kind=limited -emit-llvm %s -o - | FileCheck -check-prefix=CHECK-DEBUG-EXTERNAL %s -// CHECK-DEBUG-EXTERNAL: !DISubprogram({{.*}}file: ![[Num:[0-9]+]] -// CHECK-DEBUG-EXTERNAL: ![[Num]] = !DIFile(filename: "{{[^"]*}}Inputs{{..?}}external-names.h" +// CHECK-DEBUG-EXTERNAL: ![[Num:[0-9]+]] = !DIFile(filename: "{{[^"]*}}Inputs{{..?}}external-names.h" +// CHECK-DEBUG-EXTERNAL: !DISubprogram({{.*}}file: ![[Num]] // RUN: %clang_cc1 -I %t -ivfsoverlay %t.yaml -triple %itanium_abi_triple -debug-info-kind=limited -emit-llvm %s -o - | FileCheck -check-prefix=CHECK-DEBUG %s // CHECK-DEBUG-NOT: Inputs diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp --- a/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp @@ -165,7 +165,9 @@ } else { DeclContext = GV->getScope(); // Add name and type. - addString(*VariableDIE, dwarf::DW_AT_name, GV->getDisplayName()); + StringRef DisplayName = GV->getDisplayName(); + if (!DisplayName.empty()) + addString(*VariableDIE, dwarf::DW_AT_name, GV->getDisplayName()); if (GTy) addType(*VariableDIE, GTy);