Skip to content

Commit 554ecf6

Browse files
authored
Merge pull request #31720 from nkcsgexi/refactor-sub-astcontext-delegate
ModuleInterface: refactor compiler instance configuration to a standalone delegate class. NFC
2 parents b4cc626 + 3952fd5 commit 554ecf6

15 files changed

+415
-373
lines changed

include/swift/AST/ASTContext.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ namespace swift {
121121
class UnifiedStatsReporter;
122122
class IndexSubset;
123123
struct SILAutoDiffDerivativeFunctionKey;
124-
struct SubASTContextDelegate;
124+
struct InterfaceSubContextDelegate;
125125

126126
enum class KnownProtocolKind : uint8_t;
127127

@@ -721,7 +721,7 @@ class ASTContext final {
721721
StringRef moduleName,
722722
bool isUnderlyingClangModule,
723723
ModuleDependenciesCache &cache,
724-
SubASTContextDelegate &delegate);
724+
InterfaceSubContextDelegate &delegate);
725725

726726
/// Load extensions to the given nominal type from the external
727727
/// module loaders.

include/swift/AST/ModuleLoader.h

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class ModuleDependenciesCache;
4848
class NominalTypeDecl;
4949
class SourceFile;
5050
class TypeDecl;
51+
class CompilerInstance;
5152

5253
enum class KnownProtocolKind : uint8_t;
5354

@@ -85,13 +86,25 @@ class DependencyTracker {
8586
std::shared_ptr<clang::DependencyCollector> getClangCollector();
8687
};
8788

89+
struct SubCompilerInstanceInfo {
90+
StringRef CompilerVersion;
91+
CompilerInstance* Instance;
92+
};
93+
8894
/// Abstract interface to run an action in a sub ASTContext.
89-
struct SubASTContextDelegate {
90-
virtual bool runInSubContext(ASTContext &ctx, StringRef interfacePath,
91-
llvm::function_ref<bool(ASTContext&)> action) {
92-
llvm_unreachable("function should be overriden");
93-
}
94-
virtual ~SubASTContextDelegate() = default;
95+
struct InterfaceSubContextDelegate {
96+
virtual bool runInSubContext(StringRef moduleName,
97+
StringRef interfacePath,
98+
StringRef outputPath,
99+
SourceLoc diagLoc,
100+
llvm::function_ref<bool(ASTContext&)> action) = 0;
101+
virtual bool runInSubCompilerInstance(StringRef moduleName,
102+
StringRef interfacePath,
103+
StringRef outputPath,
104+
SourceLoc diagLoc,
105+
llvm::function_ref<bool(SubCompilerInstanceInfo&)> action) = 0;
106+
107+
virtual ~InterfaceSubContextDelegate() = default;
95108
};
96109

97110
/// Abstract interface that loads named modules into the AST.
@@ -196,7 +209,8 @@ class ModuleLoader {
196209
/// if no such module exists.
197210
virtual Optional<ModuleDependencies> getModuleDependencies(
198211
StringRef moduleName,
199-
ModuleDependenciesCache &cache, SubASTContextDelegate &delegate) = 0;
212+
ModuleDependenciesCache &cache,
213+
InterfaceSubContextDelegate &delegate) = 0;
200214
};
201215

202216
} // namespace swift

include/swift/ClangImporter/ClangImporter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ class ClangImporter final : public ClangModuleLoader {
371371

372372
Optional<ModuleDependencies> getModuleDependencies(
373373
StringRef moduleName, ModuleDependenciesCache &cache,
374-
SubASTContextDelegate &delegate) override;
374+
InterfaceSubContextDelegate &delegate) override;
375375

376376
/// Add dependency information for the bridging header.
377377
///

include/swift/Frontend/FrontendInputsAndOutputs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ class FrontendInputsAndOutputs {
173173

174174
private:
175175
friend class ArgsToFrontendOptionsConverter;
176-
friend class ModuleInterfaceBuilder;
176+
friend struct InterfaceSubContextDelegateImpl;
177177
void setMainAndSupplementaryOutputs(
178178
ArrayRef<std::string> outputFiles,
179179
ArrayRef<SupplementaryOutputPaths> supplementaryOutputs);

include/swift/Frontend/ModuleInterfaceLoader.h

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
#define SWIFT_FRONTEND_MODULEINTERFACELOADER_H
109109

110110
#include "swift/Basic/LLVM.h"
111+
#include "swift/Frontend/Frontend.h"
111112
#include "swift/Frontend/ModuleInterfaceSupport.h"
112113
#include "swift/Serialization/SerializedModuleLoader.h"
113114
#include "llvm/Support/StringSaver.h"
@@ -203,18 +204,65 @@ class ModuleInterfaceLoader : public SerializedModuleLoaderBase {
203204
std::string
204205
getModuleCachePathFromClang(const clang::CompilerInstance &Instance);
205206

206-
bool extractSwiftInterfaceVersionAndArgs(SourceManager &SM,
207-
DiagnosticEngine &Diags,
208-
StringRef InterfacePath,
209-
version::Version &Vers,
210-
StringRef &CompilerVersion,
211-
llvm::StringSaver &SubArgSaver,
212-
SmallVectorImpl<const char *> &SubArgs,
213-
SourceLoc diagnosticLoc = SourceLoc());
214-
215-
void inheritOptionsForBuildingInterface(CompilerInvocation &Invok,
216-
const SearchPathOptions &SearchPathOpts,
217-
const LangOptions &LangOpts);
207+
struct InterfaceSubContextDelegateImpl: InterfaceSubContextDelegate {
208+
private:
209+
SourceManager &SM;
210+
DiagnosticEngine &Diags;
211+
CompilerInvocation subInvocation;
212+
213+
template<typename ...ArgTypes>
214+
InFlightDiagnostic diagnose(StringRef interfacePath,
215+
SourceLoc diagnosticLoc,
216+
Diag<ArgTypes...> ID,
217+
typename detail::PassArgument<ArgTypes>::type... Args) {
218+
SourceLoc loc = diagnosticLoc;
219+
if (diagnosticLoc.isInvalid()) {
220+
// Diagnose this inside the interface file, if possible.
221+
loc = SM.getLocFromExternalSource(interfacePath, 1, 1);
222+
}
223+
return Diags.diagnose(loc, ID, std::move(Args)...);
224+
}
225+
226+
bool extractSwiftInterfaceVersionAndArgs(llvm::StringSaver &SubArgSaver,
227+
SmallVectorImpl<const char *> &SubArgs,
228+
std::string &CompilerVersion,
229+
StringRef interfacePath,
230+
SourceLoc diagnosticLoc);
231+
SubCompilerInstanceInfo createInstance(StringRef moduleName,
232+
StringRef interfacePath,
233+
StringRef outputPath,
234+
SourceLoc diagLoc);
235+
public:
236+
InterfaceSubContextDelegateImpl(SourceManager &SM,
237+
DiagnosticEngine &Diags,
238+
const SearchPathOptions &searchPathOpts,
239+
const LangOptions &langOpts,
240+
ClangModuleLoader *clangImporter = nullptr,
241+
StringRef moduleCachePath = StringRef(),
242+
StringRef prebuiltCachePath = StringRef(),
243+
bool serializeDependencyHashes = false,
244+
bool trackSystemDependencies = false,
245+
bool remarkOnRebuildFromInterface = false,
246+
bool disableInterfaceFileLock = false);
247+
bool runInSubContext(StringRef moduleName,
248+
StringRef interfacePath,
249+
StringRef outputPath,
250+
SourceLoc diagLoc,
251+
llvm::function_ref<bool(ASTContext&)> action) override;
252+
bool runInSubCompilerInstance(StringRef moduleName,
253+
StringRef interfacePath,
254+
StringRef outputPath,
255+
SourceLoc diagLoc,
256+
llvm::function_ref<bool(SubCompilerInstanceInfo&)> action) override;
257+
258+
~InterfaceSubContextDelegateImpl() = default;
259+
260+
/// includes a hash of relevant key data.
261+
void computeCachedOutputPath(StringRef moduleName,
262+
StringRef UseInterfacePath,
263+
llvm::SmallString<256> &OutPath);
264+
std::string getCacheHash(StringRef useInterfacePath);
265+
};
218266
}
219267

220268
#endif

include/swift/Sema/SourceLoader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class SourceLoader : public ModuleLoader {
9494

9595
Optional<ModuleDependencies> getModuleDependencies(
9696
StringRef moduleName, ModuleDependenciesCache &cache,
97-
SubASTContextDelegate &delegate) override {
97+
InterfaceSubContextDelegate &delegate) override {
9898
// FIXME: Implement?
9999
return None;
100100
}

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ class SerializedModuleLoaderBase : public ModuleLoader {
195195

196196
virtual Optional<ModuleDependencies> getModuleDependencies(
197197
StringRef moduleName, ModuleDependenciesCache &cache,
198-
SubASTContextDelegate &delegate) override;
198+
InterfaceSubContextDelegate &delegate) override;
199199
};
200200

201201
/// Imports serialized Swift modules into an ASTContext.

lib/AST/ASTContext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1463,7 +1463,7 @@ void ASTContext::addModuleLoader(std::unique_ptr<ModuleLoader> loader,
14631463

14641464
Optional<ModuleDependencies> ASTContext::getModuleDependencies(
14651465
StringRef moduleName, bool isUnderlyingClangModule,
1466-
ModuleDependenciesCache &cache, SubASTContextDelegate &delegate) {
1466+
ModuleDependenciesCache &cache, InterfaceSubContextDelegate &delegate) {
14671467
for (auto &loader : getImpl().ModuleLoaders) {
14681468
if (isUnderlyingClangModule &&
14691469
loader.get() != getImpl().TheClangModuleLoader)

lib/ClangImporter/ClangImporter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ bool ClangImporter::canReadPCH(StringRef PCHFilename) {
803803
CI.setInvocation(std::move(invocation));
804804
CI.setTarget(&Impl.Instance->getTarget());
805805
CI.setDiagnostics(
806-
&*CompilerInstance::createDiagnostics(new clang::DiagnosticOptions()));
806+
&*clang::CompilerInstance::createDiagnostics(new clang::DiagnosticOptions()));
807807

808808
// Note: Reusing the file manager is safe; this is a component that's already
809809
// reused when building PCM files for the module cache.

lib/ClangImporter/ClangModuleDependencyScanner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ static void recordModuleDependencies(
225225

226226
Optional<ModuleDependencies> ClangImporter::getModuleDependencies(
227227
StringRef moduleName, ModuleDependenciesCache &cache,
228-
SubASTContextDelegate &delegate) {
228+
InterfaceSubContextDelegate &delegate) {
229229
// Check whether there is already a cached result.
230230
if (auto found = cache.findDependencies(
231231
moduleName, ModuleDependenciesKind::Clang))

0 commit comments

Comments
 (0)