Index: llvm/lib/MC/MCDwarf.cpp =================================================================== --- llvm/lib/MC/MCDwarf.cpp +++ llvm/lib/MC/MCDwarf.cpp @@ -514,6 +514,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. @@ -537,8 +540,8 @@ return 0; // If any files have an MD5 checksum, they all must. - if (FileNumber > 1) - assert(HasMD5 == (Checksum != nullptr)); + if (HasMD5 != (Checksum != nullptr)) + return UINT_MAX; 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 @@ -3367,9 +3367,15 @@ // 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 { + FileNumber = getStreamer().EmitDwarfFileDirective(FileNumber, Directory, + Filename, CKMem); + if (check(FileNumber == 0, FileNumberLoc, + "file number already allocated") || + check(FileNumber == UINT_MAX, DirectiveLoc, + "inconsistent use of MD5 checksums")) + return true; + } } return false; 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"