Skip to content

Commit d8f77c6

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

File tree

1 file changed

+10
-65
lines changed

1 file changed

+10
-65
lines changed

lib/ClangImporter/ClangModuleDependencyScanner.cpp

Lines changed: 10 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,6 @@ using namespace clang::tooling;
2929
using namespace clang::tooling::dependencies;
3030

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

4640
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),
5043
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);
5644
};
5745

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

10970
// Form the basic command line.
@@ -116,7 +77,10 @@ static std::vector<std::string> getClangDepScanningInvocationArguments(
11677
commandLineArgs.begin(), commandLineArgs.end(),
11778
"<swift-imported-modules>");
11879
assert(sourceFilePos != commandLineArgs.end());
119-
*sourceFilePos = sourceFileName.str();
80+
if (sourceFileName.hasValue())
81+
*sourceFilePos = sourceFileName->str();
82+
else
83+
commandLineArgs.erase(sourceFilePos);
12084

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

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

@@ -323,16 +275,9 @@ Optional<ModuleDependencies> ClangImporter::getModuleDependencies(
323275
// Retrieve or create the shared state.
324276
auto clangImpl = getOrCreateClangImpl(cache);
325277

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-
333278
// Determine the command-line arguments for dependency scanning.
334279
std::vector<std::string> commandLineArgs =
335-
getClangDepScanningInvocationArguments(ctx, *importHackFile);
280+
getClangDepScanningInvocationArguments(ctx);
336281
// The Swift compiler does not have a concept of a working directory.
337282
// It is instead handled by the Swift driver by resolving relative paths
338283
// according to the driver's notion of a working directory. On the other hand,
@@ -354,7 +299,7 @@ Optional<ModuleDependencies> ClangImporter::getModuleDependencies(
354299
}
355300

356301
auto clangDependencies = clangImpl->tool.getFullDependencies(
357-
commandLineArgs, workingDir, clangImpl->alreadySeen);
302+
commandLineArgs, workingDir, clangImpl->alreadySeen, moduleName);
358303
if (!clangDependencies) {
359304
auto errorStr = toString(clangDependencies.takeError());
360305
// We ignore the "module 'foo' not found" error, the Swift dependency
@@ -407,7 +352,7 @@ bool ClangImporter::addBridgingHeaderDependencies(
407352

408353
// Determine the command-line arguments for dependency scanning.
409354
std::vector<std::string> commandLineArgs =
410-
getClangDepScanningInvocationArguments(ctx, bridgingHeader);
355+
getClangDepScanningInvocationArguments(ctx, StringRef(bridgingHeader));
411356
std::string workingDir =
412357
ctx.SourceMgr.getFileSystem()->getCurrentWorkingDirectory().get();
413358

0 commit comments

Comments
 (0)