[cfe-commits] r67869 - /cfe/trunk/lib/Analysis/BugReporter.cpp

Ted Kremenek kremenek at apple.com
Fri Mar 27 14:16:25 PDT 2009


Author: kremenek
Date: Fri Mar 27 16:16:25 2009
New Revision: 67869

URL: http://llvm.org/viewvc/llvm-project?rev=67869&view=rev
Log:
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*.

Modified:
    cfe/trunk/lib/Analysis/BugReporter.cpp

Modified: cfe/trunk/lib/Analysis/BugReporter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/BugReporter.cpp?rev=67869&r1=67868&r2=67869&view=diff

==============================================================================
--- cfe/trunk/lib/Analysis/BugReporter.cpp (original)
+++ cfe/trunk/lib/Analysis/BugReporter.cpp Fri Mar 27 16:16:25 2009
@@ -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"));





More information about the cfe-commits mailing list