-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathClangModuleDependencyScanner.cpp
390 lines (333 loc) · 13.9 KB
/
ClangModuleDependencyScanner.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
//===--- ClangModuleDependencyScanner.cpp - Dependency Scanning -----------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file implements dependency scanning for Clang modules.
//
//===----------------------------------------------------------------------===//
#include "ImporterImpl.h"
#include "swift/AST/ModuleDependencies.h"
#include "swift/Basic/SourceManager.h"
#include "swift/ClangImporter/ClangImporter.h"
#include "clang/Tooling/DependencyScanning/DependencyScanningService.h"
#include "clang/Tooling/DependencyScanning/DependencyScanningTool.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Signals.h"
using namespace swift;
using namespace clang::tooling;
using namespace clang::tooling::dependencies;
class swift::ClangModuleDependenciesCacheImpl {
/// Cache the names of the files used for the "import hack" to compute module
/// dependencies.
/// FIXME: This should go away once Clang's dependency scanning library
/// can scan by module name.
llvm::StringMap<std::string> importHackFileCache;
public:
/// Set containing all of the Clang modules that have already been seen.
llvm::StringSet<> alreadySeen;
DependencyScanningService service;
DependencyScanningTool tool;
ClangModuleDependenciesCacheImpl()
: importHackFileCache(),
service(ScanningMode::MinimizedSourcePreprocessing, ScanningOutputFormat::Full),
tool(service) { }
~ClangModuleDependenciesCacheImpl();
/// Retrieve the name of the file used for the "import hack" that is
/// used to scan the dependencies of a Clang module.
llvm::ErrorOr<StringRef> getImportHackFile(StringRef moduleName);
};
ClangModuleDependenciesCacheImpl::~ClangModuleDependenciesCacheImpl() {
if (!importHackFileCache.empty()) {
for (auto& it: importHackFileCache) {
llvm::sys::fs::remove(it.second);
}
}
}
llvm::ErrorOr<StringRef> ClangModuleDependenciesCacheImpl::getImportHackFile(StringRef moduleName) {
auto cacheIt = importHackFileCache.find(moduleName.str());
if (cacheIt != importHackFileCache.end())
return cacheIt->second;
// Create a temporary file.
int resultFD;
SmallString<128> resultPath;
if (auto error = llvm::sys::fs::createTemporaryFile(
"import-hack-" + moduleName.str(), "c", resultFD, resultPath))
return error;
llvm::raw_fd_ostream out(resultFD, /*shouldClose=*/true);
out << "#pragma clang module import " << moduleName.str() << ";\n";
llvm::sys::RemoveFileOnSignal(resultPath);
importHackFileCache.insert(std::make_pair(moduleName, resultPath.str().str()));
return importHackFileCache[moduleName];
}
namespace {
class SingleCommandCompilationDatabase : public CompilationDatabase {
public:
SingleCommandCompilationDatabase(CompileCommand Cmd)
: Command(std::move(Cmd)) {}
virtual std::vector<CompileCommand>
getCompileCommands(StringRef FilePath) const override {
return {Command};
}
virtual std::vector<CompileCommand> getAllCompileCommands() const override {
return {Command};
}
private:
CompileCommand Command;
};
}
// Add search paths.
// Note: This is handled differently for the Clang importer itself, which
// adds search paths to Clang's data structures rather than to its
// command line.
static void addSearchPathInvocationArguments(
std::vector<std::string> &invocationArgStrs, ASTContext &ctx) {
SearchPathOptions &searchPathOpts = ctx.SearchPathOpts;
for (const auto &framepath : searchPathOpts.FrameworkSearchPaths) {
invocationArgStrs.push_back(framepath.IsSystem ? "-iframework" : "-F");
invocationArgStrs.push_back(framepath.Path);
}
for (auto path : searchPathOpts.ImportSearchPaths) {
invocationArgStrs.push_back("-I");
invocationArgStrs.push_back(path);
}
}
/// Create the command line for Clang dependency scanning.
static std::vector<std::string> getClangDepScanningInvocationArguments(
ASTContext &ctx,
StringRef sourceFileName) {
std::vector<std::string> commandLineArgs;
// Form the basic command line.
commandLineArgs.push_back("clang");
importer::getNormalInvocationArguments(commandLineArgs, ctx);
importer::addCommonInvocationArguments(commandLineArgs, ctx);
addSearchPathInvocationArguments(commandLineArgs, ctx);
auto sourceFilePos = std::find(
commandLineArgs.begin(), commandLineArgs.end(),
"<swift-imported-modules>");
assert(sourceFilePos != commandLineArgs.end());
*sourceFilePos = sourceFileName.str();
// HACK! Drop the -fmodule-format= argument and the one that
// precedes it.
{
auto moduleFormatPos = std::find_if(commandLineArgs.begin(),
commandLineArgs.end(),
[](StringRef arg) {
return arg.startswith("-fmodule-format=");
});
assert(moduleFormatPos != commandLineArgs.end());
assert(moduleFormatPos != commandLineArgs.begin());
commandLineArgs.erase(moduleFormatPos-1, moduleFormatPos+1);
}
// HACK: No -fsyntax-only here?
{
auto syntaxOnlyPos = std::find(commandLineArgs.begin(),
commandLineArgs.end(),
"-fsyntax-only");
assert(syntaxOnlyPos != commandLineArgs.end());
*syntaxOnlyPos = "-c";
}
// HACK: Stolen from ClangScanDeps.cpp
commandLineArgs.push_back("-o");
commandLineArgs.push_back("/dev/null");
commandLineArgs.push_back("-M");
commandLineArgs.push_back("-MT");
commandLineArgs.push_back("import-hack.o");
commandLineArgs.push_back("-Xclang");
commandLineArgs.push_back("-Eonly");
commandLineArgs.push_back("-Xclang");
commandLineArgs.push_back("-sys-header-deps");
commandLineArgs.push_back("-Wno-error");
return commandLineArgs;
}
/// Get or create the Clang-specific
static ClangModuleDependenciesCacheImpl *getOrCreateClangImpl(
ModuleDependenciesCache &cache) {
auto clangImpl = cache.getClangImpl();
if (!clangImpl) {
clangImpl = new ClangModuleDependenciesCacheImpl();
cache.setClangImpl(clangImpl,
[](ClangModuleDependenciesCacheImpl *ptr) {
delete ptr;
});
}
return clangImpl;
}
/// Record the module dependencies we found by scanning Clang modules into
/// the module dependencies cache.
void ClangImporter::recordModuleDependencies(
ModuleDependenciesCache &cache,
const FullDependenciesResult &clangDependencies) {
struct ModuleInfo {
std::string PCMPath;
std::string ModuleMapPath;
};
for (const auto &clangModuleDep : clangDependencies.DiscoveredModules) {
// If we've already cached this information, we're done.
if (cache.hasDependencies(clangModuleDep.ModuleName,
ModuleDependenciesKind::Clang))
continue;
// File dependencies for this module.
std::vector<std::string> fileDeps;
for (const auto &fileDep : clangModuleDep.FileDeps) {
fileDeps.push_back(fileDep.getKey().str());
}
// Inherit all Clang driver args when creating the clang importer.
ArrayRef<std::string> allArgs = Impl.ClangArgs;
ClangImporterOptions Opts;
// Ensure the arguments we collected is sufficient to create a Clang
// invocation.
assert(createClangInvocation(this, Opts, allArgs));
std::vector<std::string> swiftArgs;
// We are using Swift frontend mode.
swiftArgs.push_back("-frontend");
// We pass the entire argument list via -Xcc, so the invocation should
// use extra clang options alone.
swiftArgs.push_back("-only-use-extra-clang-opts");
auto addClangArg = [&](StringRef arg) {
swiftArgs.push_back("-Xcc");
swiftArgs.push_back(arg.str());
};
// Add all args inheritted from creating the importer.
auto It = allArgs.begin();
while(It != allArgs.end()) {
StringRef arg = *It;
// Remove the -target arguments because we should use the target triple
// from the depending Swift modules.
if (arg == "-target") {
It += 2;
} else if (arg.startswith("-fapinotes-swift-version=")) {
// Remove the apinotes version because we should use the language version
// specified in the interface file.
It += 1;
} else {
addClangArg(*It);
++ It;
}
}
// Add all args the non-path arguments required to be passed in, according
// to the Clang scanner
for (const auto &clangArg : clangModuleDep.NonPathCommandLine) {
swiftArgs.push_back("-Xcc");
swiftArgs.push_back("-Xclang");
swiftArgs.push_back("-Xcc");
swiftArgs.push_back(clangArg);
}
// Swift frontend action: -emit-pcm
swiftArgs.push_back("-emit-pcm");
swiftArgs.push_back("-module-name");
swiftArgs.push_back(clangModuleDep.ModuleName);
// Pass down search paths to the -emit-module action.
// Unlike building Swift modules, we need to include all search paths to
// the clang invocation to build PCMs because transitive headers can only
// be found via search paths. Passing these headers as explicit inputs can
// be quite challenging.
for (auto &path: Impl.SwiftContext.SearchPathOpts.ImportSearchPaths) {
addClangArg("-I" + path);
}
for (auto &path: Impl.SwiftContext.SearchPathOpts.FrameworkSearchPaths) {
addClangArg((path.IsSystem ? "-Fsystem": "-F") + path.Path);
}
// Swift frontend option for input file path (Foo.modulemap).
swiftArgs.push_back(clangModuleDep.ClangModuleMapFile);
// Module-level dependencies.
llvm::StringSet<> alreadyAddedModules;
auto dependencies = ModuleDependencies::forClangModule(
clangModuleDep.ClangModuleMapFile,
clangModuleDep.ContextHash,
swiftArgs,
fileDeps);
for (const auto &moduleName : clangModuleDep.ClangModuleDeps) {
dependencies.addModuleDependency(moduleName.ModuleName, &alreadyAddedModules);
}
cache.recordDependencies(clangModuleDep.ModuleName,
std::move(dependencies));
}
}
Optional<ModuleDependencies> ClangImporter::getModuleDependencies(
StringRef moduleName, ModuleDependenciesCache &cache,
InterfaceSubContextDelegate &delegate) {
// Check whether there is already a cached result.
if (auto found = cache.findDependencies(
moduleName, ModuleDependenciesKind::Clang))
return found;
// Retrieve or create the shared state.
auto clangImpl = getOrCreateClangImpl(cache);
// HACK! Replace the module import buffer name with the source file hack.
auto importHackFile = clangImpl->getImportHackFile(moduleName);
if (!importHackFile) {
// FIXME: Emit a diagnostic here.
return None;
}
// Determine the command-line arguments for dependency scanning.
auto &ctx = Impl.SwiftContext;
std::vector<std::string> commandLineArgs =
getClangDepScanningInvocationArguments(ctx, *importHackFile);
std::string workingDir =
ctx.SourceMgr.getFileSystem()->getCurrentWorkingDirectory().get();
CompileCommand command(workingDir, *importHackFile, commandLineArgs, "-");
SingleCommandCompilationDatabase database(command);
auto clangDependencies = clangImpl->tool.getFullDependencies(
database, workingDir, clangImpl->alreadySeen);
if (!clangDependencies) {
// FIXME: Route this to a normal diagnostic.
llvm::logAllUnhandledErrors(clangDependencies.takeError(), llvm::errs());
return None;
}
// Record module dependencies for each module we found.
recordModuleDependencies(cache, *clangDependencies);
return cache.findDependencies(moduleName, ModuleDependenciesKind::Clang);
}
bool ClangImporter::addBridgingHeaderDependencies(
StringRef moduleName,
ModuleDependenciesCache &cache) {
auto targetModule = *cache.findDependencies(
moduleName, ModuleDependenciesKind::SwiftTextual);
// If we've already recorded bridging header dependencies, we're done.
auto swiftDeps = targetModule.getAsSwiftTextualModule();
if (!swiftDeps->bridgingSourceFiles.empty() ||
!swiftDeps->bridgingModuleDependencies.empty())
return false;
// Retrieve or create the shared state.
auto clangImpl = getOrCreateClangImpl(cache);
// Retrieve the bridging header.
std::string bridgingHeader = *targetModule.getBridgingHeader();
// Determine the command-line arguments for dependency scanning.
auto &ctx = Impl.SwiftContext;
std::vector<std::string> commandLineArgs =
getClangDepScanningInvocationArguments(ctx, bridgingHeader);
std::string workingDir =
ctx.SourceMgr.getFileSystem()->getCurrentWorkingDirectory().get();
CompileCommand command(workingDir, bridgingHeader, commandLineArgs, "-");
SingleCommandCompilationDatabase database(command);
auto clangDependencies = clangImpl->tool.getFullDependencies(
database, workingDir, clangImpl->alreadySeen);
if (!clangDependencies) {
// FIXME: Route this to a normal diagnostic.
llvm::logAllUnhandledErrors(clangDependencies.takeError(), llvm::errs());
return true;
}
// Record module dependencies for each module we found.
recordModuleDependencies(cache, *clangDependencies);
// Record dependencies for the source files the bridging header includes.
for (const auto &fileDep : clangDependencies->FullDeps.FileDeps)
targetModule.addBridgingSourceFile(fileDep);
// ... and all module dependencies.
llvm::StringSet<> alreadyAddedModules;
for (const auto &moduleDep : clangDependencies->FullDeps.ClangModuleDeps) {
targetModule.addBridgingModuleDependency(
moduleDep.ModuleName, alreadyAddedModules);
}
// Update the cache with the new information for the module.
cache.updateDependencies(
{moduleName.str(), ModuleDependenciesKind::SwiftTextual},
std::move(targetModule));
return false;
}