Skip to content

Commit e996f0c

Browse files
akyrtzitkremenek
authored andcommitted
[IDE/completion] Hide clang decls that are marked as 'swift_private', from the code-completion results. (#2762)
1 parent e6d1e01 commit e996f0c

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed

lib/IDE/CodeCompletion.cpp

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,29 @@ void getSwiftDocKeyword(const Decl* D, CommandWordsPairs &Words) {
278278
} // end namespace markup
279279
} // end namespace swift
280280

281+
static bool shouldHideDeclFromCompletionResults(const ValueDecl *D) {
282+
// Hide private stdlib declarations.
283+
if (D->isPrivateStdlibDecl(/*whitelistProtocols*/false) ||
284+
// ShowInInterfaceAttr is for decls to show in interface as exception but
285+
// they are not intended to be used directly.
286+
D->getAttrs().hasAttribute<ShowInInterfaceAttr>())
287+
return true;
288+
289+
if (AvailableAttr::isUnavailable(D))
290+
return true;
291+
292+
if (auto *ClangD = D->getClangDecl()) {
293+
if (ClangD->hasAttr<clang::SwiftPrivateAttr>())
294+
return true;
295+
}
296+
297+
// Hide editor placeholders.
298+
if (D->getName().isEditorPlaceholder())
299+
return true;
300+
301+
return false;
302+
}
303+
281304
typedef llvm::function_ref<bool(ValueDecl*, DeclVisibilityKind)> DeclFilter;
282305
DeclFilter DefaultFilter = [] (ValueDecl* VD, DeclVisibilityKind Kind) {return true;};
283306
DeclFilter KeyPathFilter = [](ValueDecl* decl, DeclVisibilityKind) -> bool {
@@ -1938,7 +1961,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
19381961
if (!VD->hasName() ||
19391962
!VD->isUserAccessible() ||
19401963
(VD->hasAccessibility() && !VD->isAccessibleFrom(CurrDeclContext)) ||
1941-
AvailableAttr::isUnavailable(VD))
1964+
shouldHideDeclFromCompletionResults(VD))
19421965
return;
19431966

19441967
StringRef Name = VD->getName().get();
@@ -2430,8 +2453,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
24302453
if (CurrDeclContext->lookupQualified(type, Ctx.Id_init, NL_QualifiedDefault,
24312454
TypeResolver.get(), initializers)) {
24322455
for (auto *init : initializers) {
2433-
if (init->isPrivateStdlibDecl(/*whitelistProtocols*/ false) ||
2434-
AvailableAttr::isUnavailable(init))
2456+
if (shouldHideDeclFromCompletionResults(init))
24352457
continue;
24362458
addConstructorCall(cast<ConstructorDecl>(init), Reason, None, name);
24372459
}
@@ -2526,7 +2548,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
25262548
bool HasTypeContext) {
25272549
if (!EED->hasName() ||
25282550
(EED->hasAccessibility() && !EED->isAccessibleFrom(CurrDeclContext)) ||
2529-
AvailableAttr::isUnavailable(EED))
2551+
shouldHideDeclFromCompletionResults(EED))
25302552
return;
25312553
CommandWordsPairs Pairs;
25322554
CodeCompletionResultBuilder Builder(
@@ -2628,15 +2650,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
26282650

26292651
// Implement swift::VisibleDeclConsumer.
26302652
void foundDecl(ValueDecl *D, DeclVisibilityKind Reason) override {
2631-
// Hide private stdlib declarations.
2632-
if (D->isPrivateStdlibDecl(/*whitelistProtocols*/false) ||
2633-
D->getAttrs().hasAttribute<ShowInInterfaceAttr>())
2634-
return;
2635-
if (AvailableAttr::isUnavailable(D))
2636-
return;
2637-
2638-
// Hide editor placeholders.
2639-
if (D->getName().isEditorPlaceholder())
2653+
if (shouldHideDeclFromCompletionResults(D))
26402654
return;
26412655

26422656
if (IsKeyPathExpr && !KeyPathFilter(D, Reason))
@@ -4008,7 +4022,7 @@ class CompletionOverrideLookup : public swift::VisibleDeclConsumer {
40084022
return;
40094023
}
40104024

4011-
if (AvailableAttr::isUnavailable(D))
4025+
if (shouldHideDeclFromCompletionResults(D))
40124026
return;
40134027

40144028
if (D->getAttrs().hasAttribute<FinalAttr>())

test/IDE/Inputs/header.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ void doSomethingInHead(int arg);
1313
@interface BaseInHead(SomeCategory)
1414
-(void)doItInCategory;
1515
@end
16+
17+
void function_as_swift_private(void) __attribute__((swift_private));

test/IDE/complete_with_header_import.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
func foo() {
77
#^TOP^#
8+
// CHECK-TOP-NOT: function_as_swift_private
89
// CHECK-TOP: Decl[FreeFunction]/OtherModule[__ObjC]: doSomethingInHead({#(arg): Int32#})[#Void#]{{; name=.+$}}
910
}
1011

0 commit comments

Comments
 (0)