aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRafael Espindola <rafael.espindola@gmail.com>2010-05-07 15:18:43 +0000
committerRafael Espindola <rafael.espindola@gmail.com>2010-05-07 15:18:43 +0000
commita7d3c04fcfe9d4af2f7758f46aef26b1a8f8ac09 (patch)
treea4af21aa17e871ab40dbcf20bcabb117380cb4cb
parent565f8d6cc0c5e5a14fa9a8c93970e23378307ff7 (diff)
Fix PR4386 by implementing gcc's old behaviour (4.2) when initializing
variables with a comparison of a function pointer with 0. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@103253 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--lib/AST/ExprConstant.cpp17
-rw-r--r--test/Sema/init.c14
2 files changed, 28 insertions, 3 deletions
diff --git a/lib/AST/ExprConstant.cpp b/lib/AST/ExprConstant.cpp
index 30ef6f3aec..7f831737d1 100644
--- a/lib/AST/ExprConstant.cpp
+++ b/lib/AST/ExprConstant.cpp
@@ -70,9 +70,20 @@ static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info);
//===----------------------------------------------------------------------===//
static bool EvalPointerValueAsBool(APValue& Value, bool& Result) {
- // FIXME: Is this accurate for all kinds of bases? If not, what would
- // the check look like?
- Result = Value.getLValueBase() || !Value.getLValueOffset().isZero();
+ const Expr* Base = Value.getLValueBase();
+
+ Result = Base || !Value.getLValueOffset().isZero();
+
+ const DeclRefExpr* DeclRef = dyn_cast<DeclRefExpr>(Base);
+ if (!DeclRef)
+ return true;
+
+ const ValueDecl* Decl = DeclRef->getDecl();
+ if (Decl->hasAttr<WeakAttr>() ||
+ Decl->hasAttr<WeakRefAttr>() ||
+ Decl->hasAttr<WeakImportAttr>())
+ return false;
+
return true;
}
diff --git a/test/Sema/init.c b/test/Sema/init.c
index b9867cf502..c2c29ad9b0 100644
--- a/test/Sema/init.c
+++ b/test/Sema/init.c
@@ -131,3 +131,17 @@ const wchar_t widestr[] = L"asdf";
// PR5447
const double pr5447 = (0.05 < -1.0) ? -1.0 : 0.0499878;
+// PR4386
+
+// None of these are constant initializers, but we implement GCC's old
+// behaviour of accepting bar and zed but not foo. GCC's behaviour was
+// changed in 2007 (rev 122551), so we should be able to change too one
+// day.
+int PR4386_bar();
+int PR4386_foo() __attribute((weak));
+int PR4386_zed();
+
+int PR4386_a = ((void *) PR4386_bar) != 0;
+int PR4386_b = ((void *) PR4386_foo) != 0; // expected-error{{initializer element is not a compile-time constant}}
+int PR4386_c = ((void *) PR4386_zed) != 0;
+int PR4386_zed() __attribute((weak));