@@ -28,6 +28,22 @@ using namespace swift;
28
28
using namespace clang ::tooling;
29
29
using namespace clang ::tooling::dependencies;
30
30
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
+
31
47
// Add search paths.
32
48
// Note: This is handled differently for the Clang importer itself, which
33
49
// adds search paths to Clang's data structures rather than to its
@@ -119,85 +135,34 @@ void ClangImporter::recordModuleDependencies(
119
135
for (const auto &fileDep : clangModuleDep.FileDeps ) {
120
136
fileDeps.push_back (fileDep.getKey ().str ());
121
137
}
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));
129
138
130
139
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" );
136
140
auto addClangArg = [&](Twine arg) {
137
141
swiftArgs.push_back (" -Xcc" );
138
142
swiftArgs.push_back (arg.str ());
139
143
};
140
- auto addClangFrontendArg = [&](Twine arg) {
141
- addClangArg (" -Xclang" );
142
- addClangArg (arg);
143
- };
144
144
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" );
176
147
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" );
195
151
196
152
// Swift frontend action: -emit-pcm
197
153
swiftArgs.push_back (" -emit-pcm" );
198
154
swiftArgs.push_back (" -module-name" );
199
155
swiftArgs.push_back (clangModuleDep.ID .ModuleName );
200
156
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
+
201
166
// Pass down search paths to the -emit-module action.
202
167
// Unlike building Swift modules, we need to include all search paths to
203
168
// the clang invocation to build PCMs because transitive headers can only
@@ -212,8 +177,6 @@ void ClangImporter::recordModuleDependencies(
212
177
addClangArg ((path.IsSystem ? " -Fsystem" : " -F" ) + path.Path );
213
178
}
214
179
215
- // Swift frontend option for input file path (Foo.modulemap).
216
- swiftArgs.push_back (clangModuleDep.ClangModuleMapFile );
217
180
// Module-level dependencies.
218
181
llvm::StringSet<> alreadyAddedModules;
219
182
auto dependencies = ModuleDependencies::forClangModule (
@@ -267,7 +230,7 @@ Optional<ModuleDependencies> ClangImporter::getModuleDependencies(
267
230
268
231
auto clangDependencies = cache.getClangScannerTool ().getFullDependencies (
269
232
commandLineArgs, workingDir, cache.getAlreadySeenClangModules (),
270
- moduleName);
233
+ lookupModuleOutput, moduleName);
271
234
if (!clangDependencies) {
272
235
auto errorStr = toString (clangDependencies.takeError ());
273
236
// We ignore the "module 'foo' not found" error, the Swift dependency
@@ -322,7 +285,8 @@ bool ClangImporter::addBridgingHeaderDependencies(
322
285
ctx.SourceMgr .getFileSystem ()->getCurrentWorkingDirectory ().get ();
323
286
324
287
auto clangDependencies = cache.getClangScannerTool ().getFullDependencies (
325
- commandLineArgs, workingDir, cache.getAlreadySeenClangModules ());
288
+ commandLineArgs, workingDir, cache.getAlreadySeenClangModules (),
289
+ lookupModuleOutput);
326
290
if (!clangDependencies) {
327
291
// FIXME: Route this to a normal diagnostic.
328
292
llvm::logAllUnhandledErrors (clangDependencies.takeError (), llvm::errs ());
0 commit comments