Skip to content

Commit 7dbf115

Browse files
authored
[llvm][DebugInfo] Add support for emitting DW_AT_language_name (#162621)
Depends on: * #162445 * #162449 Emit `DW_AT_language_name` (new in DWARFv6) if `DICompileUnit` has a `sourceLanguageName` field. Emit a `DW_AT_language` otherwise.
1 parent 0f73e75 commit 7dbf115

File tree

4 files changed

+74
-27
lines changed

4 files changed

+74
-27
lines changed

llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -569,40 +569,30 @@ void CodeViewDebug::emitCodeViewMagicVersion() {
569569
OS.emitInt32(COFF::DEBUG_SECTION_MAGIC);
570570
}
571571

572-
static SourceLanguage MapDWLangToCVLang(unsigned DWLang) {
573-
switch (DWLang) {
574-
case dwarf::DW_LANG_C:
575-
case dwarf::DW_LANG_C89:
576-
case dwarf::DW_LANG_C99:
577-
case dwarf::DW_LANG_C11:
572+
static SourceLanguage
573+
MapDWARFLanguageToCVLang(dwarf::SourceLanguageName DWLName) {
574+
switch (DWLName) {
575+
case dwarf::DW_LNAME_C:
578576
return SourceLanguage::C;
579-
case dwarf::DW_LANG_C_plus_plus:
580-
case dwarf::DW_LANG_C_plus_plus_03:
581-
case dwarf::DW_LANG_C_plus_plus_11:
582-
case dwarf::DW_LANG_C_plus_plus_14:
577+
case dwarf::DW_LNAME_C_plus_plus:
583578
return SourceLanguage::Cpp;
584-
case dwarf::DW_LANG_Fortran77:
585-
case dwarf::DW_LANG_Fortran90:
586-
case dwarf::DW_LANG_Fortran95:
587-
case dwarf::DW_LANG_Fortran03:
588-
case dwarf::DW_LANG_Fortran08:
579+
case dwarf::DW_LNAME_Fortran:
589580
return SourceLanguage::Fortran;
590-
case dwarf::DW_LANG_Pascal83:
581+
case dwarf::DW_LNAME_Pascal:
591582
return SourceLanguage::Pascal;
592-
case dwarf::DW_LANG_Cobol74:
593-
case dwarf::DW_LANG_Cobol85:
583+
case dwarf::DW_LNAME_Cobol:
594584
return SourceLanguage::Cobol;
595-
case dwarf::DW_LANG_Java:
585+
case dwarf::DW_LNAME_Java:
596586
return SourceLanguage::Java;
597-
case dwarf::DW_LANG_D:
587+
case dwarf::DW_LNAME_D:
598588
return SourceLanguage::D;
599-
case dwarf::DW_LANG_Swift:
589+
case dwarf::DW_LNAME_Swift:
600590
return SourceLanguage::Swift;
601-
case dwarf::DW_LANG_Rust:
591+
case dwarf::DW_LNAME_Rust:
602592
return SourceLanguage::Rust;
603-
case dwarf::DW_LANG_ObjC:
593+
case dwarf::DW_LNAME_ObjC:
604594
return SourceLanguage::ObjC;
605-
case dwarf::DW_LANG_ObjC_plus_plus:
595+
case dwarf::DW_LNAME_ObjC_plus_plus:
606596
return SourceLanguage::ObjCpp;
607597
default:
608598
// There's no CodeView representation for this language, and CV doesn't
@@ -612,6 +602,14 @@ static SourceLanguage MapDWLangToCVLang(unsigned DWLang) {
612602
}
613603
}
614604

605+
static SourceLanguage MapDWARFLanguageToCVLang(dwarf::SourceLanguage DWLang) {
606+
auto MaybeLName = dwarf::toDW_LNAME(DWLang);
607+
if (!MaybeLName)
608+
return MapDWARFLanguageToCVLang(static_cast<dwarf::SourceLanguageName>(0));
609+
610+
return MapDWARFLanguageToCVLang(MaybeLName->first);
611+
}
612+
615613
void CodeViewDebug::beginModule(Module *M) {
616614
// If COFF debug section is not available, skip any debug info related stuff.
617615
if (!Asm->getObjFileLowering().getCOFFDebugSymbolsSection()) {
@@ -633,8 +631,13 @@ void CodeViewDebug::beginModule(Module *M) {
633631
Node = *CUs->operands().begin();
634632
}
635633
const auto *CU = cast<DICompileUnit>(Node);
634+
DISourceLanguageName Lang = CU->getSourceLanguage();
636635
CurrentSourceLanguage =
637-
MapDWLangToCVLang(CU->getSourceLanguage().getUnversionedName());
636+
Lang.hasVersionedName()
637+
? MapDWARFLanguageToCVLang(
638+
static_cast<dwarf::SourceLanguageName>(Lang.getName()))
639+
: MapDWARFLanguageToCVLang(
640+
static_cast<dwarf::SourceLanguage>(Lang.getName()));
638641
if (!M->getCodeViewFlag() ||
639642
CU->getEmissionKind() == DICompileUnit::NoDebug) {
640643
Asm = nullptr;

llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,8 +1039,12 @@ void DwarfDebug::finishUnitAttributes(const DICompileUnit *DIUnit,
10391039
} else
10401040
NewCU.addString(Die, dwarf::DW_AT_producer, Producer);
10411041

1042-
NewCU.addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
1043-
DIUnit->getSourceLanguage().getUnversionedName());
1042+
if (auto Lang = DIUnit->getSourceLanguage(); Lang.hasVersionedName())
1043+
NewCU.addUInt(Die, dwarf::DW_AT_language_name, dwarf::DW_FORM_data2,
1044+
Lang.getName());
1045+
else
1046+
NewCU.addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
1047+
Lang.getName());
10441048

10451049
NewCU.addString(Die, dwarf::DW_AT_name, FN);
10461050
StringRef SysRoot = DIUnit->getSysRoot();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
; RUN: %llc_dwarf -filetype=obj -O0 < %s | llvm-dwarfdump -debug-info - | FileCheck %s --implicit-check-not "DW_AT_language"
2+
3+
; CHECK: DW_AT_language_name (DW_LNAME_ObjC_plus_plus)
4+
5+
source_filename = "cu.cpp"
6+
target triple = "arm64-apple-macosx"
7+
8+
@x = global i32 0, align 4, !dbg !0
9+
10+
!llvm.dbg.cu = !{!2}
11+
!llvm.module.flags = !{!6, !7}
12+
13+
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
14+
!1 = !DIGlobalVariable(name: "x", scope: !2, file: !3, line: 1, type: !5, isLocal: false, isDefinition: true)
15+
!2 = distinct !DICompileUnit(sourceLanguageName: DW_LNAME_ObjC_plus_plus, file: !3, producer: "handwritten", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, globals: !4, splitDebugInlining: false, nameTableKind: Apple, sysroot: "/")
16+
!3 = !DIFile(filename: "cu.cpp", directory: "/tmp")
17+
!4 = !{!0}
18+
!5 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
19+
!6 = !{i32 7, !"Dwarf Version", i32 5}
20+
!7 = !{i32 2, !"Debug Info Version", i32 3}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
; RUN: %llc_dwarf -filetype=obj -O0 < %s | llvm-dwarfdump -debug-info - | FileCheck %s --implicit-check-not "DW_AT_language_name"
2+
3+
; CHECK: DW_AT_language (DW_LANG_C)
4+
5+
source_filename = "cu.cpp"
6+
target triple = "arm64-apple-macosx"
7+
8+
@x = global i32 0, align 4, !dbg !0
9+
10+
!llvm.dbg.cu = !{!2}
11+
!llvm.module.flags = !{!6, !7}
12+
13+
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
14+
!1 = !DIGlobalVariable(name: "x", scope: !2, file: !3, line: 1, type: !5, isLocal: false, isDefinition: true)
15+
!2 = distinct !DICompileUnit(language: DW_LANG_C, file: !3, producer: "handwritten", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, globals: !4, splitDebugInlining: false, nameTableKind: Apple, sysroot: "/")
16+
!3 = !DIFile(filename: "cu.cpp", directory: "/tmp")
17+
!4 = !{!0}
18+
!5 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
19+
!6 = !{i32 7, !"Dwarf Version", i32 5}
20+
!7 = !{i32 2, !"Debug Info Version", i32 3}

0 commit comments

Comments
 (0)