[cfe-commits] r45732 - in /cfe/trunk: Sema/SemaExpr.cpp test/Sema/conditional-expr.c

Steve Naroff snaroff at apple.com
Mon Jan 7 17:11:38 PST 2008


Author: snaroff
Date: Mon Jan  7 19:11:38 2008
New Revision: 45732

URL: http://llvm.org/viewvc/llvm-project?rev=45732&view=rev
Log:

Fix Sema::CheckConditionalOperands(). The null pointer constant checks need to precede the check for two pointer operands. 


Added:
    cfe/trunk/test/Sema/conditional-expr.c
Modified:
    cfe/trunk/Sema/SemaExpr.cpp

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

==============================================================================
--- cfe/trunk/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/Sema/SemaExpr.cpp Mon Jan  7 19:11:38 2008
@@ -788,7 +788,17 @@
   // C99 6.5.15p5: "If both operands have void type, the result has void type."
   if (lexT->isVoidType() && rexT->isVoidType())
     return lexT.getUnqualifiedType();
-  
+
+  // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has
+  // the type of the other operand."
+  if (lexT->isPointerType() && rex->isNullPointerConstant(Context)) {
+    promoteExprToType(rex, lexT); // promote the null to a pointer.
+    return lexT;
+  }
+  if (rexT->isPointerType() && lex->isNullPointerConstant(Context)) {
+    promoteExprToType(lex, rexT); // promote the null to a pointer.
+    return rexT;
+  }
   // Handle the case where both operands are pointers before we handle null
   // pointer constants in case both operands are null pointer constants.
   if (const PointerType *LHSPT = lexT->getAsPointerType()) { // C99 6.5.15p3,6
@@ -822,18 +832,6 @@
     }
   }
   
-  // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has
-  // the type of the other operand."
-  if (lexT->isPointerType() && rex->isNullPointerConstant(Context)) {
-    promoteExprToType(rex, lexT); // promote the null to a pointer.
-    return lexT;
-  }
-  
-  if (rexT->isPointerType() && lex->isNullPointerConstant(Context)) {
-    promoteExprToType(lex, rexT); // promote the null to a pointer.
-    return rexT;
-  }
-  
   // Otherwise, the operands are not compatible.
   Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
        lexT.getAsString(), rexT.getAsString(),

Added: cfe/trunk/test/Sema/conditional-expr.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/conditional-expr.c?rev=45732&view=auto

==============================================================================
--- cfe/trunk/test/Sema/conditional-expr.c (added)
+++ cfe/trunk/test/Sema/conditional-expr.c Mon Jan  7 19:11:38 2008
@@ -0,0 +1,17 @@
+// RUN: clang -fsyntax-only -verify -pedantic %s
+void foo() {
+  *(0 ? (double *)0 : (void *)0) = 0;
+  *((void *) 0) = 0; // -expected-warning {{dereferencing void pointer}} -expected-error {{incomplete type 'void' is not assignable}}
+  double *dp;
+  int *ip;
+  void *vp;
+
+  dp = vp;
+  vp = dp;
+  ip = dp; // -expected-warning {{incompatible pointer types assigning 'double *', expected 'int *'}}
+  dp = ip; // -expected-warning {{incompatible pointer types assigning 'int *', expected 'double *'}}
+  dp = 0 ? (double *)0 : (void *)0;
+  vp = 0 ? (double *)0 : (void *)0;
+  ip = 0 ? (double *)0 : (void *)0; // -expected-warning {{incompatible pointer types assigning 'double *', expected 'int *'}}
+}
+





More information about the cfe-commits mailing list