]> granicus.if.org Git - clang/commitdiff
implement semantic analysis for @synchronized, fixing a crash on invalid
authorChris Lattner <sabre@nondot.org>
Tue, 21 Apr 2009 06:11:25 +0000 (06:11 +0000)
committerChris Lattner <sabre@nondot.org>
Tue, 21 Apr 2009 06:11:25 +0000 (06:11 +0000)
rdar://6810940 - @synchronized has no sema checks

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

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaStmt.cpp
test/Parser/objc-try-catch-1.m
test/SemaObjC/synchronized.m
test/SemaObjC/try-catch.m

index b270feccc885e7543d1bd4484792f00e5cda1b6e..acfc006738b49595cb457b8091fad91bea4c76eb 100644 (file)
@@ -1079,7 +1079,9 @@ def warn_bad_receiver_type : Warning<
   "casting it to 'id'">;
 def err_bad_receiver_type : Error<"bad receiver type %0">;
 def error_objc_throw_expects_object : Error<
-  "invalid %0 argument (expected an ObjC object type)">;
+  "@throw requires an Objective-C object type (%0 invalid)">;
+def error_objc_synchronized_expects_object : Error<
+  "@synchronized requires an Objective-C object type (%0 invalid)">;
 def error_rethrow_used_outside_catch : Error<
   "@throw (rethrow) used outside of a @catch block">;
 def err_attribute_multiple_objc_gc : Error<
index 26cba5bec74e8596f5f141d54bb697342fc46ae2..fb8a8ece437e985415654840a0c07688dd618fae 100644 (file)
@@ -1092,6 +1092,15 @@ Sema::ActOnObjCAtSynchronizedStmt(SourceLocation AtLoc, ExprArg SynchExpr,
                                   StmtArg SynchBody) {
   CurFunctionNeedsScopeChecking = true;
 
+  // Make sure the expression type is an ObjC pointer or "void *".
+  Expr *SyncExpr = static_cast<Expr*>(SynchExpr.get());
+  if (!Context.isObjCObjectPointerType(SyncExpr->getType())) {
+    const PointerType *PT = SyncExpr->getType()->getAsPointerType();
+    if (!PT || !PT->getPointeeType()->isVoidType())
+      return StmtError(Diag(AtLoc, diag::error_objc_synchronized_expects_object)
+                       << SyncExpr->getType() << SyncExpr->getSourceRange());
+  }
+  
   return Owned(new (Context) ObjCAtSynchronizedStmt(AtLoc,
                      static_cast<Stmt*>(SynchExpr.release()),
                      static_cast<Stmt*>(SynchBody.release())));
index 2c41cac3ee62394d3a3965d128036a42b480e59f..a8d37f0ab959c5f85752ae7e6713bf73d76ea6e3 100644 (file)
@@ -27,7 +27,10 @@ void * foo()
       return proc();
     }
     @catch (Frob* ex) {
-      @throw 1,2; // expected-error {{invalid 'int' argument (expected an ObjC object type)}}
+      @throw 1,2; // expected-error {{@throw requires an Objective-C object type ('int' invalid)}}
+    }
+    @catch (float x) {  // expected-error {{@catch parameter is not a pointer to an interface type}}
+      
     }
     @catch(...) {
       @throw (4,3,proc());
index d4914fd895f9a6f1cf65eec9292a07a80fe6c636..e8e668fd8bc4732138e1b4e01ab765df41431e0b 100644 (file)
@@ -1,75 +1,23 @@
 // RUN: clang-cc -fsyntax-only -verify %s
-typedef signed char BOOL;
-typedef unsigned int NSUInteger;
-typedef struct _NSZone NSZone;
-@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator;
 
-@protocol NSObject
-- (BOOL)isEqual:(id)object;
-@end
-
-@protocol NSCopying
-- (id)copyWithZone:(NSZone *)zone;
-@end
-
-@protocol NSMutableCopying
-- (id)mutableCopyWithZone:(NSZone *)zone;
-@end
-
-@protocol NSCoding
-- (void)encodeWithCoder:(NSCoder *)aCoder;
-@end
-
-@interface NSObject <NSObject> {} @end
-
-typedef float CGFloat;
-typedef struct { int a; } NSFastEnumerationState;
-
-@protocol NSFastEnumeration 
-- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len;
-@end
-
-typedef unsigned short unichar;
-
-@interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding>
-- (NSUInteger)length;
-@end
-
-@interface NSSimpleCString : NSString {} @end
-
-@interface NSConstantString : NSSimpleCString @end
-
-extern void *_NSConstantStringClassReference;
-
-@interface NSDictionary : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration>
-- (NSUInteger)count;
-@end
-
-@interface NSMutableDictionary : NSDictionary
-- (void)removeObjectForKey:(id)aKey;
-@end
-
-@class NSArray, NSSet, NSHashTable;
-
-@protocol PBXTrackableTask <NSObject>
-- (float) taskPercentComplete;
-- taskIdentifier;
-@end
-
-@interface PBXTrackableTaskManager : NSObject {
-  NSMutableDictionary *_trackableTasks;
-}
-@end
-
-NSString *XCExecutableDebugTaskIdentifier = @"XCExecutableDebugTaskIdentifier";
+@class PBXTrackableTaskManager;
 
 @implementation PBXTrackableTaskManager
 - (id) init {}
-- (void) unregisterTask:(id <PBXTrackableTask>) task {
+- (void) unregisterTask:(id) task {
   @synchronized (self) {
-  id taskID = [task taskIdentifier];
-  id task = [_trackableTasks objectForKey:taskID]; // expected-warning{{method '-objectForKey:' not found (return type defaults to 'id')}}
+  id taskID = [task taskIdentifier];  // expected-warning {{method '-taskIdentifier' not found (return type defaults to 'id')}}
   }
 }
 @end
 
+
+struct x { int a; } b;
+
+void test1() {
+  @synchronized (b) {  // expected-error {{@synchronized requires an Objective-C object type ('struct x' invalid)}}
+  }
+
+  @synchronized (42) {  // expected-error {{@synchronized requires an Objective-C object type ('int' invalid)}}
+  }
+}
index bcb4e506d5fdd013e338687535858abfda9e0e51..076eff542968486ec5ee8a6bd785fd2a19c4404b 100644 (file)
@@ -40,8 +40,8 @@ typedef struct _NSZone NSZone;
 int foo() {
   struct s { int a, b; } agg, *pagg;
 
-  @throw 42; // expected-error {{invalid 'int' argument (expected an ObjC object type)}}
-  @throw agg; // expected-error {{invalid 'struct s' argument (expected an ObjC object type)}}
-  @throw pagg; // expected-error {{invalid 'struct s *' argument (expected an ObjC object type)}}
+  @throw 42; // expected-error {{@throw requires an Objective-C object type ('int' invalid))}}
+  @throw agg; // expected-error {{@throw requires an Objective-C object type ('struct s' invalid)}}
+  @throw pagg; // expected-error {{@throw requires an Objective-C object type ('struct s *' invalid)}}
   @throw; // expected-error {{@throw (rethrow) used outside of a @catch block}}
 }