[cfe-commits] r138613 - in /cfe/trunk: include/clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h lib/StaticAnalyzer/Checkers/StackAddrEscapeChecker.cpp lib/StaticAnalyzer/Core/MemRegion.cpp test/Analysis/stack-addr-ps.cpp test/Analysis/stack-block-returned.cpp

Jeffrey Yasskin jyasskin at google.com
Thu Aug 25 17:41:31 PDT 2011


Author: jyasskin
Date: Thu Aug 25 19:41:31 2011
New Revision: 138613

URL: http://llvm.org/viewvc/llvm-project?rev=138613&view=rev
Log:
Handle CXXTempObjectRegion in StackAddrEscapeChecker.

Also convert stack-addr-ps.cpp to use the analyzer instead of just Sema, now
that it doesn't crash, and extract the stack-block test into another file since
it errors, and that prevents the analyzer from running.

Added:
    cfe/trunk/test/Analysis/stack-block-returned.cpp
Modified:
    cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h
    cfe/trunk/lib/StaticAnalyzer/Checkers/StackAddrEscapeChecker.cpp
    cfe/trunk/lib/StaticAnalyzer/Core/MemRegion.cpp
    cfe/trunk/test/Analysis/stack-addr-ps.cpp

Modified: cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h?rev=138613&r1=138612&r2=138613&view=diff
==============================================================================
--- cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h (original)
+++ cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h Thu Aug 25 19:41:31 2011
@@ -856,6 +856,8 @@
                             Expr const *E, const MemRegion *sReg);
   
 public:
+  const Expr *getExpr() const { return Ex; }
+
   QualType getValueType() const {
     return Ex->getType();
   }

Modified: cfe/trunk/lib/StaticAnalyzer/Checkers/StackAddrEscapeChecker.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Checkers/StackAddrEscapeChecker.cpp?rev=138613&r1=138612&r2=138613&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Checkers/StackAddrEscapeChecker.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Checkers/StackAddrEscapeChecker.cpp Thu Aug 25 19:41:31 2011
@@ -76,6 +76,11 @@
        << VR->getString() << '\'';
     range = VR->getDecl()->getSourceRange();
   }
+  else if (const CXXTempObjectRegion *TOR = dyn_cast<CXXTempObjectRegion>(R)) {
+    os << "stack memory associated with temporary object of type '"
+       << TOR->getValueType().getAsString() << '\'';
+    range = TOR->getExpr()->getSourceRange();
+  }
   else {
     assert(false && "Invalid region in ReturnStackAddressChecker.");
   } 

Modified: cfe/trunk/lib/StaticAnalyzer/Core/MemRegion.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/MemRegion.cpp?rev=138613&r1=138612&r2=138613&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Core/MemRegion.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Core/MemRegion.cpp Thu Aug 25 19:41:31 2011
@@ -424,7 +424,8 @@
 }
 
 void CXXTempObjectRegion::dumpToStream(raw_ostream &os) const {
-  os << "temp_object";
+  os << "temp_object{" << getValueType().getAsString() << ','
+     << (void*) Ex << '}';
 }
 
 void CXXBaseObjectRegion::dumpToStream(raw_ostream &os) const {

Modified: cfe/trunk/test/Analysis/stack-addr-ps.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/stack-addr-ps.cpp?rev=138613&r1=138612&r2=138613&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/stack-addr-ps.cpp (original)
+++ cfe/trunk/test/Analysis/stack-addr-ps.cpp Thu Aug 25 19:41:31 2011
@@ -1,38 +1,38 @@
-// RUN: %clang_cc1 -fsyntax-only -fblocks -verify %s
+// RUN: %clang_cc1 -analyze -analyzer-checker=core -analyzer-store=region -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}}
+  return s; // expected-warning{{Address of stack memory associated with local variable 's' returned}} expected-warning{{reference to stack memory associated with local variable 's' returned}}
 }
 
 const int& g2() {
   int s1;
   int &s2 = s1; // expected-note {{binding reference variable 's2' here}}
-  return s2; // expected-warning {{reference to stack memory associated with local variable 's1' returned}}
+  return s2; // expected-warning{{Address of stack memory associated with local variable 's1' returned}} expected-warning {{reference to stack memory associated with local variable 's1' returned}}
 }
 
 const int& g3() {
   int s1;
   int &s2 = s1; // expected-note {{binding reference variable 's2' here}}
   int &s3 = s2; // expected-note {{binding reference variable 's3' here}}
-  return s3; // expected-warning {{reference to stack memory associated with local variable 's1' returned}}
+  return s3; // expected-warning{{Address of stack memory associated with local variable 's1' returned}} expected-warning {{reference to stack memory associated with local variable 's1' returned}}
 }
 
 int get_value();
 
-const int &get_reference1() { return get_value(); } // expected-warning {{returning reference to local temporary}}
+const int &get_reference1() { return get_value(); } // expected-warning{{Address of stack memory associated with temporary object of type 'const int' returned}} expected-warning {{returning reference to local temporary}}
 
 const int &get_reference2() {
   const int &x = get_value(); // expected-note {{binding reference variable 'x' here}}
-  return x; // expected-warning {{returning reference to local temporary}}
+  return x; // expected-warning{{Address of stack memory associated with temporary object of type 'const int' returned}} expected-warning {{returning reference to local temporary}}
 }
 
 const int &get_reference3() {
   const int &x1 = get_value(); // expected-note {{binding reference variable 'x1' here}}
   const int &x2 = x1; // expected-note {{binding reference variable 'x2' here}}
-  return x2; // expected-warning {{returning reference to local temporary}}
+  return x2; // expected-warning{{Address of stack memory associated with temporary object of type 'const int' returned}} expected-warning {{returning reference to local temporary}}
 }
 
 int global_var;
@@ -44,7 +44,7 @@
 int *f2() {
   int x1;
   int &x2 = x1; // expected-note {{binding reference variable 'x2' here}}
-  return &x2; // expected-warning {{address of stack memory associated with local variable 'x1' returned}}
+  return &x2; // expected-warning{{Address of stack memory associated with local variable 'x1' returned}} expected-warning {{address of stack memory associated with local variable 'x1' returned}}
 }
 
 int *f3() {
@@ -56,7 +56,7 @@
 const int *f4() {
   const int &x1 = get_value(); // expected-note {{binding reference variable 'x1' here}}
   const int &x2 = x1; // expected-note {{binding reference variable 'x2' here}}
-  return &x2; // expected-warning {{returning address of local temporary}}
+  return &x2; // expected-warning{{Address of stack memory associated with temporary object of type 'const int' returned}} expected-warning {{returning address of local temporary}}
 }
 
 struct S {
@@ -67,7 +67,7 @@
   S s1;
   S &s2 = s1; // expected-note {{binding reference variable 's2' here}}
   int &x = s2.x; // expected-note {{binding reference variable 'x' here}}
-  return &x; // expected-warning {{address of stack memory associated with local variable 's1' returned}}
+  return &x; // expected-warning{{Address of stack memory associated with local variable 's1' returned}} expected-warning {{address of stack memory associated with local variable 's1' returned}}
 }
 
 void *lf() {
@@ -76,14 +76,6 @@
     return x; // expected-warning {{returning address of label, which is local}}
 }
 
-typedef void (^bptr)(void);
-
-bptr bf(int j) {
-  __block int i;
-  const bptr &qq = ^{ i=0; }; // expected-note {{binding reference variable 'qq' here}}
-  return qq; // expected-error {{returning block that lives on the local stack}}
-}
-
 template <typename T>
 struct TS {
   int *get();

Added: cfe/trunk/test/Analysis/stack-block-returned.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/stack-block-returned.cpp?rev=138613&view=auto
==============================================================================
--- cfe/trunk/test/Analysis/stack-block-returned.cpp (added)
+++ cfe/trunk/test/Analysis/stack-block-returned.cpp Thu Aug 25 19:41:31 2011
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -analyze -analyzer-checker=core -analyzer-store=region -fblocks -verify %s
+
+typedef void (^bptr)(void);
+
+bptr bf(int j) {
+  __block int i;
+  const bptr &qq = ^{ i=0; }; // expected-note {{binding reference variable 'qq' here}}
+  return qq; // expected-error {{returning block that lives on the local stack}}
+}





More information about the cfe-commits mailing list