Skip to content

Commit cce0411

Browse files
committed
[NFC] Add ASTContext::getModuleByIdentifier()
This is a reasonably common operation.
1 parent 7bfeeeb commit cce0411

File tree

9 files changed

+17
-24
lines changed

9 files changed

+17
-24
lines changed

include/swift/AST/ASTContext.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,8 @@ class ASTContext final {
879879

880880
ModuleDecl *getModuleByName(StringRef ModuleName);
881881

882+
ModuleDecl *getModuleByIdentifier(Identifier ModuleID);
883+
882884
/// Returns the standard library module, or null if the library isn't present.
883885
///
884886
/// If \p loadIfAbsent is true, the ASTContext will attempt to load the module

lib/AST/ASTContext.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1921,15 +1921,18 @@ ModuleDecl *ASTContext::getModuleByName(StringRef ModuleName) {
19211921
return getModule(builder.get());
19221922
}
19231923

1924+
ModuleDecl *ASTContext::getModuleByIdentifier(Identifier ModuleID) {
1925+
ImportPath::Module::Builder builder(ModuleID);
1926+
return getModule(builder.get());
1927+
}
1928+
19241929
ModuleDecl *ASTContext::getStdlibModule(bool loadIfAbsent) {
19251930
if (TheStdlibModule)
19261931
return TheStdlibModule;
19271932

19281933
if (loadIfAbsent) {
19291934
auto mutableThis = const_cast<ASTContext*>(this);
1930-
TheStdlibModule =
1931-
mutableThis->getModule(
1932-
ImportPath::Module::Builder(StdlibModuleName).get());
1935+
TheStdlibModule = mutableThis->getModuleByIdentifier(StdlibModuleName);
19331936
} else {
19341937
TheStdlibModule = getLoadedModule(StdlibModuleName);
19351938
}

lib/ClangImporter/ClangImporter.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3414,8 +3414,7 @@ ModuleDecl *ClangModuleUnit::getOverlayModule() const {
34143414
// FIXME: Include proper source location.
34153415
ModuleDecl *M = getParentModule();
34163416
ASTContext &Ctx = M->getASTContext();
3417-
auto overlay = Ctx.getModule(
3418-
ImportPath::Module::Builder(M->getName()).get());
3417+
auto overlay = Ctx.getModuleByIdentifier(M->getName());
34193418
if (overlay == M) {
34203419
overlay = nullptr;
34213420
} else {

lib/ClangImporter/ImportType.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2441,7 +2441,7 @@ static ModuleDecl *tryLoadModule(ASTContext &C,
24412441
if (importForwardDeclarations)
24422442
module = C.getLoadedModule(moduleName);
24432443
else
2444-
module = C.getModule(ImportPath::Module::Builder(moduleName).get());
2444+
module = C.getModuleByIdentifier(moduleName);
24452445

24462446
checkedModules[moduleName] = module;
24472447
return module;

lib/Sema/ImportResolution.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,8 +447,7 @@ ModuleImplicitImportsRequest::evaluate(Evaluator &evaluator,
447447

448448
// Add any modules we were asked to implicitly import.
449449
for (auto moduleName : importInfo.ModuleNames) {
450-
auto *importModule = ctx.getModule(
451-
ImportPath::Module::Builder(moduleName).get());
450+
auto *importModule = ctx.getModuleByIdentifier(moduleName);
452451
if (!importModule) {
453452
ctx.Diags.diagnose(SourceLoc(), diag::sema_no_import, moduleName.str());
454453
if (ctx.SearchPathOpts.SDKPath.empty() &&

tools/SourceKit/lib/SwiftLang/SwiftDocSupport.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@ using namespace SourceKit;
4040
using namespace swift;
4141
using namespace ide;
4242

43-
static ModuleDecl *getModuleByFullName(ASTContext &Ctx, Identifier ModuleName) {
44-
return Ctx.getModule(ImportPath::Module::Builder(ModuleName).get());
45-
}
46-
4743
namespace {
4844
struct TextRange {
4945
unsigned Offset;
@@ -1012,7 +1008,7 @@ static void reportSourceAnnotations(const SourceTextInfo &IFaceInfo,
10121008
static bool getModuleInterfaceInfo(ASTContext &Ctx, StringRef ModuleName,
10131009
SourceTextInfo &Info) {
10141010
// Load standard library so that Clang importer can use it.
1015-
auto *Stdlib = getModuleByFullName(Ctx, Ctx.StdlibModuleName);
1011+
auto *Stdlib = Ctx.getModuleByIdentifier(Ctx.StdlibModuleName);
10161012
if (!Stdlib)
10171013
return true;
10181014

@@ -1539,7 +1535,7 @@ findModuleGroups(StringRef ModuleName, ArrayRef<const char *> Args,
15391535

15401536
// Load standard library so that Clang importer can use it.
15411537
ASTContext &Ctx = CI.getASTContext();
1542-
auto *Stdlib = getModuleByFullName(Ctx, Ctx.StdlibModuleName);
1538+
auto *Stdlib = Ctx.getModuleByIdentifier(Ctx.StdlibModuleName);
15431539
if (!Stdlib) {
15441540
Error = "Cannot load stdlib.";
15451541
Receiver(RequestResult<ArrayRef<StringRef>>::fromError(Error));

tools/SourceKit/lib/SwiftLang/SwiftEditorInterfaceGen.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,6 @@ typedef SwiftInterfaceGenContext::Implementation::TextReference TextReference;
9191
typedef SwiftInterfaceGenContext::Implementation::TextDecl TextDecl;
9292
typedef SwiftInterfaceGenContext::Implementation::SourceTextInfo SourceTextInfo;
9393

94-
static ModuleDecl *getModuleByFullName(ASTContext &Ctx, Identifier ModuleName) {
95-
return Ctx.getModule(ImportPath::Module::Builder(ModuleName).get());
96-
}
97-
9894
namespace {
9995
class AnnotatingPrinter : public StreamPrinter {
10096
SourceTextInfo &Info;
@@ -383,7 +379,7 @@ SwiftInterfaceGenContext::create(StringRef DocumentName,
383379
CloseClangModuleFiles scopedCloseFiles(*Ctx.getClangModuleLoader());
384380

385381
// Load standard library so that Clang importer can use it.
386-
auto *Stdlib = getModuleByFullName(Ctx, Ctx.StdlibModuleName);
382+
auto *Stdlib = Ctx.getModuleByIdentifier(Ctx.StdlibModuleName);
387383
if (!Stdlib) {
388384
ErrMsg = "Could not load the stdlib module";
389385
return nullptr;
@@ -443,7 +439,7 @@ SwiftInterfaceGenContext::createForTypeInterface(CompilerInvocation Invocation,
443439
CloseClangModuleFiles scopedCloseFiles(*Ctx.getClangModuleLoader());
444440

445441
// Load standard library so that Clang importer can use it.
446-
auto *Stdlib = getModuleByFullName(Ctx, Ctx.StdlibModuleName);
442+
auto *Stdlib = Ctx.getModuleByIdentifier(Ctx.StdlibModuleName);
447443
if (!Stdlib) {
448444
ErrorMsg = "Could not load the stdlib module";
449445
return nullptr;

tools/SourceKit/lib/SwiftLang/SwiftIndexing.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,7 @@ static void indexModule(llvm::MemoryBuffer *Input,
214214
std::unique_ptr<ImplicitSerializedModuleLoader> Loader;
215215
ModuleDecl *Mod = nullptr;
216216
if (ModuleName == Ctx.StdlibModuleName.str()) {
217-
Mod = Ctx.getModule(ImportPath::Module::Builder(Ctx.StdlibModuleName)
218-
.get());
217+
Mod = Ctx.getModuleByIdentifier(Ctx.StdlibModuleName);
219218
} else {
220219
Loader = ImplicitSerializedModuleLoader::create(Ctx);
221220
auto Buf = std::unique_ptr<llvm::MemoryBuffer>(

tools/swift-ide-test/swift-ide-test.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2118,8 +2118,7 @@ static ModuleDecl *getModuleByFullName(ASTContext &Context, StringRef ModuleName
21182118
}
21192119

21202120
static ModuleDecl *getModuleByFullName(ASTContext &Context, Identifier ModuleName) {
2121-
ModuleDecl *Result = Context.getModule(
2122-
ImportPath::Module::Builder(ModuleName).get());
2121+
ModuleDecl *Result = Context.getModuleByIdentifier(ModuleName);
21232122
if (!Result || Result->failedToLoad())
21242123
return nullptr;
21252124
return Result;

0 commit comments

Comments
 (0)