Set clang module directory correctly with fmodule-file-home-is-cwd

When building and importing modules built with
`-fmodule-file-home-is-cwd` we should be setting the debug info
directory to the current working directory. This matches the
behavior in clang: https://reviews.llvm.org/D134911
This commit is contained in:
Richard Howell
2023-09-29 08:32:29 -07:00
parent 204d496c36
commit 76dd1f9069
2 changed files with 31 additions and 4 deletions

View File

@@ -48,6 +48,8 @@
#include "clang/Basic/SourceManager.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Lex/HeaderSearchOptions.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Serialization/ASTReader.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/Config/config.h"
@@ -404,7 +406,7 @@ public:
auto *CS = DS->InlinedCallSite;
if (!CS)
return nullptr;
auto CachedInlinedAt = InlinedAtCache.find(CS);
if (CachedInlinedAt != InlinedAtCache.end())
return cast<llvm::MDNode>(CachedInlinedAt->second);
@@ -769,6 +771,15 @@ private:
uint64_t Signature =
Desc.getSignature() ? Desc.getSignature().truncatedValue() : ~1ULL;
// Clang modules using fmodule-file-home-is-cwd should have their
// include path set to the working directory.
auto &HSI =
CI.getClangPreprocessor().getHeaderSearchInfo().getHeaderSearchOpts();
if (HSI.ModuleFileHomeIsCwd) {
Desc = ASTSourceDescriptor(Desc.getModuleName(), Opts.DebugCompilationDir,
Desc.getASTFile(), Desc.getSignature());
}
// Handle Clang modules.
if (ClangModule) {
llvm::DIModule *Parent = nullptr;
@@ -1308,7 +1319,7 @@ private:
}
// FIXME: assert that SizeInBits == OffsetInBits.
SizeInBits = OffsetInBits;
auto FwdDecl = llvm::TempDINode(DBuilder.createReplaceableCompositeType(
llvm::dwarf::DW_TAG_structure_type, MangledName, Scope, MainFile, 0,
llvm::dwarf::DW_LANG_Swift, SizeInBits, AlignInBits, Flags,
@@ -1561,7 +1572,7 @@ private:
return createPointerSizedStruct(Scope,
MangledName,
MainFile, 0, Flags, MangledName);
case TypeKind::BuiltinTuple:
llvm_unreachable("BuiltinTupleType should not show up here");
@@ -1818,7 +1829,7 @@ private:
return true;
}
#endif
llvm::DIType *getOrCreateType(DebugTypeInfo DbgTy) {
// Is this an empty type?
if (DbgTy.isNull())

View File

@@ -0,0 +1,16 @@
// First populate the module cache to avoid issues with implicit modules
// and -fmodule-file-home-is-cwd.
// RUN: %target-swift-frontend -module-cache-path %t.mcp -emit-ir %s \
// RUN: -g -I %S/Inputs -o /dev/null
// RUN: %target-swift-frontend -module-cache-path %t.mcp -emit-ir %s \
// RUN: -g -I %S/Inputs -file-compilation-dir /CWD \
// RUN: -Xcc -Xclang -Xcc -fmodule-file-home-is-cwd \
// RUN: -o - | %FileCheck %s
import ClangModule.SubModule
let _ = someFunc(0)
// Ensure compilation directory is set to the debug compilation directory.
// CHECK: !DIFile(filename: "ClangModule", directory: "/CWD")