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

Ted Kremenek kremenek at apple.com
Wed Mar 26 23:16:40 PDT 2008


Author: kremenek
Date: Thu Mar 27 01:16:40 2008
New Revision: 48864

URL: http://llvm.org/viewvc/llvm-project?rev=48864&view=rev
Log:
PathDiagnosticPiece no longer contains a vector of strings; just one string.
PathDiagnostic no longer contains a diagnostic ID or diagnostic level.

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=48864&r1=48863&r2=48864&view=diff

==============================================================================
--- cfe/trunk/include/clang/Analysis/PathDiagnostic.h (original)
+++ cfe/trunk/include/clang/Analysis/PathDiagnostic.h Thu Mar 27 01:16:40 2008
@@ -20,29 +20,21 @@
 #include <vector>
 #include <list>
 #include <string>
+#include <algorithm>
 
 namespace clang {
 
 class PathDiagnosticPiece {
   FullSourceLoc Pos;
-  std::vector<std::string> strs;
+  std::string str;
   std::vector<SourceRange> ranges;
 public:
   
-  PathDiagnosticPiece(FullSourceLoc pos) : Pos(pos) {}
-  
-  void addString(const std::string& s) {
-    strs.push_back(s);
-  }
-  
-  const std::string* strs_begin() const {
-    return strs.empty() ? NULL : &strs[0];
-  }
-
-  const std::string* strs_end() const {
-    return strs_begin() + strs.size();
-  }
+  PathDiagnosticPiece(FullSourceLoc pos, const std::string& s)
+    : Pos(pos), str(s) {}
   
+  const std::string& getString() const { return str; }
+    
   void addRange(SourceRange R) {
     ranges.push_back(R);
   }
@@ -68,14 +60,11 @@
   
 class PathDiagnostic {
   std::list<PathDiagnosticPiece*> path;
-  Diagnostic::Level DiagLevel;
-  diag::kind ID;
   unsigned Size;
 
 public:
   
-  PathDiagnostic(Diagnostic::Level lvl, diag::kind i)
-    : DiagLevel(lvl), ID(i), Size(0) {}
+  PathDiagnostic() : Size(0) {}
   
   ~PathDiagnostic();
 
@@ -89,10 +78,19 @@
     ++Size;
   }
   
+  unsigned size() const { return Size; }
+  bool empty() const { return Size == 0; }
+  
   class iterator {
   public:  
     typedef std::list<PathDiagnosticPiece*>::iterator ImplTy;
     
+    typedef PathDiagnosticPiece              value_type;
+    typedef value_type&                      reference;
+    typedef value_type*                      pointer;
+    typedef ptrdiff_t                        difference_type;
+    typedef std::bidirectional_iterator_tag  iterator_category;
+    
   private:
     ImplTy I;
     
@@ -113,6 +111,12 @@
   public:  
     typedef std::list<PathDiagnosticPiece*>::const_iterator ImplTy;
     
+    typedef const PathDiagnosticPiece        value_type;
+    typedef value_type&                      reference;
+    typedef value_type*                      pointer;
+    typedef ptrdiff_t                        difference_type;
+    typedef std::bidirectional_iterator_tag  iterator_category;
+    
   private:
     ImplTy I;
 
@@ -122,24 +126,30 @@
     bool operator==(const const_iterator& X) const { return I == X.I; }
     bool operator!=(const const_iterator& X) const { return I != X.I; }
     
-    const PathDiagnosticPiece& operator*() const { return **I; }
-    const PathDiagnosticPiece* operator->() const { return *I; }
+    reference operator*() const { return **I; }
+    pointer operator->() const { return *I; }
     
     const_iterator& operator++() { ++I; return *this; }
     const_iterator& operator--() { --I; return *this; }
   };
-  
+
+  typedef std::reverse_iterator<iterator>       reverse_iterator;
+  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;  
+
+
+  // forward iterator creation methods.
+
   iterator begin() { return path.begin(); }
   iterator end() { return path.end(); }
-
+  
   const_iterator begin() const { return path.begin(); }
   const_iterator end() const { return path.end(); }
   
-  unsigned size() const { return Size; }
-  bool empty() const { return Size == 0; }
-  
-  Diagnostic::Level getLevel() const { return DiagLevel; }
-  diag::kind getDiagKind() const { return ID; }
+  // reverse iterator creation methods.
+  reverse_iterator rbegin()            { return reverse_iterator(end()); }
+  const_reverse_iterator rbegin() const{ return const_reverse_iterator(end()); }
+  reverse_iterator rend()              { return reverse_iterator(begin()); }
+  const_reverse_iterator rend() const { return const_reverse_iterator(begin());}
 };
   
 class PathDiagnosticClient : public DiagnosticClient  {
@@ -156,8 +166,7 @@
                                 const SourceRange *Ranges, 
                                 unsigned NumRanges);
     
-  virtual void HandlePathDiagnostic(Diagnostic& Diag,
-                                    const PathDiagnostic& D) = 0;
+  virtual void HandlePathDiagnostic(const PathDiagnostic& D) = 0;
 };
 
 } //end clang namespace

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

==============================================================================
--- cfe/trunk/lib/Analysis/PathDiagnostic.cpp (original)
+++ cfe/trunk/lib/Analysis/PathDiagnostic.cpp Thu Mar 27 01:16:40 2008
@@ -12,6 +12,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "clang/Analysis/PathDiagnostic.h"
+#include <sstream>
 
 using namespace clang;
   
@@ -30,16 +31,37 @@
   
   // Create a PathDiagnostic with a single piece.
   
-  PathDiagnostic D(DiagLevel, ID);
+  PathDiagnostic D;
   
-  PathDiagnosticPiece* P = new PathDiagnosticPiece(Pos);
+  // Ripped from TextDiagnostics::FormatDiagnostic.  Perhaps we should
+  // centralize it somewhere?
   
-  while (NumStrs) {
-    P->addString(*Strs);
-    --NumStrs;
-    ++Strs;
+  std::ostringstream os;
+  
+  switch (DiagLevel) {
+    default: assert(0 && "Unknown diagnostic type!");
+    case Diagnostic::Note:    os << "note: "; break;
+    case Diagnostic::Warning: os << "warning: "; break;
+    case Diagnostic::Error:   os << "error: "; break;
+    case Diagnostic::Fatal:   os << "fatal error: "; break;
+      break;
+  }
+  
+  std::string Msg = Diags.getDescription(ID);
+
+  for (unsigned i = 0; i < Msg.size() - 1; ++i) {
+    if (Msg[i] == '%' && isdigit(Msg[i + 1])) {
+      unsigned StrNo = Msg[i + 1] - '0';
+      Msg = std::string(Msg.begin(), Msg.begin() + i) +
+      (StrNo < NumStrs ? Strs[StrNo] : "<<<INTERNAL ERROR>>>") +
+      std::string(Msg.begin() + i + 2, Msg.end());
+    }
   }
   
+  os << Msg;
+  
+  PathDiagnosticPiece* P = new PathDiagnosticPiece(Pos, os.str());
+  
   while (NumRanges) {
     P->addRange(*Ranges);
     --NumRanges;
@@ -48,5 +70,5 @@
   
   D.push_front(P);
 
-  HandlePathDiagnostic(Diags, D);  
+  HandlePathDiagnostic(D);  
 }





More information about the cfe-commits mailing list