Index: llvm/include/llvm/MC/MCContext.h =================================================================== --- llvm/include/llvm/MC/MCContext.h +++ llvm/include/llvm/MC/MCContext.h @@ -24,6 +24,7 @@ #include "llvm/MC/SectionKind.h" #include "llvm/Support/Allocator.h" #include "llvm/Support/Compiler.h" +#include "llvm/Support/Error.h" #include "llvm/Support/MD5.h" #include "llvm/Support/raw_ostream.h" #include @@ -493,9 +494,9 @@ void setMainFileName(StringRef S) { MainFileName = S; } /// Creates an entry in the dwarf file and directory tables. - unsigned getDwarfFile(StringRef Directory, StringRef FileName, - unsigned FileNumber, MD5::MD5Result *Checksum, - unsigned CUID); + Expected getDwarfFile(StringRef Directory, StringRef FileName, + unsigned FileNumber, + MD5::MD5Result *Checksum, unsigned CUID); bool isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID = 0); Index: llvm/include/llvm/MC/MCDwarf.h =================================================================== --- llvm/include/llvm/MC/MCDwarf.h +++ llvm/include/llvm/MC/MCDwarf.h @@ -20,6 +20,7 @@ #include "llvm/ADT/StringMap.h" #include "llvm/ADT/StringRef.h" #include "llvm/MC/MCSection.h" +#include "llvm/Support/Error.h" #include "llvm/Support/MD5.h" #include #include @@ -213,8 +214,8 @@ MCDwarfLineTableHeader() = default; - unsigned getFile(StringRef &Directory, StringRef &FileName, - MD5::MD5Result *Checksum, unsigned FileNumber = 0); + Expected getFile(StringRef &Directory, StringRef &FileName, + MD5::MD5Result *Checksum, unsigned FileNumber = 0); std::pair Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params, Optional &LineStr) const; @@ -237,8 +238,8 @@ Header.CompilationDir = CompilationDir; } - unsigned getFile(StringRef Directory, StringRef FileName, - MD5::MD5Result *Checksum) { + Expected getFile(StringRef Directory, StringRef FileName, + MD5::MD5Result *Checksum) { return Header.getFile(Directory, FileName, Checksum); } @@ -257,8 +258,8 @@ void EmitCU(MCObjectStreamer *MCOS, MCDwarfLineTableParams Params, Optional &LineStr) const; - unsigned getFile(StringRef &Directory, StringRef &FileName, - MD5::MD5Result *Checksum, unsigned FileNumber = 0); + Expected getFile(StringRef &Directory, StringRef &FileName, + MD5::MD5Result *Checksum, unsigned FileNumber = 0); MCSymbol *getLabel() const { return Header.Label; Index: llvm/include/llvm/MC/MCStreamer.h =================================================================== --- llvm/include/llvm/MC/MCStreamer.h +++ llvm/include/llvm/MC/MCStreamer.h @@ -23,6 +23,7 @@ #include "llvm/MC/MCLinkerOptimizationHint.h" #include "llvm/MC/MCSymbol.h" #include "llvm/MC/MCWinEH.h" +#include "llvm/Support/Error.h" #include "llvm/Support/MD5.h" #include "llvm/Support/SMLoc.h" #include "llvm/Support/TargetParser.h" @@ -752,10 +753,10 @@ /// \brief Associate a filename with a specified logical file number. This /// implements the DWARF2 '.file 4 "foo.c"' assembler directive. - virtual unsigned EmitDwarfFileDirective(unsigned FileNo, StringRef Directory, - StringRef Filename, - MD5::MD5Result *Checksum = nullptr, - unsigned CUID = 0); + virtual Expected + EmitDwarfFileDirective(unsigned FileNo, StringRef Directory, + StringRef Filename, MD5::MD5Result *Checksum = nullptr, + unsigned CUID = 0); /// \brief This implements the DWARF2 '.loc fileno lineno ...' assembler /// directive. Index: llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp =================================================================== --- llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp +++ llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp @@ -102,9 +102,10 @@ // extend .file to support this. unsigned CUID = Asm->OutStreamer->hasRawTextSupport() ? 0 : getUniqueID(); if (!File) - return Asm->OutStreamer->EmitDwarfFileDirective(0, "", "", nullptr, CUID); - return Asm->OutStreamer->EmitDwarfFileDirective( - 0, File->getDirectory(), File->getFilename(), getMD5AsBytes(File), CUID); + return cantFail( + Asm->OutStreamer->EmitDwarfFileDirective(0, "", "", nullptr, CUID)); + return cantFail(Asm->OutStreamer->EmitDwarfFileDirective( + 0, File->getDirectory(), File->getFilename(), getMD5AsBytes(File), CUID)); } DIE *DwarfCompileUnit::getOrCreateGlobalVariableDIE( Index: llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp =================================================================== --- llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp +++ llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp @@ -294,10 +294,10 @@ } unsigned DwarfTypeUnit::getOrCreateSourceID(const DIFile *File) { - return SplitLineTable - ? SplitLineTable->getFile(File->getDirectory(), - File->getFilename(), getMD5AsBytes(File)) - : getCU().getOrCreateSourceID(File); + return SplitLineTable ? cantFail(SplitLineTable->getFile(File->getDirectory(), + File->getFilename(), + getMD5AsBytes(File))) + : getCU().getOrCreateSourceID(File); } void DwarfUnit::addOpAddress(DIELoc &Die, const MCSymbol *Sym) { Index: llvm/lib/MC/MCAsmStreamer.cpp =================================================================== --- llvm/lib/MC/MCAsmStreamer.cpp +++ llvm/lib/MC/MCAsmStreamer.cpp @@ -211,10 +211,11 @@ SMLoc Loc) override; void EmitFileDirective(StringRef Filename) override; - unsigned EmitDwarfFileDirective(unsigned FileNo, StringRef Directory, - StringRef Filename, - MD5::MD5Result *Checksum = 0, - unsigned CUID = 0) override; + Expected EmitDwarfFileDirective(unsigned FileNo, + StringRef Directory, + StringRef Filename, + MD5::MD5Result *Checksum = 0, + unsigned CUID = 0) override; void EmitDwarfLocDirective(unsigned FileNo, unsigned Line, unsigned Column, unsigned Flags, unsigned Isa, unsigned Discriminator, @@ -1073,18 +1074,19 @@ EmitEOL(); } -unsigned MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, - StringRef Directory, - StringRef Filename, - MD5::MD5Result *Checksum, - unsigned CUID) { +Expected +MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, StringRef Directory, + StringRef Filename, + MD5::MD5Result *Checksum, unsigned CUID) { assert(CUID == 0); MCDwarfLineTable &Table = getContext().getMCDwarfLineTable(CUID); unsigned NumFiles = Table.getMCDwarfFiles().size(); - FileNo = Table.getFile(Directory, Filename, Checksum, FileNo); - if (FileNo == 0) - return 0; + Expected FileNoOrErr = + Table.getFile(Directory, Filename, Checksum, FileNo); + if (!FileNoOrErr) + return FileNoOrErr.takeError(); + FileNo = FileNoOrErr.get(); if (NumFiles == Table.getMCDwarfFiles().size()) return FileNo; Index: llvm/lib/MC/MCContext.cpp =================================================================== --- llvm/lib/MC/MCContext.cpp +++ llvm/lib/MC/MCContext.cpp @@ -536,9 +536,11 @@ /// directory tables. If the file number has already been allocated it is an /// error and zero is returned and the client reports the error, else the /// allocated file number is returned. The file numbers may be in any order. -unsigned MCContext::getDwarfFile(StringRef Directory, StringRef FileName, - unsigned FileNumber, MD5::MD5Result *Checksum, - unsigned CUID) { +Expected MCContext::getDwarfFile(StringRef Directory, + StringRef FileName, + unsigned FileNumber, + MD5::MD5Result *Checksum, + unsigned CUID) { MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID]; return Table.getFile(Directory, FileName, Checksum, FileNumber); } Index: llvm/lib/MC/MCDwarf.cpp =================================================================== --- llvm/lib/MC/MCDwarf.cpp +++ llvm/lib/MC/MCDwarf.cpp @@ -497,16 +497,17 @@ MCOS->EmitLabel(LineEndSym); } -unsigned MCDwarfLineTable::getFile(StringRef &Directory, StringRef &FileName, - MD5::MD5Result *Checksum, - unsigned FileNumber) { +Expected MCDwarfLineTable::getFile(StringRef &Directory, + StringRef &FileName, + MD5::MD5Result *Checksum, + unsigned FileNumber) { return Header.getFile(Directory, FileName, Checksum, FileNumber); } -unsigned MCDwarfLineTableHeader::getFile(StringRef &Directory, - StringRef &FileName, - MD5::MD5Result *Checksum, - unsigned FileNumber) { +Expected MCDwarfLineTableHeader::getFile(StringRef &Directory, + StringRef &FileName, + MD5::MD5Result *Checksum, + unsigned FileNumber) { if (Directory == CompilationDir) Directory = ""; if (FileName.empty()) { @@ -514,6 +515,9 @@ Directory = ""; } assert(!FileName.empty()); + // If any files have an MD5 checksum, they all must. + if (MCDwarfFiles.empty()) + HasMD5 = (Checksum != nullptr); if (FileNumber == 0) { // File numbers start with 1 and/or after any file numbers // allocated by inline-assembler .file directives. @@ -532,13 +536,15 @@ // Get the new MCDwarfFile slot for this FileNumber. MCDwarfFile &File = MCDwarfFiles[FileNumber]; - // It is an error to use see the same number more than once. + // It is an error to see the same number more than once. if (!File.Name.empty()) - return 0; + return make_error("file number already allocated", + inconvertibleErrorCode()); // If any files have an MD5 checksum, they all must. - if (FileNumber > 1) - assert(HasMD5 == (Checksum != nullptr)); + if (HasMD5 != (Checksum != nullptr)) + return make_error("inconsistent use of MD5 checksums", + inconvertibleErrorCode()); if (Directory.empty()) { // Separate the directory part from the basename of the FileName. Index: llvm/lib/MC/MCParser/AsmParser.cpp =================================================================== --- llvm/lib/MC/MCParser/AsmParser.cpp +++ llvm/lib/MC/MCParser/AsmParser.cpp @@ -848,8 +848,9 @@ bool InsertResult = getContext().addGenDwarfSection(Sec); assert(InsertResult && ".text section should not have debug info yet"); (void)InsertResult; - getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective( - 0, StringRef(), getContext().getMainFileName())); + getContext().setGenDwarfFileNumber( + cantFail(getStreamer().EmitDwarfFileDirective( + 0, StringRef(), getContext().getMainFileName()))); } // While we have input, parse each statement. @@ -2167,8 +2168,8 @@ // current Dwarf File is for the CppHashFilename if not then emit the // Dwarf File table for it and adjust the line number for the .loc. if (!CppHashInfo.Filename.empty()) { - unsigned FileNumber = getStreamer().EmitDwarfFileDirective( - 0, StringRef(), CppHashInfo.Filename); + unsigned FileNumber = cantFail(getStreamer().EmitDwarfFileDirective( + 0, StringRef(), CppHashInfo.Filename)); getContext().setGenDwarfFileNumber(FileNumber); // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's @@ -3254,7 +3255,6 @@ bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) { // FIXME: I'm not sure what this is. int64_t FileNumber = -1; - SMLoc FileNumberLoc = getLexer().getLoc(); if (getLexer().is(AsmToken::Integer)) { FileNumber = getTok().getIntVal(); Lex(); @@ -3321,9 +3321,13 @@ // we turn off -g option, directly use the existing debug info instead. if (getContext().getGenDwarfForAssembly()) getContext().setGenDwarfForAssembly(false); - else if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, - Filename, CKMem) == 0) - return Error(FileNumberLoc, "file number already allocated"); + else { + Expected FileNumOrErr = getStreamer().EmitDwarfFileDirective( + FileNumber, Directory, Filename, CKMem); + if (!FileNumOrErr) + return Error(DirectiveLoc, toString(FileNumOrErr.takeError())); + FileNumber = FileNumOrErr.get(); + } } return false; Index: llvm/lib/MC/MCStreamer.cpp =================================================================== --- llvm/lib/MC/MCStreamer.cpp +++ llvm/lib/MC/MCStreamer.cpp @@ -188,11 +188,11 @@ emitFill(NumBytes, 0); } -unsigned MCStreamer::EmitDwarfFileDirective(unsigned FileNo, - StringRef Directory, - StringRef Filename, - MD5::MD5Result *Checksum, - unsigned CUID) { +Expected MCStreamer::EmitDwarfFileDirective(unsigned FileNo, + StringRef Directory, + StringRef Filename, + MD5::MD5Result *Checksum, + unsigned CUID) { return getContext().getDwarfFile(Directory, Filename, FileNo, Checksum, CUID); } Index: llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp =================================================================== --- llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp +++ llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp @@ -834,7 +834,7 @@ if (filenameMap.find(Filename) != filenameMap.end()) continue; filenameMap[Filename] = i; - OutStreamer->EmitDwarfFileDirective(i, "", Filename); + cantFail(OutStreamer->EmitDwarfFileDirective(i, "", Filename)); ++i; } @@ -849,7 +849,7 @@ if (filenameMap.find(Filename) != filenameMap.end()) continue; filenameMap[Filename] = i; - OutStreamer->EmitDwarfFileDirective(i, "", Filename); + cantFail(OutStreamer->EmitDwarfFileDirective(i, "", Filename)); ++i; } } Index: llvm/test/MC/ELF/debug-md5-err.s =================================================================== --- llvm/test/MC/ELF/debug-md5-err.s +++ llvm/test/MC/ELF/debug-md5-err.s @@ -19,3 +19,7 @@ # Non-DWARF .file syntax with checksum. # CHECK: [[@LINE+1]]:{{[0-9]+}}: error: MD5 checksum specified, but no file number .file "baz" md5 "ffeeddccbbaa998877665544332211gg" + +# Inconsistent use of MD5 option. Note: .file 1 did not supply one. +# CHECK: [[@LINE+1]]:{{[0-9]+}}: error: inconsistent use of MD5 checksums + .file 5 "bax" md5 "ffeeddccbbaa99887766554433221100"