Browse Source

[analyzer] Small cleanups when checkers retrieving statements from exploded
nodes.

Differential Revision: https://reviews.llvm.org/D23550


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@279037 91177308-0d34-0410-b5e6-96231b3b80d8

Gabor Horvath 9 years ago
parent
commit
1779645f68

+ 1 - 6
lib/StaticAnalyzer/Checkers/DynamicTypeChecker.cpp

@@ -107,12 +107,7 @@ PathDiagnosticPiece *DynamicTypeChecker::DynamicTypeBugVisitor::VisitNode(
     return nullptr;
     return nullptr;
 
 
   // Retrieve the associated statement.
   // Retrieve the associated statement.
-  const Stmt *S = nullptr;
-  ProgramPoint ProgLoc = N->getLocation();
-  if (Optional<StmtPoint> SP = ProgLoc.getAs<StmtPoint>()) {
-    S = SP->getStmt();
-  }
-
+  const Stmt *S = PathDiagnosticLocation::getStmt(N);
   if (!S)
   if (!S)
     return nullptr;
     return nullptr;
 
 

+ 1 - 6
lib/StaticAnalyzer/Checkers/DynamicTypePropagation.cpp

@@ -909,12 +909,7 @@ PathDiagnosticPiece *DynamicTypePropagation::GenericsBugVisitor::VisitNode(
     return nullptr;
     return nullptr;
 
 
   // Retrieve the associated statement.
   // Retrieve the associated statement.
-  const Stmt *S = nullptr;
-  ProgramPoint ProgLoc = N->getLocation();
-  if (Optional<StmtPoint> SP = ProgLoc.getAs<StmtPoint>()) {
-    S = SP->getStmt();
-  }
-
+  const Stmt *S = PathDiagnosticLocation::getStmt(N);
   if (!S)
   if (!S)
     return nullptr;
     return nullptr;
 
 

+ 1 - 6
lib/StaticAnalyzer/Checkers/MacOSKeychainAPIChecker.cpp

@@ -524,12 +524,7 @@ MacOSKeychainAPIChecker::generateAllocatedDataNotReleasedReport(
   // allocated, and only report a single path.
   // allocated, and only report a single path.
   PathDiagnosticLocation LocUsedForUniqueing;
   PathDiagnosticLocation LocUsedForUniqueing;
   const ExplodedNode *AllocNode = getAllocationNode(N, AP.first, C);
   const ExplodedNode *AllocNode = getAllocationNode(N, AP.first, C);
-  const Stmt *AllocStmt = nullptr;
-  ProgramPoint P = AllocNode->getLocation();
-  if (Optional<CallExitEnd> Exit = P.getAs<CallExitEnd>())
-    AllocStmt = Exit->getCalleeContext()->getCallSite();
-  else if (Optional<clang::PostStmt> PS = P.getAs<clang::PostStmt>())
-    AllocStmt = PS->getStmt();
+  const Stmt *AllocStmt = PathDiagnosticLocation::getStmt(AllocNode);
 
 
   if (AllocStmt)
   if (AllocStmt)
     LocUsedForUniqueing = PathDiagnosticLocation::createBegin(AllocStmt,
     LocUsedForUniqueing = PathDiagnosticLocation::createBegin(AllocStmt,

+ 4 - 22
lib/StaticAnalyzer/Checkers/MallocChecker.cpp

@@ -2094,12 +2094,7 @@ void MallocChecker::reportLeak(SymbolRef Sym, ExplodedNode *N,
   const MemRegion *Region = nullptr;
   const MemRegion *Region = nullptr;
   std::tie(AllocNode, Region) = getAllocationSite(N, Sym, C);
   std::tie(AllocNode, Region) = getAllocationSite(N, Sym, C);
 
 
-  ProgramPoint P = AllocNode->getLocation();
-  const Stmt *AllocationStmt = nullptr;
-  if (Optional<CallExitEnd> Exit = P.getAs<CallExitEnd>())
-    AllocationStmt = Exit->getCalleeContext()->getCallSite();
-  else if (Optional<StmtPoint> SP = P.getAs<StmtPoint>())
-    AllocationStmt = SP->getStmt();
+  const Stmt *AllocationStmt = PathDiagnosticLocation::getStmt(AllocNode);
   if (AllocationStmt)
   if (AllocationStmt)
     LocUsedForUniqueing = PathDiagnosticLocation::createBegin(AllocationStmt,
     LocUsedForUniqueing = PathDiagnosticLocation::createBegin(AllocationStmt,
                                               C.getSourceManager(),
                                               C.getSourceManager(),
@@ -2626,22 +2621,7 @@ MallocChecker::MallocBugVisitor::VisitNode(const ExplodedNode *N,
   if (!RS)
   if (!RS)
     return nullptr;
     return nullptr;
 
 
-  const Stmt *S = nullptr;
-  const char *Msg = nullptr;
-  StackHintGeneratorForSymbol *StackHint = nullptr;
-
-  // Retrieve the associated statement.
-  ProgramPoint ProgLoc = N->getLocation();
-  if (Optional<StmtPoint> SP = ProgLoc.getAs<StmtPoint>()) {
-    S = SP->getStmt();
-  } else if (Optional<CallExitEnd> Exit = ProgLoc.getAs<CallExitEnd>()) {
-    S = Exit->getCalleeContext()->getCallSite();
-  } else if (Optional<BlockEdge> Edge = ProgLoc.getAs<BlockEdge>()) {
-    // If an assumption was made on a branch, it should be caught
-    // here by looking at the state transition.
-    S = Edge->getSrc()->getTerminator();
-  }
-
+  const Stmt *S = PathDiagnosticLocation::getStmt(N);
   if (!S)
   if (!S)
     return nullptr;
     return nullptr;
 
 
@@ -2649,6 +2629,8 @@ MallocChecker::MallocBugVisitor::VisitNode(const ExplodedNode *N,
   // (__attribute__((cleanup))).
   // (__attribute__((cleanup))).
 
 
   // Find out if this is an interesting point and what is the kind.
   // Find out if this is an interesting point and what is the kind.
+  const char *Msg = nullptr;
+  StackHintGeneratorForSymbol *StackHint = nullptr;
   if (Mode == Normal) {
   if (Mode == Normal) {
     if (isAllocated(RS, RSPrev, S)) {
     if (isAllocated(RS, RSPrev, S)) {
       Msg = "Memory is allocated";
       Msg = "Memory is allocated";

+ 1 - 4
lib/StaticAnalyzer/Checkers/NullabilityChecker.cpp

@@ -325,10 +325,7 @@ PathDiagnosticPiece *NullabilityChecker::NullabilityBugVisitor::VisitNode(
   // Retrieve the associated statement.
   // Retrieve the associated statement.
   const Stmt *S = TrackedNullab->getNullabilitySource();
   const Stmt *S = TrackedNullab->getNullabilitySource();
   if (!S) {
   if (!S) {
-    ProgramPoint ProgLoc = N->getLocation();
-    if (Optional<StmtPoint> SP = ProgLoc.getAs<StmtPoint>()) {
-      S = SP->getStmt();
-    }
+    S = PathDiagnosticLocation::getStmt(N);
   }
   }
 
 
   if (!S)
   if (!S)

+ 1 - 6
lib/StaticAnalyzer/Checkers/RetainCountChecker.cpp

@@ -2418,12 +2418,7 @@ CFRefLeakReport::CFRefLeakReport(CFRefBug &D, const LangOptions &LOpts,
   // FIXME: This will crash the analyzer if an allocation comes from an
   // FIXME: This will crash the analyzer if an allocation comes from an
   // implicit call (ex: a destructor call).
   // implicit call (ex: a destructor call).
   // (Currently there are no such allocations in Cocoa, though.)
   // (Currently there are no such allocations in Cocoa, though.)
-  const Stmt *AllocStmt = nullptr;
-  ProgramPoint P = AllocNode->getLocation();
-  if (Optional<CallExitEnd> Exit = P.getAs<CallExitEnd>())
-    AllocStmt = Exit->getCalleeContext()->getCallSite();
-  else
-    AllocStmt = P.castAs<PostStmt>().getStmt();
+  const Stmt *AllocStmt = PathDiagnosticLocation::getStmt(AllocNode);
   assert(AllocStmt && "Cannot find allocation statement");
   assert(AllocStmt && "Cannot find allocation statement");
 
 
   PathDiagnosticLocation AllocLocation =
   PathDiagnosticLocation AllocLocation =