Skip to content

Commit 9178511

Browse files
authored
Merge pull request #76887 from swiftlang/egorzhdan/6.0-msvc-bit-module
πŸ’[cxx-interop] Modularize __msvc_bit_utils on Windows
2 parents ad236a8 + 8dd36fe commit 9178511

File tree

5 files changed

+45
-13
lines changed

5 files changed

+45
-13
lines changed

β€Žlib/ClangImporter/ClangIncludePaths.cpp

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -414,17 +414,16 @@ GetWindowsAuxiliaryFile(StringRef modulemap, const SearchPathOptions &Options) {
414414
return "";
415415
}
416416

417-
SmallVector<std::pair<std::string, std::string>, 2> GetWindowsFileMappings(
418-
ASTContext &Context,
417+
void GetWindowsFileMappings(
418+
ClangInvocationFileMapping &fileMapping, ASTContext &Context,
419419
const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &driverVFS,
420420
bool &requiresBuiltinHeadersInSystemModules) {
421421
const llvm::Triple &Triple = Context.LangOpts.Target;
422422
const SearchPathOptions &SearchPathOpts = Context.SearchPathOpts;
423-
SmallVector<std::pair<std::string, std::string>, 2> Mappings;
424423
std::string AuxiliaryFile;
425424

426425
if (!Triple.isWindowsMSVCEnvironment())
427-
return Mappings;
426+
return;
428427

429428
clang::driver::Driver Driver = createClangDriver(Context, driverVFS);
430429
const llvm::opt::InputArgList Args = createClangArgs(Context, Driver);
@@ -450,7 +449,8 @@ SmallVector<std::pair<std::string, std::string>, 2> GetWindowsFileMappings(
450449

451450
AuxiliaryFile = GetWindowsAuxiliaryFile("winsdk.modulemap", SearchPathOpts);
452451
if (!AuxiliaryFile.empty())
453-
Mappings.emplace_back(std::string(WinSDKInjection), AuxiliaryFile);
452+
fileMapping.redirectedFiles.emplace_back(std::string(WinSDKInjection),
453+
AuxiliaryFile);
454454
}
455455

456456
struct {
@@ -477,7 +477,8 @@ SmallVector<std::pair<std::string, std::string>, 2> GetWindowsFileMappings(
477477
// cycle goes away. Note that -fbuiltin-headers-in-system-modules does
478478
// nothing to fix the same problem with C++ headers, and is generally
479479
// fragile.
480-
Mappings.emplace_back(std::string(UCRTInjection), AuxiliaryFile);
480+
fileMapping.redirectedFiles.emplace_back(std::string(UCRTInjection),
481+
AuxiliaryFile);
481482
requiresBuiltinHeadersInSystemModules = true;
482483
}
483484
}
@@ -503,17 +504,27 @@ SmallVector<std::pair<std::string, std::string>, 2> GetWindowsFileMappings(
503504
AuxiliaryFile =
504505
GetWindowsAuxiliaryFile("vcruntime.modulemap", SearchPathOpts);
505506
if (!AuxiliaryFile.empty())
506-
Mappings.emplace_back(std::string(VCToolsInjection), AuxiliaryFile);
507+
fileMapping.redirectedFiles.emplace_back(std::string(VCToolsInjection),
508+
AuxiliaryFile);
507509

508510
llvm::sys::path::remove_filename(VCToolsInjection);
509511
llvm::sys::path::append(VCToolsInjection, "vcruntime.apinotes");
510512
AuxiliaryFile =
511513
GetWindowsAuxiliaryFile("vcruntime.apinotes", SearchPathOpts);
512514
if (!AuxiliaryFile.empty())
513-
Mappings.emplace_back(std::string(VCToolsInjection), AuxiliaryFile);
514-
}
515+
fileMapping.redirectedFiles.emplace_back(std::string(VCToolsInjection),
516+
AuxiliaryFile);
515517

516-
return Mappings;
518+
// __msvc_bit_utils.hpp was added in a recent VS 2022 version. It has to be
519+
// referenced from the modulemap directly to avoid modularization errors.
520+
// Older VS versions might not have it. Let's inject an empty header file if
521+
// it isn't available.
522+
llvm::sys::path::remove_filename(VCToolsInjection);
523+
llvm::sys::path::append(VCToolsInjection, "__msvc_bit_utils.hpp");
524+
if (!llvm::sys::fs::exists(VCToolsInjection))
525+
fileMapping.overridenFiles.emplace_back(std::string(VCToolsInjection),
526+
"");
527+
}
517528
}
518529
} // namespace
519530

@@ -573,8 +584,7 @@ ClangInvocationFileMapping swift::getClangInvocationFileMapping(
573584
if (ctx.LangOpts.EnableCXXInterop)
574585
getLibStdCxxFileMapping(result, ctx, vfs);
575586

576-
result.redirectedFiles.append(GetWindowsFileMappings(
577-
ctx, vfs, result.requiresBuiltinHeadersInSystemModules));
578-
587+
GetWindowsFileMappings(result, ctx, vfs,
588+
result.requiresBuiltinHeadersInSystemModules);
579589
return result;
580590
}

β€Ž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 xhash {
706711
header "xhash"
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)