Skip to content

Conversation

@HighCommander4
Copy link
Collaborator

This allows completing after (*this). in a dependent context.

Fixes clangd/clangd#1952

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Mar 25, 2024
@llvmbot
Copy link
Member

llvmbot commented Mar 25, 2024

@llvm/pr-subscribers-clang

Author: Nathan Ridge (HighCommander4)

Changes

This allows completing after (*this). in a dependent context.

Fixes clangd/clangd#1952


Full diff: https://github.com/llvm/llvm-project/pull/86466.diff

2 Files Affected:

  • (modified) clang/lib/Sema/SemaCodeComplete.cpp (+6)
  • (modified) clang/test/CodeCompletion/member-access.cpp (+18-2)
diff --git a/clang/lib/Sema/SemaCodeComplete.cpp b/clang/lib/Sema/SemaCodeComplete.cpp
index c44be0df9b0a85..0e44b0f444ed73 100644
--- a/clang/lib/Sema/SemaCodeComplete.cpp
+++ b/clang/lib/Sema/SemaCodeComplete.cpp
@@ -20,6 +20,7 @@
 #include "clang/AST/ExprConcepts.h"
 #include "clang/AST/ExprObjC.h"
 #include "clang/AST/NestedNameSpecifier.h"
+#include "clang/AST/OperationKinds.h"
 #include "clang/AST/QualTypeNames.h"
 #include "clang/AST/RecursiveASTVisitor.h"
 #include "clang/AST/Type.h"
@@ -5674,6 +5675,11 @@ QualType getApproximateType(const Expr *E) {
         return getApproximateType(VD->getInit());
     }
   }
+  if (const auto *UO = llvm::dyn_cast<UnaryOperator>(E)) {
+    if (UO->getOpcode() == UnaryOperatorKind::UO_Deref) {
+      return UO->getSubExpr()->getType()->getPointeeType();
+    }
+  }
   return Unresolved;
 }
 
diff --git a/clang/test/CodeCompletion/member-access.cpp b/clang/test/CodeCompletion/member-access.cpp
index 474b909ab11592..9f8c21c0bca6de 100644
--- a/clang/test/CodeCompletion/member-access.cpp
+++ b/clang/test/CodeCompletion/member-access.cpp
@@ -348,7 +348,23 @@ namespace function_can_be_call {
     T foo(U, V);
   };
 
-  &S::f
-  // RUN: %clang_cc1 -fsyntax-only -code-completion-at=%s:351:7 %s -o - | FileCheck -check-prefix=CHECK_FUNCTION_CAN_BE_CALL %s
+  void test() {
+    &S::f
+  }
+  // RUN: %clang_cc1 -fsyntax-only -code-completion-at=%s:352:9 %s -o - | FileCheck -check-prefix=CHECK_FUNCTION_CAN_BE_CALL %s
   // CHECK_FUNCTION_CAN_BE_CALL: COMPLETION: foo : [#T#]foo<<#typename T#>, <#typename U#>>(<#U#>, <#V#>)
 }
+
+namespace deref_dependent_this {
+template <typename T>
+class A {
+  int field;
+
+  void function() {
+    (*this).field;
+// RUN: %clang_cc1 -fsyntax-only -code-completion-at=%s:364:13 %s -o - | FileCheck -check-prefix=CHECK-DEREF-THIS %s
+// CHECK-DEREF-THIS: field : [#int#]field
+// CHECK-DEREF-THIS: [#void#]function()
+  }
+};
+}

@github-actions
Copy link

✅ With the latest revision this PR passed the C/C++ code formatter.

@github-actions
Copy link

✅ With the latest revision this PR passed the Python code formatter.

Copy link
Contributor

@zyn0217 zyn0217 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. LGTM apart from one nit.

This allows completing after `(*this).` in a dependent context.

Fixes clangd/clangd#1952
@HighCommander4
Copy link
Collaborator Author

Thanks for the review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

No completions offered after (*this). in template class

3 participants