Skip to content

Commit 7affded

Browse files
committed
[llvm][DebugInfo] Support for versioned DISourceLanguageName
1 parent a52934e commit 7affded

File tree

10 files changed

+136
-38
lines changed

10 files changed

+136
-38
lines changed

llvm/include/llvm/AsmParser/LLToken.h

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -488,27 +488,28 @@ enum Kind {
488488
SummaryID, // ^42
489489

490490
// String valued tokens (StrVal).
491-
LabelStr, // foo:
492-
GlobalVar, // @foo @"foo"
493-
ComdatVar, // $foo
494-
LocalVar, // %foo %"foo"
495-
MetadataVar, // !foo
496-
StringConstant, // "foo"
497-
DwarfTag, // DW_TAG_foo
498-
DwarfAttEncoding, // DW_ATE_foo
499-
DwarfVirtuality, // DW_VIRTUALITY_foo
500-
DwarfLang, // DW_LANG_foo
501-
DwarfCC, // DW_CC_foo
502-
EmissionKind, // lineTablesOnly
503-
NameTableKind, // GNU
504-
FixedPointKind, // Fixed point
505-
DwarfOp, // DW_OP_foo
506-
DIFlag, // DIFlagFoo
507-
DISPFlag, // DISPFlagFoo
508-
DwarfMacinfo, // DW_MACINFO_foo
509-
ChecksumKind, // CSK_foo
510-
DbgRecordType, // dbg_foo
511-
DwarfEnumKind, // DW_APPLE_ENUM_KIND_foo
491+
LabelStr, // foo:
492+
GlobalVar, // @foo @"foo"
493+
ComdatVar, // $foo
494+
LocalVar, // %foo %"foo"
495+
MetadataVar, // !foo
496+
StringConstant, // "foo"
497+
DwarfTag, // DW_TAG_foo
498+
DwarfAttEncoding, // DW_ATE_foo
499+
DwarfVirtuality, // DW_VIRTUALITY_foo
500+
DwarfLang, // DW_LANG_foo
501+
DwarfSourceLangName, // DW_LNAME_foo
502+
DwarfCC, // DW_CC_foo
503+
EmissionKind, // lineTablesOnly
504+
NameTableKind, // GNU
505+
FixedPointKind, // Fixed point
506+
DwarfOp, // DW_OP_foo
507+
DIFlag, // DIFlagFoo
508+
DISPFlag, // DISPFlagFoo
509+
DwarfMacinfo, // DW_MACINFO_foo
510+
ChecksumKind, // CSK_foo
511+
DbgRecordType, // dbg_foo
512+
DwarfEnumKind, // DW_APPLE_ENUM_KIND_foo
512513

513514
// Type valued tokens (TyVal).
514515
Type,

llvm/lib/AsmParser/LLLexer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,7 @@ lltok::Kind LLLexer::LexIdentifier() {
982982
DWKEYWORD(ATE, DwarfAttEncoding);
983983
DWKEYWORD(VIRTUALITY, DwarfVirtuality);
984984
DWKEYWORD(LANG, DwarfLang);
985+
DWKEYWORD(LNAME, DwarfSourceLangName);
985986
DWKEYWORD(CC, DwarfCC);
986987
DWKEYWORD(OP, DwarfOp);
987988
DWKEYWORD(MACINFO, DwarfMacinfo);

llvm/lib/AsmParser/LLParser.cpp

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4740,6 +4740,10 @@ struct DwarfLangField : public MDUnsignedField {
47404740
DwarfLangField() : MDUnsignedField(0, dwarf::DW_LANG_hi_user) {}
47414741
};
47424742

4743+
struct DwarfSourceLangNameField : public MDUnsignedField {
4744+
DwarfSourceLangNameField() : MDUnsignedField(0, UINT32_MAX) {}
4745+
};
4746+
47434747
struct DwarfCCField : public MDUnsignedField {
47444748
DwarfCCField() : MDUnsignedField(0, dwarf::DW_CC_hi_user) {}
47454749
};
@@ -4997,6 +5001,25 @@ bool LLParser::parseMDField(LocTy Loc, StringRef Name, DwarfLangField &Result) {
49975001
return false;
49985002
}
49995003

5004+
template <>
5005+
bool LLParser::parseMDField(LocTy Loc, StringRef Name,
5006+
DwarfSourceLangNameField &Result) {
5007+
if (Lex.getKind() == lltok::APSInt)
5008+
return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
5009+
5010+
if (Lex.getKind() != lltok::DwarfSourceLangName)
5011+
return tokError("expected DWARF source language name");
5012+
5013+
unsigned Lang = dwarf::getSourceLanguageName(Lex.getStrVal());
5014+
if (!Lang)
5015+
return tokError("invalid DWARF source language name" + Twine(" '") +
5016+
Lex.getStrVal() + "'");
5017+
assert(Lang <= Result.Max && "Expected valid DWARF source language name");
5018+
Result.assign(Lang);
5019+
Lex.Lex();
5020+
return false;
5021+
}
5022+
50005023
template <>
50015024
bool LLParser::parseMDField(LocTy Loc, StringRef Name, DwarfCCField &Result) {
50025025
if (Lex.getKind() == lltok::APSInt)
@@ -5836,9 +5859,12 @@ bool LLParser::parseDICompileUnit(MDNode *&Result, bool IsDistinct) {
58365859
if (!IsDistinct)
58375860
return tokError("missing 'distinct', required for !DICompileUnit");
58385861

5862+
LocTy Loc = Lex.getLoc();
5863+
58395864
#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5840-
REQUIRED(language, DwarfLangField, ); \
58415865
REQUIRED(file, MDField, (/* AllowNull */ false)); \
5866+
OPTIONAL(language, DwarfLangField, ); \
5867+
OPTIONAL(sourceLanguageName, DwarfSourceLangNameField, ); \
58425868
OPTIONAL(producer, MDStringField, ); \
58435869
OPTIONAL(isOptimized, MDBoolField, ); \
58445870
OPTIONAL(flags, MDStringField, ); \
@@ -5860,12 +5886,23 @@ bool LLParser::parseDICompileUnit(MDNode *&Result, bool IsDistinct) {
58605886
PARSE_MD_FIELDS();
58615887
#undef VISIT_MD_FIELDS
58625888

5889+
if (!language.Seen && !sourceLanguageName.Seen)
5890+
return error(Loc, "missing one of 'language' or 'sourceLanguageName', "
5891+
"required for !DICompileUnit");
5892+
5893+
if (language.Seen && sourceLanguageName.Seen)
5894+
return error(Loc, "can only specify one of 'language' and "
5895+
"'sourceLanguageName' on !DICompileUnit");
5896+
58635897
Result = DICompileUnit::getDistinct(
5864-
Context, DISourceLanguageName(language.Val), file.Val, producer.Val,
5865-
isOptimized.Val, flags.Val, runtimeVersion.Val, splitDebugFilename.Val,
5866-
emissionKind.Val, enums.Val, retainedTypes.Val, globals.Val, imports.Val,
5867-
macros.Val, dwoId.Val, splitDebugInlining.Val, debugInfoForProfiling.Val,
5868-
nameTableKind.Val, rangesBaseAddress.Val, sysroot.Val, sdk.Val);
5898+
Context,
5899+
language.Seen ? DISourceLanguageName(language.Val)
5900+
: DISourceLanguageName(sourceLanguageName.Val, 0),
5901+
file.Val, producer.Val, isOptimized.Val, flags.Val, runtimeVersion.Val,
5902+
splitDebugFilename.Val, emissionKind.Val, enums.Val, retainedTypes.Val,
5903+
globals.Val, imports.Val, macros.Val, dwoId.Val, splitDebugInlining.Val,
5904+
debugInfoForProfiling.Val, nameTableKind.Val, rangesBaseAddress.Val,
5905+
sysroot.Val, sdk.Val);
58695906
return false;
58705907
}
58715908

llvm/lib/Bitcode/Reader/MetadataLoader.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1867,12 +1867,18 @@ Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
18671867
// distinct. It's always distinct.
18681868
IsDistinct = true;
18691869

1870+
const auto LangVersionMask = (uint64_t(1) << 63);
1871+
const bool HasVersionedLanguage = Record[1] & LangVersionMask;
1872+
18701873
auto *CU = DICompileUnit::getDistinct(
1871-
Context, DISourceLanguageName(Record[1]), getMDOrNull(Record[2]),
1872-
getMDString(Record[3]), Record[4], getMDString(Record[5]), Record[6],
1873-
getMDString(Record[7]), Record[8], getMDOrNull(Record[9]),
1874-
getMDOrNull(Record[10]), getMDOrNull(Record[12]),
1875-
getMDOrNull(Record[13]),
1874+
Context,
1875+
HasVersionedLanguage
1876+
? DISourceLanguageName(Record[1] & ~LangVersionMask, 0)
1877+
: DISourceLanguageName(Record[1]),
1878+
getMDOrNull(Record[2]), getMDString(Record[3]), Record[4],
1879+
getMDString(Record[5]), Record[6], getMDString(Record[7]), Record[8],
1880+
getMDOrNull(Record[9]), getMDOrNull(Record[10]),
1881+
getMDOrNull(Record[12]), getMDOrNull(Record[13]),
18761882
Record.size() <= 15 ? nullptr : getMDOrNull(Record[15]),
18771883
Record.size() <= 14 ? 0 : Record[14],
18781884
Record.size() <= 16 ? true : Record[16],

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2106,7 +2106,13 @@ void ModuleBitcodeWriter::writeDICompileUnit(const DICompileUnit *N,
21062106
assert(N->isDistinct() && "Expected distinct compile units");
21072107
Record.push_back(/* IsDistinct */ true);
21082108

2109-
Record.push_back(N->getSourceLanguage().getUnversionedName());
2109+
auto Lang = N->getSourceLanguage();
2110+
Record.push_back(Lang.getName());
2111+
// Set bit so the MetadataLoader can distniguish between versioned and
2112+
// unversioned names.
2113+
if (Lang.hasVersionedName())
2114+
Record.back() ^= (uint64_t(1) << 63);
2115+
21102116
Record.push_back(VE.getMetadataOrNullID(N->getFile()));
21112117
Record.push_back(VE.getMetadataOrNullID(N->getRawProducer()));
21122118
Record.push_back(N->isOptimized());

llvm/lib/IR/AsmWriter.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2370,10 +2370,16 @@ static void writeDICompileUnit(raw_ostream &Out, const DICompileUnit *N,
23702370
Out << "!DICompileUnit(";
23712371
MDFieldPrinter Printer(Out, WriterCtx);
23722372

2373-
Printer.printDwarfEnum("language",
2374-
N->getSourceLanguage().getUnversionedName(),
2375-
dwarf::LanguageString,
2376-
/* ShouldSkipZero */ false);
2373+
auto Lang = N->getSourceLanguage();
2374+
if (Lang.hasVersionedName())
2375+
Printer.printDwarfEnum(
2376+
"sourceLanguageName",
2377+
static_cast<llvm::dwarf::SourceLanguageName>(Lang.getName()),
2378+
dwarf::SourceLanguageNameString,
2379+
/* ShouldSkipZero */ false);
2380+
else
2381+
Printer.printDwarfEnum("language", Lang.getName(), dwarf::LanguageString,
2382+
/* ShouldSkipZero */ false);
23772383

23782384
Printer.printMetadata("file", N->getRawFile(), /* ShouldSkipNull */ false);
23792385
Printer.printString("producer", N->getProducer());
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
; RUN: not llvm-as < %s -disable-output 2>&1 | FileCheck %s
2+
3+
; CHECK: <stdin>:[[@LINE+1]]:15: error: can only specify one of 'language' and 'sourceLanguageName' on !DICompileUnit
4+
!0 = distinct !DICompileUnit(language: DW_LANG_C, sourceLanguageName: DW_LNAME_C, file: !DIFile(filename: "a", directory: "b"))
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
; RUN: split-file %s %t
2+
; RUN: not llvm-as < %t/invalid_dw_lang.ll -disable-output 2>&1 | FileCheck %s --check-prefix=INVALID_DW_LANG
3+
; RUN: not llvm-as < %t/invalid_dw_lang_2.ll -disable-output 2>&1 | FileCheck %s --check-prefix=INVALID_DW_LANG_2
4+
; RUN: not llvm-as < %t/invalid_dw_lname.ll -disable-output 2>&1 | FileCheck %s --check-prefix=INVALID_DW_LNAME
5+
; RUN: not llvm-as < %t/invalid_dw_lname_2.ll -disable-output 2>&1 | FileCheck %s --check-prefix=INVALID_DW_LNAME_2
6+
7+
; INVALID_DW_LANG: invalid DWARF language 'DW_LANG_blah'
8+
; INVALID_DW_LANG_2: expected DWARF language
9+
; INVALID_DW_LNAME: invalid DWARF source language name 'DW_LNAME_blah'
10+
; INVALID_DW_LNAME_2: expected DWARF source language name
11+
12+
;--- invalid_dw_lang.ll
13+
!0 = distinct !DICompileUnit(language: DW_LANG_blah)
14+
15+
;--- invalid_dw_lang_2.ll
16+
!0 = distinct !DICompileUnit(language: DW_LNAME_C)
17+
18+
;--- invalid_dw_lname.ll
19+
!0 = distinct !DICompileUnit(sourceLanguageName: DW_LNAME_blah)
20+
21+
;--- invalid_dw_lname_2.ll
22+
!0 = distinct !DICompileUnit(sourceLanguageName: DW_LANG_C)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
; RUN: not llvm-as < %s -disable-output 2>&1 | FileCheck %s
22

3-
; CHECK: <stdin>:[[@LINE+1]]:74: error: missing required field 'language'
3+
; CHECK: <stdin>:[[@LINE+1]]:15: error: missing one of 'language' or 'sourceLanguageName', required for !DICompileUnit
44
!0 = distinct !DICompileUnit(file: !DIFile(filename: "a", directory: "b"))
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
; RUN: llvm-as < %s | llvm-dis | llvm-as | llvm-dis | FileCheck %s
2+
3+
; CHECK: sourceLanguageName: DW_LNAME_ObjC_plus_plus
4+
5+
source_filename = "cu.cpp"
6+
target triple = "arm64-apple-macosx"
7+
8+
!llvm.dbg.cu = !{!0}
9+
!llvm.module.flags = !{!3, !4}
10+
11+
!0 = distinct !DICompileUnit(sourceLanguageName: DW_LNAME_ObjC_plus_plus, file: !1, producer: "handwritten", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, globals: !2, splitDebugInlining: false, nameTableKind: Apple, sysroot: "/")
12+
!1 = !DIFile(filename: "cu.cpp", directory: "/tmp")
13+
!2 = !{}
14+
!3 = !{i32 7, !"Dwarf Version", i32 5}
15+
!4 = !{i32 2, !"Debug Info Version", i32 3}

0 commit comments

Comments
 (0)