rename SourceMgr::PrintError to PrintMessage.
[oota-llvm.git] / include / llvm / Support / SourceMgr.h
1 //===- SourceMgr.h - Manager for Source Buffers & Diagnostics ---*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the SourceMgr class.  This class is used as a simple
11 // substrate for diagnostics, #include handling, and other low level things for
12 // simple parsers.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef SUPPORT_SOURCEMGR_H
17 #define SUPPORT_SOURCEMGR_H
18
19 #include <string>
20 #include <vector>
21 #include <cassert>
22
23 namespace llvm {
24   class MemoryBuffer;
25   class SourceMgr;
26   
27 class SMLoc {
28   const char *Ptr;
29 public:
30   SMLoc() : Ptr(0) {}
31   SMLoc(const SMLoc &RHS) : Ptr(RHS.Ptr) {}
32   
33   bool operator==(const SMLoc &RHS) const { return RHS.Ptr == Ptr; }
34   bool operator!=(const SMLoc &RHS) const { return RHS.Ptr != Ptr; }
35
36   const char *getPointer() const { return Ptr; }
37   
38   static SMLoc getFromPointer(const char *Ptr) {
39     SMLoc L;
40     L.Ptr = Ptr;
41     return L;
42   }
43 };
44
45 /// SourceMgr - This owns the files read by tblgen, handles include stacks,
46 /// and handles printing of diagnostics.
47 class SourceMgr {
48   struct SrcBuffer {
49     /// Buffer - The memory buffer for the file.
50     MemoryBuffer *Buffer;
51     
52     /// IncludeLoc - This is the location of the parent include, or null if at
53     /// the top level.
54     SMLoc IncludeLoc;
55   };
56   
57   /// Buffers - This is all of the buffers that we are reading from.
58   std::vector<SrcBuffer> Buffers;
59   
60   // IncludeDirectories - This is the list of directories we should search for
61   // include files in.
62   std::vector<std::string> IncludeDirectories;
63   
64   SourceMgr(const SourceMgr&);    // DO NOT IMPLEMENT
65   void operator=(const SourceMgr&); // DO NOT IMPLEMENT
66 public:
67   SourceMgr() {}
68   ~SourceMgr();
69   
70   void setIncludeDirs(const std::vector<std::string> &Dirs) {
71     IncludeDirectories = Dirs;
72   }
73   
74   const SrcBuffer &getBufferInfo(unsigned i) const {
75     assert(i < Buffers.size() && "Invalid Buffer ID!");
76     return Buffers[i];
77   }
78
79   const MemoryBuffer *getMemoryBuffer(unsigned i) const {
80     assert(i < Buffers.size() && "Invalid Buffer ID!");
81     return Buffers[i].Buffer;
82   }
83   
84   SMLoc getParentIncludeLoc(unsigned i) const {
85     assert(i < Buffers.size() && "Invalid Buffer ID!");
86     return Buffers[i].IncludeLoc;
87   }
88   
89   unsigned AddNewSourceBuffer(MemoryBuffer *F, SMLoc IncludeLoc) {
90     SrcBuffer NB;
91     NB.Buffer = F;
92     NB.IncludeLoc = IncludeLoc;
93     Buffers.push_back(NB);
94     return Buffers.size()-1;
95   }
96   
97   /// AddIncludeFile - Search for a file with the specified name in the current
98   /// directory or in one of the IncludeDirs.  If no file is found, this returns
99   /// ~0, otherwise it returns the buffer ID of the stacked file.
100   unsigned AddIncludeFile(const std::string &Filename, SMLoc IncludeLoc);
101   
102   /// FindBufferContainingLoc - Return the ID of the buffer containing the
103   /// specified location, returning -1 if not found.
104   int FindBufferContainingLoc(SMLoc Loc) const;
105   
106   /// FindLineNumber - Find the line number for the specified location in the
107   /// specified file.  This is not a fast method.
108   unsigned FindLineNumber(SMLoc Loc, int BufferID = -1) const;
109   
110   /// PrintMessage - Emit a message about the specified location with the
111   /// specified string.
112   void PrintMessage(SMLoc Loc, const std::string &Msg) const;
113   
114 private:
115   void PrintIncludeStack(SMLoc IncludeLoc) const;
116 };
117   
118 }  // end llvm namespace
119
120 #endif