]> granicus.if.org Git - clang/commitdiff
Allow @synchronized to contextually convert a C++ object to an ObjC object pointer.
authorJordan Rose <jordan_rose@apple.com>
Tue, 12 Aug 2014 16:20:36 +0000 (16:20 +0000)
committerJordan Rose <jordan_rose@apple.com>
Tue, 12 Aug 2014 16:20:36 +0000 (16:20 +0000)
Patch by Grant Paul!

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

lib/Sema/SemaStmt.cpp
test/SemaObjCXX/synchronized.mm [new file with mode: 0644]

index 6bb71eb89583d14c4afff7844b26f3878caae0ac..12e0ef7fa6c63ab3140a615c675157127e8cb448 100644 (file)
@@ -3102,9 +3102,24 @@ Sema::ActOnObjCAtSynchronizedOperand(SourceLocation atLoc, Expr *operand) {
   if (!type->isDependentType() &&
       !type->isObjCObjectPointerType()) {
     const PointerType *pointerType = type->getAs<PointerType>();
-    if (!pointerType || !pointerType->getPointeeType()->isVoidType())
-      return Diag(atLoc, diag::error_objc_synchronized_expects_object)
-               << type << operand->getSourceRange();
+    if (!pointerType || !pointerType->getPointeeType()->isVoidType()) {
+      if (getLangOpts().CPlusPlus) {
+        if (RequireCompleteType(atLoc, type,
+                                diag::err_incomplete_receiver_type))
+          return Diag(atLoc, diag::error_objc_synchronized_expects_object)
+                   << type << operand->getSourceRange();
+
+        ExprResult result = PerformContextuallyConvertToObjCPointer(operand);
+        if (!result.isUsable())
+          return Diag(atLoc, diag::error_objc_synchronized_expects_object)
+                   << type << operand->getSourceRange();
+
+        operand = result.get();
+      } else {
+          return Diag(atLoc, diag::error_objc_synchronized_expects_object)
+                   << type << operand->getSourceRange();
+      }
+    }
   }
 
   // The operand to @synchronized is a full-expression.
diff --git a/test/SemaObjCXX/synchronized.mm b/test/SemaObjCXX/synchronized.mm
new file mode 100644 (file)
index 0000000..37305c5
--- /dev/null
@@ -0,0 +1,20 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wno-objc-root-class %s
+
+@interface PBXTrackableTaskManager @end
+@implementation PBXTrackableTaskManager @end
+
+struct x {
+  operator PBXTrackableTaskManager *() const { return 0; }
+} a;
+
+struct y {
+  operator int *() const { return 0; }
+} b;
+
+void test1() {
+  @synchronized (a) {
+  }
+
+  @synchronized (b) {  // expected-error {{@synchronized requires an Objective-C object type ('struct y' invalid)}}
+  }
+}