Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions llvm/include/llvm/AsmParser/LLToken.h
Original file line number Diff line number Diff line change
Expand Up @@ -488,27 +488,28 @@ enum Kind {
SummaryID, // ^42

// String valued tokens (StrVal).
LabelStr, // foo:
GlobalVar, // @foo @"foo"
ComdatVar, // $foo
LocalVar, // %foo %"foo"
MetadataVar, // !foo
StringConstant, // "foo"
DwarfTag, // DW_TAG_foo
DwarfAttEncoding, // DW_ATE_foo
DwarfVirtuality, // DW_VIRTUALITY_foo
DwarfLang, // DW_LANG_foo
DwarfCC, // DW_CC_foo
EmissionKind, // lineTablesOnly
NameTableKind, // GNU
FixedPointKind, // Fixed point
DwarfOp, // DW_OP_foo
DIFlag, // DIFlagFoo
DISPFlag, // DISPFlagFoo
DwarfMacinfo, // DW_MACINFO_foo
ChecksumKind, // CSK_foo
DbgRecordType, // dbg_foo
DwarfEnumKind, // DW_APPLE_ENUM_KIND_foo
LabelStr, // foo:
GlobalVar, // @foo @"foo"
ComdatVar, // $foo
LocalVar, // %foo %"foo"
MetadataVar, // !foo
StringConstant, // "foo"
DwarfTag, // DW_TAG_foo
DwarfAttEncoding, // DW_ATE_foo
DwarfVirtuality, // DW_VIRTUALITY_foo
DwarfLang, // DW_LANG_foo
DwarfSourceLangName, // DW_LNAME_foo
DwarfCC, // DW_CC_foo
EmissionKind, // lineTablesOnly
NameTableKind, // GNU
FixedPointKind, // Fixed point
DwarfOp, // DW_OP_foo
DIFlag, // DIFlagFoo
DISPFlag, // DISPFlagFoo
DwarfMacinfo, // DW_MACINFO_foo
ChecksumKind, // CSK_foo
DbgRecordType, // dbg_foo
DwarfEnumKind, // DW_APPLE_ENUM_KIND_foo

// Type valued tokens (TyVal).
Type,
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/AsmParser/LLLexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,7 @@ lltok::Kind LLLexer::LexIdentifier() {
DWKEYWORD(ATE, DwarfAttEncoding);
DWKEYWORD(VIRTUALITY, DwarfVirtuality);
DWKEYWORD(LANG, DwarfLang);
DWKEYWORD(LNAME, DwarfSourceLangName);
DWKEYWORD(CC, DwarfCC);
DWKEYWORD(OP, DwarfOp);
DWKEYWORD(MACINFO, DwarfMacinfo);
Expand Down
49 changes: 43 additions & 6 deletions llvm/lib/AsmParser/LLParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4740,6 +4740,10 @@ struct DwarfLangField : public MDUnsignedField {
DwarfLangField() : MDUnsignedField(0, dwarf::DW_LANG_hi_user) {}
};

struct DwarfSourceLangNameField : public MDUnsignedField {
DwarfSourceLangNameField() : MDUnsignedField(0, UINT32_MAX) {}
};

struct DwarfCCField : public MDUnsignedField {
DwarfCCField() : MDUnsignedField(0, dwarf::DW_CC_hi_user) {}
};
Expand Down Expand Up @@ -4997,6 +5001,25 @@ bool LLParser::parseMDField(LocTy Loc, StringRef Name, DwarfLangField &Result) {
return false;
}

template <>
bool LLParser::parseMDField(LocTy Loc, StringRef Name,
DwarfSourceLangNameField &Result) {
if (Lex.getKind() == lltok::APSInt)
return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));

if (Lex.getKind() != lltok::DwarfSourceLangName)
return tokError("expected DWARF source language name");

unsigned Lang = dwarf::getSourceLanguageName(Lex.getStrVal());
if (!Lang)
return tokError("invalid DWARF source language name" + Twine(" '") +
Lex.getStrVal() + "'");
assert(Lang <= Result.Max && "Expected valid DWARF source language name");
Result.assign(Lang);
Lex.Lex();
return false;
}

template <>
bool LLParser::parseMDField(LocTy Loc, StringRef Name, DwarfCCField &Result) {
if (Lex.getKind() == lltok::APSInt)
Expand Down Expand Up @@ -5836,9 +5859,12 @@ bool LLParser::parseDICompileUnit(MDNode *&Result, bool IsDistinct) {
if (!IsDistinct)
return tokError("missing 'distinct', required for !DICompileUnit");

LocTy Loc = Lex.getLoc();

#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
REQUIRED(language, DwarfLangField, ); \
REQUIRED(file, MDField, (/* AllowNull */ false)); \
OPTIONAL(language, DwarfLangField, ); \
OPTIONAL(sourceLanguageName, DwarfSourceLangNameField, ); \
OPTIONAL(producer, MDStringField, ); \
OPTIONAL(isOptimized, MDBoolField, ); \
OPTIONAL(flags, MDStringField, ); \
Expand All @@ -5860,12 +5886,23 @@ bool LLParser::parseDICompileUnit(MDNode *&Result, bool IsDistinct) {
PARSE_MD_FIELDS();
#undef VISIT_MD_FIELDS

if (!language.Seen && !sourceLanguageName.Seen)
return error(Loc, "missing one of 'language' or 'sourceLanguageName', "
"required for !DICompileUnit");

if (language.Seen && sourceLanguageName.Seen)
return error(Loc, "can only specify one of 'language' and "
"'sourceLanguageName' on !DICompileUnit");

Result = DICompileUnit::getDistinct(
Context, DISourceLanguageName(language.Val), file.Val, producer.Val,
isOptimized.Val, flags.Val, runtimeVersion.Val, splitDebugFilename.Val,
emissionKind.Val, enums.Val, retainedTypes.Val, globals.Val, imports.Val,
macros.Val, dwoId.Val, splitDebugInlining.Val, debugInfoForProfiling.Val,
nameTableKind.Val, rangesBaseAddress.Val, sysroot.Val, sdk.Val);
Context,
language.Seen ? DISourceLanguageName(language.Val)
: DISourceLanguageName(sourceLanguageName.Val, 0),
file.Val, producer.Val, isOptimized.Val, flags.Val, runtimeVersion.Val,
splitDebugFilename.Val, emissionKind.Val, enums.Val, retainedTypes.Val,
globals.Val, imports.Val, macros.Val, dwoId.Val, splitDebugInlining.Val,
debugInfoForProfiling.Val, nameTableKind.Val, rangesBaseAddress.Val,
sysroot.Val, sdk.Val);
return false;
}

Expand Down
16 changes: 11 additions & 5 deletions llvm/lib/Bitcode/Reader/MetadataLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1867,12 +1867,18 @@ Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
// distinct. It's always distinct.
IsDistinct = true;

const auto LangVersionMask = (uint64_t(1) << 63);
const bool HasVersionedLanguage = Record[1] & LangVersionMask;

auto *CU = DICompileUnit::getDistinct(
Context, DISourceLanguageName(Record[1]), getMDOrNull(Record[2]),
getMDString(Record[3]), Record[4], getMDString(Record[5]), Record[6],
getMDString(Record[7]), Record[8], getMDOrNull(Record[9]),
getMDOrNull(Record[10]), getMDOrNull(Record[12]),
getMDOrNull(Record[13]),
Context,
HasVersionedLanguage
? DISourceLanguageName(Record[1] & ~LangVersionMask, 0)
: DISourceLanguageName(Record[1]),
getMDOrNull(Record[2]), getMDString(Record[3]), Record[4],
getMDString(Record[5]), Record[6], getMDString(Record[7]), Record[8],
getMDOrNull(Record[9]), getMDOrNull(Record[10]),
getMDOrNull(Record[12]), getMDOrNull(Record[13]),
Record.size() <= 15 ? nullptr : getMDOrNull(Record[15]),
Record.size() <= 14 ? 0 : Record[14],
Record.size() <= 16 ? true : Record[16],
Expand Down
8 changes: 7 additions & 1 deletion llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2108,7 +2108,13 @@ void ModuleBitcodeWriter::writeDICompileUnit(const DICompileUnit *N,
assert(N->isDistinct() && "Expected distinct compile units");
Record.push_back(/* IsDistinct */ true);

Record.push_back(N->getSourceLanguage().getUnversionedName());
auto Lang = N->getSourceLanguage();
Record.push_back(Lang.getName());
// Set bit so the MetadataLoader can distniguish between versioned and
// unversioned names.
if (Lang.hasVersionedName())
Record.back() ^= (uint64_t(1) << 63);

Record.push_back(VE.getMetadataOrNullID(N->getFile()));
Record.push_back(VE.getMetadataOrNullID(N->getRawProducer()));
Record.push_back(N->isOptimized());
Expand Down
14 changes: 10 additions & 4 deletions llvm/lib/IR/AsmWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2370,10 +2370,16 @@ static void writeDICompileUnit(raw_ostream &Out, const DICompileUnit *N,
Out << "!DICompileUnit(";
MDFieldPrinter Printer(Out, WriterCtx);

Printer.printDwarfEnum("language",
N->getSourceLanguage().getUnversionedName(),
dwarf::LanguageString,
/* ShouldSkipZero */ false);
auto Lang = N->getSourceLanguage();
if (Lang.hasVersionedName())
Printer.printDwarfEnum(
"sourceLanguageName",
static_cast<llvm::dwarf::SourceLanguageName>(Lang.getName()),
dwarf::SourceLanguageNameString,
/* ShouldSkipZero */ false);
else
Printer.printDwarfEnum("language", Lang.getName(), dwarf::LanguageString,
/* ShouldSkipZero */ false);

Printer.printMetadata("file", N->getRawFile(), /* ShouldSkipNull */ false);
Printer.printString("producer", N->getProducer());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
; RUN: not llvm-as < %s -disable-output 2>&1 | FileCheck %s

; CHECK: <stdin>:[[@LINE+1]]:15: error: can only specify one of 'language' and 'sourceLanguageName' on !DICompileUnit
!0 = distinct !DICompileUnit(language: DW_LANG_C, sourceLanguageName: DW_LNAME_C, file: !DIFile(filename: "a", directory: "b"))
22 changes: 22 additions & 0 deletions llvm/test/Assembler/dicompileunit-invalid-language.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
; RUN: split-file %s %t
; RUN: not llvm-as < %t/invalid_dw_lang.ll -disable-output 2>&1 | FileCheck %s --check-prefix=INVALID_DW_LANG
; RUN: not llvm-as < %t/invalid_dw_lang_2.ll -disable-output 2>&1 | FileCheck %s --check-prefix=INVALID_DW_LANG_2
; RUN: not llvm-as < %t/invalid_dw_lname.ll -disable-output 2>&1 | FileCheck %s --check-prefix=INVALID_DW_LNAME
; RUN: not llvm-as < %t/invalid_dw_lname_2.ll -disable-output 2>&1 | FileCheck %s --check-prefix=INVALID_DW_LNAME_2

; INVALID_DW_LANG: invalid DWARF language 'DW_LANG_blah'
; INVALID_DW_LANG_2: expected DWARF language
; INVALID_DW_LNAME: invalid DWARF source language name 'DW_LNAME_blah'
; INVALID_DW_LNAME_2: expected DWARF source language name

;--- invalid_dw_lang.ll
!0 = distinct !DICompileUnit(language: DW_LANG_blah)

;--- invalid_dw_lang_2.ll
!0 = distinct !DICompileUnit(language: DW_LNAME_C)

;--- invalid_dw_lname.ll
!0 = distinct !DICompileUnit(sourceLanguageName: DW_LNAME_blah)

;--- invalid_dw_lname_2.ll
!0 = distinct !DICompileUnit(sourceLanguageName: DW_LANG_C)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; RUN: not llvm-as < %s -disable-output 2>&1 | FileCheck %s

; CHECK: <stdin>:[[@LINE+1]]:74: error: missing required field 'language'
; CHECK: <stdin>:[[@LINE+1]]:15: error: missing one of 'language' or 'sourceLanguageName', required for !DICompileUnit
!0 = distinct !DICompileUnit(file: !DIFile(filename: "a", directory: "b"))
15 changes: 15 additions & 0 deletions llvm/test/Bitcode/dwarf-source-language-name.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
; RUN: llvm-as < %s | llvm-dis | llvm-as | llvm-dis | FileCheck %s

; CHECK: sourceLanguageName: DW_LNAME_ObjC_plus_plus

source_filename = "cu.cpp"
target triple = "arm64-apple-macosx"

!llvm.dbg.cu = !{!0}
!llvm.module.flags = !{!3, !4}

!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: "/")
!1 = !DIFile(filename: "cu.cpp", directory: "/tmp")
!2 = !{}
!3 = !{i32 7, !"Dwarf Version", i32 5}
!4 = !{i32 2, !"Debug Info Version", i32 3}
Loading