[cfe-commits] r143496 - in /cfe/trunk: lib/AST/ExprConstant.cpp test/Sema/const-eval.c

Richard Smith richard-llvm at metafoo.co.uk
Tue Nov 1 14:06:14 PDT 2011


Author: rsmith
Date: Tue Nov  1 16:06:14 2011
New Revision: 143496

URL: http://llvm.org/viewvc/llvm-project?rev=143496&view=rev
Log:
When constant-folding, don't look at the initializer of a global const variable
if it's marked as weak: that definition may not end up being used.

Modified:
    cfe/trunk/lib/AST/ExprConstant.cpp
    cfe/trunk/test/Sema/const-eval.c

Modified: cfe/trunk/lib/AST/ExprConstant.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprConstant.cpp?rev=143496&r1=143495&r2=143496&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ExprConstant.cpp (original)
+++ cfe/trunk/lib/AST/ExprConstant.cpp Tue Nov  1 16:06:14 2011
@@ -270,16 +270,17 @@
          !isa<MaterializeTemporaryExpr>(Value.Base);
 }
 
-static bool IsWeakLValue(const LValue &Value) {
-  const ValueDecl *Decl = GetLValueBaseDecl(Value);
-  if (!Decl)
-    return false;
-
+static bool IsWeakDecl(const ValueDecl *Decl) {
   return Decl->hasAttr<WeakAttr>() ||
          Decl->hasAttr<WeakRefAttr>() ||
          Decl->isWeakImported();
 }
 
+static bool IsWeakLValue(const LValue &Value) {
+  const ValueDecl *Decl = GetLValueBaseDecl(Value);
+  return Decl && IsWeakDecl(Decl);
+}
+
 static bool EvalPointerValueAsBool(const LValue &Value, bool &Result) {
   const Expr* Base = Value.Base;
 
@@ -394,6 +395,11 @@
     return true;
   }
 
+  // Never evaluate the initializer of a weak variable. We can't be sure that
+  // this is the definition which will be used.
+  if (IsWeakDecl(VD))
+    return false;
+
   const Expr *Init = VD->getAnyInitializer();
   if (!Init)
     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=143496&r1=143495&r2=143496&view=diff
==============================================================================
--- cfe/trunk/test/Sema/const-eval.c (original)
+++ cfe/trunk/test/Sema/const-eval.c Tue Nov  1 16:06:14 2011
@@ -95,3 +95,7 @@
 
 union u { int a; char b[4]; };
 char c = ((union u)(123456)).b[0]; // expected-error {{not a compile-time constant}}
+
+extern const int weak_int __attribute__((weak));
+const int weak_int = 42;
+int weak_int_test = weak_int; // expected-error {{not a compile-time constant}}





More information about the cfe-commits mailing list