Skip to content

Commit c0feea1

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. (cherry picked from commit c624a87)
1 parent 5c50412 commit c0feea1

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
}
@@ -54,8 +58,9 @@ enum class Bridgeability : unsigned {
5458
class DependencyTracker {
5559
std::shared_ptr<clang::DependencyCollector> clangCollector;
5660
public:
57-
58-
explicit DependencyTracker(bool TrackSystemDeps);
61+
explicit DependencyTracker(
62+
bool TrackSystemDeps,
63+
std::shared_ptr<llvm::FileCollector> FileCollector = {});
5964

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

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

@@ -145,7 +146,8 @@ class ClangImporter final : public ClangModuleLoader {
145146
/// Create a new clang::DependencyCollector customized to
146147
/// ClangImporter's specific uses.
147148
static std::shared_ptr<clang::DependencyCollector>
148-
createDependencyCollector(bool TrackSystemDeps);
149+
createDependencyCollector(bool TrackSystemDeps,
150+
std::shared_ptr<llvm::FileCollector> FileCollector);
149151

150152
/// Append visible module names to \p names. Note that names are possibly
151153
/// 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"
@@ -34,9 +32,11 @@
3432
#include "swift/Basic/Version.h"
3533
#include "swift/ClangImporter/ClangImporterOptions.h"
3634
#include "swift/Demangling/Demangle.h"
35+
#include "swift/ClangImporter/ClangModule.h"
36+
#include "swift/Config.h"
3737
#include "swift/Parse/Lexer.h"
3838
#include "swift/Parse/Parser.h"
39-
#include "swift/Config.h"
39+
#include "swift/Subsystems.h"
4040
#include "clang/AST/ASTContext.h"
4141
#include "clang/AST/Mangle.h"
4242
#include "clang/Basic/CharInfo.h"
@@ -48,18 +48,19 @@
4848
#include "clang/Frontend/FrontendActions.h"
4949
#include "clang/Frontend/Utils.h"
5050
#include "clang/Index/IndexingAction.h"
51-
#include "clang/Serialization/ASTReader.h"
52-
#include "clang/Serialization/ASTWriter.h"
5351
#include "clang/Lex/Preprocessor.h"
5452
#include "clang/Lex/PreprocessorOptions.h"
5553
#include "clang/Parse/Parser.h"
5654
#include "clang/Rewrite/Frontend/FrontendActions.h"
5755
#include "clang/Rewrite/Frontend/Rewriters.h"
5856
#include "clang/Sema/Lookup.h"
5957
#include "clang/Sema/Sema.h"
58+
#include "clang/Serialization/ASTReader.h"
59+
#include "clang/Serialization/ASTWriter.h"
6060
#include "llvm/ADT/STLExtras.h"
6161
#include "llvm/ADT/StringExtras.h"
6262
#include "llvm/Support/CrashRecoveryContext.h"
63+
#include "llvm/Support/FileCollector.h"
6364
#include "llvm/Support/Memory.h"
6465
#include "llvm/Support/Path.h"
6566
#include <algorithm>
@@ -320,11 +321,13 @@ class BridgingPPTracker : public clang::PPCallbacks {
320321
class ClangImporterDependencyCollector : public clang::DependencyCollector
321322
{
322323
llvm::StringSet<> ExcludedPaths;
324+
std::shared_ptr<llvm::FileCollector> FileCollector;
323325
const bool TrackSystemDeps;
324326

325327
public:
326-
ClangImporterDependencyCollector(bool TrackSystemDeps)
327-
: TrackSystemDeps(TrackSystemDeps) {}
328+
ClangImporterDependencyCollector(
329+
bool TrackSystemDeps, std::shared_ptr<llvm::FileCollector> FileCollector)
330+
: FileCollector(FileCollector), TrackSystemDeps(TrackSystemDeps) {}
328331

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

359370
std::shared_ptr<clang::DependencyCollector>
360-
ClangImporter::createDependencyCollector(bool TrackSystemDeps)
361-
{
362-
return std::make_shared<ClangImporterDependencyCollector>(TrackSystemDeps);
371+
ClangImporter::createDependencyCollector(
372+
bool TrackSystemDeps, std::shared_ptr<llvm::FileCollector> FileCollector) {
373+
return std::make_shared<ClangImporterDependencyCollector>(TrackSystemDeps,
374+
FileCollector);
363375
}
364376

365377
void ClangImporter::Implementation::addBridgeHeaderTopLevelDecls(

0 commit comments

Comments
 (0)