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
25namespace 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.
35class SourceMgr {
36public:
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);
47private:
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  bool isValidBufferID(unsigned i) const { return i && i <= Buffers.size(); }
70
71  SourceMgr(const SourceMgr&) LLVM_DELETED_FUNCTION;
72  void operator=(const SourceMgr&) LLVM_DELETED_FUNCTION;
73public:
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  /// 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;
100  }
101
102  unsigned getNumBuffers() const {
103    return Buffers.size();
104  }
105
106  unsigned getMainFileID() const {
107    assert(getNumBuffers());
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(MemoryBuffer *F, SMLoc IncludeLoc) {
119    SrcBuffer NB;
120    NB.Buffer = F;
121    NB.IncludeLoc = IncludeLoc;
122    Buffers.push_back(NB);
123    return Buffers.size();
124  }
125
126  /// Search for a file with the specified name in the current directory or in
127  /// one of the IncludeDirs.
128  ///
129  /// If no file is found, this returns 0, otherwise it returns the buffer ID
130  /// of the stacked file. The full path to the included file can be found in
131  /// \p IncludedFile.
132  unsigned AddIncludeFile(const std::string &Filename, SMLoc IncludeLoc,
133                          std::string &IncludedFile);
134
135  /// Return the ID of the buffer containing the specified location.
136  ///
137  /// 0 is returned if the buffer is not found.
138  unsigned FindBufferContainingLoc(SMLoc Loc) const;
139
140  /// Find the line number for the specified location in the specified file.
141  /// This is not a fast method.
142  unsigned FindLineNumber(SMLoc Loc, unsigned BufferID = 0) const {
143    return getLineAndColumn(Loc, BufferID).first;
144  }
145
146  /// Find the line and column number for the specified location in the
147  /// specified file. This is not a fast method.
148  std::pair<unsigned, unsigned> getLineAndColumn(SMLoc Loc,
149                                                 unsigned BufferID = 0) const;
150
151  /// Emit a message about the specified location with the specified string.
152  ///
153  /// \param ShowColors Display colored messages if output is a terminal and
154  /// the default error handler is used.
155  void PrintMessage(raw_ostream &OS, SMLoc Loc, DiagKind Kind,
156                    const Twine &Msg,
157                    ArrayRef<SMRange> Ranges = None,
158                    ArrayRef<SMFixIt> FixIts = None,
159                    bool ShowColors = true) const;
160
161  /// Emits a diagnostic to llvm::errs().
162  void PrintMessage(SMLoc Loc, DiagKind Kind, const Twine &Msg,
163                    ArrayRef<SMRange> Ranges = None,
164                    ArrayRef<SMFixIt> FixIts = None,
165                    bool ShowColors = true) const;
166
167  /// Emits a manually-constructed diagnostic to the given output stream.
168  ///
169  /// \param ShowColors Display colored messages if output is a terminal and
170  /// the default error handler is used.
171  void PrintMessage(raw_ostream &OS, const SMDiagnostic &Diagnostic,
172                    bool ShowColors = true) const;
173
174  /// Return an SMDiagnostic at the specified location with the specified
175  /// string.
176  ///
177  /// \param Msg If non-null, the kind of message (e.g., "error") which is
178  /// prefixed to the message.
179  SMDiagnostic GetMessage(SMLoc Loc, DiagKind Kind, const Twine &Msg,
180                          ArrayRef<SMRange> Ranges = None,
181                          ArrayRef<SMFixIt> FixIts = None) const;
182
183  /// Prints the names of included files and the line of the file they were
184  /// included from. A diagnostic handler can use this before printing its
185  /// custom formatted message.
186  ///
187  /// \param IncludeLoc The location of the include.
188  /// \param OS the raw_ostream to print on.
189  void PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const;
190};
191
192
193/// Represents a single fixit, a replacement of one range of text with another.
194class SMFixIt {
195  SMRange Range;
196
197  std::string Text;
198
199public:
200  // FIXME: Twine.str() is not very efficient.
201  SMFixIt(SMLoc Loc, const Twine &Insertion)
202    : Range(Loc, Loc), Text(Insertion.str()) {
203    assert(Loc.isValid());
204  }
205
206  // FIXME: Twine.str() is not very efficient.
207  SMFixIt(SMRange R, const Twine &Replacement)
208    : Range(R), Text(Replacement.str()) {
209    assert(R.isValid());
210  }
211
212  StringRef getText() const { return Text; }
213  SMRange getRange() const { return Range; }
214
215  bool operator<(const SMFixIt &Other) const {
216    if (Range.Start.getPointer() != Other.Range.Start.getPointer())
217      return Range.Start.getPointer() < Other.Range.Start.getPointer();
218    if (Range.End.getPointer() != Other.Range.End.getPointer())
219      return Range.End.getPointer() < Other.Range.End.getPointer();
220    return Text < Other.Text;
221  }
222};
223
224
225/// Instances of this class encapsulate one diagnostic report, allowing
226/// printing to a raw_ostream as a caret diagnostic.
227class SMDiagnostic {
228  const SourceMgr *SM;
229  SMLoc Loc;
230  std::string Filename;
231  int LineNo, ColumnNo;
232  SourceMgr::DiagKind Kind;
233  std::string Message, LineContents;
234  std::vector<std::pair<unsigned, unsigned> > Ranges;
235  SmallVector<SMFixIt, 4> FixIts;
236
237public:
238  // Null diagnostic.
239  SMDiagnostic()
240    : SM(nullptr), LineNo(0), ColumnNo(0), Kind(SourceMgr::DK_Error) {}
241  // Diagnostic with no location (e.g. file not found, command line arg error).
242  SMDiagnostic(StringRef filename, SourceMgr::DiagKind Knd, StringRef Msg)
243    : SM(nullptr), Filename(filename), LineNo(-1), ColumnNo(-1), Kind(Knd),
244      Message(Msg) {}
245
246  // Diagnostic with a location.
247  SMDiagnostic(const SourceMgr &sm, SMLoc L, StringRef FN,
248               int Line, int Col, SourceMgr::DiagKind Kind,
249               StringRef Msg, StringRef LineStr,
250               ArrayRef<std::pair<unsigned,unsigned> > Ranges,
251               ArrayRef<SMFixIt> FixIts = None);
252
253  const SourceMgr *getSourceMgr() const { return SM; }
254  SMLoc getLoc() const { return Loc; }
255  StringRef getFilename() const { return Filename; }
256  int getLineNo() const { return LineNo; }
257  int getColumnNo() const { return ColumnNo; }
258  SourceMgr::DiagKind getKind() const { return Kind; }
259  StringRef getMessage() const { return Message; }
260  StringRef getLineContents() const { return LineContents; }
261  ArrayRef<std::pair<unsigned, unsigned> > getRanges() const {
262    return Ranges;
263  }
264
265  void addFixIt(const SMFixIt &Hint) {
266    FixIts.push_back(Hint);
267  }
268
269  ArrayRef<SMFixIt> getFixIts() const {
270    return FixIts;
271  }
272
273  void print(const char *ProgName, raw_ostream &S,
274             bool ShowColors = true) const;
275};
276
277}  // end llvm namespace
278
279#endif
280