Skip to content

[clang][CodeComplete] Use HeuristicResolver to resolve pointee types #121315

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
Jan 22, 2025
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
2 changes: 2 additions & 0 deletions clang/include/clang/Sema/SemaCodeCompletion.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "clang/Sema/CodeCompleteConsumer.h"
#include "clang/Sema/DeclSpec.h"
#include "clang/Sema/Designator.h"
#include "clang/Sema/HeuristicResolver.h"
#include "clang/Sema/Ownership.h"
#include "clang/Sema/SemaBase.h"
#include "llvm/ADT/StringRef.h"
Expand All @@ -43,6 +44,7 @@ class SemaCodeCompletion : public SemaBase {

/// Code-completion consumer.
CodeCompleteConsumer *CodeCompleter;
HeuristicResolver Resolver;

/// Describes the context in which code completion occurs.
enum ParserCompletionContext {
Expand Down
15 changes: 10 additions & 5 deletions clang/lib/Sema/SemaCodeComplete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "clang/Sema/CodeCompleteConsumer.h"
#include "clang/Sema/DeclSpec.h"
#include "clang/Sema/Designator.h"
#include "clang/Sema/HeuristicResolver.h"
#include "clang/Sema/Lookup.h"
#include "clang/Sema/Overload.h"
#include "clang/Sema/ParsedAttr.h"
Expand Down Expand Up @@ -5861,8 +5862,10 @@ void SemaCodeCompletion::CodeCompleteMemberReferenceExpr(
enum CodeCompletionContext::Kind contextKind;

if (IsArrow) {
if (const auto *Ptr = ConvertedBaseType->getAs<PointerType>())
ConvertedBaseType = Ptr->getPointeeType();
if (QualType PointeeType = Resolver.getPointeeType(ConvertedBaseType);
!PointeeType.isNull()) {
ConvertedBaseType = PointeeType;
}
}

if (IsArrow) {
Expand Down Expand Up @@ -5899,8 +5902,9 @@ void SemaCodeCompletion::CodeCompleteMemberReferenceExpr(
ExprValueKind BaseKind = Base->getValueKind();

if (IsArrow) {
if (const PointerType *Ptr = BaseType->getAs<PointerType>()) {
BaseType = Ptr->getPointeeType();
if (QualType PointeeType = Resolver.getPointeeType(BaseType);
!PointeeType.isNull()) {
BaseType = PointeeType;
BaseKind = VK_LValue;
} else if (BaseType->isObjCObjectPointerType() ||
BaseType->isTemplateTypeParmType()) {
Expand Down Expand Up @@ -10472,4 +10476,5 @@ void SemaCodeCompletion::GatherGlobalCodeCompletions(

SemaCodeCompletion::SemaCodeCompletion(Sema &S,
CodeCompleteConsumer *CompletionConsumer)
: SemaBase(S), CodeCompleter(CompletionConsumer) {}
: SemaBase(S), CodeCompleter(CompletionConsumer),
Resolver(S.getASTContext()) {}
17 changes: 17 additions & 0 deletions clang/test/CodeCompletion/member-access.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,20 @@ void Foo() {
// 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()
}

namespace dependent_smart_pointer {
template <typename T>
struct smart_pointer {
T* operator->();
};

template <typename T>
struct node {
smart_pointer<node<T>> next;
void foo() {
next->next;
// RUN: %clang_cc1 -fsyntax-only -code-completion-at=%s:398:11 %s -o - | FileCheck -check-prefix=CHECK-DEPENDENT-SMARTPTR %s
// CHECK-DEPENDENT-SMARTPTR: [#smart_pointer<node<T>>#]next
}
};
}
Loading