[cfe-commits] r140131 - in /cfe/trunk: include/clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h lib/StaticAnalyzer/Core/PathDiagnostic.cpp

Anna Zaks ganna at apple.com
Mon Sep 19 18:51:41 PDT 2011


Author: zaks
Date: Mon Sep 19 20:51:40 2011
New Revision: 140131

URL: http://llvm.org/viewvc/llvm-project?rev=140131&view=rev
Log:
[analyzer] Refactor PathDiagnosticLocation: Pre-compute Range and Location with gen methods on object creation instead of computing on demand. This would allow to remove dependency on the other members which help with construction and might not even be valid at later stages (to be removed later on).

Modified:
    cfe/trunk/include/clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h
    cfe/trunk/lib/StaticAnalyzer/Core/PathDiagnostic.cpp

Modified: cfe/trunk/include/clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h?rev=140131&r1=140130&r2=140131&view=diff
==============================================================================
--- cfe/trunk/include/clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h (original)
+++ cfe/trunk/include/clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h Mon Sep 19 20:51:40 2011
@@ -27,6 +27,7 @@
 class CompoundStmt;
 class Decl;
 class LocationContext;
+class ParentMap;
 class ProgramPoint;
 class SourceManager;
 class Stmt;
@@ -79,10 +80,12 @@
 
 class PathDiagnosticRange : public SourceRange {
 public:
-  const bool isPoint;
+  bool isPoint;
 
   PathDiagnosticRange(const SourceRange &R, bool isP = false)
     : SourceRange(R), isPoint(isP) {}
+
+  PathDiagnosticRange() : isPoint(false) {}
 };
 
 class PathDiagnosticLocation {
@@ -93,24 +96,36 @@
   const Decl *D;
   const SourceManager *SM;
   const LocationContext *LC;
+  FullSourceLoc Loc;
+  PathDiagnosticRange Range;
+
+  FullSourceLoc genLocation(const ParentMap *PM=0) const;
+  PathDiagnosticRange genRange(const ParentMap *PM=0) const;
+
 public:
   PathDiagnosticLocation()
-    : K(SingleLocK), S(0), D(0), SM(0), LC(0) {}
+    : K(SingleLocK), S(0), D(0), SM(0), LC(0) {
+  }
 
   PathDiagnosticLocation(FullSourceLoc L)
-    : K(SingleLocK), R(L, L), S(0), D(0), SM(&L.getManager()), LC(0) {}
+    : K(SingleLocK), R(L, L), S(0), D(0), SM(&L.getManager()), LC(0),
+      Loc(genLocation()), Range(genRange()) {
+  }
 
   PathDiagnosticLocation(SourceLocation L, const SourceManager &sm,
                          Kind kind = SingleLocK)
-    : K(kind), R(L, L), S(0), D(0), SM(&sm), LC(0) {}
+    : K(kind), R(L, L), S(0), D(0), SM(&sm), LC(0),
+      Loc(genLocation()), Range(genRange()) {
+  }
 
   PathDiagnosticLocation(const Stmt *s,
                          const SourceManager &sm,
-                         const LocationContext *lc)
-    : K(StmtK), S(s), D(0), SM(&sm), LC(lc) {}
+                         const LocationContext *lc);
 
   PathDiagnosticLocation(const Decl *d, const SourceManager &sm)
-    : K(DeclK), S(0), D(d), SM(&sm), LC(0) {}
+    : K(DeclK), S(0), D(d), SM(&sm), LC(0),
+      Loc(genLocation()), Range(genRange()) {
+  }
 
   // Create a location for the beginning of the statement.
   static PathDiagnosticLocation createBeginStmt(const Stmt *S,
@@ -167,8 +182,14 @@
     return SM != 0;
   }
 
-  FullSourceLoc asLocation() const;
-  PathDiagnosticRange asRange() const;
+  FullSourceLoc asLocation() const {
+    return Loc;
+  }
+
+  PathDiagnosticRange asRange() const {
+    return Range;
+  }
+
   const Stmt *asStmt() const { assert(isValid()); return S; }
   const Decl *asDecl() const { assert(isValid()); return D; }
 

Modified: cfe/trunk/lib/StaticAnalyzer/Core/PathDiagnostic.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/PathDiagnostic.cpp?rev=140131&r1=140130&r2=140131&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Core/PathDiagnostic.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Core/PathDiagnostic.cpp Mon Sep 19 20:51:40 2011
@@ -131,32 +131,40 @@
 //===----------------------------------------------------------------------===//
 
 static SourceLocation getValidSourceLocation(const Stmt* S,
-                                             const LocationContext *LC) {
-  assert(LC);
+                                             const ParentMap &PM) {
   SourceLocation L = S->getLocStart();
 
   // S might be a temporary statement that does not have a location in the
   // source code, so find an enclosing statement and use it's location.
-  if (!L.isValid()) {
-    ParentMap & PM = LC->getParentMap();
-
-    while (!L.isValid()) {
-      S = PM.getParent(S);
-      L = S->getLocStart();
-    }
+  while (!L.isValid()) {
+    S = PM.getParent(S);
+    L = S->getLocStart();
   }
 
   return L;
 }
 
+PathDiagnosticLocation::PathDiagnosticLocation(const Stmt *s,
+                                               const SourceManager &sm,
+                                               const LocationContext *lc)
+  : K(StmtK), S(s), D(0), SM(&sm), LC(lc)
+{
+  const ParentMap* PM = 0;
+  if (lc)
+    PM = &lc->getParentMap();
+
+  Loc = genLocation(PM);
+  Range = genRange(PM);
+}
+
 PathDiagnosticLocation
   PathDiagnosticLocation::createBeginStmt(const Stmt *S,
                                           const SourceManager &SM,
                                           const LocationContext *LC) {
-  return PathDiagnosticLocation(getValidSourceLocation(S, LC), SM, SingleLocK);
+  return PathDiagnosticLocation(getValidSourceLocation(S, LC->getParentMap()),
+                                SM, SingleLocK);
 }
 
-
 PathDiagnosticLocation
   PathDiagnosticLocation::createOperatorLoc(const BinaryOperator *BO,
                                             const SourceManager &SM) {
@@ -246,7 +254,8 @@
   return PathDiagnosticLocation(L, L.getManager(), SingleLocK);
 }
 
-FullSourceLoc PathDiagnosticLocation::asLocation() const {
+FullSourceLoc
+  PathDiagnosticLocation::genLocation(const ParentMap *PM) const {
   assert(isValid());
   // Note that we want a 'switch' here so that the compiler can warn us in
   // case we add more cases.
@@ -255,7 +264,7 @@
     case RangeK:
       break;
     case StmtK:
-      return FullSourceLoc(getValidSourceLocation(S, LC),
+      return FullSourceLoc(getValidSourceLocation(S, LC->getParentMap()),
                            const_cast<SourceManager&>(*SM));
     case DeclK:
       return FullSourceLoc(D->getLocation(), const_cast<SourceManager&>(*SM));
@@ -264,7 +273,8 @@
   return FullSourceLoc(R.getBegin(), const_cast<SourceManager&>(*SM));
 }
 
-PathDiagnosticRange PathDiagnosticLocation::asRange() const {
+PathDiagnosticRange
+  PathDiagnosticLocation::genRange(const ParentMap *PM) const {
   assert(isValid());
   // Note that we want a 'switch' here so that the compiler can warn us in
   // case we add more cases.
@@ -299,7 +309,7 @@
         case Stmt::BinaryConditionalOperatorClass:
         case Stmt::ConditionalOperatorClass:
         case Stmt::ObjCForCollectionStmtClass: {
-          SourceLocation L = getValidSourceLocation(S, LC);
+          SourceLocation L = getValidSourceLocation(S, LC->getParentMap());
           return SourceRange(L, L);
         }
       }





More information about the cfe-commits mailing list