r279691 - [Sema][Comments] Factor out function type loc logic. NFCI

Bruno Cardoso Lopes via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 24 17:22:08 PDT 2016


Author: bruno
Date: Wed Aug 24 19:22:08 2016
New Revision: 279691

URL: http://llvm.org/viewvc/llvm-project?rev=279691&view=rev
Log:
[Sema][Comments] Factor out function type loc logic. NFCI

This is in prepatation for @param TypeAliasTemplate support.

Modified:
    cfe/trunk/lib/AST/Comment.cpp

Modified: cfe/trunk/lib/AST/Comment.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Comment.cpp?rev=279691&r1=279690&r2=279691&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Comment.cpp (original)
+++ cfe/trunk/lib/AST/Comment.cpp Wed Aug 24 19:22:08 2016
@@ -113,6 +113,65 @@ bool ParagraphComment::isWhitespaceNoCac
   return true;
 }
 
+static TypeLoc lookThroughTypedefOrTypeAliasLocs(TypeLoc &SrcTL) {
+  TypeLoc TL = SrcTL.IgnoreParens();
+
+  // Look through qualified types.
+  if (QualifiedTypeLoc QualifiedTL = TL.getAs<QualifiedTypeLoc>())
+    return QualifiedTL.getUnqualifiedLoc();
+  // Look through pointer types.
+  if (PointerTypeLoc PointerTL = TL.getAs<PointerTypeLoc>())
+    return PointerTL.getPointeeLoc().getUnqualifiedLoc();
+  // Look through reference types.
+  if (ReferenceTypeLoc ReferenceTL = TL.getAs<ReferenceTypeLoc>())
+    return ReferenceTL.getPointeeLoc().getUnqualifiedLoc();
+  // Look through adjusted types.
+  if (AdjustedTypeLoc ATL = TL.getAs<AdjustedTypeLoc>())
+    return ATL.getOriginalLoc();
+  if (BlockPointerTypeLoc BlockPointerTL = TL.getAs<BlockPointerTypeLoc>())
+    return BlockPointerTL.getPointeeLoc().getUnqualifiedLoc();
+  if (MemberPointerTypeLoc MemberPointerTL = TL.getAs<MemberPointerTypeLoc>())
+    return MemberPointerTL.getPointeeLoc().getUnqualifiedLoc();
+  if (ElaboratedTypeLoc ETL = TL.getAs<ElaboratedTypeLoc>())
+    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<FunctionTypeLoc>()) {
+    ResFTL = FTL;
+    return true;
+  }
+
+  if (TemplateSpecializationTypeLoc STL =
+          TL.getAs<TemplateSpecializationTypeLoc>()) {
+    // 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<FunctionTypeLoc>()) {
+      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<QualifiedTypeLoc>()) {
-        TL = QualifiedTL.getUnqualifiedLoc();
-        continue;
-      }
-      // Look through pointer types.
-      if (PointerTypeLoc PointerTL = TL.getAs<PointerTypeLoc>()) {
-        TL = PointerTL.getPointeeLoc().getUnqualifiedLoc();
-        continue;
-      }
-      // Look through reference types.
-      if (ReferenceTypeLoc ReferenceTL = TL.getAs<ReferenceTypeLoc>()) {
-        TL = ReferenceTL.getPointeeLoc().getUnqualifiedLoc();
-        continue;
-      }
-      // Look through adjusted types.
-      if (AdjustedTypeLoc ATL = TL.getAs<AdjustedTypeLoc>()) {
-        TL = ATL.getOriginalLoc();
-        continue;
-      }
-      if (BlockPointerTypeLoc BlockPointerTL =
-              TL.getAs<BlockPointerTypeLoc>()) {
-        TL = BlockPointerTL.getPointeeLoc().getUnqualifiedLoc();
-        continue;
-      }
-      if (MemberPointerTypeLoc MemberPointerTL =
-              TL.getAs<MemberPointerTypeLoc>()) {
-        TL = MemberPointerTL.getPointeeLoc().getUnqualifiedLoc();
-        continue;
-      }
-      if (ElaboratedTypeLoc ETL = TL.getAs<ElaboratedTypeLoc>()) {
-        TL = ETL.getNamedTypeLoc();
-        continue;
-      }
-      // Is this a typedef for a function type?
-      if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
-        Kind = FunctionKind;
-        ParamVars = FTL.getParams();
-        ReturnType = FTL.getReturnLoc().getType();
-        break;
-      }
-      if (TemplateSpecializationTypeLoc STL =
-              TL.getAs<TemplateSpecializationTypeLoc>()) {
-        // 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<FunctionTypeLoc>()) {
-          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;
   }




More information about the cfe-commits mailing list