]> granicus.if.org Git - clang/commitdiff
Audit DeclPrinter with -ast-dump on Cocoa.h and
authorFariborz Jahanian <fjahanian@apple.com>
Wed, 19 Dec 2012 23:36:00 +0000 (23:36 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Wed, 19 Dec 2012 23:36:00 +0000 (23:36 +0000)
fix any bad objectiveC syntax coming out of
DeclPrinter. This is on going. Also, introduce a new
PrintPolicy and use it as needed when declaration tag
is to be produced via DeclPrinter.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@170606 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/AST/PrettyPrinter.h
lib/AST/DeclPrinter.cpp
tools/libclang/CXComment.cpp
unittests/AST/DeclPrinterTest.cpp

index 598a86fa71f27f86e5d12d21830b4259110dbccd..10354034a5e97d5963a9f02960cf97f7352641d3 100644 (file)
@@ -40,7 +40,7 @@ struct PrintingPolicy {
       SuppressUnwrittenScope(false), SuppressInitializers(false),
       ConstantArraySizeAsWritten(false), AnonymousTagLocations(true),
       SuppressStrongLifetime(false), Bool(LO.Bool),
-      TerseOutput(false), SuppressAttributes(false),
+      TerseOutput(false), PolishForDeclaration(false),
       DumpSourceManager(0) { }
 
   /// \brief What language we're printing.
@@ -143,9 +143,10 @@ struct PrintingPolicy {
   /// only the requested declaration.
   unsigned TerseOutput : 1;
   
-  /// \brief When true, do not print attributes attached to the declaration.
+  /// \brief When true, do certain refinement needed for producing proper
+  /// declaration tag; such as, do not print attributes attached to the declaration.
   ///
-  unsigned SuppressAttributes : 1;
+  unsigned PolishForDeclaration : 1;
 
   /// \brief If we are "dumping" rather than "pretty-printing", this points to
   /// a SourceManager which will be used to dump SourceLocations. Dumping
index 34c69059c4b550b1d8a10e557e3eafc05fc0d4fd..572e3d0bc7eae2d958bac603fbbc402587258834 100644 (file)
@@ -193,7 +193,7 @@ raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
 }
 
 void DeclPrinter::prettyPrintAttributes(Decl *D) {
-  if (Policy.SuppressAttributes)
+  if (Policy.PolishForDeclaration)
     return;
   
   if (D->hasAttrs()) {
@@ -915,7 +915,7 @@ void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
     OMD->getBody()->printPretty(Out, 0, Policy);
     Out << '\n';
   }
-  else
+  else if (Policy.PolishForDeclaration)
     Out << ';';
 }
 
@@ -950,7 +950,7 @@ void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
     Out << "@class " << I << ";";
     return;
   }
-  
+  bool eolnOut = false;
   if (SID)
     Out << "@interface " << I << " : " << *SID;
   else
@@ -962,13 +962,12 @@ void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
     for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
          E = Protocols.end(); I != E; ++I)
       Out << (I == Protocols.begin() ? '<' : ',') << **I;
-  }
-
-  if (!Protocols.empty())
     Out << "> ";
+  }
 
   if (OID->ivar_size() > 0) {
     Out << "{\n";
+    eolnOut = true;
     Indentation += Policy.Indentation;
     for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
          E = OID->ivar_end(); I != E; ++I) {
@@ -977,19 +976,33 @@ void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
     Indentation -= Policy.Indentation;
     Out << "}\n";
   }
+  else if (SID) {
+    Out << "\n";
+    eolnOut = true;
+  }
 
   VisitDeclContext(OID, false);
+  if (!eolnOut)
+    Out << ' ';
   Out << "@end";
   // FIXME: implement the rest...
 }
 
 void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
   if (!PID->isThisDeclarationADefinition()) {
-    Out << "@protocol " << PID->getIdentifier() << ";\n";
+    Out << "@protocol " << *PID << ";\n";
     return;
   }
-  
-  Out << "@protocol " << *PID << '\n';
+  // Protocols?
+  const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols();
+  if (!Protocols.empty()) {
+    Out << "@protocol " << *PID;
+    for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
+         E = Protocols.end(); I != E; ++I)
+      Out << (I == Protocols.begin() ? '<' : ',') << **I;
+    Out << ">\n";
+  } else
+    Out << "@protocol " << *PID << '\n';
   VisitDeclContext(PID, false);
   Out << "@end";
 }
@@ -1095,7 +1108,9 @@ void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
     (void) first; // Silence dead store warning due to idiomatic code.
     Out << " )";
   }
-  Out << ' ' << PDecl->getType().getAsString(Policy) << ' ' << *PDecl << ';';
+  Out << ' ' << PDecl->getType().getAsString(Policy) << ' ' << *PDecl;
+  if (Policy.PolishForDeclaration)
+    Out << ';';
 }
 
 void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
index 0656ab824b4ab1d1b15523ab2bfbc759b43968e6..caf6a857f09b9fa5151a9b21f253f040c81626ef 100644 (file)
@@ -908,7 +908,7 @@ void getSourceTextOfDeclaration(const DeclInfo *ThisDecl,
   const LangOptions &LangOpts = Context.getLangOpts();
   llvm::raw_svector_ostream OS(Str);
   PrintingPolicy PPolicy(LangOpts);
-  PPolicy.SuppressAttributes = true;
+  PPolicy.PolishForDeclaration = true;
   PPolicy.TerseOutput = true;
   ThisDecl->CurrentDecl->print(OS, PPolicy,
                                /*Indentation*/0, /*PrintInstantiation*/true);
index e9fc6fdea3d7af657054cb448c307c471f70e92e..844b9a49e13adfbe077a30d5da91285f258fb6cd 100644 (file)
@@ -1234,6 +1234,6 @@ TEST(DeclPrinter, TestObjCMethod1) {
     "@end\n",
     namedDecl(hasName("A:inRange:"),
               hasDescendant(namedDecl(hasName("printThis")))).bind("id"),
-    "- (int) A:(id)anObject inRange:(long)range;"));
+    "- (int) A:(id)anObject inRange:(long)range"));
 }