Skip to content

Commit 1d0065d

Browse files
authored
Merge pull request swiftlang#32145 from CodaFi/port-of-call
[NFC] Miscellaneous Cleanups To Clang Module Loading
2 parents 8c20bcd + d2e1336 commit 1d0065d

File tree

6 files changed

+38
-72
lines changed

6 files changed

+38
-72
lines changed

include/swift/AST/Evaluator.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,7 @@ class Evaluator {
243243
public:
244244
/// Construct a new evaluator that can emit cyclic-dependency
245245
/// diagnostics through the given diagnostics engine.
246-
Evaluator(DiagnosticEngine &diags,
247-
bool debugDumpCycles,
248-
bool buildDependencyGraph,
249-
bool enableExperimentalPrivateDeps);
246+
Evaluator(DiagnosticEngine &diags, const LangOptions &opts);
250247

251248
/// Emit GraphViz output visualizing the request graph.
252249
void emitRequestEvaluatorGraphViz(llvm::StringRef graphVizPath);

lib/AST/ASTContext.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -549,10 +549,7 @@ ASTContext::ASTContext(LangOptions &langOpts, TypeCheckerOptions &typeckOpts,
549549
: LangOpts(langOpts),
550550
TypeCheckerOpts(typeckOpts),
551551
SearchPathOpts(SearchPathOpts), SourceMgr(SourceMgr), Diags(Diags),
552-
evaluator(Diags,
553-
langOpts.DebugDumpCycles,
554-
langOpts.BuildRequestDependencyGraph,
555-
langOpts.EnableExperientalPrivateIntransitiveDependencies),
552+
evaluator(Diags, langOpts),
556553
TheBuiltinModule(createBuiltinModule(*this)),
557554
StdlibModuleName(getIdentifier(STDLIB_NAME)),
558555
SwiftShimsModuleName(getIdentifier(SWIFT_SHIMS_NAME)),

lib/AST/Evaluator.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
//===----------------------------------------------------------------------===//
1717
#include "swift/AST/Evaluator.h"
1818
#include "swift/AST/DiagnosticEngine.h"
19+
#include "swift/Basic/LangOptions.h"
1920
#include "swift/Basic/Range.h"
2021
#include "swift/Basic/SourceManager.h"
2122
#include "llvm/ADT/StringExtras.h"
@@ -62,21 +63,20 @@ void Evaluator::registerRequestFunctions(
6263
}
6364

6465
static evaluator::DependencyRecorder::Mode
65-
computeDependencyModeFromFlags(bool enableExperimentalPrivateDeps) {
66+
computeDependencyModeFromFlags(const LangOptions &opts) {
6667
using Mode = evaluator::DependencyRecorder::Mode;
67-
if (enableExperimentalPrivateDeps) {
68+
if (opts.EnableExperientalPrivateIntransitiveDependencies) {
6869
return Mode::ExperimentalPrivateDependencies;
6970
}
7071

7172
return Mode::StatusQuo;
7273
}
7374

74-
Evaluator::Evaluator(DiagnosticEngine &diags, bool debugDumpCycles,
75-
bool buildDependencyGraph,
76-
bool enableExperimentalPrivateDeps)
77-
: diags(diags), debugDumpCycles(debugDumpCycles),
78-
buildDependencyGraph(buildDependencyGraph),
79-
recorder{computeDependencyModeFromFlags(enableExperimentalPrivateDeps)} {}
75+
Evaluator::Evaluator(DiagnosticEngine &diags, const LangOptions &opts)
76+
: diags(diags),
77+
debugDumpCycles(opts.DebugDumpCycles),
78+
buildDependencyGraph(opts.BuildRequestDependencyGraph),
79+
recorder{computeDependencyModeFromFlags(opts)} {}
8080

8181
void Evaluator::emitRequestEvaluatorGraphViz(llvm::StringRef graphVizPath) {
8282
std::error_code error;

lib/ClangImporter/ClangImporter.cpp

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1786,8 +1786,7 @@ ModuleDecl *ClangImporter::Implementation::loadModuleClang(
17861786
if (!clangModule)
17871787
return nullptr;
17881788

1789-
return finishLoadingClangModule(importLoc, clangModule,
1790-
/*preferOverlay=*/false);
1789+
return finishLoadingClangModule(clangModule, importLoc);
17911790
}
17921791

17931792
ModuleDecl *
@@ -1807,59 +1806,30 @@ ModuleDecl *ClangImporter::Implementation::loadModule(
18071806
}
18081807

18091808
ModuleDecl *ClangImporter::Implementation::finishLoadingClangModule(
1810-
SourceLoc importLoc, const clang::Module *clangModule, bool findOverlay) {
1809+
const clang::Module *clangModule, SourceLoc importLoc) {
18111810
assert(clangModule);
18121811

18131812
// Bump the generation count.
18141813
bumpGeneration();
18151814

1816-
auto &cacheEntry = ModuleWrappers[clangModule];
1817-
ModuleDecl *result;
1818-
ClangModuleUnit *wrapperUnit;
1819-
if ((wrapperUnit = cacheEntry.getPointer())) {
1820-
result = wrapperUnit->getParentModule();
1821-
if (!cacheEntry.getInt()) {
1822-
// Force load overlays for all imported modules.
1823-
// FIXME: This forces the creation of wrapper modules for all imports as
1824-
// well, and may do unnecessary work.
1825-
cacheEntry.setInt(true);
1826-
(void) namelookup::getAllImports(result);
1827-
}
1828-
} else {
1829-
// Build the representation of the Clang module in Swift.
1830-
// FIXME: The name of this module could end up as a key in the ASTContext,
1831-
// but that's not correct for submodules.
1832-
Identifier name = SwiftContext.getIdentifier((*clangModule).Name);
1833-
result = ModuleDecl::create(name, SwiftContext);
1834-
result->setIsSystemModule(clangModule->IsSystem);
1835-
result->setIsNonSwiftModule();
1836-
result->setHasResolvedImports();
1837-
1838-
wrapperUnit =
1839-
new (SwiftContext) ClangModuleUnit(*result, *this, clangModule);
1840-
result->addFile(*wrapperUnit);
1841-
SwiftContext.getClangModuleLoader()
1842-
->findOverlayFiles(importLoc, result, wrapperUnit);
1843-
cacheEntry.setPointerAndInt(wrapperUnit, true);
1844-
1845-
// Force load overlays for all imported modules.
1846-
// FIXME: This forces the creation of wrapper modules for all imports as
1847-
// well, and may do unnecessary work.
1815+
// Force load overlays for all imported modules.
1816+
// FIXME: This forces the creation of wrapper modules for all imports as
1817+
// well, and may do unnecessary work.
1818+
ClangModuleUnit *wrapperUnit = getWrapperForModule(clangModule, importLoc);
1819+
ModuleDecl *result = wrapperUnit->getParentModule();
1820+
if (!ModuleWrappers[clangModule].getInt()) {
1821+
ModuleWrappers[clangModule].setInt(true);
18481822
(void) namelookup::getAllImports(result);
18491823
}
18501824

18511825
if (clangModule->isSubModule()) {
1852-
finishLoadingClangModule(importLoc, clangModule->getTopLevelModule(), true);
1826+
finishLoadingClangModule(clangModule->getTopLevelModule(), importLoc);
18531827
} else {
18541828
ModuleDecl *&loaded = SwiftContext.LoadedModules[result->getName()];
18551829
if (!loaded)
18561830
loaded = result;
18571831
}
18581832

1859-
if (findOverlay)
1860-
if (ModuleDecl *overlay = wrapperUnit->getOverlayModule())
1861-
result = overlay;
1862-
18631833
return result;
18641834
}
18651835

@@ -1883,8 +1853,7 @@ void ClangImporter::Implementation::handleDeferredImports(SourceLoc diagLoc) {
18831853
// officially supported with bridging headers: app targets and unit tests
18841854
// only. Unfortunately that's not enforced.
18851855
for (size_t i = 0; i < ImportedHeaderExports.size(); ++i) {
1886-
(void)finishLoadingClangModule(diagLoc, ImportedHeaderExports[i],
1887-
/*preferOverlay=*/true);
1856+
(void)finishLoadingClangModule(ImportedHeaderExports[i], diagLoc);
18881857
}
18891858
}
18901859

@@ -2025,7 +1994,7 @@ ClangImporter::Implementation::~Implementation() {
20251994
}
20261995

20271996
ClangModuleUnit *ClangImporter::Implementation::getWrapperForModule(
2028-
const clang::Module *underlying) {
1997+
const clang::Module *underlying, SourceLoc diagLoc) {
20291998
auto &cacheEntry = ModuleWrappers[underlying];
20301999
if (ClangModuleUnit *cached = cacheEntry.getPointer())
20312000
return cached;
@@ -2040,7 +2009,7 @@ ClangModuleUnit *ClangImporter::Implementation::getWrapperForModule(
20402009
auto file = new (SwiftContext) ClangModuleUnit(*wrapper, *this,
20412010
underlying);
20422011
wrapper->addFile(*file);
2043-
SwiftContext.getClangModuleLoader()->findOverlayFiles(SourceLoc(), wrapper, file);
2012+
SwiftContext.getClangModuleLoader()->findOverlayFiles(diagLoc, wrapper, file);
20442013
cacheEntry.setPointer(file);
20452014

20462015
return file;

lib/ClangImporter/ImporterImpl.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -925,12 +925,12 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
925925

926926
/// Retrieves the Swift wrapper for the given Clang module, creating
927927
/// it if necessary.
928-
ClangModuleUnit *getWrapperForModule(const clang::Module *underlying);
928+
ClangModuleUnit *getWrapperForModule(const clang::Module *underlying,
929+
SourceLoc importLoc = SourceLoc());
929930

930931
/// Constructs a Swift module for the given Clang module.
931-
ModuleDecl *finishLoadingClangModule(SourceLoc importLoc,
932-
const clang::Module *clangModule,
933-
bool preferOverlay);
932+
ModuleDecl *finishLoadingClangModule(const clang::Module *clangModule,
933+
SourceLoc importLoc);
934934

935935
/// Call finishLoadingClangModule on each deferred import collected
936936
/// while scanning a bridging header or PCH.

unittests/AST/ArithmeticEvaluator.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "swift/AST/DiagnosticEngine.h"
1717
#include "swift/AST/Evaluator.h"
1818
#include "swift/AST/SimpleRequest.h"
19+
#include "swift/Basic/LangOptions.h"
1920
#include "swift/Basic/SourceManager.h"
2021
#include "gtest/gtest.h"
2122
#include <cmath>
@@ -218,10 +219,11 @@ TEST(ArithmeticEvaluator, Simple) {
218219

219220
SourceManager sourceMgr;
220221
DiagnosticEngine diags(sourceMgr);
221-
Evaluator evaluator(diags,
222-
/*debugDumpCycles=*/false,
223-
/*buildDependencyGraph=*/true,
224-
/*privateDependencies*/false);
222+
LangOptions opts;
223+
opts.DebugDumpCycles = false;
224+
opts.BuildRequestDependencyGraph = true;
225+
opts.EnableExperientalPrivateIntransitiveDependencies = false;
226+
Evaluator evaluator(diags, opts);
225227
evaluator.registerRequestFunctions(Zone::ArithmeticEvaluator,
226228
arithmeticRequestFunctions);
227229

@@ -344,10 +346,11 @@ TEST(ArithmeticEvaluator, Cycle) {
344346

345347
SourceManager sourceMgr;
346348
DiagnosticEngine diags(sourceMgr);
347-
Evaluator evaluator(diags,
348-
/*debugDumpCycles=*/false,
349-
/*buildDependencyGraph=*/false,
350-
/*privateDependencies*/false);
349+
LangOptions opts;
350+
opts.DebugDumpCycles = false;
351+
opts.BuildRequestDependencyGraph = false;
352+
opts.EnableExperientalPrivateIntransitiveDependencies = false;
353+
Evaluator evaluator(diags, opts);
351354
evaluator.registerRequestFunctions(Zone::ArithmeticEvaluator,
352355
arithmeticRequestFunctions);
353356

0 commit comments

Comments
 (0)