]> granicus.if.org Git - clang/commitdiff
Implement AST merging for Objective-C properties.
authorDouglas Gregor <dgregor@apple.com>
Wed, 17 Feb 2010 18:02:10 +0000 (18:02 +0000)
committerDouglas Gregor <dgregor@apple.com>
Wed, 17 Feb 2010 18:02:10 +0000 (18:02 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@96483 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticASTKinds.td
lib/AST/ASTImporter.cpp
test/ASTMerge/Inputs/property1.m [new file with mode: 0644]
test/ASTMerge/Inputs/property2.m [new file with mode: 0644]
test/ASTMerge/property.m [new file with mode: 0644]

index 62d46abd7426d6585f3d6e234b14ef760343b06f..cc89c7caec3ced27bb21462ee1e5e60898dda318 100644 (file)
@@ -74,5 +74,8 @@ def err_odr_objc_method_variadic_inconsistent : Error<
   "and not variadic in another">;
 def note_odr_objc_method_here : Note<
   "%select{class|instance}0 method %1 also declared here">;
+def err_odr_objc_property_type_inconsistent : Error<
+  "property %0 declared with incompatible types in different "
+  "translation units (%1 vs. %2)">;
 def err_unsupported_ast_node: Error<"cannot import unsupported AST node %0">;
 }
index 7b3fb00543d13669daf0da45d835ccf670850ea0..ffeaa23276b99f9119d8d0615b4b936741d7a79b 100644 (file)
@@ -96,7 +96,8 @@ namespace {
     Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
     Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
     Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
-                            
+    Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
+
     // Importing statements
     Stmt *VisitStmt(Stmt *S);
 
@@ -2315,6 +2316,66 @@ Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
   return ToIface;
 }
 
+Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
+  // Import the major distinguishing characteristics of an @property.
+  DeclContext *DC, *LexicalDC;
+  DeclarationName Name;
+  SourceLocation Loc;
+  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
+    return 0;
+
+  // Check whether we have already imported this property.
+  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
+       Lookup.first != Lookup.second; 
+       ++Lookup.first) {
+    if (ObjCPropertyDecl *FoundProp
+                                = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
+      // Check property types.
+      if (!Importer.IsStructurallyEquivalent(D->getType(), 
+                                             FoundProp->getType())) {
+        Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
+          << Name << D->getType() << FoundProp->getType();
+        Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
+          << FoundProp->getType();
+        return 0;
+      }
+
+      // FIXME: Check property attributes, getters, setters, etc.?
+
+      // Consider these properties to be equivalent.
+      Importer.Imported(D, FoundProp);
+      return FoundProp;
+    }
+  }
+
+  // Import the type.
+  QualType T = Importer.Import(D->getType());
+  if (T.isNull())
+    return 0;
+
+  // Create the new property.
+  ObjCPropertyDecl *ToProperty
+    = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
+                               Name.getAsIdentifierInfo(), 
+                               Importer.Import(D->getAtLoc()),
+                               T,
+                               D->getPropertyImplementation());
+  Importer.Imported(D, ToProperty);
+  ToProperty->setLexicalDeclContext(LexicalDC);
+  LexicalDC->addDecl(ToProperty);
+
+  ToProperty->setPropertyAttributes(D->getPropertyAttributes());
+  ToProperty->setGetterName(Importer.Import(D->getGetterName()));
+  ToProperty->setSetterName(Importer.Import(D->getSetterName()));
+  ToProperty->setGetterMethodDecl(
+     cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
+  ToProperty->setSetterMethodDecl(
+     cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
+  ToProperty->setPropertyIvarDecl(
+       cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
+  return ToProperty;
+}
+
 //----------------------------------------------------------------------------
 // Import Statements
 //----------------------------------------------------------------------------
diff --git a/test/ASTMerge/Inputs/property1.m b/test/ASTMerge/Inputs/property1.m
new file mode 100644 (file)
index 0000000..37887a3
--- /dev/null
@@ -0,0 +1,12 @@
+// Matching properties
+@interface I1 {
+}
+- (int)getProp2;
+- (void)setProp2:(int)value;
+@end
+
+// Mismatched property
+@interface I2
+@property (readonly) float Prop1;
+@end
+
diff --git a/test/ASTMerge/Inputs/property2.m b/test/ASTMerge/Inputs/property2.m
new file mode 100644 (file)
index 0000000..6039f10
--- /dev/null
@@ -0,0 +1,13 @@
+// Matching properties
+@interface I1 {
+}
+- (int)getProp2;
+- (void)setProp2:(int)value;
+@property (readonly) int Prop1;
+@property (getter = getProp2, setter = setProp2:) int Prop2;
+@end
+
+// Mismatched property
+@interface I2
+@property (readonly) int Prop1;
+@end
diff --git a/test/ASTMerge/property.m b/test/ASTMerge/property.m
new file mode 100644 (file)
index 0000000..0fd7e48
--- /dev/null
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -emit-pch -o %t.1.ast %S/Inputs/property1.m
+// RUN: %clang_cc1 -emit-pch -o %t.2.ast %S/Inputs/property2.m
+// RUN: %clang_cc1 -ast-merge %t.1.ast -ast-merge %t.2.ast -fsyntax-only %s 2>&1 | FileCheck %s
+
+// CHECK: property2.m:12:26: error: property 'Prop1' declared with incompatible types in different translation units ('int' vs. 'float')
+// CHECK: property1.m:10:28: note: declared here with type 'float'
+// CHECK: property2.m:12:26: error: instance method 'Prop1' has incompatible result types in different translation units ('int' vs. 'float')
+// CHECK: property1.m:10:28: note: instance method 'Prop1' also declared here
+// CHECK: 4 diagnostics generated.