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 SourceLocation &Loc, unsigned Length); + /// 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 @@ -5118,7 +5118,7 @@ return Name; codegenoptions::DebugTemplateNamesKind TemplateNamesKind = CGM.getCodeGenOpts().getDebugSimpleTemplateNames(); - + if (!CGM.getCodeGenOpts().hasReducedDebugInfo()) TemplateNamesKind = codegenoptions::DebugTemplateNamesKind::Full; @@ -5445,6 +5445,21 @@ ImportedDeclCache[GD.getCanonicalDecl().getDecl()].reset(ImportDI); } +void CGDebugInfo::AddStringLiteralDebugInfo(llvm::GlobalVariable *GV, + StringRef Name, + const SourceLocation &Loc, + unsigned Length) { + PresumedLoc PLoc = CGM.getContext().getSourceManager().getPresumedLoc(Loc); + if (!PLoc.isValid()) + return; + + llvm::DIGlobalVariableExpression *Debug = + DBuilder.createGlobalVariableExpression( + nullptr, Name, Name, getOrCreateFile(Loc), getLineNumber(Loc), + DBuilder.createStringType(Name, Length), 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 @@ -5624,6 +5624,12 @@ } auto GV = GenerateStringLiteral(C, LT, *this, GlobalVariableName, Alignment); + + CGDebugInfo *DI = getModuleDebugInfo(); + if (DI && getCodeGenOpts().hasReducedDebugInfo()) + DI->AddStringLiteralDebugInfo(GV, GlobalVariableName, S->getStrTokenLoc(0), + S->getByteLength()); + 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