LLVM  7.0.0svn
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/None.h"
21 #include "llvm/ADT/PointerUnion.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/ADT/Twine.h"
26 #include "llvm/Support/SMLoc.h"
27 #include <algorithm>
28 #include <cassert>
29 #include <memory>
30 #include <string>
31 #include <utility>
32 #include <vector>
33 
34 namespace llvm {
35 
36 class raw_ostream;
37 class SMDiagnostic;
38 class SMFixIt;
39 
40 /// This owns the files read by a parser, handles include stacks,
41 /// and handles diagnostic wrangling.
42 class SourceMgr {
43 public:
44  enum DiagKind {
49  };
50 
51  /// Clients that want to handle their own diagnostics in a custom way can
52  /// register a function pointer+context as a diagnostic handler.
53  /// It gets called each time PrintMessage is invoked.
54  using DiagHandlerTy = void (*)(const SMDiagnostic &, void *Context);
55 
56 private:
57  struct SrcBuffer {
58  /// The memory buffer for the file.
59  std::unique_ptr<MemoryBuffer> Buffer;
60 
61  /// Helper type for OffsetCache below: since we're storing many offsets
62  /// into relatively small files (often smaller than 2^8 or 2^16 bytes),
63  /// we select the offset vector element type dynamically based on the
64  /// size of Buffer.
65  using VariableSizeOffsets = PointerUnion4<std::vector<uint8_t> *,
66  std::vector<uint16_t> *,
67  std::vector<uint32_t> *,
68  std::vector<uint64_t> *>;
69 
70  /// Vector of offsets into Buffer at which there are line-endings
71  /// (lazily populated). Once populated, the '\n' that marks the end of
72  /// line number N from [1..] is at Buffer[OffsetCache[N-1]]. Since
73  /// these offsets are in sorted (ascending) order, they can be
74  /// binary-searched for the first one after any given offset (eg. an
75  /// offset corresponding to a particular SMLoc).
76  mutable VariableSizeOffsets OffsetCache;
77 
78  /// Populate \c OffsetCache and look up a given \p Ptr in it, assuming
79  /// it points somewhere into \c Buffer. The static type parameter \p T
80  /// must be an unsigned integer type from uint{8,16,32,64}_t large
81  /// enough to store offsets inside \c Buffer.
82  template<typename T>
83  unsigned getLineNumber(const char *Ptr) const;
84 
85  /// This is the location of the parent include, or null if at the top level.
86  SMLoc IncludeLoc;
87 
88  SrcBuffer() = default;
89  SrcBuffer(SrcBuffer &&);
90  SrcBuffer(const SrcBuffer &) = delete;
91  SrcBuffer &operator=(const SrcBuffer &) = delete;
92  ~SrcBuffer();
93  };
94 
95  /// This is all of the buffers that we are reading from.
96  std::vector<SrcBuffer> Buffers;
97 
98  // This is the list of directories we should search for include files in.
99  std::vector<std::string> IncludeDirectories;
100 
101  DiagHandlerTy DiagHandler = nullptr;
102  void *DiagContext = nullptr;
103 
104  bool isValidBufferID(unsigned i) const { return i && i <= Buffers.size(); }
105 
106 public:
107  SourceMgr() = default;
108  SourceMgr(const SourceMgr &) = delete;
109  SourceMgr &operator=(const SourceMgr &) = delete;
110  ~SourceMgr() = default;
111 
112  void setIncludeDirs(const std::vector<std::string> &Dirs) {
113  IncludeDirectories = Dirs;
114  }
115 
116  /// Specify a diagnostic handler to be invoked every time PrintMessage is
117  /// called. \p Ctx is passed into the handler when it is invoked.
118  void setDiagHandler(DiagHandlerTy DH, void *Ctx = nullptr) {
119  DiagHandler = DH;
120  DiagContext = Ctx;
121  }
122 
123  DiagHandlerTy getDiagHandler() const { return DiagHandler; }
124  void *getDiagContext() const { return DiagContext; }
125 
126  const SrcBuffer &getBufferInfo(unsigned i) const {
127  assert(isValidBufferID(i));
128  return Buffers[i - 1];
129  }
130 
131  const MemoryBuffer *getMemoryBuffer(unsigned i) const {
132  assert(isValidBufferID(i));
133  return Buffers[i - 1].Buffer.get();
134  }
135 
136  unsigned getNumBuffers() const {
137  return Buffers.size();
138  }
139 
140  unsigned getMainFileID() const {
142  return 1;
143  }
144 
145  SMLoc getParentIncludeLoc(unsigned i) const {
146  assert(isValidBufferID(i));
147  return Buffers[i - 1].IncludeLoc;
148  }
149 
150  /// Add a new source buffer to this source manager. This takes ownership of
151  /// the memory buffer.
152  unsigned AddNewSourceBuffer(std::unique_ptr<MemoryBuffer> F,
153  SMLoc IncludeLoc) {
154  SrcBuffer NB;
155  NB.Buffer = std::move(F);
156  NB.IncludeLoc = IncludeLoc;
157  Buffers.push_back(std::move(NB));
158  return Buffers.size();
159  }
160 
161  /// Search for a file with the specified name in the current directory or in
162  /// one of the IncludeDirs.
163  ///
164  /// If no file is found, this returns 0, otherwise it returns the buffer ID
165  /// of the stacked file. The full path to the included file can be found in
166  /// \p IncludedFile.
167  unsigned AddIncludeFile(const std::string &Filename, SMLoc IncludeLoc,
168  std::string &IncludedFile);
169 
170  /// Return the ID of the buffer containing the specified location.
171  ///
172  /// 0 is returned if the buffer is not found.
173  unsigned FindBufferContainingLoc(SMLoc Loc) const;
174 
175  /// Find the line number for the specified location in the specified file.
176  /// This is not a fast method.
177  unsigned FindLineNumber(SMLoc Loc, unsigned BufferID = 0) const {
178  return getLineAndColumn(Loc, BufferID).first;
179  }
180 
181  /// Find the line and column number for the specified location in the
182  /// specified file. This is not a fast method.
183  std::pair<unsigned, unsigned> getLineAndColumn(SMLoc Loc,
184  unsigned BufferID = 0) const;
185 
186  /// Emit a message about the specified location with the specified string.
187  ///
188  /// \param ShowColors Display colored messages if output is a terminal and
189  /// the default error handler is used.
190  void PrintMessage(raw_ostream &OS, SMLoc Loc, DiagKind Kind,
191  const Twine &Msg,
192  ArrayRef<SMRange> Ranges = None,
193  ArrayRef<SMFixIt> FixIts = None,
194  bool ShowColors = true) const;
195 
196  /// Emits a diagnostic to llvm::errs().
197  void PrintMessage(SMLoc Loc, DiagKind Kind, const Twine &Msg,
198  ArrayRef<SMRange> Ranges = None,
199  ArrayRef<SMFixIt> FixIts = None,
200  bool ShowColors = true) const;
201 
202  /// Emits a manually-constructed diagnostic to the given output stream.
203  ///
204  /// \param ShowColors Display colored messages if output is a terminal and
205  /// the default error handler is used.
206  void PrintMessage(raw_ostream &OS, const SMDiagnostic &Diagnostic,
207  bool ShowColors = true) const;
208 
209  /// Return an SMDiagnostic at the specified location with the specified
210  /// string.
211  ///
212  /// \param Msg If non-null, the kind of message (e.g., "error") which is
213  /// prefixed to the message.
214  SMDiagnostic GetMessage(SMLoc Loc, DiagKind Kind, const Twine &Msg,
215  ArrayRef<SMRange> Ranges = None,
216  ArrayRef<SMFixIt> FixIts = None) const;
217 
218  /// Prints the names of included files and the line of the file they were
219  /// included from. A diagnostic handler can use this before printing its
220  /// custom formatted message.
221  ///
222  /// \param IncludeLoc The location of the include.
223  /// \param OS the raw_ostream to print on.
224  void PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const;
225 };
226 
227 /// Represents a single fixit, a replacement of one range of text with another.
228 class SMFixIt {
229  SMRange Range;
230 
231  std::string Text;
232 
233 public:
234  // FIXME: Twine.str() is not very efficient.
235  SMFixIt(SMLoc Loc, const Twine &Insertion)
236  : Range(Loc, Loc), Text(Insertion.str()) {
237  assert(Loc.isValid());
238  }
239 
240  // FIXME: Twine.str() is not very efficient.
241  SMFixIt(SMRange R, const Twine &Replacement)
242  : Range(R), Text(Replacement.str()) {
243  assert(R.isValid());
244  }
245 
246  StringRef getText() const { return Text; }
247  SMRange getRange() const { return Range; }
248 
249  bool operator<(const SMFixIt &Other) const {
250  if (Range.Start.getPointer() != Other.Range.Start.getPointer())
251  return Range.Start.getPointer() < Other.Range.Start.getPointer();
252  if (Range.End.getPointer() != Other.Range.End.getPointer())
253  return Range.End.getPointer() < Other.Range.End.getPointer();
254  return Text < Other.Text;
255  }
256 };
257 
258 /// Instances of this class encapsulate one diagnostic report, allowing
259 /// printing to a raw_ostream as a caret diagnostic.
261  const SourceMgr *SM = nullptr;
262  SMLoc Loc;
263  std::string Filename;
264  int LineNo = 0;
265  int ColumnNo = 0;
267  std::string Message, LineContents;
268  std::vector<std::pair<unsigned, unsigned>> Ranges;
270 
271 public:
272  // Null diagnostic.
273  SMDiagnostic() = default;
274  // Diagnostic with no location (e.g. file not found, command line arg error).
276  : Filename(filename), LineNo(-1), ColumnNo(-1), Kind(Knd), Message(Msg) {}
277 
278  // Diagnostic with a location.
279  SMDiagnostic(const SourceMgr &sm, SMLoc L, StringRef FN,
280  int Line, int Col, SourceMgr::DiagKind Kind,
281  StringRef Msg, StringRef LineStr,
282  ArrayRef<std::pair<unsigned,unsigned>> Ranges,
283  ArrayRef<SMFixIt> FixIts = None);
284 
285  const SourceMgr *getSourceMgr() const { return SM; }
286  SMLoc getLoc() const { return Loc; }
287  StringRef getFilename() const { return Filename; }
288  int getLineNo() const { return LineNo; }
289  int getColumnNo() const { return ColumnNo; }
290  SourceMgr::DiagKind getKind() const { return Kind; }
291  StringRef getMessage() const { return Message; }
292  StringRef getLineContents() const { return LineContents; }
294 
295  void addFixIt(const SMFixIt &Hint) {
296  FixIts.push_back(Hint);
297  }
298 
300  return FixIts;
301  }
302 
303  void print(const char *ProgName, raw_ostream &S, bool ShowColors = true,
304  bool ShowKindLabel = true) const;
305 };
306 
307 } // end namespace llvm
308 
309 #endif // LLVM_SUPPORT_SOURCEMGR_H
Represents a range in source code.
Definition: SMLoc.h:49
unsigned FindBufferContainingLoc(SMLoc Loc) const
Return the ID of the buffer containing the specified location.
Definition: SourceMgr.cpp:61
LLVMContext & Context
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
SMLoc getLoc() const
Definition: SourceMgr.h:286
DiagHandlerTy getDiagHandler() const
Definition: SourceMgr.h:123
SMFixIt(SMLoc Loc, const Twine &Insertion)
Definition: SourceMgr.h:235
unsigned getNumBuffers() const
Definition: SourceMgr.h:136
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:155
F(f)
ArrayRef< SMFixIt > getFixIts() const
Definition: SourceMgr.h:299
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:130
SMRange getRange() const
Definition: SourceMgr.h:247
void addFixIt(const SMFixIt &Hint)
Definition: SourceMgr.h:295
SMLoc Start
Definition: SMLoc.h:51
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:247
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
int getLineNo() const
Definition: SourceMgr.h:288
const MemoryBuffer * getMemoryBuffer(unsigned i) const
Definition: SourceMgr.h:131
StringRef getLineContents() const
Definition: SourceMgr.h:292
unsigned AddNewSourceBuffer(std::unique_ptr< MemoryBuffer > F, SMLoc IncludeLoc)
Add a new source buffer to this source manager.
Definition: SourceMgr.h:152
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
StringRef getText() const
Definition: SourceMgr.h:246
unsigned getMainFileID() const
Definition: SourceMgr.h:140
const char * getPointer() const
Definition: SMLoc.h:35
void setDiagHandler(DiagHandlerTy DH, void *Ctx=nullptr)
Specify a diagnostic handler to be invoked every time PrintMessage is called.
Definition: SourceMgr.h:118
SourceMgr()=default
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling...
Definition: SourceMgr.h:42
Represents a single fixit, a replacement of one range of text with another.
Definition: SourceMgr.h:228
void(*)(const SMDiagnostic &, void *Context) DiagHandlerTy
Clients that want to handle their own diagnostics in a custom way can register a function pointer+con...
Definition: SourceMgr.h:54
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:168
const SrcBuffer & getBufferInfo(unsigned i) const
Definition: SourceMgr.h:126
bool isValid() const
Definition: SMLoc.h:60
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
StringRef getMessage() const
Definition: SourceMgr.h:291
ArrayRef< std::pair< unsigned, unsigned > > getRanges() const
Definition: SourceMgr.h:293
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:42
bool isValid() const
Definition: SMLoc.h:30
StringRef getFilename() const
Definition: SourceMgr.h:287
SMLoc End
Definition: SMLoc.h:51
SourceMgr::DiagKind getKind() const
Definition: SourceMgr.h:290
unsigned FindLineNumber(SMLoc Loc, unsigned BufferID=0) const
Find the line number for the specified location in the specified file.
Definition: SourceMgr.h:177
bool operator<(const SMFixIt &Other) const
Definition: SourceMgr.h:249
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:40
int getColumnNo() const
Definition: SourceMgr.h:289
SMLoc getParentIncludeLoc(unsigned i) const
Definition: SourceMgr.h:145
StringRef filename(StringRef path, Style style=Style::native)
Get filename.
Definition: Path.cpp:581
void * getDiagContext() const
Definition: SourceMgr.h:124
const unsigned Kind
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
~SourceMgr()=default
SourceMgr & operator=(const SourceMgr &)=delete
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
void setIncludeDirs(const std::vector< std::string > &Dirs)
Definition: SourceMgr.h:112
SMDiagnostic(StringRef filename, SourceMgr::DiagKind Knd, StringRef Msg)
Definition: SourceMgr.h:275
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
Represents a location in source code.
Definition: SMLoc.h:24
SMFixIt(SMRange R, const Twine &Replacement)
Definition: SourceMgr.h:241
const SourceMgr * getSourceMgr() const
Definition: SourceMgr.h:285
A pointer union of four pointer types.
Definition: PointerUnion.h:358
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
Definition: SourceMgr.h:260