Skip to content

Commit eec2848

Browse files
committed
[SourceKit] Stop using isSystemModule to represent "non-user" modules
Rather than using `ModuleDecl::isSystemModule()` to determine whether a module is not a user module, instead check whether the module was defined adjacent to the compiler or if it's part of the SDK. If no SDK path was given, then `isSystemModule` is still used as a fallback. Resolves rdar://89253201.
1 parent b7a2fa2 commit eec2848

20 files changed

+249
-202
lines changed

include/swift/AST/Decl.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
630630
HasAnyUnavailableValues : 1
631631
);
632632

633-
SWIFT_INLINE_BITFIELD(ModuleDecl, TypeDecl, 1+1+1+1+1+1+1+1+1+1+1+1+1+1+1,
633+
SWIFT_INLINE_BITFIELD(ModuleDecl, TypeDecl, 1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1,
634634
/// If the module is compiled as static library.
635635
StaticLibrary : 1,
636636

@@ -680,7 +680,11 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
680680

681681
/// If the map from @objc provided name to top level swift::Decl in this
682682
/// module is populated
683-
ObjCNameLookupCachePopulated : 1
683+
ObjCNameLookupCachePopulated : 1,
684+
685+
/// Whether the module is contained in the SDK or stdlib, or its a system
686+
/// module and no SDK was specified.
687+
IsNonUserModule : 1
684688
);
685689

686690
SWIFT_INLINE_BITFIELD(PrecedenceGroupDecl, Decl, 1+2,

include/swift/AST/Module.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -630,10 +630,20 @@ class ModuleDecl
630630
bool isSystemModule() const {
631631
return Bits.ModuleDecl.IsSystemModule;
632632
}
633-
void setIsSystemModule(bool flag = true) {
634-
Bits.ModuleDecl.IsSystemModule = flag;
635-
}
633+
void setIsSystemModule(bool flag = true);
634+
635+
/// \returns true if this module is part of the stdlib or contained within
636+
/// the SDK. If no SDK was specified, also falls back to whether the module
637+
/// was specified as a system module (ie. it's on the system search path).
638+
bool isNonUserModule() const { return Bits.ModuleDecl.IsNonUserModule; }
639+
640+
private:
641+
/// Update whether this module is a non-user module, see \c isNonUserModule.
642+
/// \p newUnit is the added unit that caused this update, or \c nullptr if
643+
/// the update wasn't caused by adding a new unit.
644+
void updateNonUserModule(FileUnit *newUnit);
636645

646+
public:
637647
/// Returns true if the module was rebuilt from a module interface instead
638648
/// of being built from the full source.
639649
bool isBuiltFromInterface() const {
@@ -946,10 +956,6 @@ class ModuleDecl
946956
/// applicable.
947957
StringRef getModuleLoadedFilename() const;
948958

949-
/// \returns true if this module is defined under the SDK path.
950-
/// If no SDK path is defined, this always returns false.
951-
bool isSDKModule() const;
952-
953959
/// \returns true if this module is the "swift" standard library module.
954960
bool isStdlibModule() const;
955961

@@ -1074,6 +1080,7 @@ class ModuleEntity {
10741080
std::string getFullName(bool useRealNameIfAliased = false) const;
10751081

10761082
bool isSystemModule() const;
1083+
bool isNonUserModule() const;
10771084
bool isBuiltinModule() const;
10781085
const ModuleDecl *getAsSwiftModule() const;
10791086
const clang::Module *getAsClangModule() const;

lib/AST/ASTPrinter.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5730,8 +5730,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
57305730
// Don't print qualifiers for types from the standard library.
57315731
if (M->isStdlibModule() ||
57325732
M->getRealName() == M->getASTContext().Id_ObjectiveC ||
5733-
M->isSystemModule() ||
5734-
isLLDBExpressionModule(M))
5733+
M->isNonUserModule() || isLLDBExpressionModule(M))
57355734
return false;
57365735

57375736
// Don't print qualifiers for imported types.

lib/AST/Module.cpp

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,49 @@ ModuleDecl::ModuleDecl(Identifier name, ASTContext &ctx,
611611
Bits.ModuleDecl.HasHermeticSealAtLink = 0;
612612
Bits.ModuleDecl.IsConcurrencyChecked = 0;
613613
Bits.ModuleDecl.ObjCNameLookupCachePopulated = 0;
614+
Bits.ModuleDecl.IsNonUserModule = 0;
615+
}
616+
617+
void ModuleDecl::setIsSystemModule(bool flag) {
618+
Bits.ModuleDecl.IsSystemModule = flag;
619+
updateNonUserModule(/*newFile=*/nullptr);
620+
}
621+
622+
static bool prefixMatches(StringRef prefix, StringRef path) {
623+
auto i = llvm::sys::path::begin(prefix), e = llvm::sys::path::end(prefix);
624+
for (auto pi = llvm::sys::path::begin(path), pe = llvm::sys::path::end(path);
625+
i != e && pi != pe; ++i, ++pi) {
626+
if (*i != *pi)
627+
return false;
628+
}
629+
return i == e;
630+
}
631+
632+
void ModuleDecl::updateNonUserModule(FileUnit *newUnit) {
633+
if (isNonUserModule())
634+
return;
635+
636+
SearchPathOptions searchPathOpts = getASTContext().SearchPathOpts;
637+
StringRef sdkPath = searchPathOpts.getSDKPath();
638+
639+
if (isStdlibModule() || (sdkPath.empty() && isSystemModule())) {
640+
Bits.ModuleDecl.IsNonUserModule = true;
641+
return;
642+
}
643+
644+
// If we loaded a serialized module, check if it was compiler adjacent or in
645+
// the SDK.
646+
647+
auto *LF = dyn_cast_or_null<LoadedFile>(newUnit);
648+
if (!LF)
649+
return;
650+
651+
StringRef runtimePath = searchPathOpts.RuntimeResourcePath;
652+
StringRef modulePath = LF->getSourceFilename();
653+
if ((!runtimePath.empty() && prefixMatches(runtimePath, modulePath)) ||
654+
(!sdkPath.empty() && prefixMatches(sdkPath, modulePath))) {
655+
Bits.ModuleDecl.IsNonUserModule = true;
656+
}
614657
}
615658

616659
ImplicitImportList ModuleDecl::getImplicitImports() const {
@@ -632,6 +675,8 @@ void ModuleDecl::addFile(FileUnit &newFile) {
632675
cast<SourceFile>(newFile).Kind == SourceFileKind::SIL);
633676
Files.push_back(&newFile);
634677
clearLookupCache();
678+
679+
updateNonUserModule(&newFile);
635680
}
636681

637682
void ModuleDecl::addAuxiliaryFile(SourceFile &sourceFile) {
@@ -2284,23 +2329,6 @@ StringRef ModuleDecl::getModuleLoadedFilename() const {
22842329
return StringRef();
22852330
}
22862331

2287-
bool ModuleDecl::isSDKModule() const {
2288-
auto sdkPath = getASTContext().SearchPathOpts.getSDKPath();
2289-
if (sdkPath.empty())
2290-
return false;
2291-
2292-
auto modulePath = getModuleSourceFilename();
2293-
auto si = llvm::sys::path::begin(sdkPath),
2294-
se = llvm::sys::path::end(sdkPath);
2295-
for (auto mi = llvm::sys::path::begin(modulePath),
2296-
me = llvm::sys::path::end(modulePath);
2297-
si != se && mi != me; ++si, ++mi) {
2298-
if (*si != *mi)
2299-
return false;
2300-
}
2301-
return si == se;
2302-
}
2303-
23042332
bool ModuleDecl::isStdlibModule() const {
23052333
return !getParent() && getName() == getASTContext().StdlibModuleName;
23062334
}
@@ -4066,6 +4094,14 @@ bool ModuleEntity::isSystemModule() const {
40664094
return getClangModule(Mod)->IsSystem;
40674095
}
40684096

4097+
bool ModuleEntity::isNonUserModule() const {
4098+
assert(!Mod.isNull());
4099+
if (auto *SwiftMod = Mod.dyn_cast<const ModuleDecl *>())
4100+
return SwiftMod->isNonUserModule();
4101+
// TODO: Should handle clang modules as well
4102+
return getClangModule(Mod)->IsSystem;
4103+
}
4104+
40694105
bool ModuleEntity::isBuiltinModule() const {
40704106
assert(!Mod.isNull());
40714107
if (auto SwiftMod = Mod.dyn_cast<const ModuleDecl*>())

lib/IDE/CodeCompletionResult.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ ContextFreeCodeCompletionResult::getCodeCompletionDeclKind(const Decl *D) {
293293
}
294294

295295
bool ContextFreeCodeCompletionResult::getDeclIsSystem(const Decl *D) {
296-
return D->getModuleContext()->isSystemModule();
296+
return D->getModuleContext()->isNonUserModule();
297297
}
298298

299299
// MARK: - CodeCompletionResult

lib/IDE/CodeCompletionStringPrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ void CodeCompletionStringPrinter::printTypeRef(Type T, const TypeDecl *TD,
131131
Identifier Name,
132132
PrintNameContext NameContext) {
133133

134-
NextChunkKind = TD->getModuleContext()->isSystemModule()
134+
NextChunkKind = TD->getModuleContext()->isNonUserModule()
135135
? ChunkKind::TypeIdSystem
136136
: ChunkKind::TypeIdUser;
137137

lib/Index/Index.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,7 +1073,7 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
10731073

10741074
void IndexSwiftASTWalker::visitDeclContext(DeclContext *Context) {
10751075
IsModuleFile = false;
1076-
isSystemModule = Context->getParentModule()->isSystemModule();
1076+
isSystemModule = Context->getParentModule()->isNonUserModule();
10771077
auto accessor = dyn_cast<AccessorDecl>(Context);
10781078
if (accessor)
10791079
ManuallyVisitedAccessorStack.push_back(accessor);
@@ -1101,7 +1101,7 @@ void IndexSwiftASTWalker::visitModule(ModuleDecl &Mod) {
11011101
walk(*SrcFile);
11021102
} else {
11031103
IsModuleFile = true;
1104-
isSystemModule = Mod.isSystemModule();
1104+
isSystemModule = Mod.isNonUserModule();
11051105
if (!handleSourceOrModuleFile(Mod))
11061106
return;
11071107
walk(Mod);
@@ -1178,7 +1178,7 @@ bool IndexSwiftASTWalker::visitImports(
11781178
ModuleName = Declaring->getNameStr();
11791179

11801180
if (!IdxConsumer.startDependency(ModuleName, Path, IsClangModule,
1181-
Mod->isSystemModule()))
1181+
Mod->isNonUserModule()))
11821182
return false;
11831183
if (!IsClangModule)
11841184
if (!visitImports(*Mod, Visited))

lib/Index/IndexRecord.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -640,8 +640,9 @@ static void addModuleDependencies(ArrayRef<ImportedModule> imports,
640640
bool withoutUnitName = true;
641641
if (FU->getKind() == FileUnitKind::ClangModule) {
642642
auto clangModUnit = cast<ClangModuleUnit>(LFU);
643-
bool shouldIndexModule = indexClangModules &&
644-
(!clangModUnit->isSystemModule() || indexSystemModules);
643+
bool shouldIndexModule =
644+
indexClangModules &&
645+
(!mod->isNonUserModule() || indexSystemModules);
645646
withoutUnitName = !shouldIndexModule;
646647
if (auto clangMod = clangModUnit->getUnderlyingClangModule()) {
647648
moduleName = clangMod->getTopLevelModuleName();
@@ -662,10 +663,7 @@ static void addModuleDependencies(ArrayRef<ImportedModule> imports,
662663
// We don't officially support binary swift modules, so normally
663664
// the index data for user modules would get generated while
664665
// building them.
665-
bool isDistributedModule = mod->isSDKModule() ||
666-
mod->getASTContext().SearchPathOpts.getSDKPath().empty();
667-
if (mod->isSystemModule() && indexSystemModules &&
668-
(isDistributedModule || mod->isStdlibModule()) &&
666+
if (mod->isNonUserModule() && indexSystemModules &&
669667
(!skipStdlib || !mod->isStdlibModule())) {
670668
emitDataForSwiftSerializedModule(mod, indexStorePath,
671669
indexClangModules,
@@ -697,7 +695,7 @@ static void addModuleDependencies(ArrayRef<ImportedModule> imports,
697695
}
698696
clang::index::writer::OpaqueModule opaqMod =
699697
moduleNameScratch.createString(moduleName);
700-
unitWriter.addASTFileDependency(*F, mod->isSystemModule(), opaqMod,
698+
unitWriter.addASTFileDependency(*F, mod->isNonUserModule(), opaqMod,
701699
withoutUnitName);
702700

703701
break;
@@ -833,7 +831,7 @@ emitDataForSwiftSerializedModule(ModuleDecl *module,
833831
}
834832

835833
auto &fileMgr = clangCI.getFileManager();
836-
bool isSystem = module->isSystemModule();
834+
bool isSystem = module->isNonUserModule();
837835
// FIXME: Get real values for the following.
838836
StringRef swiftVersion;
839837
StringRef sysrootPath = clangCI.getHeaderSearchOpts().Sysroot;
@@ -848,14 +846,13 @@ emitDataForSwiftSerializedModule(ModuleDecl *module,
848846
targetTriple, sysrootPath, clangRemapper, getModuleInfoFromOpaqueModule);
849847

850848
auto FE = fileMgr.getFile(filename);
851-
bool isSystemModule = module->isSystemModule();
852849
for (auto &pair : records) {
853850
std::string &recordFile = pair.first;
854851
std::string &groupName = pair.second;
855852
if (recordFile.empty())
856853
continue;
857854
clang::index::writer::OpaqueModule mod = &groupName;
858-
unitWriter.addRecordFile(recordFile, *FE, isSystemModule, mod);
855+
unitWriter.addRecordFile(recordFile, *FE, isSystem, mod);
859856
}
860857

861858
SmallVector<ImportedModule, 8> imports;
@@ -887,7 +884,7 @@ recordSourceFileUnit(SourceFile *primarySourceFile, StringRef indexUnitToken,
887884
DiagnosticEngine &diags) {
888885
auto &fileMgr = clangCI.getFileManager();
889886
auto *module = primarySourceFile->getParentModule();
890-
bool isSystem = module->isSystemModule();
887+
bool isSystem = module->isNonUserModule();
891888
auto mainFile = fileMgr.getFile(primarySourceFile->getFilename());
892889
auto clangRemapper = pathRemapper.asClangPathRemapper();
893890
// FIXME: Get real values for the following.
@@ -920,7 +917,7 @@ recordSourceFileUnit(SourceFile *primarySourceFile, StringRef indexUnitToken,
920917
auto file = fileMgr.getFile(filename);
921918
unitWriter.addRecordFile(
922919
recordFile, file ? *file : nullptr,
923-
module->isSystemModule(), /*Module=*/nullptr);
920+
module->isNonUserModule(), /*Module=*/nullptr);
924921
});
925922

926923
std::string error;

lib/Refactoring/Refactoring.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -611,15 +611,15 @@ class TextReplacementsRenamer : public Renamer {
611611
};
612612

613613
static const ValueDecl *getRelatedSystemDecl(const ValueDecl *VD) {
614-
if (VD->getModuleContext()->isSystemModule())
614+
if (VD->getModuleContext()->isNonUserModule())
615615
return VD;
616616
for (auto *Req : VD->getSatisfiedProtocolRequirements()) {
617-
if (Req->getModuleContext()->isSystemModule())
617+
if (Req->getModuleContext()->isNonUserModule())
618618
return Req;
619619
}
620620
for (auto Over = VD->getOverriddenDecl(); Over;
621621
Over = Over->getOverriddenDecl()) {
622-
if (Over->getModuleContext()->isSystemModule())
622+
if (Over->getModuleContext()->isNonUserModule())
623623
return Over;
624624
}
625625
return nullptr;

test/Index/Store/ignore-system-clang-modules.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22

33
// Make a basic clang framework to import
44
//
5-
// RUN: %empty-directory(%t/MySystemFramework.framework/Headers)
6-
// RUN: %empty-directory(%t/MySystemFramework.framework/Modules)
7-
// RUN: echo 'void someSystemFunc(int arg);' > %t/MySystemFramework.framework/Headers/MySystemFramework.h
8-
// RUN: echo 'framework module MySystemFramework { umbrella header "MySystemFramework.h" export * }' > %t/MySystemFramework.framework/Modules/module.modulemap
5+
// RUN: %empty-directory(%t/sdk/MySystemFramework.framework/Headers)
6+
// RUN: %empty-directory(%t/sdk/MySystemFramework.framework/Modules)
7+
// RUN: echo 'void someSystemFunc(int arg);' > %t/sdk/MySystemFramework.framework/Headers/MySystemFramework.h
8+
// RUN: echo 'framework module MySystemFramework { umbrella header "MySystemFramework.h" export * }' > %t/sdk/MySystemFramework.framework/Modules/module.modulemap
99

1010
import MySystemFramework
1111
someSystemFunc(2)
1212

1313
// Index this file with and without ignoring system frameworks
1414
//
15-
// RUN: %target-swiftc_driver -index-store-path %t/idx1 -o %t/file.o -Fsystem %t -typecheck %s
16-
// RUN: %target-swiftc_driver -index-store-path %t/idx2 -o %t/file.o -index-ignore-system-modules -Fsystem %t -typecheck %s
15+
// RUN: %target-swiftc_driver -index-store-path %t/idx1 -o %t/file.o -Fsystem %t/sdk -sdk %t/sdk -typecheck %s
16+
// RUN: %target-swiftc_driver -index-store-path %t/idx2 -o %t/file.o -index-ignore-system-modules -Fsystem %t/sdk -sdk %t/sdk -typecheck %s
1717
// RUN: c-index-test core -print-unit %t/idx1 | %FileCheck --check-prefixes=ALLOWSYSTEM,BOTH %s
1818
// RUN: c-index-test core -print-unit %t/idx2 | %FileCheck --check-prefixes=IGNORESYSTEM,BOTH %s
1919

0 commit comments

Comments
 (0)