Skip to content

Commit 7ed9a52

Browse files
[PrefixMap] Remap clang cc1 args when building swift interfaces
Fix a bug that swift clang importer is not setup correctly when prefix map is used. There are two separate issues: * CC1 args used to setup clang import when building swift (interface and sources) are not correctly remapped. * When loading SDKInfo from SDK path, the SDKSettings.json is not loading from VFS, thus the file cannot be loaded from remapped path. rdar://134458611
1 parent 4921eaf commit 7ed9a52

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

lib/AST/ASTContext.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -6555,9 +6555,8 @@ bool ASTContext::isASCIIString(StringRef s) const {
65556555

65566556
clang::DarwinSDKInfo *ASTContext::getDarwinSDKInfo() const {
65576557
if (!getImpl().SDKInfo) {
6558-
auto SDKInfoOrErr = clang::parseDarwinSDKInfo(
6559-
*llvm::vfs::getRealFileSystem(),
6560-
SearchPathOpts.SDKPath);
6558+
auto SDKInfoOrErr = clang::parseDarwinSDKInfo(*SourceMgr.getFileSystem(),
6559+
SearchPathOpts.SDKPath);
65616560
if (!SDKInfoOrErr) {
65626561
llvm::handleAllErrors(SDKInfoOrErr.takeError(),
65636562
[](const llvm::ErrorInfoBase &) {

lib/ClangImporter/ClangImporter.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
#include "llvm/Support/FileSystem.h"
9696
#include "llvm/Support/Memory.h"
9797
#include "llvm/Support/Path.h"
98+
#include "llvm/Support/PrefixMapper.h"
9899
#include "llvm/Support/VirtualFileSystem.h"
99100
#include "llvm/Support/VirtualOutputBackend.h"
100101
#include "llvm/TextAPI/InterfaceFile.h"
@@ -4141,6 +4142,16 @@ ClangImporter::getSwiftExplicitModuleDirectCC1Args() const {
41414142
FSOpts.WorkingDir.clear();
41424143
}
41434144

4145+
if (!Impl.SwiftContext.SearchPathOpts.ScannerPrefixMapper.empty()) {
4146+
// Remap all the paths if requested.
4147+
llvm::PrefixMapper Mapper;
4148+
clang::tooling::dependencies::DepscanPrefixMapping::configurePrefixMapper(
4149+
Impl.SwiftContext.SearchPathOpts.ScannerPrefixMapper, Mapper);
4150+
clang::tooling::dependencies::DepscanPrefixMapping::remapInvocationPaths(
4151+
instance, Mapper);
4152+
instance.getFrontendOpts().PathPrefixMappings.clear();
4153+
}
4154+
41444155
return instance.getCC1CommandLine();
41454156
}
41464157

lib/Frontend/ModuleInterfaceLoader.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -1787,6 +1787,9 @@ void InterfaceSubContextDelegateImpl::inheritOptionsForBuildingInterface(
17871787
genericSubInvocation.getClangImporterOptions().ClangImporterDirectCC1Scan =
17881788
clangImporterOpts.ClangImporterDirectCC1Scan;
17891789

1790+
genericSubInvocation.getSearchPathOptions().ScannerPrefixMapper =
1791+
SearchPathOpts.ScannerPrefixMapper;
1792+
17901793
// Validate Clang modules once per-build session flags must be consistent
17911794
// across all module sub-invocations
17921795
if (clangImporterOpts.ValidateModulesOnce) {

test/CAS/path_remap.swift

+17-5
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
// RUN: %target-swift-frontend -scan-dependencies -module-name Test -module-cache-path %t/clang-module-cache -O \
55
// RUN: -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import -parse-stdlib \
6-
// RUN: %t/main.swift -o %t/deps.json -swift-version 5 -cache-compile-job -cas-path %t/cas -I %t/include \
7-
// RUN: -scanner-prefix-map %swift_src_root=/^src -scanner-prefix-map %t=/^tmp -enable-cross-import-overlays
6+
// RUN: %t/main.swift -o %t/deps.json -swift-version 5 -cache-compile-job -cas-path %t/cas -I %t/include -sdk %t/sdk \
7+
// RUN: -scanner-prefix-map %swift_src_root=/^src -scanner-prefix-map %t=/^tmp -scanner-prefix-map %t/sdk=/^sdk -enable-cross-import-overlays
88

99
// RUN: %{python} %S/Inputs/BuildCommandExtractor.py %t/deps.json A > %t/A.cmd
1010
// RUN: %swift_frontend_plain @%t/A.cmd
@@ -13,16 +13,26 @@
1313
// RUN: %{python} %S/Inputs/BuildCommandExtractor.py %t/deps.json _B_A > %t/BA.cmd
1414
// RUN: %swift_frontend_plain @%t/BA.cmd
1515

16+
// RUN: %FileCheck %s --check-prefix=SDK-REMAP --input-file=%t/A.cmd
17+
// RUN: %FileCheck %s --check-prefix=SDK-REMAP --input-file=%t/B.cmd
18+
// RUN: %FileCheck %s --check-prefix=SDK-REMAP --input-file=%t/BA.cmd
19+
20+
// SDK-REMAP: -isysroot
21+
// SDK-REMAP-NEXT: -Xcc
22+
// SDK-REMAP-NEXT: /^sdk
23+
1624
// RUN: %{python} %S/Inputs/GenerateExplicitModuleMap.py %t/deps.json > %t/map.json
1725
// RUN: llvm-cas --cas %t/cas --make-blob --data %t/map.json > %t/map.casid
1826
// RUN: %{python} %S/Inputs/BuildCommandExtractor.py %t/deps.json Test > %t/MyApp.cmd
1927

28+
// RUN: %FileCheck %s --check-prefix=SDK-REMAP --input-file=%t/MyApp.cmd
29+
2030
// RUN: %target-swift-frontend \
2131
// RUN: -c -o %t/main.o -cache-compile-job -cas-path %t/cas \
2232
// RUN: -swift-version 5 -disable-implicit-swift-modules \
2333
// RUN: -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import -parse-stdlib \
2434
// RUN: -module-name Test -explicit-swift-module-map-file @%t/map.casid \
25-
// RUN: -cache-replay-prefix-map /^src=%swift_src_root -cache-replay-prefix-map /^tmp=%t \
35+
// RUN: -cache-replay-prefix-map /^src=%swift_src_root -cache-replay-prefix-map /^tmp=%t -cache-replay-prefix-map /^sdk=%t/sdk \
2636
// RUN: /^tmp/main.swift @%t/MyApp.cmd -enable-cross-import-overlays
2737

2838
// RUN: %swift-scan-test -action compute_cache_key_from_index -cas-path %t/cas -input 0 -- \
@@ -31,7 +41,7 @@
3141
// RUN: -swift-version 5 -disable-implicit-swift-modules \
3242
// RUN: -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import -parse-stdlib \
3343
// RUN: -module-name Test -explicit-swift-module-map-file @%t/map.casid \
34-
// RUN: -cache-replay-prefix-map /^src=%swift_src_root -cache-replay-prefix-map /^tmp=%t \
44+
// RUN: -cache-replay-prefix-map /^src=%swift_src_root -cache-replay-prefix-map /^tmp=%t -cache-replay-prefix-map /^sdk=%t/sdk \
3545
// RUN: /^tmp/main.swift @%t/MyApp.cmd -enable-cross-import-overlays > %t/key.casid
3646

3747
// RUN: %swift-scan-test -action replay_result -cas-path %t/cas -id @%t/key.casid -- \
@@ -40,7 +50,7 @@
4050
// RUN: -swift-version 5 -disable-implicit-swift-modules \
4151
// RUN: -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import -parse-stdlib \
4252
// RUN: -module-name Test -explicit-swift-module-map-file @%t/map.casid \
43-
// RUN: -cache-replay-prefix-map /^src=%swift_src_root -cache-replay-prefix-map /^tmp=%t \
53+
// RUN: -cache-replay-prefix-map /^src=%swift_src_root -cache-replay-prefix-map /^tmp=%t -cache-replay-prefix-map /^sdk=%t/sdk \
4454
// RUN: /^tmp/main.swift @%t/MyApp.cmd -enable-cross-import-overlays
4555

4656
//--- main.swift
@@ -70,3 +80,5 @@ public func b_a() { }
7080
version: 1
7181
modules:
7282
- name: _B_A
83+
//--- sdk/SDKSettings.json
84+
{}

0 commit comments

Comments
 (0)