[cfe-commits] r96551 - in /cfe/trunk: lib/AST/ASTImporter.cpp test/ASTMerge/Inputs/category1.m test/ASTMerge/Inputs/category2.m test/ASTMerge/category.m

Douglas Gregor dgregor at apple.com
Wed Feb 17 17:47:50 PST 2010


Author: dgregor
Date: Wed Feb 17 19:47:50 2010
New Revision: 96551

URL: http://llvm.org/viewvc/llvm-project?rev=96551&view=rev
Log:
AST import of Objective-C categories.

Added:
    cfe/trunk/test/ASTMerge/Inputs/category1.m
    cfe/trunk/test/ASTMerge/Inputs/category2.m
    cfe/trunk/test/ASTMerge/category.m
Modified:
    cfe/trunk/lib/AST/ASTImporter.cpp

Modified: cfe/trunk/lib/AST/ASTImporter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ASTImporter.cpp?rev=96551&r1=96550&r2=96551&view=diff

==============================================================================
--- cfe/trunk/lib/AST/ASTImporter.cpp (original)
+++ cfe/trunk/lib/AST/ASTImporter.cpp Wed Feb 17 19:47:50 2010
@@ -95,6 +95,7 @@
     Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
     Decl *VisitParmVarDecl(ParmVarDecl *D);
     Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
+    Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
     Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
     Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
     Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
@@ -2159,8 +2160,84 @@
   return ToMethod;
 }
 
+Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
+  // Import the major distinguishing characteristics of a category.
+  DeclContext *DC, *LexicalDC;
+  DeclarationName Name;
+  SourceLocation Loc;
+  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
+    return 0;
+  
+  ObjCInterfaceDecl *ToInterface
+    = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
+  if (!ToInterface)
+    return 0;
+  
+  // Determine if we've already encountered this category.
+  ObjCCategoryDecl *MergeWithCategory
+    = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
+  ObjCCategoryDecl *ToCategory = MergeWithCategory;
+  if (!ToCategory) {
+    ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
+                                          Importer.Import(D->getAtLoc()),
+                                          Loc, 
+                                       Importer.Import(D->getCategoryNameLoc()), 
+                                          Name.getAsIdentifierInfo());
+    ToCategory->setLexicalDeclContext(LexicalDC);
+    LexicalDC->addDecl(ToCategory);
+    Importer.Imported(D, ToCategory);
+    
+    // Link this category into its class's category list.
+    ToCategory->setClassInterface(ToInterface);
+    ToCategory->insertNextClassCategory();
+    
+    // Import protocols
+    llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
+    llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
+    ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
+      = D->protocol_loc_begin();
+    for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
+                                          FromProtoEnd = D->protocol_end();
+         FromProto != FromProtoEnd;
+         ++FromProto, ++FromProtoLoc) {
+      ObjCProtocolDecl *ToProto
+        = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
+      if (!ToProto)
+        return 0;
+      Protocols.push_back(ToProto);
+      ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
+    }
+    
+    // FIXME: If we're merging, make sure that the protocol list is the same.
+    ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
+                                ProtocolLocs.data(), Importer.getToContext());
+    
+  } else {
+    Importer.Imported(D, ToCategory);
+  }
+  
+  // Import all of the members of this category.
+  for (DeclContext::decl_iterator FromMem = D->decls_begin(), 
+                              FromMemEnd = D->decls_end();
+       FromMem != FromMemEnd;
+       ++FromMem)
+    Importer.Import(*FromMem);
+ 
+  // If we have an implementation, import it as well.
+  if (D->getImplementation()) {
+    ObjCCategoryImplDecl *Impl
+      = cast<ObjCCategoryImplDecl>(Importer.Import(D->getImplementation()));
+    if (!Impl)
+      return 0;
+    
+    ToCategory->setImplementation(Impl);
+  }
+  
+  return ToCategory;
+}
+
 Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
-  // Import the major distinguishing characteristics of an @protocol.
+  // Import the major distinguishing characteristics of a protocol.
   DeclContext *DC, *LexicalDC;
   DeclarationName Name;
   SourceLocation Loc;
@@ -2213,7 +2290,7 @@
     Importer.Imported(D, ToProto);
   }
 
-  // Import all of the members of this class.
+  // Import all of the members of this protocol.
   for (DeclContext::decl_iterator FromMem = D->decls_begin(), 
                                FromMemEnd = D->decls_end();
        FromMem != FromMemEnd;
@@ -2288,8 +2365,6 @@
     ToIface->setProtocolList(Protocols.data(), Protocols.size(),
                              ProtocolLocs.data(), Importer.getToContext());
     
-    // FIXME: Import categories
-    
     // Import @end range
     ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
   } else {
@@ -2323,6 +2398,12 @@
     }
   }
   
+  // Import categories. When the categories themselves are imported, they'll
+  // hook themselves into this interface.
+  for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
+       FromCat = FromCat->getNextClassCategory())
+    Importer.Import(FromCat);
+  
   // Import all of the members of this class.
   for (DeclContext::decl_iterator FromMem = D->decls_begin(), 
                                FromMemEnd = D->decls_end();

Added: cfe/trunk/test/ASTMerge/Inputs/category1.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ASTMerge/Inputs/category1.m?rev=96551&view=auto

==============================================================================
--- cfe/trunk/test/ASTMerge/Inputs/category1.m (added)
+++ cfe/trunk/test/ASTMerge/Inputs/category1.m Wed Feb 17 19:47:50 2010
@@ -0,0 +1,25 @@
+ at interface I1 
+ at end
+
+// Matching category
+ at interface I1 (Cat1)
+- (int)method0;
+ at end
+
+// Matching class extension
+ at interface I1 ()
+- (int)method1;
+ at end
+
+// Mismatched category
+ at interface I1 (Cat2)
+- (int)method2;
+ at end
+
+ at interface I2
+ at end
+
+// Mismatched class extension
+ at interface I2 ()
+- (int)method3;
+ at end

Added: cfe/trunk/test/ASTMerge/Inputs/category2.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ASTMerge/Inputs/category2.m?rev=96551&view=auto

==============================================================================
--- cfe/trunk/test/ASTMerge/Inputs/category2.m (added)
+++ cfe/trunk/test/ASTMerge/Inputs/category2.m Wed Feb 17 19:47:50 2010
@@ -0,0 +1,27 @@
+typedef int Int;
+
+ at interface I1 
+ at end
+
+// Matching category
+ at interface I1 (Cat1)
+- (Int)method0;
+ at end
+
+// Matching class extension
+ at interface I1 ()
+- (Int)method1;
+ at end
+
+// Mismatched category
+ at interface I1 (Cat2)
+- (float)method2;
+ at end
+
+ at interface I2
+ at end
+
+// Mismatched class extension
+ at interface I2 ()
+- (float)method3;
+ at end

Added: cfe/trunk/test/ASTMerge/category.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ASTMerge/category.m?rev=96551&view=auto

==============================================================================
--- cfe/trunk/test/ASTMerge/category.m (added)
+++ cfe/trunk/test/ASTMerge/category.m Wed Feb 17 19:47:50 2010
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -emit-pch -o %t.1.ast %S/Inputs/category1.m
+// RUN: %clang_cc1 -emit-pch -o %t.2.ast %S/Inputs/category2.m
+// RUN: %clang_cc1 -ast-merge %t.1.ast -ast-merge %t.2.ast -fsyntax-only %s 2>&1 | FileCheck %s
+
+// CHECK: category2.m:18:1: error: instance method 'method2' has incompatible result types in different translation units ('float' vs. 'int')
+// CHECK: category1.m:16:1: note: instance method 'method2' also declared here
+// CHECK: category2.m:26:1: error: instance method 'method3' has incompatible result types in different translation units ('float' vs. 'int')
+// CHECK: category1.m:24:1: note: instance method 'method3' also declared here
+// CHECK: 4 diagnostics generated.





More information about the cfe-commits mailing list