Skip to content

Commit 9a39b20

Browse files
authored
Merge pull request #61059 from artemcm/ClangScannerGardening
[Dependency Scanner][NFC] Clean up/Gardening on 'ClangModuleDependencyScanner'
2 parents 2cb6d72 + 96a82cd commit 9a39b20

File tree

4 files changed

+42
-73
lines changed

4 files changed

+42
-73
lines changed

include/swift/AST/ModuleDependencies.h

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
#include "swift/Basic/LLVM.h"
2222
#include "swift/AST/Import.h"
23+
#include "clang/Tooling/DependencyScanning/DependencyScanningService.h"
24+
#include "clang/Tooling/DependencyScanning/DependencyScanningTool.h"
2325
#include "llvm/ADT/ArrayRef.h"
2426
#include "llvm/ADT/Optional.h"
2527
#include "llvm/ADT/StringSet.h"
@@ -305,6 +307,7 @@ class SwiftPlaceholderModuleDependencyStorage : public ModuleDependenciesStorage
305307
}
306308
};
307309

310+
// MARK: Module Dependencies
308311
/// Describes the dependencies of a given module.
309312
///
310313
/// The dependencies of a module include all of the source files that go
@@ -475,6 +478,7 @@ using ModuleDependenciesKindRefMap =
475478
llvm::StringMap<const ModuleDependencies *>,
476479
ModuleDependenciesKindHash>;
477480

481+
// MARK: GlobalModuleDependenciesCache
478482
/// A cache describing the set of module dependencies that has been queried
479483
/// thus far. This cache records/stores the actual Dependency values and can be
480484
/// preserved across different scanning actions (e.g. in
@@ -523,7 +527,7 @@ class GlobalModuleDependenciesCache {
523527
getDependenciesMap(ModuleDependenciesKind kind) const;
524528

525529
public:
526-
GlobalModuleDependenciesCache() {};
530+
GlobalModuleDependenciesCache();
527531
GlobalModuleDependenciesCache(const GlobalModuleDependenciesCache &) = delete;
528532
GlobalModuleDependenciesCache &
529533
operator=(const GlobalModuleDependenciesCache &) = delete;
@@ -592,6 +596,7 @@ class GlobalModuleDependenciesCache {
592596
}
593597
};
594598

599+
// MARK: ModuleDependenciesCache
595600
/// This "local" dependencies cache persists only for the duration of a given
596601
/// scanning action, and wraps an instance of a `GlobalModuleDependenciesCache`
597602
/// which may carry cached scanning information from prior scanning actions.
@@ -609,21 +614,17 @@ class ModuleDependenciesCache {
609614

610615
/// Name of the module under scan
611616
StringRef mainScanModuleName;
617+
/// Set containing all of the Clang modules that have already been seen.
618+
llvm::StringSet<> alreadySeenClangModules;
619+
/// The 'persistent' Clang dependency scanner service
620+
/// TODO: Share this service among common scanner invocations
621+
clang::tooling::dependencies::DependencyScanningService clangScanningService;
622+
/// The Clang dependency scanner tool
623+
clang::tooling::dependencies::DependencyScanningTool clangScanningTool;
612624

613625
/// Discovered Clang modules are only cached locally.
614626
llvm::StringMap<ModuleDependenciesVector> clangModuleDependencies;
615627

616-
/// Function that will delete \c clangImpl properly.
617-
void (*clangImplDeleter)(ClangModuleDependenciesCacheImpl *) = nullptr;
618-
/// Additional information needed for Clang dependency scanning.
619-
ClangModuleDependenciesCacheImpl *clangImpl = nullptr;
620-
621-
/// Free up the storage associated with the Clang implementation.
622-
void destroyClangImpl() {
623-
if (this->clangImplDeleter)
624-
this->clangImplDeleter(this->clangImpl);
625-
}
626-
627628
/// Retrieve the dependencies map that corresponds to the given dependency
628629
/// kind.
629630
llvm::StringMap<const ModuleDependencies *> &
@@ -647,28 +648,20 @@ class ModuleDependenciesCache {
647648
StringRef mainScanModuleName);
648649
ModuleDependenciesCache(const ModuleDependenciesCache &) = delete;
649650
ModuleDependenciesCache &operator=(const ModuleDependenciesCache &) = delete;
650-
virtual ~ModuleDependenciesCache() { destroyClangImpl(); }
651651

652652
public:
653-
/// Set the Clang-specific implementation data.
654-
void
655-
setClangImpl(ClangModuleDependenciesCacheImpl *clangImpl,
656-
void (*clangImplDeleter)(ClangModuleDependenciesCacheImpl *)) {
657-
destroyClangImpl();
658-
659-
this->clangImpl = clangImpl;
660-
this->clangImplDeleter = clangImplDeleter;
661-
}
662-
663-
/// Retrieve the Clang-specific implementation data;
664-
ClangModuleDependenciesCacheImpl *getClangImpl() const {
665-
return clangImpl;
666-
}
667-
668653
/// Whether we have cached dependency information for the given module.
669654
bool hasDependencies(StringRef moduleName,
670655
ModuleLookupSpecifics details) const;
671656

657+
/// Produce a reference to the Clang scanner tool associated with this cache
658+
clang::tooling::dependencies::DependencyScanningTool& getClangScannerTool() {
659+
return clangScanningTool;
660+
}
661+
llvm::StringSet<>& getAlreadySeenClangModules() {
662+
return alreadySeenClangModules;
663+
}
664+
672665
/// Look for module dependencies for a module with the given name given
673666
/// current search paths.
674667
///

lib/AST/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ if(NOT SWIFT_BUILD_ONLY_SYNTAXPARSERLIB)
137137
clangFormat
138138
clangToolingCore
139139
clangFrontendTool
140+
clangDependencyScanning
140141
clangFrontend
141142
clangDriver
142143
clangSerialization

lib/AST/ModuleDependencies.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ void ModuleDependencies::addBridgingModuleDependency(
225225
}
226226
}
227227

228+
GlobalModuleDependenciesCache::GlobalModuleDependenciesCache() {}
228229
GlobalModuleDependenciesCache::TargetSpecificGlobalCacheState *
229230
GlobalModuleDependenciesCache::getCurrentCache() const {
230231
assert(CurrentTriple.hasValue() &&
@@ -533,7 +534,17 @@ ModuleDependenciesCache::getDependencyReferencesMap(
533534
ModuleDependenciesCache::ModuleDependenciesCache(
534535
GlobalModuleDependenciesCache &globalCache,
535536
StringRef mainScanModuleName)
536-
: globalCache(globalCache), mainScanModuleName(mainScanModuleName) {
537+
: globalCache(globalCache),
538+
mainScanModuleName(mainScanModuleName),
539+
clangScanningService(
540+
clang::tooling::dependencies::ScanningMode::DependencyDirectivesScan,
541+
clang::tooling::dependencies::ScanningOutputFormat::Full,
542+
clang::CASOptions(),
543+
/* Cache */ nullptr,
544+
/* SharedFS */ nullptr,
545+
/* ReuseFileManager */ false,
546+
/* OptimizeArgs */ false),
547+
clangScanningTool(clangScanningService) {
537548
for (auto kind = ModuleDependenciesKind::FirstKind;
538549
kind != ModuleDependenciesKind::LastKind; ++kind) {
539550
ModuleDependenciesMap.insert(

lib/ClangImporter/ClangModuleDependencyScanner.cpp

Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,6 @@ using namespace swift;
2828
using namespace clang::tooling;
2929
using namespace clang::tooling::dependencies;
3030

31-
class swift::ClangModuleDependenciesCacheImpl {
32-
public:
33-
/// Set containing all of the Clang modules that have already been seen.
34-
llvm::StringSet<> alreadySeen;
35-
36-
DependencyScanningService service;
37-
38-
DependencyScanningTool tool;
39-
40-
ClangModuleDependenciesCacheImpl()
41-
: service(ScanningMode::DependencyDirectivesScan,
42-
ScanningOutputFormat::Full, clang::CASOptions(), nullptr, nullptr),
43-
tool(service) {}
44-
};
45-
4631
// Add search paths.
4732
// Note: This is handled differently for the Clang importer itself, which
4833
// adds search paths to Clang's data structures rather than to its
@@ -107,21 +92,6 @@ static std::vector<std::string> getClangDepScanningInvocationArguments(
10792
return commandLineArgs;
10893
}
10994

110-
/// Get or create the Clang-specific
111-
static ClangModuleDependenciesCacheImpl *getOrCreateClangImpl(
112-
ModuleDependenciesCache &cache) {
113-
auto clangImpl = cache.getClangImpl();
114-
if (!clangImpl) {
115-
clangImpl = new ClangModuleDependenciesCacheImpl();
116-
cache.setClangImpl(clangImpl,
117-
[](ClangModuleDependenciesCacheImpl *ptr) {
118-
delete ptr;
119-
});
120-
}
121-
122-
return clangImpl;
123-
}
124-
12595
/// Record the module dependencies we found by scanning Clang modules into
12696
/// the module dependencies cache.
12797
void ClangImporter::recordModuleDependencies(
@@ -272,9 +242,6 @@ Optional<ModuleDependencies> ClangImporter::getModuleDependencies(
272242
{ModuleDependenciesKind::Clang, currentSwiftSearchPathSet}))
273243
return found;
274244

275-
// Retrieve or create the shared state.
276-
auto clangImpl = getOrCreateClangImpl(cache);
277-
278245
// Determine the command-line arguments for dependency scanning.
279246
std::vector<std::string> commandLineArgs =
280247
getClangDepScanningInvocationArguments(ctx);
@@ -298,16 +265,17 @@ Optional<ModuleDependencies> ClangImporter::getModuleDependencies(
298265
workingDir = *(clangWorkingDirPos - 1);
299266
}
300267

301-
auto clangDependencies = clangImpl->tool.getFullDependencies(
302-
commandLineArgs, workingDir, clangImpl->alreadySeen, moduleName);
268+
auto clangDependencies = cache.getClangScannerTool().getFullDependencies(
269+
commandLineArgs, workingDir, cache.getAlreadySeenClangModules(),
270+
moduleName);
303271
if (!clangDependencies) {
304272
auto errorStr = toString(clangDependencies.takeError());
305273
// We ignore the "module 'foo' not found" error, the Swift dependency
306274
// scanner will report such an error only if all of the module loaders
307275
// fail as well.
308-
if (errorStr.find("fatal error: module '" + moduleName.str() + "' not found") == std::string::npos)
309-
ctx.Diags.diagnose(SourceLoc(),
310-
diag::clang_dependency_scan_error,
276+
if (errorStr.find("fatal error: module '" + moduleName.str() +
277+
"' not found") == std::string::npos)
278+
ctx.Diags.diagnose(SourceLoc(), diag::clang_dependency_scan_error,
311279
errorStr);
312280
return None;
313281
}
@@ -344,9 +312,6 @@ bool ClangImporter::addBridgingHeaderDependencies(
344312
llvm_unreachable("Unexpected module dependency kind");
345313
}
346314

347-
// Retrieve or create the shared state.
348-
auto clangImpl = getOrCreateClangImpl(cache);
349-
350315
// Retrieve the bridging header.
351316
std::string bridgingHeader = *targetModule.getBridgingHeader();
352317

@@ -356,9 +321,8 @@ bool ClangImporter::addBridgingHeaderDependencies(
356321
std::string workingDir =
357322
ctx.SourceMgr.getFileSystem()->getCurrentWorkingDirectory().get();
358323

359-
auto clangDependencies = clangImpl->tool.getFullDependencies(
360-
commandLineArgs, workingDir, clangImpl->alreadySeen);
361-
324+
auto clangDependencies = cache.getClangScannerTool().getFullDependencies(
325+
commandLineArgs, workingDir, cache.getAlreadySeenClangModules());
362326
if (!clangDependencies) {
363327
// FIXME: Route this to a normal diagnostic.
364328
llvm::logAllUnhandledErrors(clangDependencies.takeError(), llvm::errs());

0 commit comments

Comments
 (0)