[cfe-commits] r64865 - in /cfe/trunk: include/clang/AST/Expr.h lib/AST/Expr.cpp test/Sema/const-eval.c

Daniel Dunbar daniel at zuster.org
Tue Feb 17 16:47:46 PST 2009


Author: ddunbar
Date: Tue Feb 17 18:47:45 2009
New Revision: 64865

URL: http://llvm.org/viewvc/llvm-project?rev=64865&view=rev
Log:
isICE was evaluating ?: incorrectly with missing-gcc-LHS extension.

Add assert to isICE that, on success, result must be the same as
EvaluateAsInt()... this enforces a minimum level of sanity.

Modified:
    cfe/trunk/include/clang/AST/Expr.h
    cfe/trunk/lib/AST/Expr.cpp
    cfe/trunk/test/Sema/const-eval.c

Modified: cfe/trunk/include/clang/AST/Expr.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Expr.h?rev=64865&r1=64864&r2=64865&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Expr.h (original)
+++ cfe/trunk/include/clang/AST/Expr.h Tue Feb 17 18:47:45 2009
@@ -180,6 +180,9 @@
   bool isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
                              SourceLocation *Loc = 0,
                              bool isEvaluated = true) const;
+  bool isIntegerConstantExprInternal(llvm::APSInt &Result, ASTContext &Ctx,
+                             SourceLocation *Loc = 0,
+                             bool isEvaluated = true) const;
   bool isIntegerConstantExpr(ASTContext &Ctx, SourceLocation *Loc = 0) const {
     llvm::APSInt X;
     return isIntegerConstantExpr(X, Ctx, Loc);
@@ -190,7 +193,7 @@
   
   /// EvalResult is a struct with detailed info about an evaluated expression.
   struct EvalResult {
-    /// Val - This is the scalar value the expression can be folded to.
+    /// Val - This is the value the expression can be folded to.
     APValue Val;
     
     /// HasSideEffects - Whether the evaluated expression has side effects.

Modified: cfe/trunk/lib/AST/Expr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Expr.cpp?rev=64865&r1=64864&r2=64865&view=diff

==============================================================================
--- cfe/trunk/lib/AST/Expr.cpp (original)
+++ cfe/trunk/lib/AST/Expr.cpp Tue Feb 17 18:47:45 2009
@@ -871,6 +871,15 @@
 /// cast+dereference.
 bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
                                  SourceLocation *Loc, bool isEvaluated) const {
+  if (!isIntegerConstantExprInternal(Result, Ctx, Loc, isEvaluated))
+    return false;
+  assert(Result == EvaluateAsInt(Ctx) && "Inconsistent Evaluate() result!");
+  return true;
+}
+
+bool Expr::isIntegerConstantExprInternal(llvm::APSInt &Result, ASTContext &Ctx,
+                                 SourceLocation *Loc, bool isEvaluated) const {
+  
   // Pretest for integral type; some parts of the code crash for types that
   // can't be sized.
   if (!getType()->isIntegralType()) {
@@ -885,6 +894,7 @@
     return cast<ParenExpr>(this)->getSubExpr()->
                      isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
   case IntegerLiteralClass:
+    // NOTE: getValue() returns an APInt, we must set sign.
     Result = cast<IntegerLiteral>(this)->getValue();
     Result.setIsUnsigned(getType()->isUnsignedIntegerType());    
     break;
@@ -1107,7 +1117,7 @@
       
       // The result of the constant expr is the RHS.
       Result = RHS;
-      return true;
+      break;
     }
 
     assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
@@ -1208,9 +1218,12 @@
       }
     
     // Evaluate the false one first, discard the result.
-    if (FalseExp && !FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
+    llvm::APSInt Tmp;
+    if (FalseExp && !FalseExp->isIntegerConstantExpr(Tmp, Ctx, Loc, false))
       return false;
-    // Evalute the true one, capture the result.
+    // Evalute the true one, capture the result. Note that if TrueExp
+    // is False then this is an instant of the gcc missing LHS
+    // extension, and we will just reuse Result.
     if (TrueExp && 
         !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
       return false;

Modified: cfe/trunk/test/Sema/const-eval.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/const-eval.c?rev=64865&r1=64864&r2=64865&view=diff

==============================================================================
--- cfe/trunk/test/Sema/const-eval.c (original)
+++ cfe/trunk/test/Sema/const-eval.c Tue Feb 17 18:47:45 2009
@@ -29,3 +29,6 @@
 
 // FIXME: Turn into EVAL_EXPR test once we have more folding.
 _Complex float g16 = (1.0f + 1.0fi);
+
+// ?: in constant expressions.
+int g17[(3?:1) - 2]; 





More information about the cfe-commits mailing list