]> granicus.if.org Git - clang/commitdiff
PR7242: Make sure to use a different context for evaluating constant
authorEli Friedman <eli.friedman@gmail.com>
Mon, 6 Sep 2010 00:10:32 +0000 (00:10 +0000)
committerEli Friedman <eli.friedman@gmail.com>
Mon, 6 Sep 2010 00:10:32 +0000 (00:10 +0000)
initializers, so the result of the evaluation doesn't leak through
inconsistently.  Also, don't evaluate references to variables with
initializers with side-effects.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@113128 91177308-0d34-0410-b5e6-96231b3b80d8

lib/AST/ExprConstant.cpp
test/CodeGen/fold-const-declref.c [new file with mode: 0644]

index 175fd85a24462857f0bf8e8a76128b594917a3df..7347f5a43e17b76863f8c6cb86f2c322c6c47be8 100644 (file)
@@ -1008,8 +1008,11 @@ bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) {
 
         VD->setEvaluatingValue();
 
-        if (Visit(const_cast<Expr*>(Init))) {
+        Expr::EvalResult EResult;
+        if (Init->Evaluate(EResult, Info.Ctx) && !EResult.HasSideEffects &&
+            EResult.Val.isInt()) {
           // Cache the evaluated value in the variable declaration.
+          Result = EResult.Val;
           VD->setEvaluatedValue(Result);
           return true;
         }
diff --git a/test/CodeGen/fold-const-declref.c b/test/CodeGen/fold-const-declref.c
new file mode 100644 (file)
index 0000000..5a7ba8e
--- /dev/null
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -verify -emit-llvm-only
+
+// PR7242: Check that this doesn't crash.
+int main(void)
+{
+  int __negative = 1;
+  const int __max = __negative && 0 ;
+  __max / 0;
+}