[cfe-commits] r70004 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaExpr.cpp test/SemaObjC/sizeof-interface.m

Chris Lattner sabre at nondot.org
Fri Apr 24 16:50:08 PDT 2009


Author: lattner
Date: Fri Apr 24 18:50:08 2009
New Revision: 70004

URL: http://llvm.org/viewvc/llvm-project?rev=70004&view=rev
Log:
reject explicit pointer arithmetic on interface pointers in 64-bit objc ABI

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/test/SemaObjC/sizeof-interface.m

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=70004&r1=70003&r2=70004&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Fri Apr 24 18:50:08 2009
@@ -917,11 +917,6 @@
   "invalid application of 'sizeof' to a function type">;
 def ext_sizeof_void_type : Extension<
   "invalid application of '%0' to a void type">;
-def err_sizeof_nonfragile_interface : Error<
-  "invalid application of '%select{alignof|sizeof}1' to interface %0 in "
-  "non-fragile ABI">;
-def err_atdef_nonfragile_interface : Error<
-  "invalid application of @defs in non-fragile ABI">;
 // FIXME: merge with %select
 def err_sizeof_incomplete_type : Error<
   "invalid application of 'sizeof' to an incomplete type %0">;
@@ -938,6 +933,20 @@
   "comparing floating point with == or != is unsafe">,
   InGroup<DiagGroup<"float-equal">>, DefaultIgnore;
 
+def err_sizeof_nonfragile_interface : Error<
+  "invalid application of '%select{alignof|sizeof}1' to interface %0 in "
+  "non-fragile ABI">;
+def err_atdef_nonfragile_interface : Error<
+  "invalid application of @defs in non-fragile ABI">;
+def err_subscript_nonfragile_interface : Error<
+  "subscript requires size of interface %0, which is not constant in "
+  "non-fragile ABI">;
+
+def err_arithmetic_nonfragile_interface : Error<
+  "arithmetic on pointer to interface %0, which is not a constant size in "
+  "non-fragile ABI">;
+
+
 def err_typecheck_subscript_value : Error<
   "subscripted value is neither array nor pointer">;
 def err_typecheck_subscript : Error<"array subscript is not an integer">;
@@ -945,9 +954,6 @@
   "subscript of pointer to function type %0">;
 def err_subscript_incomplete_type : Error<
   "subscript of pointer to incomplete type %0">;
-def err_subscript_nonfragile_interface : Error<
-  "subscript requires size of interface %0, which is not constant in "
-  "non-fragile ABI">;
 def err_typecheck_member_reference_struct_union : Error<
   "member reference base type %0 is not a structure or union">;
 def err_typecheck_member_reference_ivar : Error<

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=70004&r1=70003&r2=70004&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Fri Apr 24 18:50:08 2009
@@ -3275,10 +3275,11 @@
   if (IExp->getType()->isPointerType())
     std::swap(PExp, IExp);
 
-  if (const PointerType* PTy = PExp->getType()->getAsPointerType()) {
+  if (const PointerType *PTy = PExp->getType()->getAsPointerType()) {
     if (IExp->getType()->isIntegerType()) {
-      // Check for arithmetic on pointers to incomplete types
-      if (PTy->getPointeeType()->isVoidType()) {
+      QualType PointeeTy = PTy->getPointeeType();
+      // Check for arithmetic on pointers to incomplete types.
+      if (PointeeTy->isVoidType()) {
         if (getLangOptions().CPlusPlus) {
           Diag(Loc, diag::err_typecheck_pointer_arith_void_type)
             << lex->getSourceRange() << rex->getSourceRange();
@@ -3288,7 +3289,7 @@
         // GNU extension: arithmetic on pointer to void
         Diag(Loc, diag::ext_gnu_void_ptr)
           << lex->getSourceRange() << rex->getSourceRange();
-      } else if (PTy->getPointeeType()->isFunctionType()) {
+      } else if (PointeeTy->isFunctionType()) {
         if (getLangOptions().CPlusPlus) {
           Diag(Loc, diag::err_typecheck_pointer_arith_function_type)
             << lex->getType() << lex->getSourceRange();
@@ -3299,12 +3300,19 @@
         Diag(Loc, diag::ext_gnu_ptr_func_arith)
           << lex->getType() << lex->getSourceRange();
       } else if (!PTy->isDependentType() &&
-                 RequireCompleteType(Loc, PTy->getPointeeType(),
+                 RequireCompleteType(Loc, PointeeTy,
                                 diag::err_typecheck_arithmetic_incomplete_type,
-                                     lex->getSourceRange(), SourceRange(),
-                                     lex->getType()))
+                                     PExp->getSourceRange(), SourceRange(),
+                                     PExp->getType()))
         return QualType();
 
+      // Diagnose bad cases where we step over interface counts.
+      if (PointeeTy->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) {
+        Diag(Loc, diag::err_arithmetic_nonfragile_interface)
+          << PointeeTy << PExp->getSourceRange();
+        return QualType();
+      }
+      
       if (CompLHSTy) {
         QualType LHSTy = lex->getType();
         if (LHSTy->isPromotableIntegerType())
@@ -3371,6 +3379,13 @@
                                    lex->getType()))
       return QualType();
 
+    // Diagnose bad cases where we step over interface counts.
+    if (lpointee->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) {
+      Diag(Loc, diag::err_arithmetic_nonfragile_interface)
+        << lpointee << lex->getSourceRange();
+      return QualType();
+    }
+    
     // The result type of a pointer-int computation is the pointer type.
     if (rex->getType()->isIntegerType()) {
       if (ComplainAboutVoid)

Modified: cfe/trunk/test/SemaObjC/sizeof-interface.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/sizeof-interface.m?rev=70004&r1=70003&r2=70004&view=diff

==============================================================================
--- cfe/trunk/test/SemaObjC/sizeof-interface.m (original)
+++ cfe/trunk/test/SemaObjC/sizeof-interface.m Fri Apr 24 18:50:08 2009
@@ -7,6 +7,8 @@
 
 // rdar://6821047
 void *g3(I0 *P) {
+  P = P+5;        // expected-error {{arithmetic on pointer to incomplete type 'I0 *'}}
+
   return &P[4];   // expected-error{{subscript of pointer to incomplete type 'I0'}}
 }
 
@@ -49,6 +51,10 @@
 
 // rdar://6821047
 int bar(I0 *P) {
+  P = P+5;  // expected-error {{arithmetic on pointer to interface 'I0', which is not a constant size in non-fragile ABI}}
+  P = 5+P;  // expected-error {{arithmetic on pointer to interface 'I0', which is not a constant size in non-fragile ABI}}
+  P = P-5;  // expected-error {{arithmetic on pointer to interface 'I0', which is not a constant size in non-fragile ABI}}
+  
   return P[4].x[2];  // expected-error {{subscript requires size of interface 'I0', which is not constant in non-fragile ABI}}
 }
 





More information about the cfe-commits mailing list