From 72ae62c1c0cb31e6478b73fcf241d1fab0485ccb Mon Sep 17 00:00:00 2001 From: Bruno Cardoso Lopes Date: Thu, 25 Aug 2016 00:22:08 +0000 Subject: [PATCH] [Sema][Comments] Factor out function type loc logic. NFCI This is in prepatation for @param TypeAliasTemplate support. llvm-svn: 279691 --- clang/lib/AST/Comment.cpp | 128 +++++++++++++++++++++++----------------------- 1 file changed, 64 insertions(+), 64 deletions(-) diff --git a/clang/lib/AST/Comment.cpp b/clang/lib/AST/Comment.cpp index 89a7b7f..30710422 100644 --- a/clang/lib/AST/Comment.cpp +++ b/clang/lib/AST/Comment.cpp @@ -113,6 +113,65 @@ bool ParagraphComment::isWhitespaceNoCache() const { return true; } +static TypeLoc lookThroughTypedefOrTypeAliasLocs(TypeLoc &SrcTL) { + TypeLoc TL = SrcTL.IgnoreParens(); + + // Look through qualified types. + if (QualifiedTypeLoc QualifiedTL = TL.getAs()) + return QualifiedTL.getUnqualifiedLoc(); + // Look through pointer types. + if (PointerTypeLoc PointerTL = TL.getAs()) + return PointerTL.getPointeeLoc().getUnqualifiedLoc(); + // Look through reference types. + if (ReferenceTypeLoc ReferenceTL = TL.getAs()) + return ReferenceTL.getPointeeLoc().getUnqualifiedLoc(); + // Look through adjusted types. + if (AdjustedTypeLoc ATL = TL.getAs()) + return ATL.getOriginalLoc(); + if (BlockPointerTypeLoc BlockPointerTL = TL.getAs()) + return BlockPointerTL.getPointeeLoc().getUnqualifiedLoc(); + if (MemberPointerTypeLoc MemberPointerTL = TL.getAs()) + return MemberPointerTL.getPointeeLoc().getUnqualifiedLoc(); + if (ElaboratedTypeLoc ETL = TL.getAs()) + return ETL.getNamedTypeLoc(); + + return TL; +} + +static bool getFunctionTypeLoc(TypeLoc TL, FunctionTypeLoc &ResFTL) { + TypeLoc PrevTL; + while (PrevTL != TL) { + PrevTL = TL; + TL = lookThroughTypedefOrTypeAliasLocs(TL); + } + + if (FunctionTypeLoc FTL = TL.getAs()) { + ResFTL = FTL; + return true; + } + + if (TemplateSpecializationTypeLoc STL = + TL.getAs()) { + // If we have a typedef to a template specialization with exactly one + // template argument of a function type, this looks like std::function, + // boost::function, or other function wrapper. Treat these typedefs as + // functions. + if (STL.getNumArgs() != 1) + return false; + TemplateArgumentLoc MaybeFunction = STL.getArgLoc(0); + if (MaybeFunction.getArgument().getKind() != TemplateArgument::Type) + return false; + TypeSourceInfo *MaybeFunctionTSI = MaybeFunction.getTypeSourceInfo(); + TypeLoc TL = MaybeFunctionTSI->getTypeLoc().getUnqualifiedLoc(); + if (FunctionTypeLoc FTL = TL.getAs()) { + ResFTL = FTL; + return true; + } + } + + return false; +} + const char *ParamCommandComment::getDirectionAsString(PassDirection D) { switch (D) { case ParamCommandComment::In: @@ -238,70 +297,11 @@ void DeclInfo::fill() { if (!TSI) break; TypeLoc TL = TSI->getTypeLoc().getUnqualifiedLoc(); - while (true) { - TL = TL.IgnoreParens(); - // Look through qualified types. - if (QualifiedTypeLoc QualifiedTL = TL.getAs()) { - TL = QualifiedTL.getUnqualifiedLoc(); - continue; - } - // Look through pointer types. - if (PointerTypeLoc PointerTL = TL.getAs()) { - TL = PointerTL.getPointeeLoc().getUnqualifiedLoc(); - continue; - } - // Look through reference types. - if (ReferenceTypeLoc ReferenceTL = TL.getAs()) { - TL = ReferenceTL.getPointeeLoc().getUnqualifiedLoc(); - continue; - } - // Look through adjusted types. - if (AdjustedTypeLoc ATL = TL.getAs()) { - TL = ATL.getOriginalLoc(); - continue; - } - if (BlockPointerTypeLoc BlockPointerTL = - TL.getAs()) { - TL = BlockPointerTL.getPointeeLoc().getUnqualifiedLoc(); - continue; - } - if (MemberPointerTypeLoc MemberPointerTL = - TL.getAs()) { - TL = MemberPointerTL.getPointeeLoc().getUnqualifiedLoc(); - continue; - } - if (ElaboratedTypeLoc ETL = TL.getAs()) { - TL = ETL.getNamedTypeLoc(); - continue; - } - // Is this a typedef for a function type? - if (FunctionTypeLoc FTL = TL.getAs()) { - Kind = FunctionKind; - ParamVars = FTL.getParams(); - ReturnType = FTL.getReturnLoc().getType(); - break; - } - if (TemplateSpecializationTypeLoc STL = - TL.getAs()) { - // If we have a typedef to a template specialization with exactly one - // template argument of a function type, this looks like std::function, - // boost::function, or other function wrapper. Treat these typedefs as - // functions. - if (STL.getNumArgs() != 1) - break; - TemplateArgumentLoc MaybeFunction = STL.getArgLoc(0); - if (MaybeFunction.getArgument().getKind() != TemplateArgument::Type) - break; - TypeSourceInfo *MaybeFunctionTSI = MaybeFunction.getTypeSourceInfo(); - TypeLoc TL = MaybeFunctionTSI->getTypeLoc().getUnqualifiedLoc(); - if (FunctionTypeLoc FTL = TL.getAs()) { - Kind = FunctionKind; - ParamVars = FTL.getParams(); - ReturnType = FTL.getReturnLoc().getType(); - } - break; - } - break; + FunctionTypeLoc FTL; + if (getFunctionTypeLoc(TL, FTL)) { + Kind = FunctionKind; + ParamVars = FTL.getParams(); + ReturnType = FTL.getReturnLoc().getType(); } break; } -- 2.7.4