[cfe-commits] r110242 - in /cfe/trunk: lib/Sema/SemaChecking.cpp test/Analysis/stack-addr-ps.cpp

Ted Kremenek kremenek at apple.com
Wed Aug 4 13:01:07 PDT 2010


Author: kremenek
Date: Wed Aug  4 15:01:07 2010
New Revision: 110242

URL: http://llvm.org/viewvc/llvm-project?rev=110242&view=rev
Log:
Teach SemaChecking::CheckReturnStackAddr about ImplicitCastExprs that convert values to an lvalue.  This allows us to warn (again) about returning references to stack variables.  (fixes PR 7812).

Added:
    cfe/trunk/test/Analysis/stack-addr-ps.cpp
Modified:
    cfe/trunk/lib/Sema/SemaChecking.cpp

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=110242&r1=110241&r2=110242&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Wed Aug  4 15:01:07 2010
@@ -1952,7 +1952,7 @@
 ///  EvalVal - This function is complements EvalAddr in the mutual recursion.
 ///   See the comments for EvalAddr for more details.
 static DeclRefExpr* EvalVal(Expr *E) {
-
+do {
   // We should only be called for evaluating non-pointer expressions, or
   // expressions with a pointer type that are not used as references but instead
   // are l-values (e.g., DeclRefExpr with a pointer type).
@@ -1961,6 +1961,15 @@
   // viewed AST node.  We then recursively traverse the AST by calling
   // EvalAddr and EvalVal appropriately.
   switch (E->getStmtClass()) {
+  case Stmt::ImplicitCastExprClass: {
+    ImplicitCastExpr *IE = cast<ImplicitCastExpr>(E);
+    if (IE->getCategory() == ImplicitCastExpr::LValue) {
+      E = IE->getSubExpr();
+      continue;
+    }
+    return NULL;
+  }
+
   case Stmt::DeclRefExprClass: {
     // DeclRefExpr: the base case.  When we hit a DeclRefExpr we are looking
     //  at code that refers to a variable's name.  We check if it has local
@@ -1973,9 +1982,11 @@
     return NULL;
   }
 
-  case Stmt::ParenExprClass:
+  case Stmt::ParenExprClass: {
     // Ignore parentheses.
-    return EvalVal(cast<ParenExpr>(E)->getSubExpr());
+    E = cast<ParenExpr>(E)->getSubExpr();
+    continue;
+  }
 
   case Stmt::UnaryOperatorClass: {
     // The only unary operator that make sense to handle here
@@ -2024,6 +2035,7 @@
   default:
     return NULL;
   }
+} while (true);
 }
 
 //===--- CHECK: Floating-Point comparisons (-Wfloat-equal) ---------------===//

Added: cfe/trunk/test/Analysis/stack-addr-ps.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/stack-addr-ps.cpp?rev=110242&view=auto
==============================================================================
--- cfe/trunk/test/Analysis/stack-addr-ps.cpp (added)
+++ cfe/trunk/test/Analysis/stack-addr-ps.cpp Wed Aug  4 15:01:07 2010
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 -fsyntax-only -fblocks -verify %s
+
+// FIXME: Only the stack-address checking in Sema catches this right now, and
+// the stack analyzer doesn't handle the ImplicitCastExpr (lvalue).
+const int& g() {
+  int s;
+  return s; // expected-warning{{reference to stack memory associated with local variable 's' returned}}
+}





More information about the cfe-commits mailing list