Modernize doc comments for 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 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 /// 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   /// Clients that want to handle their own diagnostics in a custom way can
44   /// register a function pointer+context as a diagnostic handler.
45   /// It gets called each time PrintMessage is invoked.
46   typedef void (*DiagHandlerTy)(const SMDiagnostic &, void *Context);
47 private:
48   struct SrcBuffer {
49     /// The memory buffer for the file.
50     MemoryBuffer *Buffer;
51
52     /// This is the location of the parent include, or null if at the top level.
53     SMLoc IncludeLoc;
54   };
55
56   /// This is all of the buffers that we are reading from.
57   std::vector<SrcBuffer> Buffers;
58
59   // This is the list of directories we should search for include files in.
60   std::vector<std::string> IncludeDirectories;
61
62   /// This is a cache for line number queries, its implementation is really
63   /// private to SourceMgr.cpp.
64   mutable void *LineNoCache;
65
66   DiagHandlerTy DiagHandler;
67   void *DiagContext;
68
69   SourceMgr(const SourceMgr&) LLVM_DELETED_FUNCTION;
70   void operator=(const SourceMgr&) LLVM_DELETED_FUNCTION;
71 public:
72   SourceMgr()
73     : LineNoCache(nullptr), DiagHandler(nullptr), DiagContext(nullptr) {}
74   ~SourceMgr();
75
76   void setIncludeDirs(const std::vector<std::string> &Dirs) {
77     IncludeDirectories = Dirs;
78   }
79
80   /// Specify a diagnostic handler to be invoked every time PrintMessage is
81   /// called. \p Ctx is passed into the handler when it is invoked.
82   void setDiagHandler(DiagHandlerTy DH, void *Ctx = nullptr) {
83     DiagHandler = DH;
84     DiagContext = Ctx;
85   }
86
87   DiagHandlerTy getDiagHandler() const { return DiagHandler; }
88   void *getDiagContext() const { return DiagContext; }
89
90   const SrcBuffer &getBufferInfo(unsigned i) const {
91     assert(i < Buffers.size() && "Invalid Buffer ID!");
92     return Buffers[i];
93   }
94
95   const MemoryBuffer *getMemoryBuffer(unsigned i) const {
96     assert(i < Buffers.size() && "Invalid Buffer ID!");
97     return Buffers[i].Buffer;
98   }
99
100   size_t getNumBuffers() const {
101     return Buffers.size();
102   }
103
104   SMLoc getParentIncludeLoc(unsigned i) const {
105     assert(i < Buffers.size() && "Invalid Buffer ID!");
106     return Buffers[i].IncludeLoc;
107   }
108
109   /// Add a new source buffer to this source manager. This takes ownership of
110   /// the memory buffer.
111   size_t AddNewSourceBuffer(MemoryBuffer *F, SMLoc IncludeLoc) {
112     SrcBuffer NB;
113     NB.Buffer = F;
114     NB.IncludeLoc = IncludeLoc;
115     Buffers.push_back(NB);
116     return Buffers.size() - 1;
117   }
118
119   /// Search for a file with the specified name in the current directory or in
120   /// one of the IncludeDirs.
121   ///
122   /// If no file is found, this returns ~0, otherwise it returns the buffer ID
123   /// of the stacked file. The full path to the included file can be found in
124   /// \p IncludedFile.
125   size_t AddIncludeFile(const std::string &Filename, SMLoc IncludeLoc,
126                         std::string &IncludedFile);
127
128   /// Return the ID of the buffer containing the specified location.
129   ///
130   /// -1 is returned if the buffer is not found.
131   int FindBufferContainingLoc(SMLoc Loc) const;
132
133   /// Find the line number for the specified location in the specified file.
134   /// This is not a fast method.
135   unsigned FindLineNumber(SMLoc Loc, int BufferID = -1) const {
136     return getLineAndColumn(Loc, BufferID).first;
137   }
138
139   /// Find the line and column number for the specified location in the
140   /// specified file. This is not a fast method.
141   std::pair<unsigned, unsigned>
142     getLineAndColumn(SMLoc Loc, int BufferID = -1) const;
143
144   /// Emit a message about the specified location with the 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   /// Return an SMDiagnostic at the specified location with the specified
161   /// 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   /// Prints the names of included files and the line of the file they were
170   /// included from. A diagnostic handler can use this before printing its
171   /// custom formatted message.
172   ///
173   /// \param IncludeLoc The location 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 /// Instances of this class encapsulate one diagnostic report, allowing
212 /// 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