Skip to content

Commit 8a7c970

Browse files
authored
Merge pull request swiftlang#29418 from slavapestov/lazy-member-loading-inconsistency
ClangImporter: Reconcile Clang declaration hidden-ness between loadAllMembers() and lazy loading
2 parents 462978f + 305620b commit 8a7c970

File tree

15 files changed

+53
-33
lines changed

15 files changed

+53
-33
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2867,22 +2867,23 @@ void ClangModuleUnit::lookupValue(DeclName name, NLKind lookupKind,
28672867
}
28682868
}
28692869

2870-
/// Determine whether the given Clang entry is visible.
2871-
///
2872-
/// FIXME: this is an elaborate hack to badly reflect Clang's
2873-
/// submodule visibility into Swift.
2874-
static bool isVisibleClangEntry(clang::ASTContext &ctx,
2875-
SwiftLookupTable::SingleEntry entry) {
2876-
if (auto clangDecl = entry.dyn_cast<clang::NamedDecl *>()) {
2877-
// For a declaration, check whether the declaration is hidden.
2878-
if (!clangDecl->isHidden()) return true;
2870+
bool ClangImporter::Implementation::isVisibleClangEntry(
2871+
const clang::NamedDecl *clangDecl) {
2872+
// For a declaration, check whether the declaration is hidden.
2873+
if (!clangDecl->isHidden()) return true;
28792874

2880-
// Is any redeclaration visible?
2881-
for (auto redecl : clangDecl->redecls()) {
2882-
if (!cast<clang::NamedDecl>(redecl)->isHidden()) return true;
2883-
}
2875+
// Is any redeclaration visible?
2876+
for (auto redecl : clangDecl->redecls()) {
2877+
if (!cast<clang::NamedDecl>(redecl)->isHidden()) return true;
2878+
}
28842879

2885-
return false;
2880+
return false;
2881+
}
2882+
2883+
bool ClangImporter::Implementation::isVisibleClangEntry(
2884+
SwiftLookupTable::SingleEntry entry) {
2885+
if (auto clangDecl = entry.dyn_cast<clang::NamedDecl *>()) {
2886+
return isVisibleClangEntry(clangDecl);
28862887
}
28872888

28882889
// If it's a macro from a module, check whether the module has been imported.
@@ -2917,15 +2918,13 @@ ClangModuleUnit::lookupNestedType(Identifier name,
29172918
if (!baseTypeContext)
29182919
return nullptr;
29192920

2920-
auto &clangCtx = owner.getClangASTContext();
2921-
29222921
// FIXME: This is very similar to what's in Implementation::lookupValue and
29232922
// Implementation::loadAllMembers.
29242923
SmallVector<TypeDecl *, 2> results;
29252924
for (auto entry : lookupTable->lookup(SerializedSwiftName(name.str()),
29262925
baseTypeContext)) {
29272926
// If the entry is not visible, skip it.
2928-
if (!isVisibleClangEntry(clangCtx, entry)) continue;
2927+
if (!owner.isVisibleClangEntry(entry)) continue;
29292928

29302929
auto *clangDecl = entry.dyn_cast<clang::NamedDecl *>();
29312930
if (!clangDecl)
@@ -2993,14 +2992,13 @@ void ClangImporter::loadExtensions(NominalTypeDecl *nominal,
29932992

29942993
// Dig through each of the Swift lookup tables, creating extensions
29952994
// where needed.
2996-
auto &clangCtx = Impl.getClangASTContext();
29972995
(void)Impl.forEachLookupTable([&](SwiftLookupTable &table) -> bool {
29982996
// FIXME: If we already looked at this for this generation,
29992997
// skip.
30002998

30012999
for (auto entry : table.allGlobalsAsMembersInContext(effectiveClangContext)) {
30023000
// If the entry is not visible, skip it.
3003-
if (!isVisibleClangEntry(clangCtx, entry)) continue;
3001+
if (!Impl.isVisibleClangEntry(entry)) continue;
30043002

30053003
if (auto decl = entry.dyn_cast<clang::NamedDecl *>()) {
30063004
// Import the context of this declaration, which has the
@@ -3581,7 +3579,7 @@ void ClangImporter::Implementation::lookupValue(
35813579

35823580
for (auto entry : table.lookup(name.getBaseName(), clangTU)) {
35833581
// If the entry is not visible, skip it.
3584-
if (!isVisibleClangEntry(clangCtx, entry)) continue;
3582+
if (!isVisibleClangEntry(entry)) continue;
35853583

35863584
ValueDecl *decl;
35873585
// If it's a Clang declaration, try to import it.
@@ -3685,11 +3683,9 @@ void ClangImporter::Implementation::lookupObjCMembers(
36853683
SwiftLookupTable &table,
36863684
DeclName name,
36873685
VisibleDeclConsumer &consumer) {
3688-
auto &clangCtx = getClangASTContext();
3689-
36903686
for (auto clangDecl : table.lookupObjCMembers(name.getBaseName())) {
36913687
// If the entry is not visible, skip it.
3692-
if (!isVisibleClangEntry(clangCtx, clangDecl)) continue;
3688+
if (!isVisibleClangEntry(clangDecl)) continue;
36933689

36943690
forEachDistinctName(clangDecl,
36953691
[&](ImportedName importedName,
@@ -3801,8 +3797,6 @@ ClangImporter::Implementation::loadNamedMembers(
38013797
auto table = findLookupTable(*CMO);
38023798
assert(table && "clang module without lookup table");
38033799

3804-
clang::ASTContext &clangCtx = getClangASTContext();
3805-
38063800
assert(isa<clang::ObjCContainerDecl>(CD) || isa<clang::NamespaceDecl>(CD));
38073801

38083802
ensureSuperclassMembersAreLoaded(dyn_cast<ClassDecl>(D));
@@ -3812,7 +3806,7 @@ ClangImporter::Implementation::loadNamedMembers(
38123806
effectiveClangContext)) {
38133807
if (!entry.is<clang::NamedDecl *>()) continue;
38143808
auto member = entry.get<clang::NamedDecl *>();
3815-
if (!isVisibleClangEntry(clangCtx, member)) continue;
3809+
if (!isVisibleClangEntry(member)) continue;
38163810

38173811
// Skip Decls from different clang::DeclContexts
38183812
if (member->getDeclContext() != CDC) continue;
@@ -3833,7 +3827,7 @@ ClangImporter::Implementation::loadNamedMembers(
38333827
effectiveClangContext)) {
38343828
if (!entry.is<clang::NamedDecl *>()) continue;
38353829
auto member = entry.get<clang::NamedDecl *>();
3836-
if (!isVisibleClangEntry(clangCtx, member)) continue;
3830+
if (!isVisibleClangEntry(member)) continue;
38373831

38383832
// Skip Decls from different clang::DeclContexts
38393833
if (member->getDeclContext() != CDC) continue;

lib/ClangImporter/ImportDecl.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8670,7 +8670,8 @@ void ClangImporter::Implementation::collectMembersToAdd(
86708670
for (const clang::Decl *m : objcContainer->decls()) {
86718671
auto nd = dyn_cast<clang::NamedDecl>(m);
86728672
if (nd && nd == nd->getCanonicalDecl() &&
8673-
nd->getDeclContext() == objcContainer)
8673+
nd->getDeclContext() == objcContainer &&
8674+
isVisibleClangEntry(nd))
86748675
insertMembersAndAlternates(nd, members);
86758676
}
86768677

lib/ClangImporter/ImporterImpl.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,6 +1299,13 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
12991299
/// false otherwise.
13001300
bool forEachLookupTable(llvm::function_ref<bool(SwiftLookupTable &table)> fn);
13011301

1302+
/// Determine whether the given Clang entry is visible.
1303+
///
1304+
/// FIXME: this is an elaborate hack to badly reflect Clang's
1305+
/// submodule visibility into Swift.
1306+
bool isVisibleClangEntry(const clang::NamedDecl *clangDecl);
1307+
bool isVisibleClangEntry(SwiftLookupTable::SingleEntry entry);
1308+
13021309
/// Look for namespace-scope values with the given name in the given
13031310
/// Swift lookup table.
13041311
void lookupValue(SwiftLookupTable &table, DeclName name,

test/ClangImporter/Inputs/SwiftPrivateAttr.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Foundation
1+
@_exported import Foundation
22

33
protocol __PrivProto {
44
}

test/ClangImporter/Inputs/custom-modules/module.map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ module SubclassExistentialsExtra {
165165

166166
module SwiftPrivateAttr {
167167
header "SwiftPrivateAttr.h"
168+
export *
168169
}
169170

170171
module TestProtocols { header "Protocols.h" }

test/IDE/Inputs/custom-modules/ImportAsMemberC.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@import ObjectiveC;
2+
13
typedef const void *CFTypeRef __attribute__((objc_bridge(id)));
24

35
typedef const struct __attribute__((objc_bridge(id))) CCPowerSupply *CCPowerSupplyRef;

test/IDE/Inputs/custom-modules/module.map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ module ImportAsMember {
3535
module C {
3636
requires objc
3737
header "ImportAsMemberC.h"
38+
export *
3839
}
3940

4041
module APINotes {

test/IDE/import_as_member.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@
106106
// PRINT-APINOTES-4: @available(swift, obsoleted: 3, renamed: "Struct1.NewApiNoteType")
107107
// PRINT-APINOTES-4-NEXT: typealias IAMStruct1APINoteType = Struct1.NewApiNoteType
108108

109+
#if canImport(Foundation)
110+
import Foundation
111+
#endif
109112
import ImportAsMember.A
110113
import ImportAsMember.B
111114
import ImportAsMember.APINotes
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,40 @@
11

22
module gizmo {
33
header "Gizmo.h"
4+
export *
45
}
56

67
module SRoA {
78
header "SRoA.h"
9+
export *
810
}
911

1012
module ObjectiveC {
1113
header "BridgeTestObjectiveC.h"
14+
export *
1215
}
1316

1417
module CoreCooling {
1518
header "CoreCooling.h"
19+
export *
1620
}
1721

1822
module Foundation {
1923
header "BridgeTestFoundation.h"
24+
export *
2025
}
2126

2227
module CoreFoundation {
2328
header "BridgeTestCoreFoundation.h"
29+
export *
2430
}
2531

2632
module ObjCInterop {
2733
header "ObjCInterop.h"
34+
export *
2835
}
2936

3037
module error_domains {
3138
header "error_domains.h"
39+
export *
3240
}

test/SILGen/import_as_member.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// REQUIRES: objc_interop
33
import ImportAsMember.A
44
import ImportAsMember.Class
5+
import Foundation
56

67
public func returnGlobalVar() -> Double {
78
return Struct1.globalVar
@@ -65,7 +66,6 @@ extension SomeClass {
6566
}
6667

6768
public func useSpecialInit() -> Struct1 {
68-
// FIXME: the below triggers an assert, due to number or params mismatch
69-
// return Struct1(specialLabel:())
69+
return Struct1(specialLabel:())
7070
}
7171

test/api-digester/Inputs/Foo-new-version/foo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#import <Foundation.h>
1+
@import ObjectiveC;
22

33
@protocol ObjcProt
44
-(void) someFunctionFromProt;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
module Foo {
22
header "foo.h"
3+
export *
34
}

test/api-digester/Inputs/Foo/foo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#import <Foundation.h>
1+
@import ObjectiveC;
22

33
@protocol ObjcProt
44
-(void) someFunctionFromProt;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
module Foo {
22
header "foo.h"
3+
export *
34
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
module ObjCClasses {
22
header "ObjCClasses.h"
3+
export *
34
}

0 commit comments

Comments
 (0)