Skip to content

Commit e9787c7

Browse files
committed
[Dependency Scanner] Use Clang scanner's module name query API instead of creating a dummy file
1 parent 445cbd7 commit e9787c7

File tree

1 file changed

+9
-64
lines changed

1 file changed

+9
-64
lines changed

lib/ClangImporter/ClangModuleDependencyScanner.cpp

Lines changed: 9 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,6 @@ using namespace clang::tooling;
2828
using namespace clang::tooling::dependencies;
2929

3030
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-
3731
public:
3832
/// Set containing all of the Clang modules that have already been seen.
3933
llvm::StringSet<> alreadySeen;
@@ -43,44 +37,11 @@ class swift::ClangModuleDependenciesCacheImpl {
4337
DependencyScanningTool tool;
4438

4539
ClangModuleDependenciesCacheImpl()
46-
: importHackFileCache(),
47-
service(ScanningMode::DependencyDirectivesScan,
40+
: service(ScanningMode::DependencyDirectivesScan,
4841
ScanningOutputFormat::Full, clang::CASOptions(), nullptr),
4942
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);
5543
};
5644

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-
8445
// Add search paths.
8546
// Note: This is handled differently for the Clang importer itself, which
8647
// adds search paths to Clang's data structures rather than to its
@@ -102,7 +63,7 @@ static void addSearchPathInvocationArguments(
10263
/// Create the command line for Clang dependency scanning.
10364
static std::vector<std::string> getClangDepScanningInvocationArguments(
10465
ASTContext &ctx,
105-
StringRef sourceFileName) {
66+
Optional<StringRef> sourceFileName = None) {
10667
std::vector<std::string> commandLineArgs;
10768

10869
// Form the basic command line.
@@ -115,7 +76,10 @@ static std::vector<std::string> getClangDepScanningInvocationArguments(
11576
commandLineArgs.begin(), commandLineArgs.end(),
11677
"<swift-imported-modules>");
11778
assert(sourceFilePos != commandLineArgs.end());
118-
*sourceFilePos = sourceFileName.str();
79+
if (sourceFileName.hasValue())
80+
*sourceFilePos = sourceFileName->str();
81+
else
82+
commandLineArgs.erase(sourceFilePos);
11983

12084
// HACK! Drop the -fmodule-format= argument and the one that
12185
// precedes it.
@@ -139,18 +103,6 @@ static std::vector<std::string> getClangDepScanningInvocationArguments(
139103
*syntaxOnlyPos = "-c";
140104
}
141105

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-
154106
return commandLineArgs;
155107
}
156108

@@ -322,21 +274,14 @@ Optional<ModuleDependencies> ClangImporter::getModuleDependencies(
322274
// Retrieve or create the shared state.
323275
auto clangImpl = getOrCreateClangImpl(cache);
324276

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-
332277
// Determine the command-line arguments for dependency scanning.
333278
std::vector<std::string> commandLineArgs =
334-
getClangDepScanningInvocationArguments(ctx, *importHackFile);
279+
getClangDepScanningInvocationArguments(ctx);
335280
std::string workingDir =
336281
ctx.SourceMgr.getFileSystem()->getCurrentWorkingDirectory().get();
337282

338283
auto clangDependencies = clangImpl->tool.getFullDependencies(
339-
commandLineArgs, workingDir, clangImpl->alreadySeen);
284+
commandLineArgs, workingDir, clangImpl->alreadySeen, moduleName);
340285
if (!clangDependencies) {
341286
// FIXME: Route this to a normal diagnostic.
342287
llvm::logAllUnhandledErrors(clangDependencies.takeError(), llvm::errs());
@@ -383,7 +328,7 @@ bool ClangImporter::addBridgingHeaderDependencies(
383328

384329
// Determine the command-line arguments for dependency scanning.
385330
std::vector<std::string> commandLineArgs =
386-
getClangDepScanningInvocationArguments(ctx, bridgingHeader);
331+
getClangDepScanningInvocationArguments(ctx, StringRef(bridgingHeader));
387332
std::string workingDir =
388333
ctx.SourceMgr.getFileSystem()->getCurrentWorkingDirectory().get();
389334

0 commit comments

Comments
 (0)