[PATCH] D125120: Initial work on parameter info for function pointers
Qwinci created this revision. Herald added subscribers: usaxena95, kadircet, arphaman. Herald added a project: All. Qwinci requested review of this revision. Herald added projects: clang, clang-tools-extra. Herald added a subscriber: cfe-commits. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D125120 Files: clang-tools-extra/clangd/CodeComplete.cpp clang/include/clang/Sema/CodeCompleteConsumer.h clang/lib/Sema/CodeCompleteConsumer.cpp clang/lib/Sema/SemaCodeComplete.cpp Index: clang/lib/Sema/SemaCodeComplete.cpp === --- clang/lib/Sema/SemaCodeComplete.cpp +++ clang/lib/Sema/SemaCodeComplete.cpp @@ -3722,6 +3722,7 @@ const PrintingPolicy &Policy, const FunctionDecl *Function, const FunctionProtoType *Prototype, + FunctionProtoTypeLoc PrototypeLoc, CodeCompletionBuilder &Result, unsigned CurrentArg, unsigned Start = 0, bool InOptional = false) { @@ -3743,7 +3744,7 @@ if (!FirstParameter) Opt.AddChunk(CodeCompletionString::CK_Comma); // Optional sections are nested. - AddOverloadParameterChunks(Context, Policy, Function, Prototype, Opt, + AddOverloadParameterChunks(Context, Policy, Function, Prototype, PrototypeLoc, Opt, CurrentArg, P, /*InOptional=*/true); Result.AddOptionalChunk(Opt.TakeString()); return; @@ -3764,6 +3765,14 @@ if (Param->hasDefaultArg()) Placeholder += GetDefaultValueString(Param, Context.getSourceManager(), Context.getLangOpts()); +} else if (!PrototypeLoc.isNull()) { + if (P < PrototypeLoc.getNumParams()) { +const ParmVarDecl *Param = PrototypeLoc.getParam(P); +Placeholder = FormatFunctionParameter(Policy, Param); +if (Param->hasDefaultArg()) + Placeholder += GetDefaultValueString(Param, Context.getSourceManager(), +Context.getLangOpts()); + } } else { Placeholder = Prototype->getParamType(P).getAsString(Policy); } @@ -3912,7 +3921,7 @@ if (getKind() == CK_Aggregate) AddOverloadAggregateChunks(getAggregate(), Policy, Result, CurrentArg); else -AddOverloadParameterChunks(S.getASTContext(), Policy, FDecl, Proto, Result, +AddOverloadParameterChunks(S.getASTContext(), Policy, FDecl, Proto, ProtoTypeLoc, Result, CurrentArg); Result.AddChunk(Braced ? CodeCompletionString::CK_RightBrace : CodeCompletionString::CK_RightParen); @@ -5991,6 +6000,16 @@ return getParamType(SemaRef, Candidates, CurrentArg); } +static FunctionProtoTypeLoc GetPrototypeLoc(Expr *Fn) { + if (const auto *T = Fn->getType().getTypePtr()->getAs()) { +const auto *D = T->getDecl(); +if (auto F = D->getTypeSourceInfo()->getTypeLoc().getAs()) { + return F; +} + } + return FunctionProtoTypeLoc(); +} + QualType Sema::ProduceCallSignatureHelp(Expr *Fn, ArrayRef Args, SourceLocation OpenParLoc) { Fn = unwrapParenList(Fn); @@ -6072,6 +6091,8 @@ } else { // Lastly we check whether expression's type is function pointer or // function. + + FunctionProtoTypeLoc P = GetPrototypeLoc(NakedFn); QualType T = NakedFn->getType(); if (!T->getPointeeType().isNull()) T = T->getPointeeType(); @@ -6080,8 +6101,13 @@ if (!TooManyArguments(FP->getNumParams(), ArgsWithoutDependentTypes.size(), /*PartialOverloading=*/true) || -FP->isVariadic()) - Results.push_back(ResultCandidate(FP)); +FP->isVariadic()) { + if (!P.isNull()) { +Results.push_back(ResultCandidate(P)); + } else { +Results.push_back(ResultCandidate(FP)); + } +} } else if (auto FT = T->getAs()) // No prototype and declaration, it may be a K & R style function. Results.push_back(ResultCandidate(FT)); Index: clang/lib/Sema/CodeCompleteConsumer.cpp === --- clang/lib/Sema/CodeCompleteConsumer.cpp +++ clang/lib/Sema/CodeCompleteConsumer.cpp @@ -506,7 +506,8 @@ case CK_FunctionType: return Type; - + case CK_FunctionProtoTypeLoc: +return ProtoTypeLoc.getTypePtr(); case CK_Template: case CK_Aggregate: return nullptr; @@ -515,6 +516,12 @@ llvm_unreachable("Invalid CandidateKind!"); } + const FunctionProtoTypeLoc CodeCompleteConsumer::OverloadCandidate::getFunctionProtoTypeLoc() const { + if (K
[PATCH] D125189: Added parameter names to signature help.
Qwinci created this revision. Herald added subscribers: usaxena95, kadircet, arphaman. Herald added a project: All. Qwinci requested review of this revision. Herald added projects: clang, clang-tools-extra. Herald added a subscriber: cfe-commits. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D125189 Files: clang-tools-extra/clangd/CodeComplete.cpp clang/include/clang/Sema/CodeCompleteConsumer.h clang/lib/Sema/CodeCompleteConsumer.cpp clang/lib/Sema/SemaCodeComplete.cpp Index: clang/lib/Sema/SemaCodeComplete.cpp === --- clang/lib/Sema/SemaCodeComplete.cpp +++ clang/lib/Sema/SemaCodeComplete.cpp @@ -3722,6 +3722,7 @@ const PrintingPolicy &Policy, const FunctionDecl *Function, const FunctionProtoType *Prototype, + FunctionProtoTypeLoc PrototypeLoc, CodeCompletionBuilder &Result, unsigned CurrentArg, unsigned Start = 0, bool InOptional = false) { @@ -3743,7 +3744,7 @@ if (!FirstParameter) Opt.AddChunk(CodeCompletionString::CK_Comma); // Optional sections are nested. - AddOverloadParameterChunks(Context, Policy, Function, Prototype, Opt, + AddOverloadParameterChunks(Context, Policy, Function, Prototype, PrototypeLoc, Opt, CurrentArg, P, /*InOptional=*/true); Result.AddOptionalChunk(Opt.TakeString()); return; @@ -3764,6 +3765,14 @@ if (Param->hasDefaultArg()) Placeholder += GetDefaultValueString(Param, Context.getSourceManager(), Context.getLangOpts()); +} else if (!PrototypeLoc.isNull()) { + if (P < PrototypeLoc.getNumParams()) { +const ParmVarDecl *Param = PrototypeLoc.getParam(P); +Placeholder = FormatFunctionParameter(Policy, Param); +if (Param->hasDefaultArg()) + Placeholder += GetDefaultValueString(Param, Context.getSourceManager(), +Context.getLangOpts()); + } } else { Placeholder = Prototype->getParamType(P).getAsString(Policy); } @@ -3912,7 +3921,7 @@ if (getKind() == CK_Aggregate) AddOverloadAggregateChunks(getAggregate(), Policy, Result, CurrentArg); else -AddOverloadParameterChunks(S.getASTContext(), Policy, FDecl, Proto, Result, +AddOverloadParameterChunks(S.getASTContext(), Policy, FDecl, Proto, getFunctionProtoTypeLoc(), Result, CurrentArg); Result.AddChunk(Braced ? CodeCompletionString::CK_RightBrace : CodeCompletionString::CK_RightParen); @@ -5991,6 +6000,42 @@ return getParamType(SemaRef, Candidates, CurrentArg); } +#include +static FunctionProtoTypeLoc GetPrototypeLoc(Expr *Fn) { + if (const auto *T = Fn->getType().getTypePtr()->getAs()) { +const auto *D = T->getDecl(); + +TypeLoc TypedefTarget = D->getTypeSourceInfo()->getTypeLoc(); +if (auto P = TypedefTarget.getAs()) { + TypedefTarget = P.getPointeeLoc(); +} +if (auto P = TypedefTarget.getAs()) { + TypedefTarget = P.getInnerLoc(); +} +if (auto F = TypedefTarget.getAs()) { + return F; +} + } + else if (const auto *DR = dyn_cast(Fn)) { +const auto *D = DR->getDecl(); +if (const auto *const VD = dyn_cast(D)) { +TypeLoc Target = VD->getTypeSourceInfo()->getTypeLoc(); + +if (auto P = Target.getAs()) { + Target = P.getPointeeLoc(); +} +if (auto P = Target.getAs()) { + Target = P.getInnerLoc(); +} +if (auto F = Target.getAs()) { + return F; +} +} + } + + return FunctionProtoTypeLoc(); +} + QualType Sema::ProduceCallSignatureHelp(Expr *Fn, ArrayRef Args, SourceLocation OpenParLoc) { Fn = unwrapParenList(Fn); @@ -6072,6 +6117,8 @@ } else { // Lastly we check whether expression's type is function pointer or // function. + + FunctionProtoTypeLoc P = GetPrototypeLoc(NakedFn); QualType T = NakedFn->getType(); if (!T->getPointeeType().isNull()) T = T->getPointeeType(); @@ -6080,8 +6127,13 @@ if (!TooManyArguments(FP->getNumParams(), ArgsWithoutDependentTypes.size(), /*PartialOverloading=*/true) || -FP->isVariadic()) - Results.push_back(ResultCandidate(FP)); +FP->isVariadic()) { + if (!P.isNull()) { +Results.push_back(ResultCandidate(P)); + } else { +Results.push_back(ResultCandidate(FP)); + } +} } else if (auto FT = T->getAs())