Skip to content

Commit af276b7

Browse files
authored
Merge pull request #60882 from apple/jan_svoboda/cherry-pick
WIP: [importer] Adjust for new dependency scanner API
2 parents 4dec193 + c5bd61b commit af276b7

File tree

2 files changed

+33
-72
lines changed

2 files changed

+33
-72
lines changed

lib/ClangImporter/ClangModuleDependencyScanner.cpp

Lines changed: 33 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@ using namespace swift;
2828
using namespace clang::tooling;
2929
using namespace clang::tooling::dependencies;
3030

31+
static std::string lookupModuleOutput(const ModuleID &MID,
32+
ModuleOutputKind MOK) {
33+
// Deciding the output paths is done in swift-driver.
34+
switch (MOK) {
35+
case ModuleOutputKind::ModuleFile:
36+
return "<replace-me>";
37+
case ModuleOutputKind::DependencyFile:
38+
return "<replace-me>";
39+
case ModuleOutputKind::DependencyTargets:
40+
return MID.ModuleName + "-" + MID.ContextHash;
41+
case ModuleOutputKind::DiagnosticSerializationFile:
42+
return "<replace-me>";
43+
}
44+
llvm_unreachable("Fully covered switch above!");
45+
}
46+
3147
// Add search paths.
3248
// Note: This is handled differently for the Clang importer itself, which
3349
// adds search paths to Clang's data structures rather than to its
@@ -119,85 +135,34 @@ void ClangImporter::recordModuleDependencies(
119135
for (const auto &fileDep : clangModuleDep.FileDeps) {
120136
fileDeps.push_back(fileDep.getKey().str());
121137
}
122-
// Inherit all Clang driver args when creating the clang importer.
123-
ArrayRef<std::string> allArgs = Impl.ClangArgs;
124-
ClangImporterOptions Opts;
125-
126-
// Ensure the arguments we collected is sufficient to create a Clang
127-
// invocation.
128-
assert(createClangInvocation(this, Opts, nullptr, allArgs));
129138

130139
std::vector<std::string> swiftArgs;
131-
// We are using Swift frontend mode.
132-
swiftArgs.push_back("-frontend");
133-
// We pass the entire argument list via -Xcc, so the invocation should
134-
// use extra clang options alone.
135-
swiftArgs.push_back("-only-use-extra-clang-opts");
136140
auto addClangArg = [&](Twine arg) {
137141
swiftArgs.push_back("-Xcc");
138142
swiftArgs.push_back(arg.str());
139143
};
140-
auto addClangFrontendArg = [&](Twine arg) {
141-
addClangArg("-Xclang");
142-
addClangArg(arg);
143-
};
144144

145-
// Add all args inherited from creating the importer.
146-
auto It = allArgs.begin();
147-
148-
{
149-
StringRef arg = *It;
150-
if (arg == "clang" ||
151-
arg.endswith(llvm::sys::path::get_separator().str() + "clang")) {
152-
// Remove the initial path to clang executable argument, to avoid
153-
// treating it as an executable input to compilation. It is not needed
154-
// because the consumer of this command-line will invoke the emit-PCM
155-
// action via swift-frontend.
156-
It += 1;
157-
}
158-
}
159-
160-
while(It != allArgs.end()) {
161-
StringRef arg = *It;
162-
// Remove the -target arguments because we should use the target triple
163-
// specified with `-clang-target` on the scanner invocation, or
164-
// from the depending Swift modules.
165-
if (arg == "-target") {
166-
It += 2;
167-
} else if (arg.startswith("-fapinotes-swift-version=")) {
168-
// Remove the apinotes version because we should use the language version
169-
// specified in the interface file.
170-
It += 1;
171-
} else {
172-
addClangArg(*It);
173-
++ It;
174-
}
175-
}
145+
// We are using Swift frontend mode.
146+
swiftArgs.push_back("-frontend");
176147

177-
// Add the equivalent of the old `getAdditionalArgsWithoutModulePaths`.
178-
// TODO: Should we be passing all cc1 args (ie.
179-
// `getCanonicalCommandLineWithoutModulePaths`)?
180-
addClangFrontendArg("-fno-implicit-modules");
181-
addClangFrontendArg("-emit-module");
182-
addClangFrontendArg(Twine("-fmodule-name=") + clangModuleDep.ID.ModuleName);
183-
if (clangModuleDep.IsSystem)
184-
addClangFrontendArg("-fsystem-module");
185-
if (clangModuleDep.BuildInvocation.getLangOpts()->NeededByPCHOrCompilationUsesPCH)
186-
addClangFrontendArg("-fmodule-related-to-pch");
187-
188-
// If the scanner is invoked with '-clang-target', ensure this is the target
189-
// used to build this PCM.
190-
if (Impl.SwiftContext.LangOpts.ClangTarget.hasValue()) {
191-
llvm::Triple triple = Impl.SwiftContext.LangOpts.ClangTarget.getValue();
192-
addClangArg("-target");
193-
addClangArg(triple.str());
194-
}
148+
// We pass the entire argument list via -Xcc, so the invocation should
149+
// use extra clang options alone.
150+
swiftArgs.push_back("-only-use-extra-clang-opts");
195151

196152
// Swift frontend action: -emit-pcm
197153
swiftArgs.push_back("-emit-pcm");
198154
swiftArgs.push_back("-module-name");
199155
swiftArgs.push_back(clangModuleDep.ID.ModuleName);
200156

157+
// Ensure that the resulting PCM build invocation uses Clang frontend directly
158+
swiftArgs.push_back("-direct-clang-cc1-module-build");
159+
160+
// Swift frontend option for input file path (Foo.modulemap).
161+
swiftArgs.push_back(clangModuleDep.ClangModuleMapFile);
162+
163+
// Add args reported by the scanner.
164+
llvm::for_each(clangModuleDep.BuildArguments, addClangArg);
165+
201166
// Pass down search paths to the -emit-module action.
202167
// Unlike building Swift modules, we need to include all search paths to
203168
// the clang invocation to build PCMs because transitive headers can only
@@ -212,8 +177,6 @@ void ClangImporter::recordModuleDependencies(
212177
addClangArg((path.IsSystem ? "-Fsystem": "-F") + path.Path);
213178
}
214179

215-
// Swift frontend option for input file path (Foo.modulemap).
216-
swiftArgs.push_back(clangModuleDep.ClangModuleMapFile);
217180
// Module-level dependencies.
218181
llvm::StringSet<> alreadyAddedModules;
219182
auto dependencies = ModuleDependencies::forClangModule(
@@ -267,7 +230,7 @@ Optional<ModuleDependencies> ClangImporter::getModuleDependencies(
267230

268231
auto clangDependencies = cache.getClangScannerTool().getFullDependencies(
269232
commandLineArgs, workingDir, cache.getAlreadySeenClangModules(),
270-
moduleName);
233+
lookupModuleOutput, moduleName);
271234
if (!clangDependencies) {
272235
auto errorStr = toString(clangDependencies.takeError());
273236
// We ignore the "module 'foo' not found" error, the Swift dependency
@@ -322,7 +285,8 @@ bool ClangImporter::addBridgingHeaderDependencies(
322285
ctx.SourceMgr.getFileSystem()->getCurrentWorkingDirectory().get();
323286

324287
auto clangDependencies = cache.getClangScannerTool().getFullDependencies(
325-
commandLineArgs, workingDir, cache.getAlreadySeenClangModules());
288+
commandLineArgs, workingDir, cache.getAlreadySeenClangModules(),
289+
lookupModuleOutput);
326290
if (!clangDependencies) {
327291
// FIXME: Route this to a normal diagnostic.
328292
llvm::logAllUnhandledErrors(clangDependencies.takeError(), llvm::errs());

test/ScanDependencies/module_deps_cache_reuse.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,6 @@ import SubE
114114
// CHECK-NEXT: "-frontend"
115115
// CHECK-NEXT: "-only-use-extra-clang-opts
116116
// CHECK-NOT: "BUILD_DIR/bin/clang"
117-
// CHECK: "-Xcc"
118-
// CHECK-NEXT: "-fsyntax-only",
119-
// CHECK: "-fsystem-module",
120117
// CHECK: "-emit-pcm",
121118
// CHECK: "-module-name",
122119
// CHECK-NEXT: "C"

0 commit comments

Comments
 (0)