switch the .ll parser to use SourceMgr.
[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 SMLoc, SMDiagnostic and SourceMgr classes.  This
11 // provides a simple substrate for diagnostics, #include handling, and other low
12 // level things for 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   class SMDiagnostic;
27   class raw_ostream;
28   
29 class SMLoc {
30   const char *Ptr;
31 public:
32   SMLoc() : Ptr(0) {}
33   SMLoc(const SMLoc &RHS) : Ptr(RHS.Ptr) {}
34   
35   bool isValid() const { return Ptr != 0; }
36
37   bool operator==(const SMLoc &RHS) const { return RHS.Ptr == Ptr; }
38   bool operator!=(const SMLoc &RHS) const { return RHS.Ptr != Ptr; }
39
40   const char *getPointer() const { return Ptr; }
41   
42   static SMLoc getFromPointer(const char *Ptr) {
43     SMLoc L;
44     L.Ptr = Ptr;
45     return L;
46   }
47 };
48
49 /// SourceMgr - This owns the files read by a parser, handles include stacks,
50 /// and handles diagnostic wrangling.
51 class SourceMgr {
52   struct SrcBuffer {
53     /// Buffer - The memory buffer for the file.
54     MemoryBuffer *Buffer;
55     
56     /// IncludeLoc - This is the location of the parent include, or null if at
57     /// the top level.
58     SMLoc IncludeLoc;
59   };
60   
61   /// Buffers - This is all of the buffers that we are reading from.
62   std::vector<SrcBuffer> Buffers;
63   
64   // IncludeDirectories - This is the list of directories we should search for
65   // include files in.
66   std::vector<std::string> IncludeDirectories;
67   
68   SourceMgr(const SourceMgr&);    // DO NOT IMPLEMENT
69   void operator=(const SourceMgr&); // DO NOT IMPLEMENT
70 public:
71   SourceMgr() {}
72   ~SourceMgr();
73   
74   void setIncludeDirs(const std::vector<std::string> &Dirs) {
75     IncludeDirectories = Dirs;
76   }
77   
78   const SrcBuffer &getBufferInfo(unsigned i) const {
79     assert(i < Buffers.size() && "Invalid Buffer ID!");
80     return Buffers[i];
81   }
82
83   const MemoryBuffer *getMemoryBuffer(unsigned i) const {
84     assert(i < Buffers.size() && "Invalid Buffer ID!");
85     return Buffers[i].Buffer;
86   }
87   
88   SMLoc getParentIncludeLoc(unsigned i) const {
89     assert(i < Buffers.size() && "Invalid Buffer ID!");
90     return Buffers[i].IncludeLoc;
91   }
92   
93   unsigned AddNewSourceBuffer(MemoryBuffer *F, SMLoc IncludeLoc) {
94     SrcBuffer NB;
95     NB.Buffer = F;
96     NB.IncludeLoc = IncludeLoc;
97     Buffers.push_back(NB);
98     return Buffers.size()-1;
99   }
100   
101   /// AddIncludeFile - Search for a file with the specified name in the current
102   /// directory or in one of the IncludeDirs.  If no file is found, this returns
103   /// ~0, otherwise it returns the buffer ID of the stacked file.
104   unsigned AddIncludeFile(const std::string &Filename, SMLoc IncludeLoc);
105   
106   /// FindBufferContainingLoc - Return the ID of the buffer containing the
107   /// specified location, returning -1 if not found.
108   int FindBufferContainingLoc(SMLoc Loc) const;
109   
110   /// FindLineNumber - Find the line number for the specified location in the
111   /// specified file.  This is not a fast method.
112   unsigned FindLineNumber(SMLoc Loc, int BufferID = -1) const;
113   
114   /// PrintMessage - Emit a message about the specified location with the
115   /// specified string.
116   ///
117   /// @param Type - If non-null, the kind of message (e.g., "error") which is
118   /// prefixed to the message.
119   void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const;
120   
121   
122   /// GetMessage - Return an SMDiagnostic at the specified location with the
123   /// specified string.
124   ///
125   /// @param Type - If non-null, the kind of message (e.g., "error") which is
126   /// prefixed to the message.
127   SMDiagnostic GetMessage(SMLoc Loc,
128                           const std::string &Msg, const char *Type) const;
129   
130   
131 private:
132   void PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const;
133 };
134
135   
136 /// SMDiagnostic - Instances of this class encapsulate one diagnostic report,
137 /// allowing printing to a raw_ostream as a caret diagnostic.
138 class SMDiagnostic {
139   std::string Filename;
140   int LineNo, ColumnNo;
141   std::string Message, LineContents;
142 public:
143   SMDiagnostic() : LineNo(0), ColumnNo(0) {}
144   SMDiagnostic(const std::string &FN, int Line, int Col,
145                const std::string &Msg, const std::string &LineStr)
146     : Filename(FN), LineNo(Line), ColumnNo(Col), Message(Msg),
147       LineContents(LineStr) {}
148   SMDiagnostic(const SMDiagnostic &RHS) {
149     operator=(RHS);
150   }
151
152   void operator=(const SMDiagnostic &E) {
153     Filename = E.Filename;
154     LineNo = E.LineNo;
155     ColumnNo = E.ColumnNo;
156     Message = E.Message;
157     LineContents = E.LineContents;
158   }
159
160   void Print(const char *ProgName, raw_ostream &S);
161 };
162   
163 }  // end llvm namespace
164
165 #endif