2323#include " llvm/Support/FileSystem.h"
2424#include " llvm/Support/Signals.h"
2525
26+ // TODO: Move back into Clang header.
27+ #include " llvm/CAS/CachingOnDiskFileSystem.h"
28+
2629using namespace swift ;
2730
2831using namespace clang ::tooling;
2932using namespace clang ::tooling::dependencies;
3033
34+ static std::string lookupModuleOutput (const ModuleID &MID,
35+ ModuleOutputKind MOK) {
36+ // Deciding the output paths is done in swift-driver.
37+ switch (MOK) {
38+ case ModuleOutputKind::ModuleFile:
39+ return " <replace-me>" ;
40+ case ModuleOutputKind::DependencyFile:
41+ return " <replace-me>" ;
42+ case ModuleOutputKind::DependencyTargets:
43+ return MID.ModuleName + " -" + MID.ContextHash ;
44+ case ModuleOutputKind::DiagnosticSerializationFile:
45+ return " <replace-me>" ;
46+ }
47+ llvm_unreachable (" Fully covered switch above!" );
48+ }
49+
3150// Add search paths.
3251// Note: This is handled differently for the Clang importer itself, which
3352// adds search paths to Clang's data structures rather than to its
@@ -119,13 +138,6 @@ void ClangImporter::recordModuleDependencies(
119138 for (const auto &fileDep : clangModuleDep.FileDeps ) {
120139 fileDeps.push_back (fileDep.getKey ().str ());
121140 }
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));
129141
130142 std::vector<std::string> swiftArgs;
131143 // We are using Swift frontend mode.
@@ -142,22 +154,13 @@ void ClangImporter::recordModuleDependencies(
142154 addClangArg (arg);
143155 };
144156
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- }
157+ // Add args reported by the scanner.
158+ auto It = clangModuleDep.BuildArguments .begin ();
159+ auto ItEnd = clangModuleDep.BuildArguments .end ();
160+ // Skip -cc1.
161+ ++It;
159162
160- while (It != allArgs. end () ) {
163+ while (It != ItEnd ) {
161164 StringRef arg = *It;
162165 // Remove the -target arguments because we should use the target triple
163166 // specified with `-clang-target` on the scanner invocation, or
@@ -169,22 +172,11 @@ void ClangImporter::recordModuleDependencies(
169172 // specified in the interface file.
170173 It += 1 ;
171174 } else {
172- addClangArg (*It);
175+ addClangFrontendArg (*It);
173176 ++ It;
174177 }
175178 }
176179
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-
188180 // If the scanner is invoked with '-clang-target', ensure this is the target
189181 // used to build this PCM.
190182 if (Impl.SwiftContext .LangOpts .ClangTarget .hasValue ()) {
@@ -267,7 +259,7 @@ Optional<ModuleDependencies> ClangImporter::getModuleDependencies(
267259
268260 auto clangDependencies = cache.getClangScannerTool ().getFullDependencies (
269261 commandLineArgs, workingDir, cache.getAlreadySeenClangModules (),
270- moduleName);
262+ lookupModuleOutput, moduleName);
271263 if (!clangDependencies) {
272264 auto errorStr = toString (clangDependencies.takeError ());
273265 // We ignore the "module 'foo' not found" error, the Swift dependency
@@ -322,7 +314,8 @@ bool ClangImporter::addBridgingHeaderDependencies(
322314 ctx.SourceMgr .getFileSystem ()->getCurrentWorkingDirectory ().get ();
323315
324316 auto clangDependencies = cache.getClangScannerTool ().getFullDependencies (
325- commandLineArgs, workingDir, cache.getAlreadySeenClangModules ());
317+ commandLineArgs, workingDir, cache.getAlreadySeenClangModules (),
318+ lookupModuleOutput);
326319 if (!clangDependencies) {
327320 // FIXME: Route this to a normal diagnostic.
328321 llvm::logAllUnhandledErrors (clangDependencies.takeError (), llvm::errs ());
0 commit comments