@@ -29,12 +29,6 @@ using namespace clang::tooling;
29
29
using namespace clang ::tooling::dependencies;
30
30
31
31
class swift ::ClangModuleDependenciesCacheImpl {
32
- // / Cache the names of the files used for the "import hack" to compute module
33
- // / dependencies.
34
- // / FIXME: This should go away once Clang's dependency scanning library
35
- // / can scan by module name.
36
- llvm::StringMap<std::string> importHackFileCache;
37
-
38
32
public:
39
33
// / Set containing all of the Clang modules that have already been seen.
40
34
llvm::StringSet<> alreadySeen;
@@ -44,44 +38,11 @@ class swift::ClangModuleDependenciesCacheImpl {
44
38
DependencyScanningTool tool;
45
39
46
40
ClangModuleDependenciesCacheImpl ()
47
- : importHackFileCache(), service(ScanningMode::DependencyDirectivesScan,
48
- ScanningOutputFormat::Full,
49
- clang::CASOptions (), nullptr, nullptr),
41
+ : service(ScanningMode::DependencyDirectivesScan,
42
+ ScanningOutputFormat::Full, clang::CASOptions(), nullptr , nullptr ),
50
43
tool (service) {}
51
- ~ClangModuleDependenciesCacheImpl ();
52
-
53
- // / Retrieve the name of the file used for the "import hack" that is
54
- // / used to scan the dependencies of a Clang module.
55
- llvm::ErrorOr<StringRef> getImportHackFile (StringRef moduleName);
56
44
};
57
45
58
- ClangModuleDependenciesCacheImpl::~ClangModuleDependenciesCacheImpl () {
59
- if (!importHackFileCache.empty ()) {
60
- for (auto & it: importHackFileCache) {
61
- llvm::sys::fs::remove (it.second );
62
- }
63
- }
64
- }
65
-
66
- llvm::ErrorOr<StringRef> ClangModuleDependenciesCacheImpl::getImportHackFile (StringRef moduleName) {
67
- auto cacheIt = importHackFileCache.find (moduleName.str ());
68
- if (cacheIt != importHackFileCache.end ())
69
- return cacheIt->second ;
70
-
71
- // Create a temporary file.
72
- int resultFD;
73
- SmallString<128 > resultPath;
74
- if (auto error = llvm::sys::fs::createTemporaryFile (
75
- " import-hack-" + moduleName.str (), " c" , resultFD, resultPath))
76
- return error;
77
-
78
- llvm::raw_fd_ostream out (resultFD, /* shouldClose=*/ true );
79
- out << " #pragma clang module import " << moduleName.str () << " ;\n " ;
80
- llvm::sys::RemoveFileOnSignal (resultPath);
81
- importHackFileCache.insert (std::make_pair (moduleName, resultPath.str ().str ()));
82
- return importHackFileCache[moduleName];
83
- }
84
-
85
46
// Add search paths.
86
47
// Note: This is handled differently for the Clang importer itself, which
87
48
// adds search paths to Clang's data structures rather than to its
@@ -103,7 +64,7 @@ static void addSearchPathInvocationArguments(
103
64
// / Create the command line for Clang dependency scanning.
104
65
static std::vector<std::string> getClangDepScanningInvocationArguments (
105
66
ASTContext &ctx,
106
- StringRef sourceFileName) {
67
+ Optional< StringRef> sourceFileName = None ) {
107
68
std::vector<std::string> commandLineArgs;
108
69
109
70
// Form the basic command line.
@@ -116,7 +77,10 @@ static std::vector<std::string> getClangDepScanningInvocationArguments(
116
77
commandLineArgs.begin (), commandLineArgs.end (),
117
78
" <swift-imported-modules>" );
118
79
assert (sourceFilePos != commandLineArgs.end ());
119
- *sourceFilePos = sourceFileName.str ();
80
+ if (sourceFileName.hasValue ())
81
+ *sourceFilePos = sourceFileName->str ();
82
+ else
83
+ commandLineArgs.erase (sourceFilePos);
120
84
121
85
// HACK! Drop the -fmodule-format= argument and the one that
122
86
// precedes it.
@@ -140,18 +104,6 @@ static std::vector<std::string> getClangDepScanningInvocationArguments(
140
104
*syntaxOnlyPos = " -c" ;
141
105
}
142
106
143
- // HACK: Stolen from ClangScanDeps.cpp
144
- commandLineArgs.push_back (" -o" );
145
- commandLineArgs.push_back (" /dev/null" );
146
- commandLineArgs.push_back (" -M" );
147
- commandLineArgs.push_back (" -MT" );
148
- commandLineArgs.push_back (" import-hack.o" );
149
- commandLineArgs.push_back (" -Xclang" );
150
- commandLineArgs.push_back (" -Eonly" );
151
- commandLineArgs.push_back (" -Xclang" );
152
- commandLineArgs.push_back (" -sys-header-deps" );
153
- commandLineArgs.push_back (" -Wno-error" );
154
-
155
107
return commandLineArgs;
156
108
}
157
109
@@ -323,21 +275,14 @@ Optional<ModuleDependencies> ClangImporter::getModuleDependencies(
323
275
// Retrieve or create the shared state.
324
276
auto clangImpl = getOrCreateClangImpl (cache);
325
277
326
- // HACK! Replace the module import buffer name with the source file hack.
327
- auto importHackFile = clangImpl->getImportHackFile (moduleName);
328
- if (!importHackFile) {
329
- // FIXME: Emit a diagnostic here.
330
- return None;
331
- }
332
-
333
278
// Determine the command-line arguments for dependency scanning.
334
279
std::vector<std::string> commandLineArgs =
335
- getClangDepScanningInvocationArguments (ctx, *importHackFile );
280
+ getClangDepScanningInvocationArguments (ctx);
336
281
std::string workingDir =
337
282
ctx.SourceMgr .getFileSystem ()->getCurrentWorkingDirectory ().get ();
338
283
339
284
auto clangDependencies = clangImpl->tool .getFullDependencies (
340
- commandLineArgs, workingDir, clangImpl->alreadySeen );
285
+ commandLineArgs, workingDir, clangImpl->alreadySeen , moduleName );
341
286
if (!clangDependencies) {
342
287
auto errorStr = toString (clangDependencies.takeError ());
343
288
// We ignore the "module 'foo' not found" error, the Swift dependency
@@ -390,7 +335,7 @@ bool ClangImporter::addBridgingHeaderDependencies(
390
335
391
336
// Determine the command-line arguments for dependency scanning.
392
337
std::vector<std::string> commandLineArgs =
393
- getClangDepScanningInvocationArguments (ctx, bridgingHeader);
338
+ getClangDepScanningInvocationArguments (ctx, StringRef ( bridgingHeader) );
394
339
std::string workingDir =
395
340
ctx.SourceMgr .getFileSystem ()->getCurrentWorkingDirectory ().get ();
396
341
0 commit comments