LLVM API Documentation

SourceMgr.h

Go to the documentation of this file.
00001 //===- SourceMgr.h - Manager for Source Buffers & Diagnostics ---*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file declares the SMLoc, SMDiagnostic and SourceMgr classes.  This
00011 // provides a simple substrate for diagnostics, #include handling, and other low
00012 // level things for simple parsers.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #ifndef SUPPORT_SOURCEMGR_H
00017 #define SUPPORT_SOURCEMGR_H
00018 
00019 #include <string>
00020 #include <vector>
00021 #include <cassert>
00022 
00023 namespace llvm {
00024   class MemoryBuffer;
00025   class SourceMgr;
00026   class SMDiagnostic;
00027   class raw_ostream;
00028   
00029 class SMLoc {
00030   const char *Ptr;
00031 public:
00032   SMLoc() : Ptr(0) {}
00033   SMLoc(const SMLoc &RHS) : Ptr(RHS.Ptr) {}
00034   
00035   bool isValid() const { return Ptr != 0; }
00036 
00037   bool operator==(const SMLoc &RHS) const { return RHS.Ptr == Ptr; }
00038   bool operator!=(const SMLoc &RHS) const { return RHS.Ptr != Ptr; }
00039 
00040   const char *getPointer() const { return Ptr; }
00041   
00042   static SMLoc getFromPointer(const char *Ptr) {
00043     SMLoc L;
00044     L.Ptr = Ptr;
00045     return L;
00046   }
00047 };
00048 
00049 /// SourceMgr - This owns the files read by a parser, handles include stacks,
00050 /// and handles diagnostic wrangling.
00051 class SourceMgr {
00052   struct SrcBuffer {
00053     /// Buffer - The memory buffer for the file.
00054     MemoryBuffer *Buffer;
00055     
00056     /// IncludeLoc - This is the location of the parent include, or null if at
00057     /// the top level.
00058     SMLoc IncludeLoc;
00059   };
00060   
00061   /// Buffers - This is all of the buffers that we are reading from.
00062   std::vector<SrcBuffer> Buffers;
00063   
00064   // IncludeDirectories - This is the list of directories we should search for
00065   // include files in.
00066   std::vector<std::string> IncludeDirectories;
00067   
00068   /// LineNoCache - This is a cache for line number queries, its implementation
00069   /// is really private to SourceMgr.cpp.
00070   mutable void *LineNoCache;
00071   
00072   SourceMgr(const SourceMgr&);    // DO NOT IMPLEMENT
00073   void operator=(const SourceMgr&); // DO NOT IMPLEMENT
00074 public:
00075   SourceMgr() : LineNoCache(0) {}
00076   ~SourceMgr();
00077   
00078   void setIncludeDirs(const std::vector<std::string> &Dirs) {
00079     IncludeDirectories = Dirs;
00080   }
00081   
00082   const SrcBuffer &getBufferInfo(unsigned i) const {
00083     assert(i < Buffers.size() && "Invalid Buffer ID!");
00084     return Buffers[i];
00085   }
00086 
00087   const MemoryBuffer *getMemoryBuffer(unsigned i) const {
00088     assert(i < Buffers.size() && "Invalid Buffer ID!");
00089     return Buffers[i].Buffer;
00090   }
00091   
00092   SMLoc getParentIncludeLoc(unsigned i) const {
00093     assert(i < Buffers.size() && "Invalid Buffer ID!");
00094     return Buffers[i].IncludeLoc;
00095   }
00096   
00097   unsigned AddNewSourceBuffer(MemoryBuffer *F, SMLoc IncludeLoc) {
00098     SrcBuffer NB;
00099     NB.Buffer = F;
00100     NB.IncludeLoc = IncludeLoc;
00101     Buffers.push_back(NB);
00102     return Buffers.size()-1;
00103   }
00104   
00105   /// AddIncludeFile - Search for a file with the specified name in the current
00106   /// directory or in one of the IncludeDirs.  If no file is found, this returns
00107   /// ~0, otherwise it returns the buffer ID of the stacked file.
00108   unsigned AddIncludeFile(const std::string &Filename, SMLoc IncludeLoc);
00109   
00110   /// FindBufferContainingLoc - Return the ID of the buffer containing the
00111   /// specified location, returning -1 if not found.
00112   int FindBufferContainingLoc(SMLoc Loc) const;
00113   
00114   /// FindLineNumber - Find the line number for the specified location in the
00115   /// specified file.  This is not a fast method.
00116   unsigned FindLineNumber(SMLoc Loc, int BufferID = -1) const;
00117   
00118   /// PrintMessage - Emit a message about the specified location with the
00119   /// specified string.
00120   ///
00121   /// @param Type - If non-null, the kind of message (e.g., "error") which is
00122   /// prefixed to the message.
00123   void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const;
00124   
00125   
00126   /// GetMessage - Return an SMDiagnostic at the specified location with the
00127   /// specified string.
00128   ///
00129   /// @param Type - If non-null, the kind of message (e.g., "error") which is
00130   /// prefixed to the message.
00131   SMDiagnostic GetMessage(SMLoc Loc,
00132                           const std::string &Msg, const char *Type) const;
00133   
00134   
00135 private:
00136   void PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const;
00137 };
00138 
00139   
00140 /// SMDiagnostic - Instances of this class encapsulate one diagnostic report,
00141 /// allowing printing to a raw_ostream as a caret diagnostic.
00142 class SMDiagnostic {
00143   std::string Filename;
00144   int LineNo, ColumnNo;
00145   std::string Message, LineContents;
00146 public:
00147   SMDiagnostic() : LineNo(0), ColumnNo(0) {}
00148   SMDiagnostic(const std::string &FN, int Line, int Col,
00149                const std::string &Msg, const std::string &LineStr)
00150     : Filename(FN), LineNo(Line), ColumnNo(Col), Message(Msg),
00151       LineContents(LineStr) {}
00152 
00153   void Print(const char *ProgName, raw_ostream &S);
00154 };
00155   
00156 }  // end llvm namespace
00157 
00158 #endif