[llvm-commits] [llvm] r100503 - in /llvm/trunk: include/llvm/Support/IRReader.h include/llvm/Support/SourceMgr.h lib/AsmParser/Parser.cpp lib/Support/SourceMgr.cpp

Chris Lattner sabre at nondot.org
Mon Apr 5 17:26:48 PDT 2010


Author: lattner
Date: Mon Apr  5 19:26:48 2010
New Revision: 100503

URL: http://llvm.org/viewvc/llvm-project?rev=100503&view=rev
Log:
Give llvm::SourceMgr the ability to have a client-specified
diagnostic handler.

Modified:
    llvm/trunk/include/llvm/Support/IRReader.h
    llvm/trunk/include/llvm/Support/SourceMgr.h
    llvm/trunk/lib/AsmParser/Parser.cpp
    llvm/trunk/lib/Support/SourceMgr.cpp

Modified: llvm/trunk/include/llvm/Support/IRReader.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/IRReader.h?rev=100503&r1=100502&r2=100503&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/IRReader.h (original)
+++ llvm/trunk/include/llvm/Support/IRReader.h Mon Apr  5 19:26:48 2010
@@ -38,7 +38,8 @@
       std::string ErrMsg;
       Module *M = getLazyBitcodeModule(Buffer, Context, &ErrMsg);
       if (M == 0) {
-        Err = SMDiagnostic(Buffer->getBufferIdentifier(), -1, -1, ErrMsg, "");
+        Err = SMDiagnostic(SMLoc(), Buffer->getBufferIdentifier(), -1, -1,
+                           ErrMsg, "");
         // ParseBitcodeFile does not take ownership of the Buffer in the
         // case of an error.
         delete Buffer;
@@ -59,7 +60,7 @@
     std::string ErrMsg;
     MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrMsg);
     if (F == 0) {
-      Err = SMDiagnostic(Filename, -1, -1,
+      Err = SMDiagnostic(SMLoc(), Filename, -1, -1,
                          "Could not open input file '" + Filename + "'", "");
       return 0;
     }
@@ -81,7 +82,8 @@
       // ParseBitcodeFile does not take ownership of the Buffer.
       delete Buffer;
       if (M == 0)
-        Err = SMDiagnostic(Buffer->getBufferIdentifier(), -1, -1, ErrMsg, "");
+        Err = SMDiagnostic(SMLoc(), Buffer->getBufferIdentifier(),
+                           -1, -1, ErrMsg, "");
       return M;
     }
 
@@ -97,7 +99,7 @@
     std::string ErrMsg;
     MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrMsg);
     if (F == 0) {
-      Err = SMDiagnostic(Filename, -1, -1,
+      Err = SMDiagnostic(SMLoc(), Filename, -1, -1,
                          "Could not open input file '" + Filename + "'", "");
       return 0;
     }

Modified: llvm/trunk/include/llvm/Support/SourceMgr.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/SourceMgr.h?rev=100503&r1=100502&r2=100503&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/SourceMgr.h (original)
+++ llvm/trunk/include/llvm/Support/SourceMgr.h Mon Apr  5 19:26:48 2010
@@ -31,6 +31,12 @@
 /// SourceMgr - This owns the files read by a parser, handles include stacks,
 /// and handles diagnostic wrangling.
 class SourceMgr {
+public:
+  /// DiagHandlerTy - Clients that want to handle their own diagnostics in a
+  /// custom way can register a function pointer+context as a diagnostic
+  /// handler.  It gets called each time PrintMessage is invoked.
+  typedef void (*DiagHandlerTy)(const SMDiagnostic&, void *Context);
+private:
   struct SrcBuffer {
     /// Buffer - The memory buffer for the file.
     MemoryBuffer *Buffer;
@@ -51,16 +57,26 @@
   /// is really private to SourceMgr.cpp.
   mutable void *LineNoCache;
 
+  DiagHandlerTy DiagHandler;
+  void *DiagContext;
+  
   SourceMgr(const SourceMgr&);    // DO NOT IMPLEMENT
   void operator=(const SourceMgr&); // DO NOT IMPLEMENT
 public:
-  SourceMgr() : LineNoCache(0) {}
+  SourceMgr() : LineNoCache(0), DiagHandler(0), DiagContext(0) {}
   ~SourceMgr();
 
   void setIncludeDirs(const std::vector<std::string> &Dirs) {
     IncludeDirectories = Dirs;
   }
 
+  /// setDiagHandler - Specify a diagnostic handler to be invoked every time
+  /// PrintMessage is called.
+  void setDiagHandler(DiagHandlerTy DH, void *Ctx = 0) {
+    DiagHandler = DH;
+    DiagContext = Ctx;
+  }
+
   const SrcBuffer &getBufferInfo(unsigned i) const {
     assert(i < Buffers.size() && "Invalid Buffer ID!");
     return Buffers[i];
@@ -128,6 +144,7 @@
 /// SMDiagnostic - Instances of this class encapsulate one diagnostic report,
 /// allowing printing to a raw_ostream as a caret diagnostic.
 class SMDiagnostic {
+  SMLoc Loc;
   std::string Filename;
   int LineNo, ColumnNo;
   std::string Message, LineContents;
@@ -135,12 +152,20 @@
 
 public:
   SMDiagnostic() : LineNo(0), ColumnNo(0), ShowLine(0) {}
-  SMDiagnostic(const std::string &FN, int Line, int Col,
+  SMDiagnostic(SMLoc L, const std::string &FN, int Line, int Col,
                const std::string &Msg, const std::string &LineStr,
                bool showline = true)
-    : Filename(FN), LineNo(Line), ColumnNo(Col), Message(Msg),
+    : Loc(L), Filename(FN), LineNo(Line), ColumnNo(Col), Message(Msg),
       LineContents(LineStr), ShowLine(showline) {}
 
+  SMLoc getLoc() const { return Loc; }
+  const std::string getFilename() { return Filename; }
+  int getLineNo() const { return LineNo; }
+  int getColumnNo() const { return ColumnNo; }
+  const std::string &getMessage() const { return Message; }
+  const std::string &getLineContents() const { return LineContents; }
+  bool getShowLine() const { return ShowLine; }
+  
   void Print(const char *ProgName, raw_ostream &S) const;
 };
 

Modified: llvm/trunk/lib/AsmParser/Parser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/AsmParser/Parser.cpp?rev=100503&r1=100502&r2=100503&view=diff
==============================================================================
--- llvm/trunk/lib/AsmParser/Parser.cpp (original)
+++ llvm/trunk/lib/AsmParser/Parser.cpp Mon Apr  5 19:26:48 2010
@@ -44,7 +44,7 @@
   std::string ErrorStr;
   MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrorStr);
   if (F == 0) {
-    Err = SMDiagnostic("", -1, -1,
+    Err = SMDiagnostic(SMLoc(), "", -1, -1,
                        "Could not open input file '" + Filename + "': " +
                        ErrorStr, "");
     return 0;

Modified: llvm/trunk/lib/Support/SourceMgr.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/SourceMgr.cpp?rev=100503&r1=100502&r2=100503&view=diff
==============================================================================
--- llvm/trunk/lib/Support/SourceMgr.cpp (original)
+++ llvm/trunk/lib/Support/SourceMgr.cpp Mon Apr  5 19:26:48 2010
@@ -168,13 +168,20 @@
   }
   PrintedMsg += Msg;
 
-  return SMDiagnostic(CurMB->getBufferIdentifier(), FindLineNumber(Loc, CurBuf),
+  return SMDiagnostic(Loc,
+                      CurMB->getBufferIdentifier(), FindLineNumber(Loc, CurBuf),
                       Loc.getPointer()-LineStart, PrintedMsg,
                       LineStr, ShowLine);
 }
 
 void SourceMgr::PrintMessage(SMLoc Loc, const std::string &Msg,
                              const char *Type, bool ShowLine) const {
+  // Report the message with the diagnostic handler if present.
+  if (DiagHandler) {
+    DiagHandler(GetMessage(Loc, Msg, Type, ShowLine), DiagContext);
+    return;
+  }
+  
   raw_ostream &OS = errs();
 
   int CurBuf = FindBufferContainingLoc(Loc);





More information about the llvm-commits mailing list