Skip to content

Commit fd84ae9

Browse files
authored
Merge pull request #64367 from bnbarham/system-to-sdk
[SourceKit] Stop using `isSystemModule` to represent "non-user" modules
2 parents fe2f857 + eec2848 commit fd84ae9

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
@@ -658,10 +658,20 @@ class ModuleDecl
658658
bool isSystemModule() const {
659659
return Bits.ModuleDecl.IsSystemModule;
660660
}
661-
void setIsSystemModule(bool flag = true) {
662-
Bits.ModuleDecl.IsSystemModule = flag;
663-
}
661+
void setIsSystemModule(bool flag = true);
662+
663+
/// \returns true if this module is part of the stdlib or contained within
664+
/// the SDK. If no SDK was specified, also falls back to whether the module
665+
/// was specified as a system module (ie. it's on the system search path).
666+
bool isNonUserModule() const { return Bits.ModuleDecl.IsNonUserModule; }
667+
668+
private:
669+
/// Update whether this module is a non-user module, see \c isNonUserModule.
670+
/// \p newUnit is the added unit that caused this update, or \c nullptr if
671+
/// the update wasn't caused by adding a new unit.
672+
void updateNonUserModule(FileUnit *newUnit);
664673

674+
public:
665675
/// Returns true if the module was rebuilt from a module interface instead
666676
/// of being built from the full source.
667677
bool isBuiltFromInterface() const {
@@ -974,10 +984,6 @@ class ModuleDecl
974984
/// applicable.
975985
StringRef getModuleLoadedFilename() const;
976986

977-
/// \returns true if this module is defined under the SDK path.
978-
/// If no SDK path is defined, this always returns false.
979-
bool isSDKModule() const;
980-
981987
/// \returns true if this module is the "swift" standard library module.
982988
bool isStdlibModule() const;
983989

@@ -1102,6 +1108,7 @@ class ModuleEntity {
11021108
std::string getFullName(bool useRealNameIfAliased = false) const;
11031109

11041110
bool isSystemModule() const;
1111+
bool isNonUserModule() const;
11051112
bool isBuiltinModule() const;
11061113
const ModuleDecl *getAsSwiftModule() const;
11071114
const clang::Module *getAsClangModule() const;

lib/AST/ASTPrinter.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5757,8 +5757,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
57575757
// Don't print qualifiers for types from the standard library.
57585758
if (M->isStdlibModule() ||
57595759
M->getRealName() == M->getASTContext().Id_ObjectiveC ||
5760-
M->isSystemModule() ||
5761-
isLLDBExpressionModule(M))
5760+
M->isNonUserModule() || isLLDBExpressionModule(M))
57625761
return false;
57635762

57645763
// 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
@@ -605,6 +605,49 @@ ModuleDecl::ModuleDecl(Identifier name, ASTContext &ctx,
605605
Bits.ModuleDecl.HasHermeticSealAtLink = 0;
606606
Bits.ModuleDecl.IsConcurrencyChecked = 0;
607607
Bits.ModuleDecl.ObjCNameLookupCachePopulated = 0;
608+
Bits.ModuleDecl.IsNonUserModule = 0;
609+
}
610+
611+
void ModuleDecl::setIsSystemModule(bool flag) {
612+
Bits.ModuleDecl.IsSystemModule = flag;
613+
updateNonUserModule(/*newFile=*/nullptr);
614+
}
615+
616+
static bool prefixMatches(StringRef prefix, StringRef path) {
617+
auto i = llvm::sys::path::begin(prefix), e = llvm::sys::path::end(prefix);
618+
for (auto pi = llvm::sys::path::begin(path), pe = llvm::sys::path::end(path);
619+
i != e && pi != pe; ++i, ++pi) {
620+
if (*i != *pi)
621+
return false;
622+
}
623+
return i == e;
624+
}
625+
626+
void ModuleDecl::updateNonUserModule(FileUnit *newUnit) {
627+
if (isNonUserModule())
628+
return;
629+
630+
SearchPathOptions searchPathOpts = getASTContext().SearchPathOpts;
631+
StringRef sdkPath = searchPathOpts.getSDKPath();
632+
633+
if (isStdlibModule() || (sdkPath.empty() && isSystemModule())) {
634+
Bits.ModuleDecl.IsNonUserModule = true;
635+
return;
636+
}
637+
638+
// If we loaded a serialized module, check if it was compiler adjacent or in
639+
// the SDK.
640+
641+
auto *LF = dyn_cast_or_null<LoadedFile>(newUnit);
642+
if (!LF)
643+
return;
644+
645+
StringRef runtimePath = searchPathOpts.RuntimeResourcePath;
646+
StringRef modulePath = LF->getSourceFilename();
647+
if ((!runtimePath.empty() && prefixMatches(runtimePath, modulePath)) ||
648+
(!sdkPath.empty() && prefixMatches(sdkPath, modulePath))) {
649+
Bits.ModuleDecl.IsNonUserModule = true;
650+
}
608651
}
609652

610653
ImplicitImportList ModuleDecl::getImplicitImports() const {
@@ -626,6 +669,8 @@ void ModuleDecl::addFile(FileUnit &newFile) {
626669
cast<SourceFile>(newFile).Kind == SourceFileKind::SIL);
627670
Files.push_back(&newFile);
628671
clearLookupCache();
672+
673+
updateNonUserModule(&newFile);
629674
}
630675

631676
void ModuleDecl::addAuxiliaryFile(SourceFile &sourceFile) {
@@ -2278,23 +2323,6 @@ StringRef ModuleDecl::getModuleLoadedFilename() const {
22782323
return StringRef();
22792324
}
22802325

2281-
bool ModuleDecl::isSDKModule() const {
2282-
auto sdkPath = getASTContext().SearchPathOpts.getSDKPath();
2283-
if (sdkPath.empty())
2284-
return false;
2285-
2286-
auto modulePath = getModuleSourceFilename();
2287-
auto si = llvm::sys::path::begin(sdkPath),
2288-
se = llvm::sys::path::end(sdkPath);
2289-
for (auto mi = llvm::sys::path::begin(modulePath),
2290-
me = llvm::sys::path::end(modulePath);
2291-
si != se && mi != me; ++si, ++mi) {
2292-
if (*si != *mi)
2293-
return false;
2294-
}
2295-
return si == se;
2296-
}
2297-
22982326
bool ModuleDecl::isStdlibModule() const {
22992327
return !getParent() && getName() == getASTContext().StdlibModuleName;
23002328
}
@@ -4049,6 +4077,14 @@ bool ModuleEntity::isSystemModule() const {
40494077
return getClangModule(Mod)->IsSystem;
40504078
}
40514079

4080+
bool ModuleEntity::isNonUserModule() const {
4081+
assert(!Mod.isNull());
4082+
if (auto *SwiftMod = Mod.dyn_cast<const ModuleDecl *>())
4083+
return SwiftMod->isNonUserModule();
4084+
// TODO: Should handle clang modules as well
4085+
return getClangModule(Mod)->IsSystem;
4086+
}
4087+
40524088
bool ModuleEntity::isBuiltinModule() const {
40534089
assert(!Mod.isNull());
40544090
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

test/Interop/Cxx/symbolic-imports/indexing-emit-libcxx-symbolic-module-interface.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,10 @@ import CxxStdlib
3232
import CxxModule
3333

3434
// REMARK_NEW: remark: emitting symbolic interface at {{.*}}/interfaces/std-{{.*}}.pcm.symbolicswiftinterface{{$}}
35-
// REMARK_NEW-NEXT: remark: emitting symbolic interface at {{.*}}/interfaces/CxxModule-{{.*}}.pcm.symbolicswiftinterface{{$}}
36-
// REMARK_NEW-NEXT: EOF
35+
// REMARK_NEW: remark: emitting symbolic interface at {{.*}}/interfaces/CxxModule-{{.*}}.pcm.symbolicswiftinterface{{$}}
3736

38-
// REMARK_NO_UPDATE: remark: emitting symbolic interface at {{.*}}/interfaces/std-{{.*}}.pcm.symbolicswiftinterface; skipping because it's up to date{{$}}
39-
// REMARK_NO_UPDATE: remark: emitting symbolic interface at {{.*}}/interfaces/CxxModule-{{.*}}.pcm.symbolicswiftinterface; skipping because it's up to date{{$}}
37+
// REMARK_NO_UPDATE-NOT: remark: emitting symbolic interface at {{.*}}/interfaces/std-{{.*}}.pcm.symbolicswiftinterface{{$}}
38+
// REMARK_NO_UPDATE-NOT: remark: emitting symbolic interface at {{.*}}/interfaces/CxxModule-{{.*}}.pcm.symbolicswiftinterface{{$}}
4039

4140
// FILES: CxxModule-{{.*}}.pcm.symbolicswiftinterface
4241
// FILES: std-{{.*}}.pcm.symbolicswiftinterface

test/SourceKit/CursorInfo/cursor_stdlib.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ func foo3(a: Float, b: Bool) {}
3434
// CHECK-OVERLAY-NEXT: UInt
3535
// CHECK-OVERLAY-NEXT: $sSuD
3636
// CHECK-OVERLAY-NEXT: Foundation
37-
// CHECK-OVERLAY-NEXT: SYSTEM
3837
// CHECK-OVERLAY-NEXT: <Declaration>let NSUTF8StringEncoding: <Type usr="s:Su">UInt</Type></Declaration>
3938

4039
// RUN: %sourcekitd-test -req=cursor -pos=5:13 %s -- %s -target %target-triple %clang-importer-sdk-nosource -I %t | %FileCheck -check-prefix=CHECK-ITERATOR %s

0 commit comments

Comments
 (0)