Add SMLoc::isValid method.
[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 isValid() const { return Ptr != 0; }
34
35   bool operator==(const SMLoc &RHS) const { return RHS.Ptr == Ptr; }
36   bool operator!=(const SMLoc &RHS) const { return RHS.Ptr != Ptr; }
37
38   const char *getPointer() const { return Ptr; }
39   
40   static SMLoc getFromPointer(const char *Ptr) {
41     SMLoc L;
42     L.Ptr = Ptr;
43     return L;
44   }
45 };
46
47 /// SourceMgr - This owns the files read by tblgen, handles include stacks,
48 /// and handles printing of diagnostics.
49 class SourceMgr {
50   struct SrcBuffer {
51     /// Buffer - The memory buffer for the file.
52     MemoryBuffer *Buffer;
53     
54     /// IncludeLoc - This is the location of the parent include, or null if at
55     /// the top level.
56     SMLoc IncludeLoc;
57   };
58   
59   /// Buffers - This is all of the buffers that we are reading from.
60   std::vector<SrcBuffer> Buffers;
61   
62   // IncludeDirectories - This is the list of directories we should search for
63   // include files in.
64   std::vector<std::string> IncludeDirectories;
65   
66   SourceMgr(const SourceMgr&);    // DO NOT IMPLEMENT
67   void operator=(const SourceMgr&); // DO NOT IMPLEMENT
68 public:
69   SourceMgr() {}
70   ~SourceMgr();
71   
72   void setIncludeDirs(const std::vector<std::string> &Dirs) {
73     IncludeDirectories = Dirs;
74   }
75   
76   const SrcBuffer &getBufferInfo(unsigned i) const {
77     assert(i < Buffers.size() && "Invalid Buffer ID!");
78     return Buffers[i];
79   }
80
81   const MemoryBuffer *getMemoryBuffer(unsigned i) const {
82     assert(i < Buffers.size() && "Invalid Buffer ID!");
83     return Buffers[i].Buffer;
84   }
85   
86   SMLoc getParentIncludeLoc(unsigned i) const {
87     assert(i < Buffers.size() && "Invalid Buffer ID!");
88     return Buffers[i].IncludeLoc;
89   }
90   
91   unsigned AddNewSourceBuffer(MemoryBuffer *F, SMLoc IncludeLoc) {
92     SrcBuffer NB;
93     NB.Buffer = F;
94     NB.IncludeLoc = IncludeLoc;
95     Buffers.push_back(NB);
96     return Buffers.size()-1;
97   }
98   
99   /// AddIncludeFile - Search for a file with the specified name in the current
100   /// directory or in one of the IncludeDirs.  If no file is found, this returns
101   /// ~0, otherwise it returns the buffer ID of the stacked file.
102   unsigned AddIncludeFile(const std::string &Filename, SMLoc IncludeLoc);
103   
104   /// FindBufferContainingLoc - Return the ID of the buffer containing the
105   /// specified location, returning -1 if not found.
106   int FindBufferContainingLoc(SMLoc Loc) const;
107   
108   /// FindLineNumber - Find the line number for the specified location in the
109   /// specified file.  This is not a fast method.
110   unsigned FindLineNumber(SMLoc Loc, int BufferID = -1) const;
111   
112   /// PrintMessage - Emit a message about the specified location with the
113   /// specified string.
114   void PrintMessage(SMLoc Loc, const std::string &Msg) const;
115   
116 private:
117   void PrintIncludeStack(SMLoc IncludeLoc) const;
118 };
119   
120 }  // end llvm namespace
121
122 #endif