Skip to content

Commit e07af8c

Browse files
[clang][HeuristicResolver] Use HeuristicResolver to implement getApproximateType() in SemaCodeComplete (#156282)
This patch ports over the remaining functionality present in SemaCodeComplete's getApproximateType() but not in HeuristicResolver, into HeuristicResolver, and uses HeuristicResolver to reimplement getApproximateType(). This has the effect of enhancing both code completion (which now benefit from the full range of HeuristicResolver's capabilities), and existing consumers of HeuristicResolver like clangd's go-to-definition whose behaviour now has parity with code completion. Fixes clangd/clangd#2432
1 parent 32203e6 commit e07af8c

File tree

4 files changed

+174
-120
lines changed

4 files changed

+174
-120
lines changed

clang/include/clang/Sema/HeuristicResolver.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ class HeuristicResolver {
5454
std::vector<const NamedDecl *>
5555
resolveDeclRefExpr(const DependentScopeDeclRefExpr *RE) const;
5656
std::vector<const NamedDecl *>
57-
resolveTypeOfCallExpr(const CallExpr *CE) const;
58-
std::vector<const NamedDecl *>
5957
resolveCalleeOfCallExpr(const CallExpr *CE) const;
6058
std::vector<const NamedDecl *>
6159
resolveUsingValueDecl(const UnresolvedUsingValueDecl *UUVD) const;
@@ -93,6 +91,10 @@ class HeuristicResolver {
9391
// during simplification, and the operation fails if no pointer type is found.
9492
QualType simplifyType(QualType Type, const Expr *E, bool UnwrapPointer);
9593

94+
// Try to heuristically resolve the type of a possibly-dependent expression
95+
// `E`.
96+
QualType resolveExprToType(const Expr *E) const;
97+
9698
// Given an expression `Fn` representing the callee in a function call,
9799
// if the call is through a function pointer, try to find the declaration of
98100
// the corresponding function pointer type, so that we can recover argument

clang/lib/Sema/HeuristicResolver.cpp

Lines changed: 66 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ class HeuristicResolverImpl {
3535
resolveMemberExpr(const CXXDependentScopeMemberExpr *ME);
3636
std::vector<const NamedDecl *>
3737
resolveDeclRefExpr(const DependentScopeDeclRefExpr *RE);
38-
std::vector<const NamedDecl *> resolveTypeOfCallExpr(const CallExpr *CE);
3938
std::vector<const NamedDecl *> resolveCalleeOfCallExpr(const CallExpr *CE);
4039
std::vector<const NamedDecl *>
4140
resolveUsingValueDecl(const UnresolvedUsingValueDecl *UUVD);
@@ -50,6 +49,7 @@ class HeuristicResolverImpl {
5049
llvm::function_ref<bool(const NamedDecl *ND)> Filter);
5150
TagDecl *resolveTypeToTagDecl(QualType T);
5251
QualType simplifyType(QualType Type, const Expr *E, bool UnwrapPointer);
52+
QualType resolveExprToType(const Expr *E);
5353
FunctionProtoTypeLoc getFunctionProtoTypeLoc(const Expr *Fn);
5454

5555
private:
@@ -71,10 +71,8 @@ class HeuristicResolverImpl {
7171
resolveDependentMember(QualType T, DeclarationName Name,
7272
llvm::function_ref<bool(const NamedDecl *ND)> Filter);
7373

74-
// Try to heuristically resolve the type of a possibly-dependent expression
75-
// `E`.
76-
QualType resolveExprToType(const Expr *E);
7774
std::vector<const NamedDecl *> resolveExprToDecls(const Expr *E);
75+
QualType resolveTypeOfCallExpr(const CallExpr *CE);
7876

7977
bool findOrdinaryMemberInDependentClasses(const CXXBaseSpecifier *Specifier,
8078
CXXBasePath &Path,
@@ -96,18 +94,25 @@ const auto TemplateFilter = [](const NamedDecl *D) {
9694
return isa<TemplateDecl>(D);
9795
};
9896

99-
QualType resolveDeclsToType(const std::vector<const NamedDecl *> &Decls,
100-
ASTContext &Ctx) {
101-
if (Decls.size() != 1) // Names an overload set -- just bail.
102-
return QualType();
103-
if (const auto *TD = dyn_cast<TypeDecl>(Decls[0]))
97+
QualType resolveDeclToType(const NamedDecl *D, ASTContext &Ctx) {
98+
if (const auto *TempD = dyn_cast<TemplateDecl>(D)) {
99+
D = TempD->getTemplatedDecl();
100+
}
101+
if (const auto *TD = dyn_cast<TypeDecl>(D))
104102
return Ctx.getCanonicalTypeDeclType(TD);
105-
if (const auto *VD = dyn_cast<ValueDecl>(Decls[0])) {
103+
if (const auto *VD = dyn_cast<ValueDecl>(D)) {
106104
return VD->getType();
107105
}
108106
return QualType();
109107
}
110108

109+
QualType resolveDeclsToType(const std::vector<const NamedDecl *> &Decls,
110+
ASTContext &Ctx) {
111+
if (Decls.size() != 1) // Names an overload set -- just bail.
112+
return QualType();
113+
return resolveDeclToType(Decls[0], Ctx);
114+
}
115+
111116
TemplateName getReferencedTemplateName(const Type *T) {
112117
if (const auto *TST = T->getAs<TemplateSpecializationType>()) {
113118
return TST->getTemplateName();
@@ -329,19 +334,29 @@ HeuristicResolverImpl::resolveDeclRefExpr(const DependentScopeDeclRefExpr *RE) {
329334
return resolveDependentMember(Qualifier, RE->getDeclName(), StaticFilter);
330335
}
331336

332-
std::vector<const NamedDecl *>
333-
HeuristicResolverImpl::resolveTypeOfCallExpr(const CallExpr *CE) {
334-
QualType CalleeType = resolveExprToType(CE->getCallee());
335-
if (CalleeType.isNull())
336-
return {};
337-
if (const auto *FnTypePtr = CalleeType->getAs<PointerType>())
338-
CalleeType = FnTypePtr->getPointeeType();
339-
if (const FunctionType *FnType = CalleeType->getAs<FunctionType>()) {
340-
if (const auto *D = resolveTypeToTagDecl(FnType->getReturnType())) {
341-
return {D};
337+
QualType HeuristicResolverImpl::resolveTypeOfCallExpr(const CallExpr *CE) {
338+
// resolveExprToType(CE->getCallee()) would bail in the case of multiple
339+
// overloads, as it can't produce a single type for them. We can be more
340+
// permissive here, and allow multiple overloads with a common return type.
341+
std::vector<const NamedDecl *> CalleeDecls =
342+
resolveExprToDecls(CE->getCallee());
343+
QualType CommonReturnType;
344+
for (const NamedDecl *CalleeDecl : CalleeDecls) {
345+
QualType CalleeType = resolveDeclToType(CalleeDecl, Ctx);
346+
if (CalleeType.isNull())
347+
continue;
348+
if (const auto *FnTypePtr = CalleeType->getAs<PointerType>())
349+
CalleeType = FnTypePtr->getPointeeType();
350+
if (const FunctionType *FnType = CalleeType->getAs<FunctionType>()) {
351+
QualType ReturnType =
352+
simplifyType(FnType->getReturnType(), nullptr, false);
353+
if (!CommonReturnType.isNull() && CommonReturnType != ReturnType) {
354+
return {}; // conflicting return types
355+
}
356+
CommonReturnType = ReturnType;
342357
}
343358
}
344-
return {};
359+
return CommonReturnType;
345360
}
346361

347362
std::vector<const NamedDecl *>
@@ -393,15 +408,41 @@ HeuristicResolverImpl::resolveExprToDecls(const Expr *E) {
393408
return {OE->decls_begin(), OE->decls_end()};
394409
}
395410
if (const auto *CE = dyn_cast<CallExpr>(E)) {
396-
return resolveTypeOfCallExpr(CE);
411+
QualType T = resolveTypeOfCallExpr(CE);
412+
if (const auto *D = resolveTypeToTagDecl(T)) {
413+
return {D};
414+
}
415+
return {};
397416
}
398417
if (const auto *ME = dyn_cast<MemberExpr>(E))
399418
return {ME->getMemberDecl()};
419+
if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
420+
return {DRE->getDecl()};
400421

401422
return {};
402423
}
403424

404425
QualType HeuristicResolverImpl::resolveExprToType(const Expr *E) {
426+
// resolveExprToDecls on a CallExpr only succeeds if the return type is
427+
// a TagDecl, but we may want the type of a call in other cases as well.
428+
// (FIXME: There are probably other cases where we can do something more
429+
// flexible than resoveExprToDecls + resolveDeclsToType, e.g. in the case
430+
// of OverloadExpr we can probably accept overloads with a common type).
431+
if (const auto *CE = dyn_cast<CallExpr>(E)) {
432+
if (QualType Resolved = resolveTypeOfCallExpr(CE); !Resolved.isNull())
433+
return Resolved;
434+
}
435+
// Similarly, unwrapping a unary dereference operation does not work via
436+
// resolveExprToDecls.
437+
if (const auto *UO = dyn_cast<UnaryOperator>(E->IgnoreParenCasts())) {
438+
if (UO->getOpcode() == UnaryOperatorKind::UO_Deref) {
439+
if (auto Pointee = getPointeeType(resolveExprToType(UO->getSubExpr()));
440+
!Pointee.isNull()) {
441+
return Pointee;
442+
}
443+
}
444+
}
445+
405446
std::vector<const NamedDecl *> Decls = resolveExprToDecls(E);
406447
if (!Decls.empty())
407448
return resolveDeclsToType(Decls, Ctx);
@@ -580,10 +621,6 @@ std::vector<const NamedDecl *> HeuristicResolver::resolveDeclRefExpr(
580621
return HeuristicResolverImpl(Ctx).resolveDeclRefExpr(RE);
581622
}
582623
std::vector<const NamedDecl *>
583-
HeuristicResolver::resolveTypeOfCallExpr(const CallExpr *CE) const {
584-
return HeuristicResolverImpl(Ctx).resolveTypeOfCallExpr(CE);
585-
}
586-
std::vector<const NamedDecl *>
587624
HeuristicResolver::resolveCalleeOfCallExpr(const CallExpr *CE) const {
588625
return HeuristicResolverImpl(Ctx).resolveCalleeOfCallExpr(CE);
589626
}
@@ -619,7 +656,9 @@ QualType HeuristicResolver::simplifyType(QualType Type, const Expr *E,
619656
bool UnwrapPointer) {
620657
return HeuristicResolverImpl(Ctx).simplifyType(Type, E, UnwrapPointer);
621658
}
622-
659+
QualType HeuristicResolver::resolveExprToType(const Expr *E) const {
660+
return HeuristicResolverImpl(Ctx).resolveExprToType(E);
661+
}
623662
FunctionProtoTypeLoc
624663
HeuristicResolver::getFunctionProtoTypeLoc(const Expr *Fn) const {
625664
return HeuristicResolverImpl(Ctx).getFunctionProtoTypeLoc(Fn);

clang/lib/Sema/SemaCodeComplete.cpp

Lines changed: 7 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -5827,96 +5827,13 @@ class ConceptInfo {
58275827
// We accept some lossiness (like dropping parameters).
58285828
// We only try to handle common expressions on the LHS of MemberExpr.
58295829
QualType getApproximateType(const Expr *E, HeuristicResolver &Resolver) {
5830-
if (E->getType().isNull())
5831-
return QualType();
5832-
// Don't drop implicit cast if it's an array decay.
5833-
if (auto *ICE = dyn_cast<ImplicitCastExpr>(E);
5834-
!ICE || ICE->getCastKind() != CK_ArrayToPointerDecay)
5835-
E = E->IgnoreParenImpCasts();
5836-
QualType Unresolved = E->getType();
5837-
// Resolve DependentNameType
5838-
if (const auto *DNT = Unresolved->getAs<DependentNameType>()) {
5839-
if (auto Decls = Resolver.resolveDependentNameType(DNT);
5840-
Decls.size() == 1) {
5841-
if (const auto *TD = dyn_cast<TypeDecl>(Decls[0]))
5842-
return TD->getASTContext().getTypeDeclType(TD);
5843-
}
5844-
}
5845-
// We only resolve DependentTy, or undeduced autos (including auto* etc).
5846-
if (!Unresolved->isSpecificBuiltinType(BuiltinType::Dependent)) {
5847-
AutoType *Auto = Unresolved->getContainedAutoType();
5848-
if (!Auto || !Auto->isUndeducedAutoType())
5849-
return Unresolved;
5850-
}
5851-
// A call: approximate-resolve callee to a function type, get its return type
5852-
if (const CallExpr *CE = llvm::dyn_cast<CallExpr>(E)) {
5853-
QualType Callee = getApproximateType(CE->getCallee(), Resolver);
5854-
if (Callee.isNull() ||
5855-
Callee->isSpecificPlaceholderType(BuiltinType::BoundMember))
5856-
Callee = Expr::findBoundMemberType(CE->getCallee());
5857-
if (Callee.isNull())
5858-
return Unresolved;
5859-
5860-
if (const auto *FnTypePtr = Callee->getAs<PointerType>()) {
5861-
Callee = FnTypePtr->getPointeeType();
5862-
} else if (const auto *BPT = Callee->getAs<BlockPointerType>()) {
5863-
Callee = BPT->getPointeeType();
5864-
}
5865-
if (const FunctionType *FnType = Callee->getAs<FunctionType>())
5866-
return FnType->getReturnType().getNonReferenceType();
5867-
5868-
// Unresolved call: try to guess the return type.
5869-
if (const auto *OE = llvm::dyn_cast<OverloadExpr>(CE->getCallee())) {
5870-
// If all candidates have the same approximate return type, use it.
5871-
// Discard references and const to allow more to be "the same".
5872-
// (In particular, if there's one candidate + ADL, resolve it).
5873-
const Type *Common = nullptr;
5874-
for (const auto *D : OE->decls()) {
5875-
QualType ReturnType;
5876-
if (const auto *FD = llvm::dyn_cast<FunctionDecl>(D))
5877-
ReturnType = FD->getReturnType();
5878-
else if (const auto *FTD = llvm::dyn_cast<FunctionTemplateDecl>(D))
5879-
ReturnType = FTD->getTemplatedDecl()->getReturnType();
5880-
if (ReturnType.isNull())
5881-
continue;
5882-
const Type *Candidate =
5883-
ReturnType.getNonReferenceType().getCanonicalType().getTypePtr();
5884-
if (Common && Common != Candidate)
5885-
return Unresolved; // Multiple candidates.
5886-
Common = Candidate;
5887-
}
5888-
if (Common != nullptr)
5889-
return QualType(Common, 0);
5890-
}
5891-
}
5892-
// A dependent member: resolve using HeuristicResolver.
5893-
if (const auto *CDSME = llvm::dyn_cast<CXXDependentScopeMemberExpr>(E)) {
5894-
for (const auto *Member : Resolver.resolveMemberExpr(CDSME)) {
5895-
if (const auto *VD = dyn_cast<ValueDecl>(Member)) {
5896-
return VD->getType().getNonReferenceType();
5897-
}
5898-
}
5899-
}
5900-
// A reference to an `auto` variable: approximate-resolve its initializer.
5901-
if (const auto *DRE = llvm::dyn_cast<DeclRefExpr>(E)) {
5902-
if (const auto *VD = llvm::dyn_cast<VarDecl>(DRE->getDecl())) {
5903-
if (VD->hasInit())
5904-
return getApproximateType(VD->getInit(), Resolver);
5905-
}
5906-
}
5907-
if (const auto *UO = llvm::dyn_cast<UnaryOperator>(E)) {
5908-
if (UO->getOpcode() == UnaryOperatorKind::UO_Deref) {
5909-
// We recurse into the subexpression because it could be of dependent
5910-
// type.
5911-
if (auto Pointee =
5912-
getApproximateType(UO->getSubExpr(), Resolver)->getPointeeType();
5913-
!Pointee.isNull())
5914-
return Pointee;
5915-
// Our caller expects a non-null result, even though the SubType is
5916-
// supposed to have a pointee. Fall through to Unresolved anyway.
5917-
}
5918-
}
5919-
return Unresolved;
5830+
QualType Result = Resolver.resolveExprToType(E);
5831+
if (Result.isNull())
5832+
return Result;
5833+
Result = Resolver.simplifyType(Result.getNonReferenceType(), E, false);
5834+
if (Result.isNull())
5835+
return Result;
5836+
return Result.getNonReferenceType();
59205837
}
59215838

59225839
// If \p Base is ParenListExpr, assume a chain of comma operators and pick the

0 commit comments

Comments
 (0)