Create a new PrintingPolicy class, which we pass down through the AST
printing logic to help customize the output. For now, we use this
rather than a special flag to suppress the "struct" when printing
"struct X" and to print the Boolean type as "bool" in C++ but "_Bool"
in C.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@72590 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Frontend/ASTConsumers.cpp b/lib/Frontend/ASTConsumers.cpp
index ba482b0..c35f4c9 100644
--- a/lib/Frontend/ASTConsumers.cpp
+++ b/lib/Frontend/ASTConsumers.cpp
@@ -20,6 +20,7 @@
 #include "clang/AST/AST.h"
 #include "clang/AST/ASTConsumer.h"
 #include "clang/AST/ASTContext.h"
+#include "clang/AST/PrettyPrinter.h"
 #include "clang/CodeGen/ModuleBuilder.h"
 #include "llvm/Module.h"
 #include "llvm/Support/Streams.h"
@@ -35,10 +36,13 @@
   class DeclPrinter {
   public:
     llvm::raw_ostream& Out;
+    PrintingPolicy Policy;
     unsigned Indentation;
 
-    DeclPrinter(llvm::raw_ostream* out) : Out(out ? *out : llvm::errs()),
-                                          Indentation(0) {}
+    DeclPrinter(llvm::raw_ostream* out,
+                const PrintingPolicy &Policy = PrintingPolicy()) 
+      : Out(out ? *out : llvm::errs()), Policy(Policy),
+        Indentation(0) {}
     DeclPrinter() : Out(llvm::errs()), Indentation(0) {}
     virtual ~DeclPrinter();
     
@@ -84,7 +88,7 @@
     // FIXME: Pass a context here so we can use getBody()
     if (FD->getBodyIfAvailable()) {
       Out << ' ';
-      FD->getBodyIfAvailable()->printPretty(Out, 0, Indentation, true);
+      FD->getBodyIfAvailable()->printPretty(Out, 0, Policy);
       Out << '\n';
     }
   } else if (isa<ObjCMethodDecl>(D)) {
@@ -162,7 +166,7 @@
     PrintLinkageSpec(LSD);
   } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) {
     Out << "asm(";
-    AD->getAsmString()->printPretty(Out);
+    AD->getAsmString()->printPretty(Out, 0, Policy);
     Out << ")\n";
   } else if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
     Print(ND);
@@ -193,12 +197,12 @@
     }
     std::string Name = ND->getNameAsString();
     // This forms: "int a".
-    dyn_cast<ValueDecl>(ND)->getType().getAsStringInternal(Name);
+    dyn_cast<ValueDecl>(ND)->getType().getAsStringInternal(Name, Policy);
     Out << Name;
     if (VarDecl *Var = dyn_cast<VarDecl>(ND)) {
       if (Var->getInit()) {
         Out << " = ";
-        Var->getInit()->printPretty(Out);
+        Var->getInit()->printPretty(Out, 0, Policy);
       }
     }
     Out << ";\n";
@@ -252,7 +256,7 @@
       std::string ParamStr;
       if (HasBody) ParamStr = FD->getParamDecl(i)->getNameAsString();
       
-      FT->getArgType(i).getAsStringInternal(ParamStr);
+      FT->getArgType(i).getAsStringInternal(ParamStr, Policy);
       Proto += ParamStr;
     }
     
@@ -266,7 +270,7 @@
     Proto += "()";
   }
 
-  AFT->getResultType().getAsStringInternal(Proto);
+  AFT->getResultType().getAsStringInternal(Proto, Policy);
   Out << Proto;
   
   if (!FD->getBodyIfAvailable())
@@ -276,7 +280,7 @@
 
 void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) {
   std::string S = TD->getNameAsString();
-  TD->getUnderlyingType().getAsStringInternal(S);
+  TD->getUnderlyingType().getAsStringInternal(S, Policy);
   Out << "typedef " << S << ";\n";
 }
 
@@ -358,7 +362,7 @@
     PrintObjCMethodDecl(OMD);
     if (OMD->getBody()) {
       Out << ' ';
-      OMD->getBody()->printPretty(Out);
+      OMD->getBody()->printPretty(Out, 0, Policy);
       Out << '\n';
     }
   }
@@ -371,7 +375,7 @@
     PrintObjCMethodDecl(OMD);
     if (OMD->getBody()) {
       Out << ' ';
-      OMD->getBody()->printPretty(Out);
+      OMD->getBody()->printPretty(Out, 0, Policy);
       Out << '\n';
     }
   }
diff --git a/lib/Frontend/DocumentXML.cpp b/lib/Frontend/DocumentXML.cpp
index 80060f5..ac1d7d2 100644
--- a/lib/Frontend/DocumentXML.cpp
+++ b/lib/Frontend/DocumentXML.cpp
@@ -194,7 +194,7 @@
     // don't use the get methods as they strip of typedef infos
     if (const BuiltinType *BT = dyn_cast<BuiltinType>(i->first)) {
       addSubNode("FundamentalType");
-      addAttribute("name", BT->getName());
+      addAttribute("name", BT->getName(Ctx->getLangOptions().CPlusPlus));
     }
     else if (const PointerType *PT = dyn_cast<PointerType>(i->first)) {
       addSubNode("PointerType");
diff --git a/lib/Frontend/RewriteBlocks.cpp b/lib/Frontend/RewriteBlocks.cpp
index 29f0578..6d0f10d 100644
--- a/lib/Frontend/RewriteBlocks.cpp
+++ b/lib/Frontend/RewriteBlocks.cpp
@@ -396,7 +396,7 @@
          E = BD->param_end(); AI != E; ++AI) {
       if (AI != BD->param_begin()) S += ", ";
       ParamStr = (*AI)->getNameAsString();
-      (*AI)->getType().getAsStringInternal(ParamStr);
+      (*AI)->getType().getAsStringInternal(ParamStr, Context->PrintingPolicy);
       S += ParamStr;
     }
     if (FT->isVariadic()) {
@@ -413,7 +413,8 @@
        E = BlockByRefDecls.end(); I != E; ++I) {
     S += "  ";
     std::string Name = (*I)->getNameAsString();
-    Context->getPointerType((*I)->getType()).getAsStringInternal(Name);
+    Context->getPointerType((*I)->getType()).getAsStringInternal(Name,
+                                                     Context->PrintingPolicy);
     S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n";
   }    
   // Next, emit a declaration for all "by copy" declarations.
@@ -434,7 +435,7 @@
     if (isBlockPointerType((*I)->getType()))
       S += "struct __block_impl *";
     else
-      (*I)->getType().getAsStringInternal(Name);
+      (*I)->getType().getAsStringInternal(Name, Context->PrintingPolicy);
     S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by copy\n";
   }
   std::string RewrittenStr = RewrittenBlockExprs[CE];
@@ -515,8 +516,8 @@
         S += "struct __block_impl *";
         Constructor += ", void *" + ArgName;
       } else {
-        (*I)->getType().getAsStringInternal(FieldName);
-        (*I)->getType().getAsStringInternal(ArgName);
+        (*I)->getType().getAsStringInternal(FieldName, Context->PrintingPolicy);
+        (*I)->getType().getAsStringInternal(ArgName, Context->PrintingPolicy);
         Constructor += ", " + ArgName;
       }
       S += FieldName + ";\n";
@@ -541,8 +542,10 @@
         S += "struct __block_impl *";
         Constructor += ", void *" + ArgName;
       } else {
-        Context->getPointerType((*I)->getType()).getAsStringInternal(FieldName);
-        Context->getPointerType((*I)->getType()).getAsStringInternal(ArgName);
+        Context->getPointerType((*I)->getType()).getAsStringInternal(FieldName,
+                                                       Context->PrintingPolicy);
+        Context->getPointerType((*I)->getType()).getAsStringInternal(ArgName,
+                                                       Context->PrintingPolicy);
         Constructor += ", " + ArgName;
       }
       S += FieldName + "; // by ref\n";
@@ -947,7 +950,8 @@
   std::string FunkTypeStr;
   
   // Get a pointer to the function type so we can cast appropriately.
-  Context->getPointerType(QualType(Exp->getFunctionType(),0)).getAsStringInternal(FunkTypeStr);
+  Context->getPointerType(QualType(Exp->getFunctionType(),0))
+    .getAsStringInternal(FunkTypeStr, Context->PrintingPolicy);
   
   // Rewrite the closure block with a compound literal. The first cast is
   // to prevent warnings from the C compiler.
diff --git a/lib/Frontend/RewriteObjC.cpp b/lib/Frontend/RewriteObjC.cpp
index 79ae08c..55f0b40 100644
--- a/lib/Frontend/RewriteObjC.cpp
+++ b/lib/Frontend/RewriteObjC.cpp
@@ -944,9 +944,10 @@
       if (isTopLevelBlockPointerType(PDecl->getType())) {
         // Make sure we convert "t (^)(...)" to "t (*)(...)".
         const BlockPointerType *BPT = PDecl->getType()->getAsBlockPointerType();
-        Context->getPointerType(BPT->getPointeeType()).getAsStringInternal(Name);
+        Context->getPointerType(BPT->getPointeeType()).getAsStringInternal(Name,
+                                                        Context->PrintingPolicy);
       } else
-        PDecl->getType().getAsStringInternal(Name);
+        PDecl->getType().getAsStringInternal(Name, Context->PrintingPolicy);
       ResultStr += Name;
     }
   }
@@ -3600,7 +3601,7 @@
          E = BD->param_end(); AI != E; ++AI) {
       if (AI != BD->param_begin()) S += ", ";
       ParamStr = (*AI)->getNameAsString();
-      (*AI)->getType().getAsStringInternal(ParamStr);
+      (*AI)->getType().getAsStringInternal(ParamStr, Context->PrintingPolicy);
       S += ParamStr;
     }
     if (FT->isVariadic()) {
@@ -3617,7 +3618,8 @@
        E = BlockByRefDecls.end(); I != E; ++I) {
     S += "  ";
     std::string Name = (*I)->getNameAsString();
-    Context->getPointerType((*I)->getType()).getAsStringInternal(Name);
+    Context->getPointerType((*I)->getType()).getAsStringInternal(Name, 
+                                                      Context->PrintingPolicy);
     S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n";
   }    
   // Next, emit a declaration for all "by copy" declarations.
@@ -3638,7 +3640,7 @@
     if (isTopLevelBlockPointerType((*I)->getType()))
       S += "struct __block_impl *";
     else
-      (*I)->getType().getAsStringInternal(Name);
+      (*I)->getType().getAsStringInternal(Name, Context->PrintingPolicy);
     S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by copy\n";
   }
   std::string RewrittenStr = RewrittenBlockExprs[CE];
@@ -3719,8 +3721,8 @@
         S += "struct __block_impl *";
         Constructor += ", void *" + ArgName;
       } else {
-        (*I)->getType().getAsStringInternal(FieldName);
-        (*I)->getType().getAsStringInternal(ArgName);
+        (*I)->getType().getAsStringInternal(FieldName, Context->PrintingPolicy);
+        (*I)->getType().getAsStringInternal(ArgName, Context->PrintingPolicy);
         Constructor += ", " + ArgName;
       }
       S += FieldName + ";\n";
@@ -3745,8 +3747,10 @@
         S += "struct __block_impl *";
         Constructor += ", void *" + ArgName;
       } else {
-        Context->getPointerType((*I)->getType()).getAsStringInternal(FieldName);
-        Context->getPointerType((*I)->getType()).getAsStringInternal(ArgName);
+        Context->getPointerType((*I)->getType()).getAsStringInternal(FieldName, 
+                                                       Context->PrintingPolicy);
+        Context->getPointerType((*I)->getType()).getAsStringInternal(ArgName, 
+                                                       Context->PrintingPolicy);
         Constructor += ", " + ArgName;
       }
       S += FieldName + "; // by ref\n";