Skip to content

Commit c624a87

Browse files
committed
[Reproducers] Add FileCollector support to DependencyTracker
For reproducers in LLDB, we need to collect module dependency information coming from the clang importer. However, we cannot override the dependency collector, like we do in LLDB, because its interface is completely hidden in the clang importer. The solution is to pass the FileCollector through the DependencyTracker.
1 parent 22f2119 commit c624a87

File tree

4 files changed

+46
-23
lines changed

4 files changed

+46
-23
lines changed

include/swift/AST/ModuleLoader.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
#include "llvm/ADT/SmallSet.h"
2525
#include "llvm/ADT/TinyPtrVector.h"
2626

27+
namespace llvm {
28+
class FileCollector;
29+
}
30+
2731
namespace clang {
2832
class DependencyCollector;
2933
}
@@ -53,8 +57,9 @@ enum class Bridgeability : unsigned {
5357
class DependencyTracker {
5458
std::shared_ptr<clang::DependencyCollector> clangCollector;
5559
public:
56-
57-
explicit DependencyTracker(bool TrackSystemDeps);
60+
explicit DependencyTracker(
61+
bool TrackSystemDeps,
62+
std::shared_ptr<llvm::FileCollector> FileCollector = {});
5863

5964
/// Adds a file as a dependency.
6065
///

include/swift/ClangImporter/ClangImporter.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
namespace llvm {
2525
class Triple;
26+
class FileCollector;
2627
template<typename Fn> class function_ref;
2728
}
2829

@@ -117,7 +118,8 @@ class ClangImporter final : public ClangModuleLoader {
117118
/// Create a new clang::DependencyCollector customized to
118119
/// ClangImporter's specific uses.
119120
static std::shared_ptr<clang::DependencyCollector>
120-
createDependencyCollector(bool TrackSystemDeps);
121+
createDependencyCollector(bool TrackSystemDeps,
122+
std::shared_ptr<llvm::FileCollector> FileCollector);
121123

122124
/// Append visible module names to \p names. Note that names are possibly
123125
/// duplicated, and not guaranteed to be ordered in any way.

lib/AST/ModuleLoader.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,21 @@
1818
#include "clang/Frontend/Utils.h"
1919
#include "swift/ClangImporter/ClangImporter.h"
2020

21+
namespace llvm {
22+
class FileCollector;
23+
}
24+
2125
namespace swift {
2226

23-
DependencyTracker::DependencyTracker(bool TrackSystemDeps)
24-
// NB: The ClangImporter believes it's responsible for the construction of
25-
// this instance, and it static_cast<>s the instance pointer to its own
26-
// subclass based on that belief. If you change this to be some other
27-
// instance, you will need to change ClangImporter's code to handle the
28-
// difference.
29-
: clangCollector(ClangImporter::createDependencyCollector(TrackSystemDeps))
30-
{
31-
}
27+
DependencyTracker::DependencyTracker(
28+
bool TrackSystemDeps, std::shared_ptr<llvm::FileCollector> FileCollector)
29+
// NB: The ClangImporter believes it's responsible for the construction of
30+
// this instance, and it static_cast<>s the instance pointer to its own
31+
// subclass based on that belief. If you change this to be some other
32+
// instance, you will need to change ClangImporter's code to handle the
33+
// difference.
34+
: clangCollector(ClangImporter::createDependencyCollector(TrackSystemDeps,
35+
FileCollector)) {}
3236

3337
void
3438
DependencyTracker::addDependency(StringRef File, bool IsSystem) {

lib/ClangImporter/ClangImporter.cpp

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@
1414
//
1515
//===----------------------------------------------------------------------===//
1616
#include "swift/ClangImporter/ClangImporter.h"
17-
#include "swift/ClangImporter/ClangModule.h"
17+
#include "ClangDiagnosticConsumer.h"
1818
#include "IAMInference.h"
1919
#include "ImporterImpl.h"
20-
#include "ClangDiagnosticConsumer.h"
21-
#include "swift/Subsystems.h"
2220
#include "swift/AST/ASTContext.h"
2321
#include "swift/AST/DiagnosticEngine.h"
2422
#include "swift/AST/DiagnosticsClangImporter.h"
@@ -33,9 +31,11 @@
3331
#include "swift/Basic/StringExtras.h"
3432
#include "swift/Basic/Version.h"
3533
#include "swift/ClangImporter/ClangImporterOptions.h"
34+
#include "swift/ClangImporter/ClangModule.h"
35+
#include "swift/Config.h"
3636
#include "swift/Parse/Lexer.h"
3737
#include "swift/Parse/Parser.h"
38-
#include "swift/Config.h"
38+
#include "swift/Subsystems.h"
3939
#include "clang/AST/ASTContext.h"
4040
#include "clang/AST/Mangle.h"
4141
#include "clang/Basic/CharInfo.h"
@@ -47,18 +47,19 @@
4747
#include "clang/Frontend/FrontendActions.h"
4848
#include "clang/Frontend/Utils.h"
4949
#include "clang/Index/IndexingAction.h"
50-
#include "clang/Serialization/ASTReader.h"
51-
#include "clang/Serialization/ASTWriter.h"
5250
#include "clang/Lex/Preprocessor.h"
5351
#include "clang/Lex/PreprocessorOptions.h"
5452
#include "clang/Parse/Parser.h"
5553
#include "clang/Rewrite/Frontend/FrontendActions.h"
5654
#include "clang/Rewrite/Frontend/Rewriters.h"
5755
#include "clang/Sema/Lookup.h"
5856
#include "clang/Sema/Sema.h"
57+
#include "clang/Serialization/ASTReader.h"
58+
#include "clang/Serialization/ASTWriter.h"
5959
#include "llvm/ADT/STLExtras.h"
6060
#include "llvm/ADT/StringExtras.h"
6161
#include "llvm/Support/CrashRecoveryContext.h"
62+
#include "llvm/Support/FileCollector.h"
6263
#include "llvm/Support/Memory.h"
6364
#include "llvm/Support/Path.h"
6465
#include <algorithm>
@@ -319,11 +320,13 @@ class BridgingPPTracker : public clang::PPCallbacks {
319320
class ClangImporterDependencyCollector : public clang::DependencyCollector
320321
{
321322
llvm::StringSet<> ExcludedPaths;
323+
std::shared_ptr<llvm::FileCollector> FileCollector;
322324
const bool TrackSystemDeps;
323325

324326
public:
325-
ClangImporterDependencyCollector(bool TrackSystemDeps)
326-
: TrackSystemDeps(TrackSystemDeps) {}
327+
ClangImporterDependencyCollector(
328+
bool TrackSystemDeps, std::shared_ptr<llvm::FileCollector> FileCollector)
329+
: FileCollector(FileCollector), TrackSystemDeps(TrackSystemDeps) {}
327330

328331
void excludePath(StringRef filename) {
329332
ExcludedPaths.insert(filename);
@@ -352,13 +355,22 @@ class ClangImporterDependencyCollector : public clang::DependencyCollector
352355
return false;
353356
return true;
354357
}
358+
359+
void maybeAddDependency(StringRef Filename, bool FromModule, bool IsSystem,
360+
bool IsModuleFile, bool IsMissing) override {
361+
if (FileCollector)
362+
FileCollector->addFile(Filename);
363+
clang::DependencyCollector::maybeAddDependency(
364+
Filename, FromModule, IsSystem, IsModuleFile, IsMissing);
365+
}
355366
};
356367
} // end anonymous namespace
357368

358369
std::shared_ptr<clang::DependencyCollector>
359-
ClangImporter::createDependencyCollector(bool TrackSystemDeps)
360-
{
361-
return std::make_shared<ClangImporterDependencyCollector>(TrackSystemDeps);
370+
ClangImporter::createDependencyCollector(
371+
bool TrackSystemDeps, std::shared_ptr<llvm::FileCollector> FileCollector) {
372+
return std::make_shared<ClangImporterDependencyCollector>(TrackSystemDeps,
373+
FileCollector);
362374
}
363375

364376
void ClangImporter::Implementation::addBridgeHeaderTopLevelDecls(

0 commit comments

Comments
 (0)