[cfe-commits] r43501 - in /cfe/trunk: AST/Decl.cpp Driver/RewriteTest.cpp Parse/ParseObjc.cpp Sema/Sema.h Sema/SemaDecl.cpp include/clang/AST/DeclObjC.h include/clang/Parse/Action.h

Steve Naroff snaroff at apple.com
Tue Oct 30 06:30:57 PDT 2007


Author: snaroff
Date: Tue Oct 30 08:30:57 2007
New Revision: 43501

URL: http://llvm.org/viewvc/llvm-project?rev=43501&view=rev
Log:

- Add location info to category/protocol AST's
- Rewrite categories.


Modified:
    cfe/trunk/AST/Decl.cpp
    cfe/trunk/Driver/RewriteTest.cpp
    cfe/trunk/Parse/ParseObjc.cpp
    cfe/trunk/Sema/Sema.h
    cfe/trunk/Sema/SemaDecl.cpp
    cfe/trunk/include/clang/AST/DeclObjC.h
    cfe/trunk/include/clang/Parse/Action.h

Modified: cfe/trunk/AST/Decl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/AST/Decl.cpp?rev=43501&r1=43500&r2=43501&view=diff

==============================================================================
--- cfe/trunk/AST/Decl.cpp (original)
+++ cfe/trunk/AST/Decl.cpp Tue Oct 30 08:30:57 2007
@@ -345,7 +345,7 @@
                                   unsigned numInsMembers,
                                   ObjcMethodDecl **clsMethods,
                                   unsigned numClsMembers,
-                                  SourceLocation AtEndLoc) {
+                                  SourceLocation endLoc) {
   NumInstanceMethods = numInsMembers;
   if (numInsMembers) {
     InstanceMethods = new ObjcMethodDecl*[numInsMembers];
@@ -356,6 +356,7 @@
     ClassMethods = new ObjcMethodDecl*[numClsMembers];
     memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
   }
+  AtEndLoc = endLoc;
 }
 
 /// addMethods - Insert instance and methods declarations into
@@ -365,7 +366,7 @@
                                   unsigned numInsMembers,
                                   ObjcMethodDecl **clsMethods,
                                   unsigned numClsMembers,
-                                  SourceLocation AtEndLoc) {
+                                  SourceLocation endLoc) {
   NumInstanceMethods = numInsMembers;
   if (numInsMembers) {
     InstanceMethods = new ObjcMethodDecl*[numInsMembers];
@@ -376,6 +377,7 @@
     ClassMethods = new ObjcMethodDecl*[numClsMembers];
     memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
   }
+  AtEndLoc = endLoc;
 }
 
 /// addMethods - Insert instance and methods declarations into

Modified: cfe/trunk/Driver/RewriteTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/RewriteTest.cpp?rev=43501&r1=43500&r2=43501&view=diff

==============================================================================
--- cfe/trunk/Driver/RewriteTest.cpp (original)
+++ cfe/trunk/Driver/RewriteTest.cpp Tue Oct 30 08:30:57 2007
@@ -60,6 +60,8 @@
     void RewriteTabs();
     void RewriteForwardClassDecl(ObjcClassDecl *Dcl);
     void RewriteInterfaceDecl(ObjcInterfaceDecl *Dcl);
+    void RewriteCategoryDecl(ObjcCategoryDecl *Dcl);
+    void RewriteMethods(int nMethods, ObjcMethodDecl **Methods);
     
     // Expression Rewriting.
     Stmt *RewriteFunctionBody(Stmt *S);
@@ -122,6 +124,8 @@
       SelGetUidFunctionDecl = FD;
   } else if (ObjcInterfaceDecl *MD = dyn_cast<ObjcInterfaceDecl>(D)) {
     RewriteInterfaceDecl(MD);
+  } else if (ObjcCategoryDecl *CD = dyn_cast<ObjcCategoryDecl>(D)) {
+    RewriteCategoryDecl(CD);
   }
   // If we have a decl in the main file, see if we should rewrite it.
   if (SM->getDecomposedFileLoc(Loc).first == MainFileID)
@@ -264,6 +268,31 @@
                       typedefString.c_str(), typedefString.size());
 }
 
+void RewriteTest::RewriteMethods(int nMethods, ObjcMethodDecl **Methods) {
+  for (int i = 0; i < nMethods; i++) {
+    ObjcMethodDecl *Method = Methods[i];
+    SourceLocation Loc = Method->getLocStart();
+
+    Rewrite.ReplaceText(Loc, 0, "// ", 3);
+    
+    // FIXME: handle methods that are declared across multiple lines.
+  }
+}
+
+void RewriteTest::RewriteCategoryDecl(ObjcCategoryDecl *CatDecl) {
+  SourceLocation LocStart = CatDecl->getLocStart();
+  
+  // FIXME: handle category headers that are declared across multiple lines.
+  Rewrite.ReplaceText(LocStart, 0, "// ", 3);
+  
+  RewriteMethods(CatDecl->getNumInstanceMethods(),
+                 CatDecl->getInstanceMethods());
+  RewriteMethods(CatDecl->getNumClassMethods(),
+                 CatDecl->getClassMethods());
+  // Lastly, comment out the @end.
+  Rewrite.ReplaceText(CatDecl->getAtEndLoc(), 0, "// ", 3);
+}
+
 void RewriteTest::RewriteInterfaceDecl(ObjcInterfaceDecl *ClassDecl) {
 
   SourceLocation LocStart = ClassDecl->getLocStart();
@@ -280,28 +309,11 @@
   Rewrite.ReplaceText(LocStart, endBuf-startBuf, 
                       ResultStr.c_str(), ResultStr.size());
   
-  int nInstanceMethods = ClassDecl->getNumInstanceMethods();
-  ObjcMethodDecl **instanceMethods = ClassDecl->getInstanceMethods();
+  RewriteMethods(ClassDecl->getNumInstanceMethods(),
+                 ClassDecl->getInstanceMethods());
+  RewriteMethods(ClassDecl->getNumClassMethods(),
+                 ClassDecl->getClassMethods());
   
-  for (int i = 0; i < nInstanceMethods; i++) {
-    ObjcMethodDecl *instanceMethod = instanceMethods[i];
-    SourceLocation Loc = instanceMethod->getLocStart();
-
-    Rewrite.ReplaceText(Loc, 0, "// ", 3);
-    
-    // FIXME: handle methods that are declared across multiple lines.
-  }
-  int nClassMethods = ClassDecl->getNumClassMethods();
-  ObjcMethodDecl **classMethods = ClassDecl->getClassMethods();
-  
-  for (int i = 0; i < nClassMethods; i++) {
-    ObjcMethodDecl *classMethod = classMethods[i];
-    SourceLocation Loc = classMethod->getLocStart();
-
-    Rewrite.ReplaceText(Loc, 0, "// ", 3);
-    
-    // FIXME: handle methods that are declared across multiple lines.
-  }
   // Lastly, comment out the @end.
   Rewrite.ReplaceText(ClassDecl->getAtEndLoc(), 0, "// ", 3);
 }

Modified: cfe/trunk/Parse/ParseObjc.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Parse/ParseObjc.cpp?rev=43501&r1=43500&r2=43501&view=diff

==============================================================================
--- cfe/trunk/Parse/ParseObjc.cpp (original)
+++ cfe/trunk/Parse/ParseObjc.cpp Tue Oct 30 08:30:57 2007
@@ -157,7 +157,8 @@
     
     DeclTy *CategoryType = Actions.ActOnStartCategoryInterface(atLoc, 
                                      nameId, nameLoc, categoryId, categoryLoc,
-                                     &ProtocolRefs[0], ProtocolRefs.size());
+                                     &ProtocolRefs[0], ProtocolRefs.size(),
+                                     endProtoLoc);
     
     ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
 

Modified: cfe/trunk/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/Sema.h?rev=43501&r1=43500&r2=43501&view=diff

==============================================================================
--- cfe/trunk/Sema/Sema.h (original)
+++ cfe/trunk/Sema/Sema.h Tue Oct 30 08:30:57 2007
@@ -489,7 +489,8 @@
 		    SourceLocation AtInterfaceLoc,
                     IdentifierInfo *ClassName, SourceLocation ClassLoc,
                     IdentifierInfo *CategoryName, SourceLocation CategoryLoc,
-                    IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs);
+                    IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs,
+                    SourceLocation EndProtoLoc);
   
   virtual DeclTy *ActOnStartClassImplementation(
 		    SourceLocation AtClassImplLoc,

Modified: cfe/trunk/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/SemaDecl.cpp?rev=43501&r1=43500&r2=43501&view=diff

==============================================================================
--- cfe/trunk/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/Sema/SemaDecl.cpp Tue Oct 30 08:30:57 2007
@@ -1081,16 +1081,18 @@
     ObjcProtocols[ProtocolName] = PDecl;
   }    
   
-  /// Check then save referenced protocols
-  for (unsigned int i = 0; i != NumProtoRefs; i++) {
-    ObjcProtocolDecl* RefPDecl = ObjcProtocols[ProtoRefNames[i]];
-    if (!RefPDecl || RefPDecl->isForwardDecl())
-      Diag(ProtocolLoc, diag::err_undef_protocolref,
-           ProtoRefNames[i]->getName(),
-           ProtocolName->getName());
-    PDecl->setReferencedProtocols((int)i, RefPDecl);
+  if (NumProtoRefs) {
+    /// Check then save referenced protocols
+    for (unsigned int i = 0; i != NumProtoRefs; i++) {
+      ObjcProtocolDecl* RefPDecl = ObjcProtocols[ProtoRefNames[i]];
+      if (!RefPDecl || RefPDecl->isForwardDecl())
+        Diag(ProtocolLoc, diag::err_undef_protocolref,
+             ProtoRefNames[i]->getName(),
+             ProtocolName->getName());
+      PDecl->setReferencedProtocols((int)i, RefPDecl);
+    }
+    PDecl->setLocEnd(EndProtoLoc);
   }
-
   return PDecl;
 }
 
@@ -1137,7 +1139,8 @@
                       SourceLocation AtInterfaceLoc,
                       IdentifierInfo *ClassName, SourceLocation ClassLoc,
                       IdentifierInfo *CategoryName, SourceLocation CategoryLoc,
-                      IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs) {
+                      IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs,
+                      SourceLocation EndProtoLoc) {
   ObjcInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
   
   /// Check that class of this category is already completely declared.
@@ -1161,17 +1164,19 @@
   if (!CDeclChain)
     CDecl->insertNextClassCategory();
 
-  /// Check then save referenced protocols
-  for (unsigned int i = 0; i != NumProtoRefs; i++) {
-    ObjcProtocolDecl* RefPDecl = ObjcProtocols[ProtoRefNames[i]];
-    if (!RefPDecl || RefPDecl->isForwardDecl()) {
-      Diag(CategoryLoc, diag::err_undef_protocolref,
-           ProtoRefNames[i]->getName(),
-           CategoryName->getName());
+  if (NumProtoRefs) {
+    /// Check then save referenced protocols
+    for (unsigned int i = 0; i != NumProtoRefs; i++) {
+      ObjcProtocolDecl* RefPDecl = ObjcProtocols[ProtoRefNames[i]];
+      if (!RefPDecl || RefPDecl->isForwardDecl()) {
+        Diag(CategoryLoc, diag::err_undef_protocolref,
+             ProtoRefNames[i]->getName(),
+             CategoryName->getName());
+      }
+      CDecl->setCatReferencedProtocols((int)i, RefPDecl);
     }
-    CDecl->setCatReferencedProtocols((int)i, RefPDecl);
+    CDecl->setLocEnd(EndProtoLoc);
   }
-  
   return CDecl;
 }
 

Modified: cfe/trunk/include/clang/AST/DeclObjC.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/DeclObjC.h?rev=43501&r1=43500&r2=43501&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/DeclObjC.h (original)
+++ cfe/trunk/include/clang/AST/DeclObjC.h Tue Oct 30 08:30:57 2007
@@ -307,6 +307,9 @@
   int NumClassMethods;  // -1 if not defined
 
   bool isForwardProtoDecl; // declared with @protocol.
+  
+  SourceLocation EndLoc; // marks the '>' or identifier.
+  SourceLocation AtEndLoc; // marks the end of the entire interface.
 public:
   ObjcProtocolDecl(SourceLocation L, unsigned numRefProtos,
                    IdentifierInfo *Id, bool FD = false)
@@ -348,6 +351,14 @@
   bool isForwardDecl() const { return isForwardProtoDecl; }
   void setForwardDecl(bool val) { isForwardProtoDecl = val; }
 
+  // Location information, modeled after the Stmt API. 
+  SourceLocation getLocStart() const { return getLocation(); } // '@'protocol
+  SourceLocation getLocEnd() const { return EndLoc; }
+  void setLocEnd(SourceLocation LE) { EndLoc = LE; };
+  
+  // We also need to record the @end location.
+  SourceLocation getAtEndLoc() const { return AtEndLoc; }
+
   static bool classof(const Decl *D) { return D->getKind() == ObjcProtocol; }
   static bool classof(const ObjcProtocolDecl *D) { return true; }
 };
@@ -459,6 +470,8 @@
   /// Next category belonging to this class
   ObjcCategoryDecl *NextClassCategory;
   
+  SourceLocation EndLoc; // marks the '>' or identifier.
+  SourceLocation AtEndLoc; // marks the end of the entire interface.
 public:
   ObjcCategoryDecl(SourceLocation L, unsigned numRefProtocol,IdentifierInfo *Id)
     : NamedDecl(ObjcCategory, L, Id),
@@ -502,6 +515,13 @@
     NextClassCategory = ClassInterface->getCategoryList();
     ClassInterface->setCategoryList(this);
   }
+  // Location information, modeled after the Stmt API. 
+  SourceLocation getLocStart() const { return getLocation(); } // '@'interface
+  SourceLocation getLocEnd() const { return EndLoc; }
+  void setLocEnd(SourceLocation LE) { EndLoc = LE; };
+  
+  // We also need to record the @end location.
+  SourceLocation getAtEndLoc() const { return AtEndLoc; }
   
   static bool classof(const Decl *D) { return D->getKind() == ObjcCategory; }
   static bool classof(const ObjcCategoryDecl *D) { return true; }

Modified: cfe/trunk/include/clang/Parse/Action.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Parse/Action.h?rev=43501&r1=43500&r2=43501&view=diff

==============================================================================
--- cfe/trunk/include/clang/Parse/Action.h (original)
+++ cfe/trunk/include/clang/Parse/Action.h Tue Oct 30 08:30:57 2007
@@ -499,7 +499,8 @@
     IdentifierInfo *CategoryName, 
     SourceLocation CategoryLoc,
     IdentifierInfo **ProtoRefNames, 
-    unsigned NumProtoRefs) {
+    unsigned NumProtoRefs,
+    SourceLocation EndProtoLoc) {
     return 0;
   }
   // ActOnStartClassImplementation - this action is called immdiately after





More information about the cfe-commits mailing list