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