]> granicus.if.org Git - clang/commitdiff
Emit an error when trying to @encode an incomplete type.
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>
Sat, 14 May 2011 20:32:39 +0000 (20:32 +0000)
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>
Sat, 14 May 2011 20:32:39 +0000 (20:32 +0000)
There are APIs, e.g. [NSValue valueWithBytes:objCType:], which use the encoding to find out
the size of an object pointed to by a pointer. Make things safer by making it illegal to @encode
incomplete types.

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

include/clang/Basic/DiagnosticSemaKinds.td
include/clang/Sema/Sema.h
lib/Sema/SemaExprObjC.cpp
test/SemaObjC/exprs.m

index ea1ef56010a26d2a287d931391472c84dfd21856..b40aa61ba57e0dce7ec26ccae006084bf439d432 100644 (file)
@@ -2869,6 +2869,8 @@ def err_objc_pointer_cxx_catch_fragile : Error<
   "exception model">;
 def err_objc_object_catch : Error<
   "can't catch an Objective C object by value">;
+def err_incomplete_type_objc_at_encode : Error<
+  "'@encode' of incomplete type %0">;
 
 def warn_setter_getter_impl_required : Warning<
   "property %0 requires method %1 to be defined - "
index 6265c67c07632d6daf7180e206d7fa6c0d0c2b41..d1487cc3174c19c53989716411c1952ca6d29ffb 100644 (file)
@@ -3105,7 +3105,7 @@ public:
                                     Expr **Strings,
                                     unsigned NumStrings);
 
-  Expr *BuildObjCEncodeExpression(SourceLocation AtLoc,
+  ExprResult BuildObjCEncodeExpression(SourceLocation AtLoc,
                                   TypeSourceInfo *EncodedTypeInfo,
                                   SourceLocation RParenLoc);
   ExprResult BuildCXXMemberCallExpr(Expr *Exp, NamedDecl *FoundDecl,
index 2a262f09392211ef57000565943492c5e8d7868a..3444cb5a494506368644cee83f3d1af92d4daac3 100644 (file)
@@ -119,7 +119,7 @@ ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs,
   return new (Context) ObjCStringLiteral(S, Ty, AtLocs[0]);
 }
 
-Expr *Sema::BuildObjCEncodeExpression(SourceLocation AtLoc,
+ExprResult Sema::BuildObjCEncodeExpression(SourceLocation AtLoc,
                                       TypeSourceInfo *EncodedTypeInfo,
                                       SourceLocation RParenLoc) {
   QualType EncodedType = EncodedTypeInfo->getType();
@@ -127,6 +127,12 @@ Expr *Sema::BuildObjCEncodeExpression(SourceLocation AtLoc,
   if (EncodedType->isDependentType())
     StrTy = Context.DependentTy;
   else {
+    if (!EncodedType->getAsArrayTypeUnsafe()) // Incomplete array is handled.
+      if (RequireCompleteType(AtLoc, EncodedType,
+                         PDiag(diag::err_incomplete_type_objc_at_encode)
+                             << EncodedTypeInfo->getTypeLoc().getSourceRange()))
+        return ExprError();
+
     std::string Str;
     Context.getObjCEncodingForType(EncodedType, Str);
 
index d7c122356f5864895310bb6fe15f179c544e4df7..2b505e0eae332c88a0e05709e79cdeb97e7b2f0e 100644 (file)
@@ -32,3 +32,13 @@ void test3(Object *o) {
   // this is ok.
   __sync_bool_compare_and_swap(&g, 0, o);
 }
+
+@class Incomplete_ObjC_class;
+struct Incomplete_struct; // expected-note {{forward declaration}}
+
+void test_encode() {
+  (void)@encode(Incomplete_ObjC_class); // expected-error {{incomplete type}}
+  (void)@encode(struct Incomplete_struct); // expected-error {{incomplete type}}
+  (void)@encode(Incomplete_ObjC_class*);
+  (void)@encode(id);
+}