[DebugInfo] Handle -main-file-name correctly for asm source.
This option provides only the base filename, not a full relative path.
Part of the fix for PR41839.
Differential Revision: https://reviews.llvm.org/D62071
llvm-svn: 361245
diff --git a/llvm/lib/MC/MCContext.cpp b/llvm/lib/MC/MCContext.cpp
index 6ca7d3d..e83702e 100644
--- a/llvm/lib/MC/MCContext.cpp
+++ b/llvm/lib/MC/MCContext.cpp
@@ -582,10 +582,21 @@
}
// Canonicalize the root filename. It cannot be empty, and should not
// repeat the compilation dir.
- StringRef FileName =
- !getMainFileName().empty() ? StringRef(getMainFileName()) : InputFileName;
- if (FileName.empty() || FileName == "-")
- FileName = "<stdin>";
+ // The MCContext ctor initializes MainFileName to the name associated with
+ // the SrcMgr's main file ID, which might be the same as InputFileName (and
+ // possibly include directory components).
+ // Or, MainFileName might have been overridden by a -main-file-name option,
+ // which is supposed to be just a base filename with no directory component.
+ // So, if the InputFileName and MainFileName are not equal, assume
+ // MainFileName is a substitute basename and replace the last component.
+ SmallString<1024> FileNameBuf = InputFileName;
+ if (FileNameBuf.empty() || FileNameBuf == "-")
+ FileNameBuf = "<stdin>";
+ if (!getMainFileName().empty() && FileNameBuf != getMainFileName()) {
+ llvm::sys::path::remove_filename(FileNameBuf);
+ llvm::sys::path::append(FileNameBuf, getMainFileName());
+ }
+ StringRef FileName = FileNameBuf;
if (FileName.consume_front(getCompilationDir()))
if (llvm::sys::path::is_separator(FileName.front()))
FileName = FileName.drop_front();