[cfe-commits] r60305 - in /cfe/trunk/lib/Sema: Sema.h SemaExpr.cpp

Anders Carlsson andersca at mac.com
Sun Nov 30 11:50:32 PST 2008


Author: andersca
Date: Sun Nov 30 13:50:32 2008
New Revision: 60305

URL: http://llvm.org/viewvc/llvm-project?rev=60305&view=rev
Log:
Add Sema::VerifyIntegerConstantExpression

Modified:
    cfe/trunk/lib/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaExpr.cpp

Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=60305&r1=60304&r2=60305&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Sun Nov 30 13:50:32 2008
@@ -1347,6 +1347,11 @@
 
   void InitBuiltinVaListType();
 
+  /// VerifyIntegerConstantExpression - verifies that an expression is an ICE,
+  /// and reports the appropriate diagnostics. Returns false on success.
+  /// Can optionally return the value of the expression.
+  bool VerifyIntegerConstantExpression(const Expr* E, llvm::APSInt *Result = 0);
+
   //===--------------------------------------------------------------------===//
   // Extra semantic analysis beyond the C type system
 private:

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Sun Nov 30 13:50:32 2008
@@ -3673,3 +3673,36 @@
     << SrcExpr->getSourceRange();
   return isInvalid;
 }
+
+bool Sema::VerifyIntegerConstantExpression(const Expr* E, llvm::APSInt *Result)
+{
+  Expr::EvalResult EvalResult;
+
+  if (!E->Evaluate(EvalResult, Context) || !EvalResult.Val.isInt() || 
+      EvalResult.HasSideEffects) {
+    Diag(E->getExprLoc(), diag::err_expr_not_ice) << E->getSourceRange();
+
+    if (EvalResult.Diag) {
+      // We only show the note if it's not the usual "invalid subexpression"
+      // or if it's actually in a subexpression.
+      if (EvalResult.Diag != diag::note_invalid_subexpr_in_ice ||
+          E->IgnoreParens() != EvalResult.DiagExpr->IgnoreParens())
+        Diag(EvalResult.DiagLoc, EvalResult.Diag);
+    }
+    
+    return true;
+  }
+
+  if (EvalResult.Diag) {
+    Diag(E->getExprLoc(), diag::ext_expr_not_ice) << 
+      E->getSourceRange();
+
+    // Print the reason it's not a constant.
+    if (Diags.getDiagnosticLevel(diag::ext_expr_not_ice) != Diagnostic::Ignored)
+      Diag(EvalResult.DiagLoc, EvalResult.Diag);
+  }
+  
+  if (Result)
+    *Result = EvalResult.Val.getInt();
+  return false;
+}





More information about the cfe-commits mailing list