hctim created this revision. hctim added reviewers: echristo, cmtice. Herald added a project: All. hctim requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
An upcoming patch will extend llvm-symbolizer to provide the source line information for global variables. The goal is to move AddressSanitizer off of internal debug info for symbolization onto the DWARF standard (and doing a clean-up in the process). Currently, ASan reports the line information for constant strings if a memory safety bug happens around them. We want to keep this behaviour, so we need to emit debuginfo for these variables as well. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D123534 Files: clang/lib/CodeGen/CGDebugInfo.cpp clang/lib/CodeGen/CGDebugInfo.h clang/lib/CodeGen/CodeGenModule.cpp clang/test/CodeGen/debug-info-variables.c clang/test/VFS/external-names.c
Index: clang/test/VFS/external-names.c =================================================================== --- clang/test/VFS/external-names.c +++ 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 Index: clang/test/CodeGen/debug-info-variables.c =================================================================== --- /dev/null +++ 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; +} Index: clang/lib/CodeGen/CodeGenModule.cpp =================================================================== --- clang/lib/CodeGen/CodeGenModule.cpp +++ 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; Index: clang/lib/CodeGen/CGDebugInfo.h =================================================================== --- clang/lib/CodeGen/CGDebugInfo.h +++ 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); Index: clang/lib/CodeGen/CGDebugInfo.cpp =================================================================== --- clang/lib/CodeGen/CGDebugInfo.cpp +++ 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();
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits