r318367 - [DeclPrinter] Extract function PrintConstructorInitializers, NFC

Alex Lorenz via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 15 17:31:27 PST 2017


Author: arphaman
Date: Wed Nov 15 17:31:27 2017
New Revision: 318367

URL: http://llvm.org/viewvc/llvm-project?rev=318367&view=rev
Log:
[DeclPrinter] Extract function PrintConstructorInitializers, NFC

Patch by Nikolai Kosjar!

Differential Revision: https://reviews.llvm.org/D40066

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

Modified: cfe/trunk/lib/AST/DeclPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclPrinter.cpp?rev=318367&r1=318366&r2=318367&view=diff
==============================================================================
--- cfe/trunk/lib/AST/DeclPrinter.cpp (original)
+++ cfe/trunk/lib/AST/DeclPrinter.cpp Wed Nov 15 17:31:27 2017
@@ -37,6 +37,8 @@ namespace {
     void ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls);
 
     void Print(AccessSpecifier AS);
+    void PrintConstructorInitializers(CXXConstructorDecl *CDecl,
+                                      std::string &Proto);
 
     /// Print an Objective-C method type in parentheses.
     ///
@@ -274,6 +276,71 @@ void DeclPrinter::Print(AccessSpecifier
   }
 }
 
+void DeclPrinter::PrintConstructorInitializers(CXXConstructorDecl *CDecl,
+                                               std::string &Proto) {
+  bool HasInitializerList = false;
+  for (const auto *BMInitializer : CDecl->inits()) {
+    if (BMInitializer->isInClassMemberInitializer())
+      continue;
+
+    if (!HasInitializerList) {
+      Proto += " : ";
+      Out << Proto;
+      Proto.clear();
+      HasInitializerList = true;
+    } else
+      Out << ", ";
+
+    if (BMInitializer->isAnyMemberInitializer()) {
+      FieldDecl *FD = BMInitializer->getAnyMember();
+      Out << *FD;
+    } else {
+      Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
+    }
+
+    Out << "(";
+    if (!BMInitializer->getInit()) {
+      // Nothing to print
+    } else {
+      Expr *Init = BMInitializer->getInit();
+      if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
+        Init = Tmp->getSubExpr();
+
+      Init = Init->IgnoreParens();
+
+      Expr *SimpleInit = nullptr;
+      Expr **Args = nullptr;
+      unsigned NumArgs = 0;
+      if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
+        Args = ParenList->getExprs();
+        NumArgs = ParenList->getNumExprs();
+      } else if (CXXConstructExpr *Construct =
+                     dyn_cast<CXXConstructExpr>(Init)) {
+        Args = Construct->getArgs();
+        NumArgs = Construct->getNumArgs();
+      } else
+        SimpleInit = Init;
+
+      if (SimpleInit)
+        SimpleInit->printPretty(Out, nullptr, Policy, Indentation);
+      else {
+        for (unsigned I = 0; I != NumArgs; ++I) {
+          assert(Args[I] != nullptr && "Expected non-null Expr");
+          if (isa<CXXDefaultArgExpr>(Args[I]))
+            break;
+
+          if (I)
+            Out << ", ";
+          Args[I]->printPretty(Out, nullptr, Policy, Indentation);
+        }
+      }
+    }
+    Out << ")";
+    if (BMInitializer->isPackExpansion())
+      Out << "...";
+  }
+}
+
 //----------------------------------------------------------------------------
 // Common C declarations
 //----------------------------------------------------------------------------
@@ -608,70 +675,8 @@ void DeclPrinter::VisitFunctionDecl(Func
     }
 
     if (CDecl) {
-      if (!Policy.TerseOutput) {
-        bool HasInitializerList = false;
-        for (const auto *BMInitializer : CDecl->inits()) {
-          if (BMInitializer->isInClassMemberInitializer())
-            continue;
-
-          if (!HasInitializerList) {
-            Proto += " : ";
-            Out << Proto;
-            Proto.clear();
-            HasInitializerList = true;
-          } else
-            Out << ", ";
-
-          if (BMInitializer->isAnyMemberInitializer()) {
-            FieldDecl *FD = BMInitializer->getAnyMember();
-            Out << *FD;
-          } else {
-            Out << QualType(BMInitializer->getBaseClass(), 0)
-                       .getAsString(Policy);
-          }
-
-          Out << "(";
-          if (!BMInitializer->getInit()) {
-            // Nothing to print
-          } else {
-            Expr *Init = BMInitializer->getInit();
-            if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
-              Init = Tmp->getSubExpr();
-
-            Init = Init->IgnoreParens();
-
-            Expr *SimpleInit = nullptr;
-            Expr **Args = nullptr;
-            unsigned NumArgs = 0;
-            if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
-              Args = ParenList->getExprs();
-              NumArgs = ParenList->getNumExprs();
-            } else if (CXXConstructExpr *Construct =
-                           dyn_cast<CXXConstructExpr>(Init)) {
-              Args = Construct->getArgs();
-              NumArgs = Construct->getNumArgs();
-            } else
-              SimpleInit = Init;
-
-            if (SimpleInit)
-              SimpleInit->printPretty(Out, nullptr, Policy, Indentation);
-            else {
-              for (unsigned I = 0; I != NumArgs; ++I) {
-                assert(Args[I] != nullptr && "Expected non-null Expr");
-                if (isa<CXXDefaultArgExpr>(Args[I]))
-                  break;
-
-                if (I)
-                  Out << ", ";
-                Args[I]->printPretty(Out, nullptr, Policy, Indentation);
-              }
-            }
-          }
-          Out << ")";
-          if (BMInitializer->isPackExpansion())
-            Out << "...";
-        }
-      }
+      if (!Policy.TerseOutput)
+        PrintConstructorInitializers(CDecl, Proto);
     } else if (!ConversionDecl && !isa<CXXDestructorDecl>(D)) {
       if (FT && FT->hasTrailingReturn()) {
         if (!GuideDecl)




More information about the cfe-commits mailing list