Give llvm::SourceMgr the ability to have a client-specified
[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 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 "llvm/Support/SMLoc.h"
20
21 #include <string>
22 #include <vector>
23 #include <cassert>
24
25 namespace llvm {
26   class MemoryBuffer;
27   class SourceMgr;
28   class SMDiagnostic;
29   class raw_ostream;
30
31 /// SourceMgr - This owns the files read by a parser, handles include stacks,
32 /// and handles diagnostic wrangling.
33 class SourceMgr {
34 public:
35   /// DiagHandlerTy - Clients that want to handle their own diagnostics in a
36   /// custom way can register a function pointer+context as a diagnostic
37   /// handler.  It gets called each time PrintMessage is invoked.
38   typedef void (*DiagHandlerTy)(const SMDiagnostic&, void *Context);
39 private:
40   struct SrcBuffer {
41     /// Buffer - The memory buffer for the file.
42     MemoryBuffer *Buffer;
43
44     /// IncludeLoc - This is the location of the parent include, or null if at
45     /// the top level.
46     SMLoc IncludeLoc;
47   };
48
49   /// Buffers - This is all of the buffers that we are reading from.
50   std::vector<SrcBuffer> Buffers;
51
52   // IncludeDirectories - This is the list of directories we should search for
53   // include files in.
54   std::vector<std::string> IncludeDirectories;
55
56   /// LineNoCache - This is a cache for line number queries, its implementation
57   /// is really private to SourceMgr.cpp.
58   mutable void *LineNoCache;
59
60   DiagHandlerTy DiagHandler;
61   void *DiagContext;
62   
63   SourceMgr(const SourceMgr&);    // DO NOT IMPLEMENT
64   void operator=(const SourceMgr&); // DO NOT IMPLEMENT
65 public:
66   SourceMgr() : LineNoCache(0), DiagHandler(0), DiagContext(0) {}
67   ~SourceMgr();
68
69   void setIncludeDirs(const std::vector<std::string> &Dirs) {
70     IncludeDirectories = Dirs;
71   }
72
73   /// setDiagHandler - Specify a diagnostic handler to be invoked every time
74   /// PrintMessage is called.
75   void setDiagHandler(DiagHandlerTy DH, void *Ctx = 0) {
76     DiagHandler = DH;
77     DiagContext = Ctx;
78   }
79
80   const SrcBuffer &getBufferInfo(unsigned i) const {
81     assert(i < Buffers.size() && "Invalid Buffer ID!");
82     return Buffers[i];
83   }
84
85   const MemoryBuffer *getMemoryBuffer(unsigned i) const {
86     assert(i < Buffers.size() && "Invalid Buffer ID!");
87     return Buffers[i].Buffer;
88   }
89
90   SMLoc getParentIncludeLoc(unsigned i) const {
91     assert(i < Buffers.size() && "Invalid Buffer ID!");
92     return Buffers[i].IncludeLoc;
93   }
94
95   /// AddNewSourceBuffer - Add a new source buffer to this source manager.  This
96   /// takes ownership of the memory buffer.
97   unsigned AddNewSourceBuffer(MemoryBuffer *F, SMLoc IncludeLoc) {
98     SrcBuffer NB;
99     NB.Buffer = F;
100     NB.IncludeLoc = IncludeLoc;
101     Buffers.push_back(NB);
102     return Buffers.size()-1;
103   }
104
105   /// AddIncludeFile - Search for a file with the specified name in the current
106   /// directory or in one of the IncludeDirs.  If no file is found, this returns
107   /// ~0, otherwise it returns the buffer ID of the stacked file.
108   unsigned AddIncludeFile(const std::string &Filename, SMLoc IncludeLoc);
109
110   /// FindBufferContainingLoc - Return the ID of the buffer containing the
111   /// specified location, returning -1 if not found.
112   int FindBufferContainingLoc(SMLoc Loc) const;
113
114   /// FindLineNumber - Find the line number for the specified location in the
115   /// specified file.  This is not a fast method.
116   unsigned FindLineNumber(SMLoc Loc, int BufferID = -1) const;
117
118   /// PrintMessage - Emit a message about the specified location with the
119   /// specified string.
120   ///
121   /// @param Type - If non-null, the kind of message (e.g., "error") which is
122   /// prefixed to the message.
123   /// @param ShowLine - Should the diagnostic show the source line.
124   void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type,
125                     bool ShowLine = true) const;
126
127
128   /// GetMessage - Return an SMDiagnostic at the specified location with the
129   /// specified string.
130   ///
131   /// @param Type - If non-null, the kind of message (e.g., "error") which is
132   /// prefixed to the message.
133   /// @param ShowLine - Should the diagnostic show the source line.
134   SMDiagnostic GetMessage(SMLoc Loc,
135                           const std::string &Msg, const char *Type,
136                           bool ShowLine = true) const;
137
138
139 private:
140   void PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const;
141 };
142
143
144 /// SMDiagnostic - Instances of this class encapsulate one diagnostic report,
145 /// allowing printing to a raw_ostream as a caret diagnostic.
146 class SMDiagnostic {
147   SMLoc Loc;
148   std::string Filename;
149   int LineNo, ColumnNo;
150   std::string Message, LineContents;
151   unsigned ShowLine : 1;
152
153 public:
154   SMDiagnostic() : LineNo(0), ColumnNo(0), ShowLine(0) {}
155   SMDiagnostic(SMLoc L, const std::string &FN, int Line, int Col,
156                const std::string &Msg, const std::string &LineStr,
157                bool showline = true)
158     : Loc(L), Filename(FN), LineNo(Line), ColumnNo(Col), Message(Msg),
159       LineContents(LineStr), ShowLine(showline) {}
160
161   SMLoc getLoc() const { return Loc; }
162   const std::string getFilename() { return Filename; }
163   int getLineNo() const { return LineNo; }
164   int getColumnNo() const { return ColumnNo; }
165   const std::string &getMessage() const { return Message; }
166   const std::string &getLineContents() const { return LineContents; }
167   bool getShowLine() const { return ShowLine; }
168   
169   void Print(const char *ProgName, raw_ostream &S) const;
170 };
171
172 }  // end llvm namespace
173
174 #endif