aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Sema/SemaCodeComplete.cpp
diff options
context:
space:
mode:
authorTom Praschan <13141438+tom-anders@users.noreply.github.com>2022-11-17 00:37:15 +0100
committerTom Praschan <13141438+tom-anders@users.noreply.github.com>2022-11-17 00:37:16 +0100
commit0e00611cbc2b2f27e247a58b512cb2cec0624290 (patch)
tree7738e816072c0933968bd47b766927b73b14e488 /clang/lib/Sema/SemaCodeComplete.cpp
parent9e52db182794d87bb8527dda313afd1ac491ab11 (diff)
downloadllvm-0e00611cbc2b2f27e247a58b512cb2cec0624290.zip
llvm-0e00611cbc2b2f27e247a58b512cb2cec0624290.tar.gz
llvm-0e00611cbc2b2f27e247a58b512cb2cec0624290.tar.bz2
[clangd] Add heuristic for dropping snippet when completing member function pointer
This implements the 1st heuristic mentioned in https://github.com/clangd/clangd/issues/968#issuecomment-1002242704: When completing a function that names a non-static member of a class, and we are not inside that class's scope, assume the reference will not be a call (and thus don't add the snippetSuffix) Reviewed By: nridge Differential Revision: https://reviews.llvm.org/D137040
Diffstat (limited to 'clang/lib/Sema/SemaCodeComplete.cpp')
-rw-r--r--clang/lib/Sema/SemaCodeComplete.cpp27
1 files changed, 27 insertions, 0 deletions
diff --git a/clang/lib/Sema/SemaCodeComplete.cpp b/clang/lib/Sema/SemaCodeComplete.cpp
index baf5676..2172692 100644
--- a/clang/lib/Sema/SemaCodeComplete.cpp
+++ b/clang/lib/Sema/SemaCodeComplete.cpp
@@ -1379,6 +1379,33 @@ void ResultBuilder::AddResult(Result R, DeclContext *CurContext,
OverloadSet.Add(Method, Results.size());
}
+ // When completing a non-static member function (and not via
+ // dot/arrow member access) and we're not inside that class' scope,
+ // it can't be a call.
+ if (CompletionContext.getKind() == clang::CodeCompletionContext::CCC_Symbol) {
+ const auto *Method = dyn_cast<CXXMethodDecl>(R.getDeclaration());
+ if (Method && !Method->isStatic()) {
+ // Find the class scope that we're currently in.
+ // We could e.g. be inside a lambda, so walk up the DeclContext until we
+ // find a CXXMethodDecl.
+ const auto *CurrentClassScope = [&]() -> const CXXRecordDecl * {
+ for (DeclContext *Ctx = SemaRef.CurContext; Ctx;
+ Ctx = Ctx->getParent()) {
+ const auto *CtxMethod = llvm::dyn_cast<CXXMethodDecl>(Ctx);
+ if (CtxMethod && !CtxMethod->getParent()->isLambda()) {
+ return CtxMethod->getParent();
+ }
+ }
+ return nullptr;
+ }();
+
+ R.FunctionCanBeCall =
+ CurrentClassScope &&
+ (CurrentClassScope == Method->getParent() ||
+ CurrentClassScope->isDerivedFrom(Method->getParent()));
+ }
+ }
+
// Insert this result into the set of results.
Results.push_back(R);