Enhance path-sensitive return-of-stack-address check to print out the line number of a compound literal (whose address is being returned) instead of printing out the hex representation of the pointer address of the CompoundLiteralExpr.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@58478 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Analysis/GRExprEngineInternalChecks.cpp b/lib/Analysis/GRExprEngineInternalChecks.cpp
index c313697..428ffb2 100644
--- a/lib/Analysis/GRExprEngineInternalChecks.cpp
+++ b/lib/Analysis/GRExprEngineInternalChecks.cpp
@@ -14,6 +14,7 @@
 
 #include "clang/Analysis/PathSensitive/BugReporter.h"
 #include "clang/Analysis/PathSensitive/GRExprEngine.h"
+#include "clang/Basic/SourceManager.h"
 #include "llvm/Support/Compiler.h"
 #include <sstream>
 
@@ -200,13 +201,29 @@
       
       // Generate a report for this bug.
       std::ostringstream os;
-      os << "Address of stack memory associated with local variable '"
-         << V.getRegion()->getString() << "' returned.";
+      SourceRange R;
       
-      std::string s = os.str();
+      // Check if the region is a compound literal.
+      if (const CompoundLiteralRegion* CR = 
+            dyn_cast<CompoundLiteralRegion>(V.getRegion())) {
+        
+        const CompoundLiteralExpr* CL = CR->getLiteralExpr();
+        os << "Address of stack memory associated with a compound literal "
+              "declared on line "
+            << BR.getSourceManager().getLogicalLineNumber(CL->getLocStart())
+            << " returned.";
+        
+        R = CL->getSourceRange();
+      }
+      else {        
+        os << "Address of stack memory associated with local variable '"
+           << V.getRegion()->getString() << "' returned.";
+      }
       
+      std::string s = os.str();      
       RangedBugReport report(*this, N, s.c_str());
       report.addRange(E->getSourceRange());
+      if (R.isValid()) report.addRange(R);
       
       // Emit the warning.
       BR.EmitWarning(report);