[cfe-commits] r67767 - in /cfe/trunk: include/clang/Analysis/PathDiagnostic.h lib/Analysis/PathDiagnostic.cpp

Ted Kremenek kremenek at apple.com
Thu Mar 26 14:21:36 PDT 2009


Author: kremenek
Date: Thu Mar 26 16:21:35 2009
New Revision: 67767

URL: http://llvm.org/viewvc/llvm-project?rev=67767&view=rev
Log:
PathDiagnostics (analyzer):
- Added a new class, 'PathDiagnosticLocation', that is a variant for
  SourceLocation, SourceRange, or Stmt*. This will be used soon by
  PathDiagnosticPieces to describe locations for targets of branches, locations
  of events, etc.
- Did some prep. refactoring of PathDiagnosticPieces to prepare them for
  adopting the new PathDiagnosticLocation

Modified:
    cfe/trunk/include/clang/Analysis/PathDiagnostic.h
    cfe/trunk/lib/Analysis/PathDiagnostic.cpp

Modified: cfe/trunk/include/clang/Analysis/PathDiagnostic.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Analysis/PathDiagnostic.h?rev=67767&r1=67766&r2=67767&view=diff

==============================================================================
--- cfe/trunk/include/clang/Analysis/PathDiagnostic.h (original)
+++ cfe/trunk/include/clang/Analysis/PathDiagnostic.h Thu Mar 26 16:21:35 2009
@@ -14,6 +14,7 @@
 #ifndef LLVM_CLANG_PATH_DIAGNOSTIC_H
 #define LLVM_CLANG_PATH_DIAGNOSTIC_H
 
+#include "clang/Basic/SourceManager.h"
 #include "clang/Basic/Diagnostic.h"
 #include "llvm/ADT/OwningPtr.h"
 
@@ -28,8 +29,8 @@
 // High-level interface for handlers of path-sensitive diagnostics.
 //===----------------------------------------------------------------------===//
 
-  
 class PathDiagnostic;
+class Stmt;
   
 class PathDiagnosticClient : public DiagnosticClient  {
 public:
@@ -47,6 +48,27 @@
 //===----------------------------------------------------------------------===//
   
 class PathDiagnosticPiece;
+  
+class PathDiagnosticLocation {
+private:
+  enum Kind { Range, SingleLoc, Statement } K;
+  SourceRange R;
+  const Stmt *S;
+  const SourceManager &SM;
+public:
+  PathDiagnosticLocation(FullSourceLoc L)
+    : K(SingleLoc), R(L, L), S(0), SM(L.getManager()) {}
+  
+  PathDiagnosticLocation(const Stmt *s, const SourceManager &sm)
+    : K(Statement), S(s), SM(sm) {}
+  
+  PathDiagnosticLocation(SourceRange r, const SourceManager &sm)
+    : K(Range), R(r), S(0), SM(sm) {}
+    
+  FullSourceLoc asLocation() const;
+  SourceRange asRange() const;
+  const Stmt *asStmt() const;
+};
 
 class PathDiagnostic {
   std::list<PathDiagnosticPiece*> path;
@@ -68,7 +90,7 @@
   
   const std::string& getDescription() const { return Desc; }
   const std::string& getBugType() const { return BugType; }
-  const std::string& getCategory() const { return Category; }
+  const std::string& getCategory() const { return Category; }  
   
   typedef std::list<std::string>::const_iterator meta_iterator;
   meta_iterator meta_begin() const { return OtherDesc.begin(); }
@@ -180,7 +202,6 @@
   enum DisplayHint { Above, Below };
 
 private:
-  const FullSourceLoc Pos;
   const std::string str;
   std::vector<CodeModificationHint> CodeModificationHints;
   const Kind kind;
@@ -193,13 +214,11 @@
   PathDiagnosticPiece& operator=(const PathDiagnosticPiece &P);
 
 protected:
-  PathDiagnosticPiece(FullSourceLoc pos, const std::string& s,
-                      Kind k, DisplayHint hint = Below);
+  PathDiagnosticPiece(const std::string& s, Kind k, DisplayHint hint = Below);
   
-  PathDiagnosticPiece(FullSourceLoc pos, const char* s,
-                      Kind k, DisplayHint hint = Below);
+  PathDiagnosticPiece(const char* s, Kind k, DisplayHint hint = Below);
 
-  PathDiagnosticPiece(FullSourceLoc pos, Kind k, DisplayHint hint = Below);
+  PathDiagnosticPiece(Kind k, DisplayHint hint = Below);
   
 public:
   virtual ~PathDiagnosticPiece();
@@ -210,6 +229,8 @@
   ///  be displayed by the PathDiagnosticClient.
   DisplayHint getDisplayHint() const { return Hint; }
   
+  virtual FullSourceLoc getLocation() const = 0;
+  
   Kind getKind() const { return kind; }
   
   void addRange(SourceRange R) { ranges.push_back(R); }
@@ -243,24 +264,33 @@
                    : &CodeModificationHints[0] + CodeModificationHints.size();
   }
 
-  const SourceManager& getSourceManager() const {
-    return Pos.getManager();
-  }
-    
-  FullSourceLoc getLocation() const { return Pos; }
-  
   static inline bool classof(const PathDiagnosticPiece* P) {
     return true;
   }
 };
   
-class PathDiagnosticEventPiece : public PathDiagnosticPiece {
+class PathDiagnosticSpotPiece : public PathDiagnosticPiece {
+private:
+  FullSourceLoc Pos;
+public:
+  PathDiagnosticSpotPiece(FullSourceLoc pos, const std::string& s,
+                          PathDiagnosticPiece::Kind k)
+  : PathDiagnosticPiece(s, k), Pos(pos) {
+    assert(Pos.isValid() &&
+           "PathDiagnosticSpotPiece's must have a valid location.");
+  }  
+
+  FullSourceLoc getLocation() const { return Pos; }
+};
+  
+class PathDiagnosticEventPiece : public PathDiagnosticSpotPiece {
+
 public:
   PathDiagnosticEventPiece(FullSourceLoc pos, const std::string& s)
-  : PathDiagnosticPiece(pos, s, Event) {}
+    : PathDiagnosticSpotPiece(pos, s, Event) {}
   
   PathDiagnosticEventPiece(FullSourceLoc pos, const char* s)
-  : PathDiagnosticPiece(pos, s, Event) {}
+    : PathDiagnosticSpotPiece(pos, s, Event) {}
   
   ~PathDiagnosticEventPiece();
 
@@ -270,34 +300,40 @@
 };
   
 class PathDiagnosticControlFlowPiece : public PathDiagnosticPiece {
-  const SourceLocation EndPos;
+  FullSourceLoc StartPos;
+  SourceLocation EndPos;
 public:
   PathDiagnosticControlFlowPiece(FullSourceLoc startPos, SourceLocation endPos,
                                  const std::string& s)
-    : PathDiagnosticPiece(startPos, s, ControlFlow), EndPos(endPos) {}
+    : PathDiagnosticPiece(s, ControlFlow), StartPos(startPos), EndPos(endPos) {}
   
   PathDiagnosticControlFlowPiece(FullSourceLoc startPos, SourceLocation endPos,
                                  const char* s)
-    : PathDiagnosticPiece(startPos, s, ControlFlow), EndPos(endPos) {}
+    : PathDiagnosticPiece(s, ControlFlow), StartPos(startPos), EndPos(endPos) {}
   
   PathDiagnosticControlFlowPiece(FullSourceLoc startPos, SourceLocation endPos)
-    : PathDiagnosticPiece(startPos, ControlFlow), EndPos(endPos) {}  
+    : PathDiagnosticPiece(ControlFlow), StartPos(startPos), EndPos(endPos) {}  
   
   ~PathDiagnosticControlFlowPiece();
   
-  SourceLocation getStartLocation() const { return getLocation(); }
-  SourceLocation getEndLocation() const { return EndPos; }  
+  FullSourceLoc getStartLocation() const { return StartPos; }
+  FullSourceLoc getEndLocation() const {
+      return FullSourceLoc(EndPos,
+                           const_cast<SourceManager&>(StartPos.getManager()));
+  }
+  
+  virtual FullSourceLoc getLocation() const { return StartPos; }
   
   static inline bool classof(const PathDiagnosticPiece* P) {
     return P->getKind() == ControlFlow;
   }
 };
   
-class PathDiagnosticMacroPiece : public PathDiagnosticPiece {
-  std::vector<PathDiagnosticPiece*> SubPieces;  
+class PathDiagnosticMacroPiece : public PathDiagnosticSpotPiece {
+  std::vector<PathDiagnosticPiece*> SubPieces;
 public:
   PathDiagnosticMacroPiece(FullSourceLoc pos)
-    : PathDiagnosticPiece(pos, "", Macro) {}
+    : PathDiagnosticSpotPiece(pos, "", Macro) {}
   
   ~PathDiagnosticMacroPiece();
   

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

==============================================================================
--- cfe/trunk/lib/Analysis/PathDiagnostic.cpp (original)
+++ cfe/trunk/lib/Analysis/PathDiagnostic.cpp Thu Mar 26 16:21:35 2009
@@ -49,28 +49,16 @@
   return s.empty () ? 0 : GetNumCharsToLastNonPeriod(&s[0]);
 }
 
-PathDiagnosticPiece::PathDiagnosticPiece(FullSourceLoc pos,
-                                         const std::string& s,
+PathDiagnosticPiece::PathDiagnosticPiece(const std::string& s,
                                          Kind k, DisplayHint hint)
-  : Pos(pos), str(s, 0, GetNumCharsToLastNonPeriod(s)), kind(k), Hint(hint) {
-    assert(Pos.isValid() &&
-           "PathDiagnosticPiece's must have a valid location.");
-}
+  : str(s, 0, GetNumCharsToLastNonPeriod(s)), kind(k), Hint(hint) {}
 
-PathDiagnosticPiece::PathDiagnosticPiece(FullSourceLoc pos,
-                                         const char* s, Kind k,
+PathDiagnosticPiece::PathDiagnosticPiece(const char* s, Kind k,
                                          DisplayHint hint)
-  : Pos(pos), str(s, GetNumCharsToLastNonPeriod(s)), kind(k), Hint(hint) {
-  assert(Pos.isValid() &&
-         "PathDiagnosticPiece's must have a valid location.");
-}
+  : str(s, GetNumCharsToLastNonPeriod(s)), kind(k), Hint(hint) {}
 
-PathDiagnosticPiece::PathDiagnosticPiece(FullSourceLoc pos, Kind k,
-                                         DisplayHint hint)
-  : Pos(pos), kind(k), Hint(hint) {
-  assert(Pos.isValid() &&
-         "PathDiagnosticPiece's must have a valid location.");
-}  
+PathDiagnosticPiece::PathDiagnosticPiece(Kind k, DisplayHint hint)
+  : kind(k), Hint(hint) {}
 
 PathDiagnosticPiece::~PathDiagnosticPiece() {}
 PathDiagnosticEventPiece::~PathDiagnosticEventPiece() {}





More information about the cfe-commits mailing list