From e299ba66f5f8e270f7a0f2f44f90e72ad67ad8a3 Mon Sep 17 00:00:00 2001 From: Chandler Carruth Date: Sun, 16 May 2010 09:32:51 +0000 Subject: When constant folding reference variables with an initializer to the initializer, don't fold paramters. Their initializers are just default arguments which can be overridden. This fixes some spectacular regressions due to more things making it into the constant folding. llvm-svn: 103904 --- clang/lib/AST/ExprConstant.cpp | 4 ++++ clang/test/CodeGenCXX/references.cpp | 13 +++++++++++++ 2 files changed, 17 insertions(+) (limited to 'clang') diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index f903035d220..dc614018ec2 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -355,6 +355,10 @@ bool LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E) { } else if (VarDecl* VD = dyn_cast(E->getDecl())) { if (!VD->getType()->isReferenceType()) return Success(E); + // Reference parameters can refer to anything even if they have an + // "initializer" in the form of a default argument. + if (isa(VD)) + return false; // FIXME: Check whether VD might be overridden! if (const Expr *Init = VD->getAnyInitializer()) return Visit(const_cast(Init)); diff --git a/clang/test/CodeGenCXX/references.cpp b/clang/test/CodeGenCXX/references.cpp index 5a5947dd816..3ae1e474f88 100644 --- a/clang/test/CodeGenCXX/references.cpp +++ b/clang/test/CodeGenCXX/references.cpp @@ -155,3 +155,16 @@ void f0(s1 a) { s1 b = a; } // CHECK: load // CHECK: ret const int &f2() { return 0; } + +// Don't constant fold const reference parameters with default arguments to +// their default arguments. +namespace N1 { + const int foo = 1; + // CHECK: @_ZN2N14test + int test(const int& arg = foo) { + // Ensure this array is on the stack where we can set values instead of + // being a global constant. + // CHECK: %args_array = alloca + const int* const args_array[] = { &arg }; + } +} -- cgit v1.2.3