Skip to content

[cxx-interop] Do not auto-complete unsafe underscored methods, part 2 #64453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/swift/AST/ClangModuleLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace swift {

class ConcreteDeclRef;
class Decl;
class FuncDecl;
class VarDecl;
class DeclContext;
class EffectiveClangContext;
Expand Down Expand Up @@ -267,6 +268,8 @@ class ClangModuleLoader : public ModuleLoader {

virtual bool isCXXMethodMutating(const clang::CXXMethodDecl *method) = 0;

virtual bool isUnsafeCXXMethod(const FuncDecl *func) = 0;

virtual Type importFunctionReturnType(const clang::FunctionDecl *clangDecl,
DeclContext *dc) = 0;

Expand Down
2 changes: 2 additions & 0 deletions include/swift/ClangImporter/ClangImporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,8 @@ class ClangImporter final : public ClangModuleLoader {

bool isCXXMethodMutating(const clang::CXXMethodDecl *method) override;

bool isUnsafeCXXMethod(const FuncDecl *func) override;

bool isAnnotatedWith(const clang::CXXMethodDecl *method, StringRef attr);

/// Find the lookup table that corresponds to the given Clang module.
Expand Down
18 changes: 5 additions & 13 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1952,19 +1952,11 @@ bool ShouldPrintChecker::shouldPrint(const Decl *D,
D->isPrivateStdlibDecl(!Options.SkipUnderscoredStdlibProtocols))
return false;

auto isUnsafeCxxMethod = [](const Decl* decl) {
if (!decl->hasClangNode()) return false;
auto clangDecl = decl->getClangNode().getAsDecl();
if (!clangDecl) return false;
auto cxxMethod = dyn_cast<clang::CXXMethodDecl>(clangDecl);
if (!cxxMethod) return false;
auto func = dyn_cast<FuncDecl>(decl);
if (!func || !func->hasName()) return false;
auto id = func->getBaseIdentifier().str();
return id.startswith("__") && id.endswith("Unsafe");
};
if (Options.SkipUnsafeCXXMethods && isUnsafeCxxMethod(D))
return false;
auto &ctx = D->getASTContext();
if (Options.SkipUnsafeCXXMethods)
if (auto func = dyn_cast<FuncDecl>(D))
if (ctx.getClangModuleLoader()->isUnsafeCXXMethod(func))
return false;

if (Options.SkipEmptyExtensionDecls && isa<ExtensionDecl>(D)) {
auto Ext = cast<ExtensionDecl>(D);
Expand Down
15 changes: 15 additions & 0 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6199,6 +6199,21 @@ bool ClangImporter::isCXXMethodMutating(const clang::CXXMethodDecl *method) {
return false;
}

bool ClangImporter::isUnsafeCXXMethod(const FuncDecl *func) {
if (!func->hasClangNode())
return false;
auto clangDecl = func->getClangNode().getAsDecl();
if (!clangDecl)
return false;
auto cxxMethod = dyn_cast<clang::CXXMethodDecl>(clangDecl);
if (!cxxMethod)
return false;
if (!func->hasName())
return false;
auto id = func->getBaseIdentifier().str();
return id.startswith("__") && id.endswith("Unsafe");
}

bool ClangImporter::isAnnotatedWith(const clang::CXXMethodDecl *method,
StringRef attr) {
return method->hasAttrs() &&
Expand Down
4 changes: 4 additions & 0 deletions lib/IDE/CompletionLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1471,6 +1471,10 @@ void CompletionLookup::addMethodCall(const FuncDecl *FD,
Builder.addFlair(CodeCompletionFlairBit::ExpressionSpecific);
};

// Do not add imported C++ methods that are treated as unsafe in Swift.
if (Importer->isUnsafeCXXMethod(FD))
return;

if (!AFT || IsImplicitlyCurriedInstanceMethod) {
addMethodImpl();
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=METHOD -I %S/Inputs | %FileCheck %s -check-prefix=CHECK-METHOD
// RUN: %target-swift-ide-test -code-completion -enable-experimental-cxx-interop -source-filename %s -code-completion-token=METHOD -I %S/Inputs | %FileCheck %s -check-prefix=CHECK-METHOD

import TypeClassification

func foo(x: HasMethodThatReturnsIterator) {
x.#^METHOD^#
}
// CHECK-METHOD: Begin completions
// CHECK-METHOD-NOT: getIterator
// CHECK-METHOD-NOT: Decl[InstanceMethod]/CurrNominal: getIterator
// CHECK-METHOD-NOT: __getIteratorUnsafe
// CHECK-METHOD-NOT: Decl[InstanceMethod]/CurrNominal: __getIteratorUnsafe
// CHECK-METHOD: End completions