[C++11] Make use of 'nullptr' in the Support library.
[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 LLVM_SUPPORT_SOURCEMGR_H
17 #define LLVM_SUPPORT_SOURCEMGR_H
18
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/Twine.h"
22 #include "llvm/Support/SMLoc.h"
23 #include <string>
24
25 namespace llvm {
26   class MemoryBuffer;
27   class SourceMgr;
28   class SMDiagnostic;
29   class SMFixIt;
30   class Twine;
31   class raw_ostream;
32
33 /// SourceMgr - This owns the files read by a parser, handles include stacks,
34 /// and handles diagnostic wrangling.
35 class SourceMgr {
36 public:
37   enum DiagKind {
38     DK_Error,
39     DK_Warning,
40     DK_Note
41   };
42
43   /// DiagHandlerTy - Clients that want to handle their own diagnostics in a
44   /// custom way can register a function pointer+context as a diagnostic
45   /// handler.  It gets called each time PrintMessage is invoked.
46   typedef void (*DiagHandlerTy)(const SMDiagnostic &, void *Context);
47 private:
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   /// LineNoCache - This is a cache for line number queries, its implementation
65   /// is really private to SourceMgr.cpp.
66   mutable void *LineNoCache;
67
68   DiagHandlerTy DiagHandler;
69   void *DiagContext;
70
71   SourceMgr(const SourceMgr&) LLVM_DELETED_FUNCTION;
72   void operator=(const SourceMgr&) LLVM_DELETED_FUNCTION;
73 public:
74   SourceMgr()
75     : LineNoCache(nullptr), DiagHandler(nullptr), DiagContext(nullptr) {}
76   ~SourceMgr();
77
78   void setIncludeDirs(const std::vector<std::string> &Dirs) {
79     IncludeDirectories = Dirs;
80   }
81
82   /// setDiagHandler - Specify a diagnostic handler to be invoked every time
83   /// PrintMessage is called. Ctx is passed into the handler when it is invoked.
84   void setDiagHandler(DiagHandlerTy DH, void *Ctx = nullptr) {
85     DiagHandler = DH;
86     DiagContext = Ctx;
87   }
88
89   DiagHandlerTy getDiagHandler() const { return DiagHandler; }
90   void *getDiagContext() const { return DiagContext; }
91
92   const SrcBuffer &getBufferInfo(unsigned i) const {
93     assert(i < Buffers.size() && "Invalid Buffer ID!");
94     return Buffers[i];
95   }
96
97   const MemoryBuffer *getMemoryBuffer(unsigned i) const {
98     assert(i < Buffers.size() && "Invalid Buffer ID!");
99     return Buffers[i].Buffer;
100   }
101
102   size_t getNumBuffers() const {
103     return Buffers.size();
104   }
105
106   SMLoc getParentIncludeLoc(unsigned i) const {
107     assert(i < Buffers.size() && "Invalid Buffer ID!");
108     return Buffers[i].IncludeLoc;
109   }
110
111   /// AddNewSourceBuffer - Add a new source buffer to this source manager.  This
112   /// takes ownership of the memory buffer.
113   size_t AddNewSourceBuffer(MemoryBuffer *F, SMLoc IncludeLoc) {
114     SrcBuffer NB;
115     NB.Buffer = F;
116     NB.IncludeLoc = IncludeLoc;
117     Buffers.push_back(NB);
118     return Buffers.size() - 1;
119   }
120
121   /// AddIncludeFile - Search for a file with the specified name in the current
122   /// directory or in one of the IncludeDirs.  If no file is found, this returns
123   /// ~0, otherwise it returns the buffer ID of the stacked file.
124   /// The full path to the included file can be found in IncludedFile.
125   size_t AddIncludeFile(const std::string &Filename, SMLoc IncludeLoc,
126                         std::string &IncludedFile);
127
128   /// FindBufferContainingLoc - Return the ID of the buffer containing the
129   /// specified location, returning -1 if not found.
130   int FindBufferContainingLoc(SMLoc Loc) const;
131
132   /// FindLineNumber - Find the line number for the specified location in the
133   /// specified file.  This is not a fast method.
134   unsigned FindLineNumber(SMLoc Loc, int BufferID = -1) const {
135     return getLineAndColumn(Loc, BufferID).first;
136   }
137
138   /// getLineAndColumn - Find the line and column number for the specified
139   /// location in the specified file.  This is not a fast method.
140   std::pair<unsigned, unsigned>
141     getLineAndColumn(SMLoc Loc, int BufferID = -1) const;
142
143   /// PrintMessage - Emit a message about the specified location with the
144   /// specified string.
145   ///
146   /// @param ShowColors - Display colored messages if output is a terminal and
147   /// the default error handler is used.
148   void PrintMessage(raw_ostream &OS, SMLoc Loc, DiagKind Kind,
149                     const Twine &Msg,
150                     ArrayRef<SMRange> Ranges = None,
151                     ArrayRef<SMFixIt> FixIts = None,
152                     bool ShowColors = true) const;
153
154   /// Emits a diagnostic to llvm::errs().
155   void PrintMessage(SMLoc Loc, DiagKind Kind, const Twine &Msg,
156                     ArrayRef<SMRange> Ranges = None,
157                     ArrayRef<SMFixIt> FixIts = None,
158                     bool ShowColors = true) const;
159
160   /// GetMessage - Return an SMDiagnostic at the specified location with the
161   /// specified string.
162   ///
163   /// @param Msg If non-null, the kind of message (e.g., "error") which is
164   /// prefixed to the message.
165   SMDiagnostic GetMessage(SMLoc Loc, DiagKind Kind, const Twine &Msg,
166                           ArrayRef<SMRange> Ranges = None,
167                           ArrayRef<SMFixIt> FixIts = None) const;
168
169   /// PrintIncludeStack - Prints the names of included files and the line of the
170   /// file they were included from.  A diagnostic handler can use this before
171   /// printing its custom formatted message.
172   ///
173   /// @param IncludeLoc - The line of the include.
174   /// @param OS the raw_ostream to print on.
175   void PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const;
176 };
177
178
179 /// Represents a single fixit, a replacement of one range of text with another.
180 class SMFixIt {
181   SMRange Range;
182
183   std::string Text;
184
185 public:
186   // FIXME: Twine.str() is not very efficient.
187   SMFixIt(SMLoc Loc, const Twine &Insertion)
188     : Range(Loc, Loc), Text(Insertion.str()) {
189     assert(Loc.isValid());
190   }
191
192   // FIXME: Twine.str() is not very efficient.
193   SMFixIt(SMRange R, const Twine &Replacement)
194     : Range(R), Text(Replacement.str()) {
195     assert(R.isValid());
196   }
197
198   StringRef getText() const { return Text; }
199   SMRange getRange() const { return Range; }
200
201   bool operator<(const SMFixIt &Other) const {
202     if (Range.Start.getPointer() != Other.Range.Start.getPointer())
203       return Range.Start.getPointer() < Other.Range.Start.getPointer();
204     if (Range.End.getPointer() != Other.Range.End.getPointer())
205       return Range.End.getPointer() < Other.Range.End.getPointer();
206     return Text < Other.Text;
207   }
208 };
209
210
211 /// SMDiagnostic - Instances of this class encapsulate one diagnostic report,
212 /// allowing printing to a raw_ostream as a caret diagnostic.
213 class SMDiagnostic {
214   const SourceMgr *SM;
215   SMLoc Loc;
216   std::string Filename;
217   int LineNo, ColumnNo;
218   SourceMgr::DiagKind Kind;
219   std::string Message, LineContents;
220   std::vector<std::pair<unsigned, unsigned> > Ranges;
221   SmallVector<SMFixIt, 4> FixIts;
222
223 public:
224   // Null diagnostic.
225   SMDiagnostic()
226     : SM(nullptr), LineNo(0), ColumnNo(0), Kind(SourceMgr::DK_Error) {}
227   // Diagnostic with no location (e.g. file not found, command line arg error).
228   SMDiagnostic(StringRef filename, SourceMgr::DiagKind Knd, StringRef Msg)
229     : SM(nullptr), Filename(filename), LineNo(-1), ColumnNo(-1), Kind(Knd),
230       Message(Msg) {}
231
232   // Diagnostic with a location.
233   SMDiagnostic(const SourceMgr &sm, SMLoc L, StringRef FN,
234                int Line, int Col, SourceMgr::DiagKind Kind,
235                StringRef Msg, StringRef LineStr,
236                ArrayRef<std::pair<unsigned,unsigned> > Ranges,
237                ArrayRef<SMFixIt> FixIts = None);
238
239   const SourceMgr *getSourceMgr() const { return SM; }
240   SMLoc getLoc() const { return Loc; }
241   StringRef getFilename() const { return Filename; }
242   int getLineNo() const { return LineNo; }
243   int getColumnNo() const { return ColumnNo; }
244   SourceMgr::DiagKind getKind() const { return Kind; }
245   StringRef getMessage() const { return Message; }
246   StringRef getLineContents() const { return LineContents; }
247   ArrayRef<std::pair<unsigned, unsigned> > getRanges() const {
248     return Ranges;
249   }
250
251   void addFixIt(const SMFixIt &Hint) {
252     FixIts.push_back(Hint);
253   }
254
255   ArrayRef<SMFixIt> getFixIts() const {
256     return FixIts;
257   }
258
259   void print(const char *ProgName, raw_ostream &S,
260              bool ShowColors = true) const;
261 };
262
263 }  // end llvm namespace
264
265 #endif