Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions include/swift/ClangImporter/ClangImporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ namespace dependencies {
namespace swift {
enum class ResultConvention : uint8_t;
class ASTContext;
class CASOptions;
class CompilerInvocation;
class ClangImporterOptions;
class ClangInheritanceInfo;
Expand All @@ -82,6 +83,7 @@ class ClangNode;
class ConcreteDeclRef;
class Decl;
class DeclContext;
class DiagnosticEngine;
class EffectiveClangContext;
class EnumDecl;
class FuncDecl;
Expand Down Expand Up @@ -881,22 +883,41 @@ struct ClangInvocationFileMapping {
bool requiresBuiltinHeadersInSystemModules;
};

class ClangInvocationFileMappingContext {
public:
const LangOptions &LangOpts;
SearchPathOptions &SearchPathOpts;
ClangImporterOptions &ClangImporterOpts;
const CASOptions &CASOpts;
DiagnosticEngine &Diags;

ClangInvocationFileMappingContext(
const LangOptions &LangOpts, SearchPathOptions &SearchPathOpts,
ClangImporterOptions &ClangImporterOpts, const CASOptions &CASOpts,
DiagnosticEngine &Diags)
: LangOpts(LangOpts), SearchPathOpts(SearchPathOpts),
ClangImporterOpts(ClangImporterOpts), CASOpts(CASOpts),
Diags(Diags) {}

ClangInvocationFileMappingContext(const swift::ASTContext &Ctx);
};

/// On Linux, some platform libraries (glibc, libstdc++) are not modularized.
/// We inject modulemaps for those libraries into their include directories
/// to allow using them from Swift.
///
/// `suppressDiagnostic` prevents us from emitting warning messages when we
/// are unable to find headers.
ClangInvocationFileMapping getClangInvocationFileMapping(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer it if we unified the API here so that the client doesn't need to wonder at a glance which one they need to call. If we're committing to being able to do this without an ASTContext, then we should stick to only having the general version.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels to me like the one without an ASTContext is more of a special version because it is limited to this particular case before ASTContext is created and is more complicated to have an ownership transfer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. There is only one version now

const ASTContext &ctx,
const ClangInvocationFileMappingContext &ctx,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> vfs = nullptr,
bool suppressDiagnostic = false);

/// Apply the given file mapping to the specified 'fileSystem', used
/// primarily to inject modulemaps on platforms with non-modularized
/// platform libraries.
ClangInvocationFileMapping applyClangInvocationMapping(
const ASTContext &ctx,
const ClangInvocationFileMappingContext &ctx,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> baseVFS,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &fileSystem,
bool suppressDiagnostics = false);
Expand Down
34 changes: 20 additions & 14 deletions lib/ClangImporter/ClangIncludePaths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ ClangImporter::createClangArgs(const ClangImporterOptions &ClangImporterOpts,
}

static SmallVector<std::pair<std::string, std::string>, 2>
getLibcFileMapping(const ASTContext &ctx, StringRef modulemapFileName,
getLibcFileMapping(const ClangInvocationFileMappingContext &ctx,
StringRef modulemapFileName,
std::optional<ArrayRef<StringRef>> maybeHeaderFileNames,
const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &vfs,
bool suppressDiagnostic) {
Expand Down Expand Up @@ -267,7 +268,8 @@ getLibcFileMapping(const ASTContext &ctx, StringRef modulemapFileName,
}

static void getLibStdCxxFileMapping(
ClangInvocationFileMapping &fileMapping, const ASTContext &ctx,
ClangInvocationFileMapping &fileMapping,
const ClangInvocationFileMappingContext &ctx,
const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &vfs,
bool suppressDiagnostic) {
assert(ctx.LangOpts.EnableCXXInterop &&
Expand Down Expand Up @@ -472,7 +474,8 @@ GetPlatformAuxiliaryFile(StringRef Platform, StringRef File,
}

void GetWindowsFileMappings(
ClangInvocationFileMapping &fileMapping, const ASTContext &Context,
ClangInvocationFileMapping &fileMapping,
const ClangInvocationFileMappingContext &Context,
const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &driverVFS,
bool &requiresBuiltinHeadersInSystemModules) {
const llvm::Triple &Triple = Context.LangOpts.Target;
Expand Down Expand Up @@ -611,8 +614,14 @@ void GetWindowsFileMappings(
}
} // namespace

ClangInvocationFileMappingContext::ClangInvocationFileMappingContext(
const swift::ASTContext &Ctx)
: ClangInvocationFileMappingContext(Ctx.LangOpts, Ctx.SearchPathOpts,
Ctx.ClangImporterOpts, Ctx.CASOpts, Ctx.Diags) {}

ClangInvocationFileMapping swift::getClangInvocationFileMapping(
const ASTContext &ctx, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> vfs,
const ClangInvocationFileMappingContext &ctx,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> vfs,
bool suppressDiagnostic) {
ClangInvocationFileMapping result;
if (!vfs)
Expand Down Expand Up @@ -683,10 +692,11 @@ ClangInvocationFileMapping swift::getClangInvocationFileMapping(
return result;
}

ClangInvocationFileMapping swift::applyClangInvocationMapping(const ASTContext &ctx,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> baseVFS,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &fileSystem,
bool suppressDiagnostics) {
ClangInvocationFileMapping swift::applyClangInvocationMapping(
const ClangInvocationFileMappingContext &ctx,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> baseVFS,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &fileSystem,
bool suppressDiagnostics) {
if (ctx.CASOpts.HasImmutableFileSystem)
return ClangInvocationFileMapping();

Expand Down Expand Up @@ -715,13 +725,9 @@ ClangInvocationFileMapping swift::applyClangInvocationMapping(const ASTContext &
<< "' with the following contents:\n";
llvm::errs() << file.second << "\n";
}
auto contents = ctx.Allocate<char>(file.second.size() + 1);
std::copy(file.second.begin(), file.second.end(), contents.begin());
// null terminate the buffer.
contents[contents.size() - 1] = '\0';
// Note MemoryBuffer is guaranteeed to be null-terminated.
overridenVFS->addFile(file.first, 0,
llvm::MemoryBuffer::getMemBuffer(StringRef(
contents.begin(), contents.size() - 1)));
llvm::MemoryBuffer::getMemBufferCopy(file.second));
}
llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> overlayVFS =
new llvm::vfs::OverlayFileSystem(fileSystem);
Expand Down
8 changes: 8 additions & 0 deletions lib/DependencyScan/ModuleDependencyScanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1992,6 +1992,14 @@ ModuleDependencyInfo ModuleDependencyScanner::bridgeClangModuleDependency(
}
}

// Pass the -sdk flag to make the system header VFS overlay finable
// for the -direct-clang-cc1-module-build emit-pcm command on Windows.
StringRef SDKPath = ScanASTContext.SearchPathOpts.getSDKPath();
if (!SDKPath.empty()) {
swiftArgs.push_back("-sdk");
swiftArgs.push_back(SDKPath.str());
}

// Add args reported by the scanner.
auto clangArgs = invocation.getCC1CommandLine();
llvm::for_each(clangArgs, addClangArg);
Expand Down
19 changes: 19 additions & 0 deletions lib/Frontend/Frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,25 @@ bool CompilerInstance::setUpVirtualFileSystemOverlays() {
new llvm::vfs::OverlayFileSystem(MemFS);
OverlayVFS->pushOverlay(SourceMgr.getFileSystem());
SourceMgr.setFileSystem(std::move(OverlayVFS));
} else {
// For non-caching -direct-clang-cc1-module-build emit-pcm build,
// setup the clang VFS so it can find system modulemap files
// (like vcruntime.modulemap) as an input file.
if (Invocation.getClangImporterOptions().DirectClangCC1ModuleBuild &&
Invocation.getFrontendOptions().RequestedAction ==
FrontendOptions::ActionType::EmitPCM) {
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS =
SourceMgr.getFileSystem();
ClangInvocationFileMappingContext Context(
Invocation.getLangOptions(), Invocation.getSearchPathOptions(),
Invocation.getClangImporterOptions(), Invocation.getCASOptions(),
Diagnostics);
ClangInvocationFileMapping FileMapping = applyClangInvocationMapping(
Context, nullptr, VFS, /*suppressDiagnostic=*/false);
if (!FileMapping.redirectedFiles.empty()) {
SourceMgr.setFileSystem(std::move(VFS));
}
}
}

auto ExpectedOverlay =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module SAL [system] {
header "sal.h"
export *
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// REQUIRES: OS=windows-msvc

// Test that the -direct-clang-cc1-module-build is able to create a module from the VC runtime with an overlaid modulemap file

// RUN: %empty-directory(%t)
// RUN: split-file %s %t

// RUN: %swift_frontend_plain -target %target-triple -module-cache-path %t/clang-module-cache -scan-dependencies -module-name Test -sdk %S/Inputs/WinSDK %t/Test.swift -o %t/deps.json
// RUN: %{python} %S/../CAS/Inputs/BuildCommandExtractor.py %t/deps.json clang:SAL > %t/SAL.cmd
// RUN: %swift_frontend_plain @%t/SAL.cmd

//--- Test.swift
import SAL