BugReporter: For control-flow edges from 'if', 'for', 'do', 'while' to
successor, using 'getEnclosingStmt()' to have the end location be the top-level
Stmt* enclosing the target Expr*.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@67869 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Analysis/BugReporter.cpp b/lib/Analysis/BugReporter.cpp
index 88d4fa1..ac3d961 100644
--- a/lib/Analysis/BugReporter.cpp
+++ b/lib/Analysis/BugReporter.cpp
@@ -108,6 +108,8 @@
     return *PM.get();
   }
   
+  PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S);
+  
   bool supportsLogicalOpControlFlow() const {
     return PDC ? PDC->supportsLogicalOpControlFlow() : true;
   }  
@@ -142,6 +144,23 @@
   return Loc;
 }
 
+PathDiagnosticLocation
+PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
+  assert(S && "Null Stmt* passed to getEnclosingStmtLocation");
+  ParentMap &P = getParentMap();
+  while (isa<Expr>(S)) {
+    const Stmt *Parent = P.getParent(S);
+    
+    if (!Parent || isa<CompoundStmt>(Parent) || isa<StmtExpr>(Parent))
+      return PathDiagnosticLocation(S, SMgr);
+    
+    S = Parent;
+  }
+  
+  assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
+  return PathDiagnosticLocation(S, SMgr);
+}
+
 //===----------------------------------------------------------------------===//
 // Methods for BugType and subclasses.
 //===----------------------------------------------------------------------===//
@@ -772,7 +791,7 @@
           
           std::string sbuf;
           llvm::raw_string_ostream os(sbuf);          
-          PathDiagnosticLocation End(S->getLocStart(), SMgr);
+          const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
           
           os << "Control jumps to line "
              << End.asLocation().getInstantiationLineNumber();
@@ -921,12 +940,20 @@
             llvm::raw_string_ostream os(sbuf);
             
             os << "Loop condition is true. ";
-            PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);            
+            PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
+
+            if (const Stmt *S = End.asStmt())
+              End = PDB.getEnclosingStmtLocation(S);
+            
             PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
                                                              os.str()));
           }
           else {
             PathDiagnosticLocation End = PDB.ExecutionContinues(N);
+            
+            if (const Stmt *S = End.asStmt())
+              End = PDB.getEnclosingStmtLocation(S);
+
             PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
                                     "Loop condition is false.  Exiting loop"));
           }
@@ -935,18 +962,23 @@
         }
           
         case Stmt::WhileStmtClass:
-        case Stmt::ForStmtClass: {                    
+        case Stmt::ForStmtClass: {          
           if (*(Src->succ_begin()+1) == Dst) {
             std::string sbuf;
             llvm::raw_string_ostream os(sbuf);
 
             os << "Loop condition is false. ";
             PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
+            if (const Stmt *S = End.asStmt())
+              End = PDB.getEnclosingStmtLocation(S);
+
             PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
                                                              os.str()));
           }
           else {
             PathDiagnosticLocation End = PDB.ExecutionContinues(N);
+            if (const Stmt *S = End.asStmt())
+              End = PDB.getEnclosingStmtLocation(S);
             
             PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
                                "Loop condition is true.  Entering loop body"));
@@ -956,7 +988,11 @@
         }
           
         case Stmt::IfStmtClass: {
-          PathDiagnosticLocation End = PDB.ExecutionContinues(N);          
+          PathDiagnosticLocation End = PDB.ExecutionContinues(N);
+
+          if (const Stmt *S = End.asStmt())
+            End = PDB.getEnclosingStmtLocation(S);
+          
           if (*(Src->succ_begin()+1) == Dst)
             PD.push_front(new PathDiagnosticControlFlowPiece(Start, End,
                                                        "Taking false branch"));