From 920ae143886231df632ca4b8e24bc55a198e45bb Mon Sep 17 00:00:00 2001 From: Alex Lorenz Date: Tue, 18 Oct 2016 10:38:58 +0000 Subject: [PATCH] [CodeCompletion][NFC] Extract a function that formats block placeholders. This commit extracts a new function named `formatBlockPlaceholder` from the function `FormatFunctionParameter` so that it can be reused in follow-up commits that improve code completion for block property setters. Differential Revision: https://reviews.llvm.org/D25519 llvm-svn: 284468 --- clang/lib/Sema/SemaCodeComplete.cpp | 47 +++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/clang/lib/Sema/SemaCodeComplete.cpp b/clang/lib/Sema/SemaCodeComplete.cpp index 8b89861..6000fc6 100644 --- a/clang/lib/Sema/SemaCodeComplete.cpp +++ b/clang/lib/Sema/SemaCodeComplete.cpp @@ -2209,6 +2209,12 @@ static void findTypeLocationForBlockDecl(const TypeSourceInfo *TSInfo, } } +static std::string +formatBlockPlaceholder(const PrintingPolicy &Policy, const NamedDecl *BlockDecl, + FunctionTypeLoc &Block, FunctionProtoTypeLoc &BlockProto, + bool SuppressBlock = false, + Optional> ObjCSubsts = None); + static std::string FormatFunctionParameter(const PrintingPolicy &Policy, const ParmVarDecl *Param, bool SuppressName = false, @@ -2271,12 +2277,30 @@ static std::string FormatFunctionParameter(const PrintingPolicy &Policy, // We have the function prototype behind the block pointer type, as it was // written in the source. + return formatBlockPlaceholder(Policy, Param, Block, BlockProto, SuppressBlock, + ObjCSubsts); +} + +/// \brief Returns a placeholder string that corresponds to an Objective-C block +/// declaration. +/// +/// \param BlockDecl A declaration with an Objective-C block type. +/// +/// \param Block The most relevant type location for that block type. +/// +/// \param SuppressBlockName Determines wether or not the name of the block +/// declaration is included in the resulting string. +static std::string +formatBlockPlaceholder(const PrintingPolicy &Policy, const NamedDecl *BlockDecl, + FunctionTypeLoc &Block, FunctionProtoTypeLoc &BlockProto, + bool SuppressBlock, + Optional> ObjCSubsts) { std::string Result; QualType ResultType = Block.getTypePtr()->getReturnType(); if (ObjCSubsts) - ResultType = ResultType.substObjCTypeArgs(Param->getASTContext(), - *ObjCSubsts, - ObjCSubstitutionContext::Result); + ResultType = + ResultType.substObjCTypeArgs(BlockDecl->getASTContext(), *ObjCSubsts, + ObjCSubstitutionContext::Result); if (!ResultType->isVoidType() || SuppressBlock) ResultType.getAsStringInternal(Result, Policy); @@ -2294,31 +2318,30 @@ static std::string FormatFunctionParameter(const PrintingPolicy &Policy, Params += ", "; Params += FormatFunctionParameter(Policy, Block.getParam(I), /*SuppressName=*/false, - /*SuppressBlock=*/true, - ObjCSubsts); + /*SuppressBlock=*/true, ObjCSubsts); if (I == N - 1 && BlockProto.getTypePtr()->isVariadic()) Params += ", ..."; } Params += ")"; } - + if (SuppressBlock) { // Format as a parameter. Result = Result + " (^"; - if (Param->getIdentifier()) - Result += Param->getIdentifier()->getName(); + if (BlockDecl->getIdentifier()) + Result += BlockDecl->getIdentifier()->getName(); Result += ")"; Result += Params; } else { // Format as a block literal argument. Result = '^' + Result; Result += Params; - - if (Param->getIdentifier()) - Result += Param->getIdentifier()->getName(); + + if (BlockDecl->getIdentifier()) + Result += BlockDecl->getIdentifier()->getName(); } - + return Result; } -- 2.7.4