[cfe-commits] r51228 - in /cfe/trunk: lib/Sema/SemaExpr.cpp test/Sema/pointer-addition.c

Eli Friedman eli.friedman at gmail.com
Sun May 18 11:08:51 PDT 2008


Author: efriedma
Date: Sun May 18 13:08:51 2008
New Revision: 51228

URL: http://llvm.org/viewvc/llvm-project?rev=51228&view=rev
Log:
Add proper type-checking for pointer additiion; before, we were accepting
addition with a pointer and an integer even when it didn't make sense.


Added:
    cfe/trunk/test/Sema/pointer-addition.c
Modified:
    cfe/trunk/lib/Sema/SemaExpr.cpp

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Sun May 18 13:08:51 2008
@@ -1405,15 +1405,33 @@
     return CheckVectorOperands(loc, lex, rex);
 
   QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
-  
+
   // handle the common case first (both operands are arithmetic).
   if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
     return compType;
 
-  if (lex->getType()->isPointerType() && rex->getType()->isIntegerType())
-    return lex->getType();
-  if (lex->getType()->isIntegerType() && rex->getType()->isPointerType())
-    return rex->getType();
+  // Put any potential pointer into PExp
+  Expr* PExp = lex, *IExp = rex;
+  if (IExp->getType()->isPointerType())
+    std::swap(PExp, IExp);
+
+  if (const PointerType* PTy = PExp->getType()->getAsPointerType()) {
+    if (IExp->getType()->isIntegerType()) {
+      // Check for arithmetic on pointers to incomplete types
+      if (!PTy->getPointeeType()->isObjectType()) {
+        if (PTy->getPointeeType()->isVoidType()) {
+          Diag(loc, diag::ext_gnu_void_ptr, 
+               lex->getSourceRange(), rex->getSourceRange());
+        } else {
+          Diag(loc, diag::err_typecheck_arithmetic_incomplete_type,
+               lex->getType().getAsString(), lex->getSourceRange());
+          return QualType();
+        }
+      }
+      return PExp->getType();
+    }
+  }
+
   return InvalidOperands(loc, lex, rex);
 }
 
@@ -1681,7 +1699,10 @@
 
   // C99 6.5.2.4p1: We allow complex as a GCC extension.
   if (const PointerType *pt = resType->getAsPointerType()) {
-    if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2
+    if (pt->getPointeeType()->isVoidType()) {
+      Diag(OpLoc, diag::ext_gnu_void_ptr, op->getSourceRange());
+    } else if (!pt->getPointeeType()->isObjectType()) {
+      // C99 6.5.2.4p2, 6.5.6p2
       Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
            resType.getAsString(), op->getSourceRange());
       return QualType();

Added: cfe/trunk/test/Sema/pointer-addition.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/pointer-addition.c?rev=51228&view=auto

==============================================================================
--- cfe/trunk/test/Sema/pointer-addition.c (added)
+++ cfe/trunk/test/Sema/pointer-addition.c Sun May 18 13:08:51 2008
@@ -0,0 +1,14 @@
+// RUN: clang %s -fsyntax-only -verify -pedantic
+
+typedef struct S S;
+void a(S* b, void* c) {
+  b++;       // expected-error {{arithmetic on pointer to incomplete type}}
+  b += 1;    // expected-error {{arithmetic on pointer to incomplete type}}
+  c++;       // expected-warning {{use of GNU void* extension}}
+  c += 1;    // expected-warning {{use of GNU void* extension}}
+  b = 1+b;   // expected-error {{arithmetic on pointer to incomplete type}}
+  /* The next couple tests are only pedantic warnings in gcc */
+  void (*d)(S*,void*) = a;
+  d += 1;    // expected-error {{pointer to incomplete type}}
+  d++;       // expected-error {{pointer to incomplete type}}
+}





More information about the cfe-commits mailing list