Skip to content

Commit 201a3b1

Browse files
authored
Merge pull request #64287 from apple/egorzhdan/do-not-auto-complete-unsafe
[cxx-interop] Do not auto-complete unsafe underscored methods
2 parents 8862b1b + 8326c84 commit 201a3b1

File tree

5 files changed

+39
-0
lines changed

5 files changed

+39
-0
lines changed

include/swift/AST/PrintOptions.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@ struct PrintOptions {
257257
/// Protocols marked with @_show_in_interface are still printed.
258258
bool SkipUnderscoredStdlibProtocols = false;
259259

260+
/// Whether to skip unsafe C++ class methods that were renamed
261+
/// (e.g. __fooUnsafe). See IsSafeUseOfCxxDecl.
262+
bool SkipUnsafeCXXMethods = false;
263+
260264
/// Whether to skip extensions that don't add protocols or no members.
261265
bool SkipEmptyExtensionDecls = true;
262266

@@ -642,6 +646,7 @@ struct PrintOptions {
642646
result.SkipSwiftPrivateClangDecls = true;
643647
result.SkipPrivateStdlibDecls = true;
644648
result.SkipUnderscoredStdlibProtocols = true;
649+
result.SkipUnsafeCXXMethods = true;
645650
result.SkipDeinit = true;
646651
result.EmptyLineBetweenMembers = true;
647652
result.CascadeDocComment = true;
@@ -756,6 +761,7 @@ struct PrintOptions {
756761
PO.PrintDocumentationComments = false;
757762
PO.ExcludeAttrList.push_back(DAK_Available);
758763
PO.SkipPrivateStdlibDecls = true;
764+
PO.SkipUnsafeCXXMethods = true;
759765
PO.ExplodeEnumCaseDecls = true;
760766
PO.ShouldQualifyNestedDeclarations = QualifyNestedDeclarations::TypesOnly;
761767
PO.PrintParameterSpecifiers = true;

lib/AST/ASTPrinter.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1952,6 +1952,20 @@ bool ShouldPrintChecker::shouldPrint(const Decl *D,
19521952
D->isPrivateStdlibDecl(!Options.SkipUnderscoredStdlibProtocols))
19531953
return false;
19541954

1955+
auto isUnsafeCxxMethod = [](const Decl* decl) {
1956+
if (!decl->hasClangNode()) return false;
1957+
auto clangDecl = decl->getClangNode().getAsDecl();
1958+
if (!clangDecl) return false;
1959+
auto cxxMethod = dyn_cast<clang::CXXMethodDecl>(clangDecl);
1960+
if (!cxxMethod) return false;
1961+
auto func = dyn_cast<FuncDecl>(decl);
1962+
if (!func || !func->hasName()) return false;
1963+
auto id = func->getBaseIdentifier().str();
1964+
return id.startswith("__") && id.endswith("Unsafe");
1965+
};
1966+
if (Options.SkipUnsafeCXXMethods && isUnsafeCxxMethod(D))
1967+
return false;
1968+
19551969
if (Options.SkipEmptyExtensionDecls && isa<ExtensionDecl>(D)) {
19561970
auto Ext = cast<ExtensionDecl>(D);
19571971
// If the extension doesn't add protocols or has no members that we should
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=METHOD -I %S/Inputs | %FileCheck %s -check-prefix=CHECK-METHOD
2+
3+
import TypeClassification
4+
5+
func foo(x: HasMethodThatReturnsIterator) {
6+
x.#^METHOD^#
7+
}
8+
// CHECK-METHOD-NOT: getIterator
9+
// CHECK-METHOD-NOT: __getIteratorUnsafe

test/Interop/Cxx/class/type-classification-module-interface.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: %target-swift-ide-test -print-module -module-to-print=TypeClassification -I %S/Inputs -source-filename=x -enable-experimental-cxx-interop | %FileCheck %s
2+
// RUN: %target-swift-ide-test -print-module -skip-unsafe-cxx-methods -module-to-print=TypeClassification -I %S/Inputs -source-filename=x -enable-experimental-cxx-interop | %FileCheck %s -check-prefix=CHECK-SKIP-UNSAFE
23

34
// Make sure we don't import objects that we can't copy or destroy.
45
// CHECK-NOT: StructWithPrivateDefaultedCopyConstructor
@@ -24,11 +25,13 @@
2425

2526
// CHECK: struct HasMethodThatReturnsIterator {
2627
// CHECK: func __getIteratorUnsafe() -> Iterator
28+
// CHECK-SKIP-UNSAFE-NOT: func __getIteratorUnsafe() -> Iterator
2729
// CHECK: }
2830

2931
// CHECK: struct IteratorBox {
3032
// CHECK: }
3133

3234
// CHECK: struct HasMethodThatReturnsIteratorBox {
3335
// CHECK: func __getIteratorBoxUnsafe() -> IteratorBox
36+
// CHECK-SKIP-UNSAFE-NOT: func __getIteratorBoxUnsafe() -> IteratorBox
3437
// CHECK: }

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,12 @@ SkipUnderscoredStdlibProtocols("skip-underscored-stdlib-protocols",
681681
llvm::cl::cat(Category),
682682
llvm::cl::init(false));
683683

684+
static llvm::cl::opt<bool>
685+
SkipUnsafeCXXMethods("skip-unsafe-cxx-methods",
686+
llvm::cl::desc("Don't print unsafe C++ class methods that were renamed as unsafe"),
687+
llvm::cl::cat(Category),
688+
llvm::cl::init(false));
689+
684690
static llvm::cl::opt<bool>
685691
SkipDocumentationComments("skip-print-doc-comments",
686692
llvm::cl::desc("Don't print documentation comments from clang module headers"),
@@ -4506,6 +4512,7 @@ int main(int argc, char *argv[]) {
45064512
PrintOpts.PrintDocumentationComments = !options::SkipDocumentationComments;
45074513
PrintOpts.PrintRegularClangComments = options::PrintRegularComments;
45084514
PrintOpts.SkipPrivateStdlibDecls = options::SkipPrivateStdlibDecls;
4515+
PrintOpts.SkipUnsafeCXXMethods = options::SkipUnsafeCXXMethods;
45094516
PrintOpts.SkipUnavailable = options::SkipUnavailable;
45104517
PrintOpts.SkipDeinit = options::SkipDeinit;
45114518
PrintOpts.SkipImports = options::SkipImports;

0 commit comments

Comments
 (0)