Skip to content

Commit 168413e

Browse files
authored
Merge pull request swiftlang#76823 from swiftlang/egorzhdan/msvc-bit-module
[cxx-interop] Modularize __msvc_bit_utils on Windows
2 parents 3334adb + 16e7cbe commit 168413e

File tree

6 files changed

+50
-12
lines changed

6 files changed

+50
-12
lines changed

include/swift/ClangImporter/ClangImporter.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,8 +718,13 @@ ValueDecl *getImportedMemberOperator(const DeclBaseName &name,
718718
} // namespace importer
719719

720720
struct ClangInvocationFileMapping {
721+
/// Mapping from a file name to an existing file path.
721722
SmallVector<std::pair<std::string, std::string>, 2> redirectedFiles;
723+
724+
/// Mapping from a file name to a string of characters that represents the
725+
/// contents of the file.
722726
SmallVector<std::pair<std::string, std::string>, 1> overridenFiles;
727+
723728
bool requiresBuiltinHeadersInSystemModules;
724729
};
725730

lib/ClangImporter/ClangIncludePaths.cpp

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -439,17 +439,16 @@ GetPlatformAuxiliaryFile(StringRef Platform, StringRef File,
439439
return "";
440440
}
441441

442-
SmallVector<std::pair<std::string, std::string>, 2> GetWindowsFileMappings(
443-
ASTContext &Context,
442+
void GetWindowsFileMappings(
443+
ClangInvocationFileMapping &fileMapping, ASTContext &Context,
444444
const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &driverVFS,
445445
bool &requiresBuiltinHeadersInSystemModules) {
446446
const llvm::Triple &Triple = Context.LangOpts.Target;
447447
const SearchPathOptions &SearchPathOpts = Context.SearchPathOpts;
448-
SmallVector<std::pair<std::string, std::string>, 2> Mappings;
449448
std::string AuxiliaryFile;
450449

451450
if (!Triple.isWindowsMSVCEnvironment())
452-
return Mappings;
451+
return;
453452

454453
auto [Driver, clangDiagEngine] = ClangImporter::createClangDriver(
455454
Context.LangOpts, Context.ClangImporterOpts, driverVFS);
@@ -478,7 +477,8 @@ SmallVector<std::pair<std::string, std::string>, 2> GetWindowsFileMappings(
478477
AuxiliaryFile =
479478
GetPlatformAuxiliaryFile("windows", "winsdk.modulemap", SearchPathOpts);
480479
if (!AuxiliaryFile.empty())
481-
Mappings.emplace_back(std::string(WinSDKInjection), AuxiliaryFile);
480+
fileMapping.redirectedFiles.emplace_back(std::string(WinSDKInjection),
481+
AuxiliaryFile);
482482
}
483483

484484
struct {
@@ -506,7 +506,8 @@ SmallVector<std::pair<std::string, std::string>, 2> GetWindowsFileMappings(
506506
// cycle goes away. Note that -fbuiltin-headers-in-system-modules does
507507
// nothing to fix the same problem with C++ headers, and is generally
508508
// fragile.
509-
Mappings.emplace_back(std::string(UCRTInjection), AuxiliaryFile);
509+
fileMapping.redirectedFiles.emplace_back(std::string(UCRTInjection),
510+
AuxiliaryFile);
510511
requiresBuiltinHeadersInSystemModules = true;
511512
}
512513
}
@@ -533,18 +534,28 @@ SmallVector<std::pair<std::string, std::string>, 2> GetWindowsFileMappings(
533534
GetPlatformAuxiliaryFile("windows", "vcruntime.modulemap",
534535
SearchPathOpts);
535536
if (!AuxiliaryFile.empty())
536-
Mappings.emplace_back(std::string(VCToolsInjection), AuxiliaryFile);
537+
fileMapping.redirectedFiles.emplace_back(std::string(VCToolsInjection),
538+
AuxiliaryFile);
537539

538540
llvm::sys::path::remove_filename(VCToolsInjection);
539541
llvm::sys::path::append(VCToolsInjection, "vcruntime.apinotes");
540542
AuxiliaryFile =
541543
GetPlatformAuxiliaryFile("windows", "vcruntime.apinotes",
542544
SearchPathOpts);
543545
if (!AuxiliaryFile.empty())
544-
Mappings.emplace_back(std::string(VCToolsInjection), AuxiliaryFile);
545-
}
546+
fileMapping.redirectedFiles.emplace_back(std::string(VCToolsInjection),
547+
AuxiliaryFile);
546548

547-
return Mappings;
549+
// __msvc_bit_utils.hpp was added in a recent VS 2022 version. It has to be
550+
// referenced from the modulemap directly to avoid modularization errors.
551+
// Older VS versions might not have it. Let's inject an empty header file if
552+
// it isn't available.
553+
llvm::sys::path::remove_filename(VCToolsInjection);
554+
llvm::sys::path::append(VCToolsInjection, "__msvc_bit_utils.hpp");
555+
if (!llvm::sys::fs::exists(VCToolsInjection))
556+
fileMapping.overridenFiles.emplace_back(std::string(VCToolsInjection),
557+
"");
558+
}
548559
}
549560
} // namespace
550561

@@ -615,7 +626,7 @@ ClangInvocationFileMapping swift::getClangInvocationFileMapping(
615626
if (ctx.LangOpts.EnableCXXInterop)
616627
getLibStdCxxFileMapping(result, ctx, vfs, suppressDiagnostic);
617628

618-
result.redirectedFiles.append(GetWindowsFileMappings(
619-
ctx, vfs, result.requiresBuiltinHeadersInSystemModules));
629+
GetWindowsFileMappings(result, ctx, vfs,
630+
result.requiresBuiltinHeadersInSystemModules);
620631
return result;
621632
}

stdlib/public/Platform/vcruntime.modulemap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,11 @@ module std [system] {
702702
module _Private [system] {
703703
requires cplusplus
704704

705+
explicit module __msvc_bit_utils {
706+
header "__msvc_bit_utils.hpp"
707+
export *
708+
}
709+
705710
explicit module xatomic {
706711
header "xatomic.h"
707712
export *

test/Interop/Cxx/stdlib/Inputs/module.modulemap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
module StdNumeric {
2+
header "std-numeric.h"
3+
requires cplusplus
4+
export *
5+
}
6+
17
module StdVector {
28
header "std-vector.h"
39
requires cplusplus
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include <numeric>
2+
3+
inline int64_t getGCD(int64_t a, int64_t b) {
4+
return std::gcd(a, b);
5+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: %target-typecheck-verify-swift -I %S/Inputs -cxx-interoperability-mode=upcoming-swift -Xcc -std=c++17
2+
// RUN: %target-typecheck-verify-swift -I %S/Inputs -cxx-interoperability-mode=upcoming-swift -Xcc -std=c++20
3+
4+
import StdNumeric
5+
6+
let _ = getGCD(12, 15)

0 commit comments

Comments
 (0)