[clang] 7429950 - [clang][CodeComplete] Recurse into the subexpression of deref operator in getApproximateType (#93404)

via cfe-commits cfe-commits at lists.llvm.org
Mon May 27 00:56:41 PDT 2024


Author: Younan Zhang
Date: 2024-05-27T15:56:37+08:00
New Revision: 7429950d840b8fec3d9a48d00e612a3240c2be83

URL: https://github.com/llvm/llvm-project/commit/7429950d840b8fec3d9a48d00e612a3240c2be83
DIFF: https://github.com/llvm/llvm-project/commit/7429950d840b8fec3d9a48d00e612a3240c2be83.diff

LOG: [clang][CodeComplete] Recurse into the subexpression of deref operator in getApproximateType (#93404)

The issue with the previous implementation bc31be7 was that
getApproximateType could potentially return a null QualType for a
dereferencing operator, which is not what its caller wants.

Added: 
    

Modified: 
    clang/lib/Sema/SemaCodeComplete.cpp
    clang/test/CodeCompletion/member-access.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaCodeComplete.cpp b/clang/lib/Sema/SemaCodeComplete.cpp
index ad3ca4cc94ca6..cd1c5f9391ccd 100644
--- a/clang/lib/Sema/SemaCodeComplete.cpp
+++ b/clang/lib/Sema/SemaCodeComplete.cpp
@@ -5692,8 +5692,15 @@ QualType getApproximateType(const Expr *E) {
     }
   }
   if (const auto *UO = llvm::dyn_cast<UnaryOperator>(E)) {
-    if (UO->getOpcode() == UnaryOperatorKind::UO_Deref)
-      return UO->getSubExpr()->getType()->getPointeeType();
+    if (UO->getOpcode() == UnaryOperatorKind::UO_Deref) {
+      // We recurse into the subexpression because it could be of dependent
+      // type.
+      if (auto Pointee = getApproximateType(UO->getSubExpr())->getPointeeType();
+          !Pointee.isNull())
+        return Pointee;
+      // Our caller expects a non-null result, even though the SubType is
+      // supposed to have a pointee. Fall through to Unresolved anyway.
+    }
   }
   return Unresolved;
 }

diff  --git a/clang/test/CodeCompletion/member-access.cpp b/clang/test/CodeCompletion/member-access.cpp
index 9f8c21c0bca6d..912f269db6c1a 100644
--- a/clang/test/CodeCompletion/member-access.cpp
+++ b/clang/test/CodeCompletion/member-access.cpp
@@ -367,4 +367,20 @@ class A {
 // CHECK-DEREF-THIS: [#void#]function()
   }
 };
+
+template <typename Element>
+struct RepeatedField {
+  void Add();
+};
+
+template <typename T>
+RepeatedField<T>* MutableRepeatedField() {}
+
+template <class T>
+void Foo() {
+  auto& C = *MutableRepeatedField<T>();
+  C.
+}
+// RUN: %clang_cc1 -fsyntax-only -code-completion-at=%s:382:5 %s -o - | FileCheck -check-prefix=CHECK-DEREF-DEPENDENT %s
+// CHECK-DEREF-DEPENDENT: [#void#]Add()
 }


        


More information about the cfe-commits mailing list