Index: llvm/lib/MC/MCDwarf.cpp =================================================================== --- llvm/lib/MC/MCDwarf.cpp +++ llvm/lib/MC/MCDwarf.cpp @@ -576,11 +576,6 @@ // Get the new MCDwarfFile slot for this FileNumber. MCDwarfFile &File = MCDwarfFiles[FileNumber]; - // It is an error to see the same number more than once. - if (!File.Name.empty()) - return make_error("file number already allocated", - inconvertibleErrorCode()); - // If any files have embedded source, they all must. if (HasSource != (Source != None)) return make_error("inconsistent use of embedded source", Index: llvm/lib/MC/MCParser/AsmParser.cpp =================================================================== --- llvm/lib/MC/MCParser/AsmParser.cpp +++ llvm/lib/MC/MCParser/AsmParser.cpp @@ -3364,14 +3364,6 @@ if (FileNumber == -1) getStreamer().EmitFileDirective(Filename); else { - // In case there is a -g option as well as debug info from directive .file, - // we turn off the -g option, directly use the existing debug info instead. - // Also reset any implicit ".file 0" for the assembler source. - if (Ctx.getGenDwarfForAssembly()) { - Ctx.getMCDwarfLineTable(0).resetRootFile(); - Ctx.setGenDwarfForAssembly(false); - } - MD5::MD5Result *CKMem = nullptr; if (HasMD5) { CKMem = (MD5::MD5Result *)Ctx.allocate(sizeof(MD5::MD5Result), 1); Index: llvm/test/MC/AsmParser/directive_file-4.s =================================================================== --- /dev/null +++ llvm/test/MC/AsmParser/directive_file-4.s @@ -0,0 +1,31 @@ +// RUN: llvm-mc -g -triple i386-unknown-unknown %s | FileCheck -check-prefix=CHECK-DEBUG-ASM %s +// RUN: llvm-mc -triple i386-unknown-unknown %s | FileCheck -check-prefix=CHECK-DEFAULT %s +// RUN: llvm-mc -g -triple i386-unknown-unknown %s -filetype=obj | obj2yaml | FileCheck -check-prefix=CHECK-DEBUG-OBJ %s + +// Test for Bug 38994 +// This testcase has a single function and two .file directives: +// one with the [file-num] argument and one without. +// When compiled with -g, this testcase will not report an error, +// and generate new debug info. The '.file 1' directive will be +// replaced by the assembler-generated '.file 1' directive. + + .file "xyz.c" +f1: + .file 1 "abc.c" + nop +.size f1, .-f1 + +// CHECK-DEFAULT: .file "xyz.c" +// CHECK-DEFAULT-NEXT: f1: +// CHECK-DEFAULT-NEXT: .file 1 "abc.c" + +// CHECK-DEBUG-ASM: .file "xyz.c" +// CHECK-DEBUG-ASM-NEXT: f1: +// CHECK-DEBUG-ASM-NEXT: .file 1 {{.*}} "directive_file-4.s" + +// CHECK-DEBUG-OBJ: Sections: +// CHECK-DEBUG-OBJ: - Name: .text +// CHECK-DEBUG-OBJ: - Name: .debug_info +// CHECK-DEBUG-OBJ: - Name: .rel.debug_info +// CHECK-DEBUG-OBJ: Info: .debug_info +// CHECK-DEBUG-OBJ: Symbols: Index: llvm/test/MC/AsmParser/directive_file-5.s =================================================================== --- /dev/null +++ llvm/test/MC/AsmParser/directive_file-5.s @@ -0,0 +1,16 @@ +// RUN: not llvm-mc -g -triple i386-unknown-unknown %s | FileCheck -check-prefix=CHECK-DEBUG %s +// RUN: llvm-mc -triple i386-unknown-unknown %s -filetype=obj | obj2yaml | FileCheck -check-prefix=CHECK-DEFAULT %s + +// Test for Bug 38994 +// This testcase .... TBD + + .file "xyz.c" +.text + +f1: + nop + .file 1 "abc.c" + +// CHECK-DEBUG: error + +// CHECK-DEFAULT-NOT: - Name: .debug_info