[cfe-commits] r44322 - in /cfe/trunk: CodeGen/CGExprScalar.cpp Sema/SemaExpr.cpp test/CodeGen/conditional-gnu-ext.c

Chris Lattner sabre at nondot.org
Sun Nov 25 17:40:58 PST 2007


Author: lattner
Date: Sun Nov 25 19:40:58 2007
New Revision: 44322

URL: http://llvm.org/viewvc/llvm-project?rev=44322&view=rev
Log:
Fix sema support for the gnu ?: expression with a 
missing middle expression, and fix a codegen bug where
we didn't correctly promote the condition to the right 
result type.  This fixes PR1824.

Added:
    cfe/trunk/test/CodeGen/conditional-gnu-ext.c
Modified:
    cfe/trunk/CodeGen/CGExprScalar.cpp
    cfe/trunk/Sema/SemaExpr.cpp

Modified: cfe/trunk/CodeGen/CGExprScalar.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CGExprScalar.cpp?rev=44322&r1=44321&r2=44322&view=diff

==============================================================================
--- cfe/trunk/CodeGen/CGExprScalar.cpp (original)
+++ cfe/trunk/CodeGen/CGExprScalar.cpp Sun Nov 25 19:40:58 2007
@@ -884,13 +884,23 @@
   llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("cond.:");
   llvm::BasicBlock *ContBlock = new llvm::BasicBlock("cond.cont");
   
-  Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
-  Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
+  // Evaluate the conditional, then convert it to bool.  We do this explicitly
+  // because we need the unconverted value if this is a GNU ?: expression with
+  // missing middle value.
+  Value *CondVal = CGF.EmitScalarExpr(E->getCond());
+  Value *CondBoolVal = CGF.EmitScalarConversion(CondVal, E->getCond()->getType(), 
+                                                CGF.getContext().BoolTy);
+  Builder.CreateCondBr(CondBoolVal, LHSBlock, RHSBlock);
   
   CGF.EmitBlock(LHSBlock);
   
   // Handle the GNU extension for missing LHS.
-  Value *LHS = E->getLHS() ? Visit(E->getLHS()) : Cond;
+  Value *LHS;
+  if (E->getLHS())
+    LHS = Visit(E->getLHS());
+  else    // Perform promotions, to handle cases like "short ?: int"
+    LHS = EmitScalarConversion(CondVal, E->getCond()->getType(), E->getType());
+  
   Builder.CreateBr(ContBlock);
   LHSBlock = Builder.GetInsertBlock();
   

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

==============================================================================
--- cfe/trunk/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/Sema/SemaExpr.cpp Sun Nov 25 19:40:58 2007
@@ -738,6 +738,8 @@
   return;
 }
 
+/// Note that lex is not null here, even if this is the gnu "x ?: y" extension.
+/// In that case, lex = cond.
 inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
   Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) {
   UsualUnaryConversions(cond);
@@ -760,8 +762,7 @@
   }
   if (const RecordType *LHSRT = lexT->getAsRecordType()) {    // C99 6.5.15p3
     if (const RecordType *RHSRT = rexT->getAsRecordType()) {
-      
-      if (LHSRT->getDecl()->getIdentifier() ==RHSRT->getDecl()->getIdentifier())
+      if (LHSRT->getDecl() == RHSRT->getDecl())
         return lexT;
       
       Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
@@ -826,11 +827,19 @@
                                             ExprTy *RHS) {
   Expr *CondExpr = (Expr *) Cond;
   Expr *LHSExpr = (Expr *) LHS, *RHSExpr = (Expr *) RHS;
+
+  // If this is the gnu "x ?: y" extension, analyze the types as though the LHS
+  // was the condition.
+  bool isLHSNull = LHSExpr == 0;
+  if (isLHSNull)
+    LHSExpr = CondExpr;
+  
   QualType result = CheckConditionalOperands(CondExpr, LHSExpr, 
                                              RHSExpr, QuestionLoc);
   if (result.isNull())
     return true;
-  return new ConditionalOperator(CondExpr, LHSExpr, RHSExpr, result);
+  return new ConditionalOperator(CondExpr, isLHSNull ? 0 : LHSExpr,
+                                 RHSExpr, result);
 }
 
 /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that

Added: cfe/trunk/test/CodeGen/conditional-gnu-ext.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/conditional-gnu-ext.c?rev=44322&view=auto

==============================================================================
--- cfe/trunk/test/CodeGen/conditional-gnu-ext.c (added)
+++ cfe/trunk/test/CodeGen/conditional-gnu-ext.c Sun Nov 25 19:40:58 2007
@@ -0,0 +1,6 @@
+// RUN: clang -emit-llvm %s
+// PR1824
+
+int foo(int x, short y) {
+  return x ?: y;
+}





More information about the cfe-commits mailing list