Skip to content

Commit 4c65b2a

Browse files
[clang][Sema] Unify getPrototypeLoc helpers in SemaCodeComplete and clangd
HeuristicResolver houses the unified implementation. Fixes #143240
1 parent 392bd57 commit 4c65b2a

File tree

4 files changed

+65
-98
lines changed

4 files changed

+65
-98
lines changed

clang-tools-extra/clangd/InlayHints.cpp

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
#include "llvm/ADT/StringExtras.h"
3434
#include "llvm/ADT/StringRef.h"
3535
#include "llvm/ADT/Twine.h"
36-
#include "llvm/ADT/identity.h"
3736
#include "llvm/Support/Casting.h"
3837
#include "llvm/Support/ErrorHandling.h"
3938
#include "llvm/Support/FormatVariadic.h"
@@ -339,53 +338,6 @@ QualType maybeDesugar(ASTContext &AST, QualType QT) {
339338
return QT;
340339
}
341340

342-
// Given a callee expression `Fn`, if the call is through a function pointer,
343-
// try to find the declaration of the corresponding function pointer type,
344-
// so that we can recover argument names from it.
345-
// FIXME: This function is mostly duplicated in SemaCodeComplete.cpp; unify.
346-
static FunctionProtoTypeLoc getPrototypeLoc(Expr *Fn) {
347-
TypeLoc Target;
348-
Expr *NakedFn = Fn->IgnoreParenCasts();
349-
if (const auto *T = NakedFn->getType().getTypePtr()->getAs<TypedefType>()) {
350-
Target = T->getDecl()->getTypeSourceInfo()->getTypeLoc();
351-
} else if (const auto *DR = dyn_cast<DeclRefExpr>(NakedFn)) {
352-
const auto *D = DR->getDecl();
353-
if (const auto *const VD = dyn_cast<VarDecl>(D)) {
354-
Target = VD->getTypeSourceInfo()->getTypeLoc();
355-
}
356-
}
357-
358-
if (!Target)
359-
return {};
360-
361-
// Unwrap types that may be wrapping the function type
362-
while (true) {
363-
if (auto P = Target.getAs<PointerTypeLoc>()) {
364-
Target = P.getPointeeLoc();
365-
continue;
366-
}
367-
if (auto A = Target.getAs<AttributedTypeLoc>()) {
368-
Target = A.getModifiedLoc();
369-
continue;
370-
}
371-
if (auto P = Target.getAs<ParenTypeLoc>()) {
372-
Target = P.getInnerLoc();
373-
continue;
374-
}
375-
break;
376-
}
377-
378-
if (auto F = Target.getAs<FunctionProtoTypeLoc>()) {
379-
// In some edge cases the AST can contain a "trivial" FunctionProtoTypeLoc
380-
// which has null parameters. Avoid these as they don't contain useful
381-
// information.
382-
if (llvm::all_of(F.getParams(), llvm::identity<ParmVarDecl *>()))
383-
return F;
384-
}
385-
386-
return {};
387-
}
388-
389341
ArrayRef<const ParmVarDecl *>
390342
maybeDropCxxExplicitObjectParameters(ArrayRef<const ParmVarDecl *> Params) {
391343
if (!Params.empty() && Params.front()->isExplicitObjectParameter())
@@ -514,7 +466,8 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
514466
Callee.Decl = FD;
515467
else if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(CalleeDecls[0]))
516468
Callee.Decl = FTD->getTemplatedDecl();
517-
else if (FunctionProtoTypeLoc Loc = getPrototypeLoc(E->getCallee()))
469+
else if (FunctionProtoTypeLoc Loc =
470+
Resolver->getProtoTypeLoc(E->getCallee()))
518471
Callee.Loc = Loc;
519472
else
520473
return true;

clang/include/clang/Sema/HeuristicResolver.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class CXXBasePath;
2020
class CXXDependentScopeMemberExpr;
2121
class DeclarationName;
2222
class DependentScopeDeclRefExpr;
23+
class FunctionProtoTypeLoc;
2324
class NamedDecl;
2425
class Type;
2526
class UnresolvedUsingValueDecl;
@@ -93,6 +94,12 @@ class HeuristicResolver {
9394
// during simplification, and the operation fails if no pointer type is found.
9495
QualType simplifyType(QualType Type, const Expr *E, bool UnwrapPointer);
9596

97+
// Given an expression `Fn` representing the callee in a function call,
98+
// if the call is through a function pointer, try to find the declaration of
99+
// the corresponding function pointer type, so that we can recover argument
100+
// names from it.
101+
FunctionProtoTypeLoc getProtoTypeLoc(Expr *Fn) const;
102+
96103
private:
97104
ASTContext &Ctx;
98105
};

clang/lib/Sema/HeuristicResolver.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "clang/AST/ExprCXX.h"
1414
#include "clang/AST/TemplateBase.h"
1515
#include "clang/AST/Type.h"
16+
#include "llvm/ADT/identity.h"
1617

1718
namespace clang {
1819

@@ -50,6 +51,7 @@ class HeuristicResolverImpl {
5051
llvm::function_ref<bool(const NamedDecl *ND)> Filter);
5152
TagDecl *resolveTypeToTagDecl(QualType T);
5253
QualType simplifyType(QualType Type, const Expr *E, bool UnwrapPointer);
54+
FunctionProtoTypeLoc getProtoTypeLoc(Expr *Fn);
5355

5456
private:
5557
ASTContext &Ctx;
@@ -506,6 +508,55 @@ std::vector<const NamedDecl *> HeuristicResolverImpl::resolveDependentMember(
506508
}
507509
return {};
508510
}
511+
512+
FunctionProtoTypeLoc HeuristicResolverImpl::getProtoTypeLoc(Expr *Fn) {
513+
TypeLoc Target;
514+
Expr *NakedFn = Fn->IgnoreParenCasts();
515+
if (const auto *T = NakedFn->getType().getTypePtr()->getAs<TypedefType>()) {
516+
Target = T->getDecl()->getTypeSourceInfo()->getTypeLoc();
517+
} else if (const auto *DR = dyn_cast<DeclRefExpr>(NakedFn)) {
518+
const auto *D = DR->getDecl();
519+
if (const auto *const VD = dyn_cast<VarDecl>(D)) {
520+
Target = VD->getTypeSourceInfo()->getTypeLoc();
521+
}
522+
} else if (const auto *ME = dyn_cast<MemberExpr>(Fn)) {
523+
const auto *MD = ME->getMemberDecl();
524+
if (const auto *FD = dyn_cast<FieldDecl>(MD)) {
525+
Target = FD->getTypeSourceInfo()->getTypeLoc();
526+
}
527+
}
528+
529+
if (!Target)
530+
return {};
531+
532+
// Unwrap types that may be wrapping the function type
533+
while (true) {
534+
if (auto P = Target.getAs<PointerTypeLoc>()) {
535+
Target = P.getPointeeLoc();
536+
continue;
537+
}
538+
if (auto A = Target.getAs<AttributedTypeLoc>()) {
539+
Target = A.getModifiedLoc();
540+
continue;
541+
}
542+
if (auto P = Target.getAs<ParenTypeLoc>()) {
543+
Target = P.getInnerLoc();
544+
continue;
545+
}
546+
break;
547+
}
548+
549+
if (auto F = Target.getAs<FunctionProtoTypeLoc>()) {
550+
// In some edge cases the AST can contain a "trivial" FunctionProtoTypeLoc
551+
// which has null parameters. Avoid these as they don't contain useful
552+
// information.
553+
if (llvm::all_of(F.getParams(), llvm::identity<ParmVarDecl *>()))
554+
return F;
555+
}
556+
557+
return {};
558+
}
559+
509560
} // namespace
510561

511562
std::vector<const NamedDecl *> HeuristicResolver::resolveMemberExpr(
@@ -557,4 +608,8 @@ QualType HeuristicResolver::simplifyType(QualType Type, const Expr *E,
557608
return HeuristicResolverImpl(Ctx).simplifyType(Type, E, UnwrapPointer);
558609
}
559610

611+
FunctionProtoTypeLoc HeuristicResolver::getProtoTypeLoc(Expr *Fn) const {
612+
return HeuristicResolverImpl(Ctx).getProtoTypeLoc(Fn);
613+
}
614+
560615
} // namespace clang

clang/lib/Sema/SemaCodeComplete.cpp

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6283,54 +6283,6 @@ ProduceSignatureHelp(Sema &SemaRef, MutableArrayRef<ResultCandidate> Candidates,
62836283
return getParamType(SemaRef, Candidates, CurrentArg);
62846284
}
62856285

6286-
// Given a callee expression `Fn`, if the call is through a function pointer,
6287-
// try to find the declaration of the corresponding function pointer type,
6288-
// so that we can recover argument names from it.
6289-
static FunctionProtoTypeLoc GetPrototypeLoc(Expr *Fn) {
6290-
TypeLoc Target;
6291-
6292-
if (const auto *T = Fn->getType().getTypePtr()->getAs<TypedefType>()) {
6293-
Target = T->getDecl()->getTypeSourceInfo()->getTypeLoc();
6294-
6295-
} else if (const auto *DR = dyn_cast<DeclRefExpr>(Fn)) {
6296-
const auto *D = DR->getDecl();
6297-
if (const auto *const VD = dyn_cast<VarDecl>(D)) {
6298-
Target = VD->getTypeSourceInfo()->getTypeLoc();
6299-
}
6300-
} else if (const auto *ME = dyn_cast<MemberExpr>(Fn)) {
6301-
const auto *MD = ME->getMemberDecl();
6302-
if (const auto *FD = dyn_cast<FieldDecl>(MD)) {
6303-
Target = FD->getTypeSourceInfo()->getTypeLoc();
6304-
}
6305-
}
6306-
6307-
if (!Target)
6308-
return {};
6309-
6310-
// Unwrap types that may be wrapping the function type
6311-
while (true) {
6312-
if (auto P = Target.getAs<PointerTypeLoc>()) {
6313-
Target = P.getPointeeLoc();
6314-
continue;
6315-
}
6316-
if (auto A = Target.getAs<AttributedTypeLoc>()) {
6317-
Target = A.getModifiedLoc();
6318-
continue;
6319-
}
6320-
if (auto P = Target.getAs<ParenTypeLoc>()) {
6321-
Target = P.getInnerLoc();
6322-
continue;
6323-
}
6324-
break;
6325-
}
6326-
6327-
if (auto F = Target.getAs<FunctionProtoTypeLoc>()) {
6328-
return F;
6329-
}
6330-
6331-
return {};
6332-
}
6333-
63346286
QualType
63356287
SemaCodeCompletion::ProduceCallSignatureHelp(Expr *Fn, ArrayRef<Expr *> Args,
63366288
SourceLocation OpenParLoc) {
@@ -6419,7 +6371,7 @@ SemaCodeCompletion::ProduceCallSignatureHelp(Expr *Fn, ArrayRef<Expr *> Args,
64196371
// Lastly we check whether expression's type is function pointer or
64206372
// function.
64216373

6422-
FunctionProtoTypeLoc P = GetPrototypeLoc(NakedFn);
6374+
FunctionProtoTypeLoc P = Resolver.getProtoTypeLoc(NakedFn);
64236375
QualType T = NakedFn->getType();
64246376
if (!T->getPointeeType().isNull())
64256377
T = T->getPointeeType();

0 commit comments

Comments
 (0)