diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp index cca0b51a0ffd50c64c42c521ef036c3c93bb28f0..fe2de36ac42b8aec583831eb898d92b1a047b0c2 100644 --- a/clang/lib/CodeGen/CGDebugInfo.cpp +++ b/clang/lib/CodeGen/CGDebugInfo.cpp @@ -29,6 +29,7 @@ #include "clang/Basic/SourceManager.h" #include "clang/Basic/Version.h" #include "clang/Frontend/CodeGenOptions.h" +#include "clang/Frontend/FrontendOptions.h" #include "clang/Lex/HeaderSearchOptions.h" #include "clang/Lex/ModuleMap.h" #include "clang/Lex/PreprocessorOptions.h" @@ -495,6 +496,16 @@ void CGDebugInfo::CreateCompileUnit() { llvm::sys::path::append(MainFileDirSS, MainFileName); MainFileName = MainFileDirSS.str(); } + // If the main file name provided is identical to the input file name, and + // if the input file is a preprocessed source, use the module name for + // debug info. The module name comes from the name specified in the first + // linemarker if the input is a preprocessed source. + if (MainFile->getName() == MainFileName && + FrontendOptions::getInputKindForExtension( + MainFile->getName().rsplit('.').second) + .isPreprocessed()) + MainFileName = CGM.getModule().getName().str(); + CSKind = computeChecksum(SM.getMainFileID(), Checksum); } diff --git a/clang/test/CodeGen/debug-info-preprocessed-file.i b/clang/test/CodeGen/debug-info-preprocessed-file.i new file mode 100644 index 0000000000000000000000000000000000000000..d231b45d67c2e43c8a970dcd8feb0edf108dc116 --- /dev/null +++ b/clang/test/CodeGen/debug-info-preprocessed-file.i @@ -0,0 +1,11 @@ +# 1 "/foo/bar/preprocessed-input.c" +# 1 "" 1 +# 1 "" 3 +# 318 "" 3 +# 1 "" 1 +# 1 "" 2 +# 1 "preprocessed-input.c" 2 + +// RUN: %clang -g -c -S -emit-llvm -o - %s | FileCheck %s +// CHECK: !DICompileUnit(language: DW_LANG_C99, file: ![[FILE:[0-9]+]] +// CHECK: ![[FILE]] = !DIFile(filename: "/foo/bar/preprocessed-input.c"