LLVM  4.0.0
SourceMgr.h
Go to the documentation of this file.
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"
23 #include "llvm/Support/SMLoc.h"
24 #include <string>
25 
26 namespace llvm {
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 {
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  std::unique_ptr<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  bool isValidBufferID(unsigned i) const { return i && i <= Buffers.size(); }
70 
71  SourceMgr(const SourceMgr&) = delete;
72  void operator=(const SourceMgr&) = delete;
73 public:
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  /// Specify a diagnostic handler to be invoked every time PrintMessage is
83  /// called. \p 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(isValidBufferID(i));
94  return Buffers[i - 1];
95  }
96 
97  const MemoryBuffer *getMemoryBuffer(unsigned i) const {
98  assert(isValidBufferID(i));
99  return Buffers[i - 1].Buffer.get();
100  }
101 
102  unsigned getNumBuffers() const {
103  return Buffers.size();
104  }
105 
106  unsigned getMainFileID() const {
108  return 1;
109  }
110 
111  SMLoc getParentIncludeLoc(unsigned i) const {
112  assert(isValidBufferID(i));
113  return Buffers[i - 1].IncludeLoc;
114  }
115 
116  /// Add a new source buffer to this source manager. This takes ownership of
117  /// the memory buffer.
118  unsigned AddNewSourceBuffer(std::unique_ptr<MemoryBuffer> F,
119  SMLoc IncludeLoc) {
120  SrcBuffer NB;
121  NB.Buffer = std::move(F);
122  NB.IncludeLoc = IncludeLoc;
123  Buffers.push_back(std::move(NB));
124  return Buffers.size();
125  }
126 
127  /// Search for a file with the specified name in the current directory or in
128  /// one of the IncludeDirs.
129  ///
130  /// If no file is found, this returns 0, otherwise it returns the buffer ID
131  /// of the stacked file. The full path to the included file can be found in
132  /// \p IncludedFile.
133  unsigned AddIncludeFile(const std::string &Filename, SMLoc IncludeLoc,
134  std::string &IncludedFile);
135 
136  /// Return the ID of the buffer containing the specified location.
137  ///
138  /// 0 is returned if the buffer is not found.
139  unsigned FindBufferContainingLoc(SMLoc Loc) const;
140 
141  /// Find the line number for the specified location in the specified file.
142  /// This is not a fast method.
143  unsigned FindLineNumber(SMLoc Loc, unsigned BufferID = 0) const {
144  return getLineAndColumn(Loc, BufferID).first;
145  }
146 
147  /// Find the line and column number for the specified location in the
148  /// specified file. This is not a fast method.
149  std::pair<unsigned, unsigned> getLineAndColumn(SMLoc Loc,
150  unsigned BufferID = 0) const;
151 
152  /// Emit a message about the specified location with the specified string.
153  ///
154  /// \param ShowColors Display colored messages if output is a terminal and
155  /// the default error handler is used.
156  void PrintMessage(raw_ostream &OS, SMLoc Loc, DiagKind Kind,
157  const Twine &Msg,
158  ArrayRef<SMRange> Ranges = None,
159  ArrayRef<SMFixIt> FixIts = None,
160  bool ShowColors = true) const;
161 
162  /// Emits a diagnostic to llvm::errs().
163  void PrintMessage(SMLoc Loc, DiagKind Kind, const Twine &Msg,
164  ArrayRef<SMRange> Ranges = None,
165  ArrayRef<SMFixIt> FixIts = None,
166  bool ShowColors = true) const;
167 
168  /// Emits a manually-constructed diagnostic to the given output stream.
169  ///
170  /// \param ShowColors Display colored messages if output is a terminal and
171  /// the default error handler is used.
172  void PrintMessage(raw_ostream &OS, const SMDiagnostic &Diagnostic,
173  bool ShowColors = true) const;
174 
175  /// Return an SMDiagnostic at the specified location with the specified
176  /// string.
177  ///
178  /// \param Msg If non-null, the kind of message (e.g., "error") which is
179  /// prefixed to the message.
180  SMDiagnostic GetMessage(SMLoc Loc, DiagKind Kind, const Twine &Msg,
181  ArrayRef<SMRange> Ranges = None,
182  ArrayRef<SMFixIt> FixIts = None) const;
183 
184  /// Prints the names of included files and the line of the file they were
185  /// included from. A diagnostic handler can use this before printing its
186  /// custom formatted message.
187  ///
188  /// \param IncludeLoc The location of the include.
189  /// \param OS the raw_ostream to print on.
190  void PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const;
191 };
192 
193 
194 /// Represents a single fixit, a replacement of one range of text with another.
195 class SMFixIt {
196  SMRange Range;
197 
198  std::string Text;
199 
200 public:
201  // FIXME: Twine.str() is not very efficient.
202  SMFixIt(SMLoc Loc, const Twine &Insertion)
203  : Range(Loc, Loc), Text(Insertion.str()) {
204  assert(Loc.isValid());
205  }
206 
207  // FIXME: Twine.str() is not very efficient.
208  SMFixIt(SMRange R, const Twine &Replacement)
209  : Range(R), Text(Replacement.str()) {
210  assert(R.isValid());
211  }
212 
213  StringRef getText() const { return Text; }
214  SMRange getRange() const { return Range; }
215 
216  bool operator<(const SMFixIt &Other) const {
217  if (Range.Start.getPointer() != Other.Range.Start.getPointer())
218  return Range.Start.getPointer() < Other.Range.Start.getPointer();
219  if (Range.End.getPointer() != Other.Range.End.getPointer())
220  return Range.End.getPointer() < Other.Range.End.getPointer();
221  return Text < Other.Text;
222  }
223 };
224 
225 
226 /// Instances of this class encapsulate one diagnostic report, allowing
227 /// printing to a raw_ostream as a caret diagnostic.
229  const SourceMgr *SM;
230  SMLoc Loc;
231  std::string Filename;
232  int LineNo, ColumnNo;
233  SourceMgr::DiagKind Kind;
234  std::string Message, LineContents;
235  std::vector<std::pair<unsigned, unsigned> > Ranges;
237 
238 public:
239  // Null diagnostic.
241  : SM(nullptr), LineNo(0), ColumnNo(0), Kind(SourceMgr::DK_Error) {}
242  // Diagnostic with no location (e.g. file not found, command line arg error).
244  : SM(nullptr), Filename(filename), LineNo(-1), ColumnNo(-1), Kind(Knd),
245  Message(Msg) {}
246 
247  // Diagnostic with a location.
248  SMDiagnostic(const SourceMgr &sm, SMLoc L, StringRef FN,
249  int Line, int Col, SourceMgr::DiagKind Kind,
250  StringRef Msg, StringRef LineStr,
251  ArrayRef<std::pair<unsigned,unsigned> > Ranges,
252  ArrayRef<SMFixIt> FixIts = None);
253 
254  const SourceMgr *getSourceMgr() const { return SM; }
255  SMLoc getLoc() const { return Loc; }
256  StringRef getFilename() const { return Filename; }
257  int getLineNo() const { return LineNo; }
258  int getColumnNo() const { return ColumnNo; }
259  SourceMgr::DiagKind getKind() const { return Kind; }
260  StringRef getMessage() const { return Message; }
261  StringRef getLineContents() const { return LineContents; }
263  return Ranges;
264  }
265 
266  void addFixIt(const SMFixIt &Hint) {
267  FixIts.push_back(Hint);
268  }
269 
271  return FixIts;
272  }
273 
274  void print(const char *ProgName, raw_ostream &S, bool ShowColors = true,
275  bool ShowKindLabel = true) const;
276 };
277 
278 } // end llvm namespace
279 
280 #endif
MachineLoop * L
void print(const char *ProgName, raw_ostream &S, bool ShowColors=true, bool ShowKindLabel=true) const
Definition: SourceMgr.cpp:336
Represents a range in source code.
Definition: SMLoc.h:49
LLVMContext & Context
void PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const
Prints the names of included files and the line of the file they were included from.
Definition: SourceMgr.cpp:122
const char * getPointer() const
Definition: SMLoc.h:35
size_t i
SMFixIt(SMLoc Loc, const Twine &Insertion)
Definition: SourceMgr.h:202
std::pair< unsigned, unsigned > getLineAndColumn(SMLoc Loc, unsigned BufferID=0) const
Find the line and column number for the specified location in the specified file. ...
Definition: SourceMgr.cpp:78
void * getDiagContext() const
Definition: SourceMgr.h:90
void addFixIt(const SMFixIt &Hint)
Definition: SourceMgr.h:266
SMLoc Start
Definition: SMLoc.h:51
unsigned getMainFileID() const
Definition: SourceMgr.h:106
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
SMLoc getLoc() const
Definition: SourceMgr.h:255
StringRef getLineContents() const
Definition: SourceMgr.h:261
bool isValid() const
Definition: SMLoc.h:30
#define F(x, y, z)
Definition: MD5.cpp:51
StringRef getMessage() const
Definition: SourceMgr.h:260
unsigned AddNewSourceBuffer(std::unique_ptr< MemoryBuffer > F, SMLoc IncludeLoc)
Add a new source buffer to this source manager.
Definition: SourceMgr.h:118
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
StringRef getFilename() const
Definition: SourceMgr.h:256
SMRange getRange() const
Definition: SourceMgr.h:214
StringRef filename(StringRef path)
Get filename.
Definition: Path.cpp:584
void setDiagHandler(DiagHandlerTy DH, void *Ctx=nullptr)
Specify a diagnostic handler to be invoked every time PrintMessage is called.
Definition: SourceMgr.h:84
StringRef getText() const
Definition: SourceMgr.h:213
unsigned getNumBuffers() const
Definition: SourceMgr.h:102
void(* DiagHandlerTy)(const SMDiagnostic &, void *Context)
Clients that want to handle their own diagnostics in a custom way can register a function pointer+con...
Definition: SourceMgr.h:46
ArrayRef< SMFixIt > getFixIts() const
Definition: SourceMgr.h:270
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling...
Definition: SourceMgr.h:35
const SrcBuffer & getBufferInfo(unsigned i) const
Definition: SourceMgr.h:92
Represents a single fixit, a replacement of one range of text with another.
Definition: SourceMgr.h:195
ArrayRef< std::pair< unsigned, unsigned > > getRanges() const
Definition: SourceMgr.h:262
int getColumnNo() const
Definition: SourceMgr.h:258
SourceMgr::DiagKind getKind() const
Definition: SourceMgr.h:259
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:843
bool operator<(const SMFixIt &Other) const
Definition: SourceMgr.h:216
int getLineNo() const
Definition: SourceMgr.h:257
SMLoc getParentIncludeLoc(unsigned i) const
Definition: SourceMgr.h:111
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:40
unsigned FindLineNumber(SMLoc Loc, unsigned BufferID=0) const
Find the line number for the specified location in the specified file.
Definition: SourceMgr.h:143
SMLoc End
Definition: SMLoc.h:51
unsigned AddIncludeFile(const std::string &Filename, SMLoc IncludeLoc, std::string &IncludedFile)
Search for a file with the specified name in the current directory or in one of the IncludeDirs...
Definition: SourceMgr.cpp:46
const MemoryBuffer * getMemoryBuffer(unsigned i) const
Definition: SourceMgr.h:97
bool isValid() const
Definition: SMLoc.h:60
unsigned FindBufferContainingLoc(SMLoc Loc) const
Return the ID of the buffer containing the specified location.
Definition: SourceMgr.cpp:67
const unsigned Kind
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
void setIncludeDirs(const std::vector< std::string > &Dirs)
Definition: SourceMgr.h:78
SMDiagnostic(StringRef filename, SourceMgr::DiagKind Knd, StringRef Msg)
Definition: SourceMgr.h:243
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
SMDiagnostic GetMessage(SMLoc Loc, DiagKind Kind, const Twine &Msg, ArrayRef< SMRange > Ranges=None, ArrayRef< SMFixIt > FixIts=None) const
Return an SMDiagnostic at the specified location with the specified string.
Definition: SourceMgr.cpp:136
const SourceMgr * getSourceMgr() const
Definition: SourceMgr.h:254
void PrintMessage(raw_ostream &OS, SMLoc Loc, DiagKind Kind, const Twine &Msg, ArrayRef< SMRange > Ranges=None, ArrayRef< SMFixIt > FixIts=None, bool ShowColors=true) const
Emit a message about the specified location with the specified string.
Definition: SourceMgr.cpp:216
Represents a location in source code.
Definition: SMLoc.h:24
SMFixIt(SMRange R, const Twine &Replacement)
Definition: SourceMgr.h:208
DiagHandlerTy getDiagHandler() const
Definition: SourceMgr.h:89
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
Definition: SourceMgr.h:228