1 //===- SourceMgr.h - Manager for Source Buffers & Diagnostics ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_SUPPORT_SOURCEMGR_H
17 #define LLVM_SUPPORT_SOURCEMGR_H
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/Twine.h"
22 #include "llvm/Support/SMLoc.h"
33 /// This owns the files read by a parser, handles include stacks,
34 /// and handles diagnostic wrangling.
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);
49 /// The memory buffer for the file.
52 /// This is the location of the parent include, or null if at the top level.
56 /// This is all of the buffers that we are reading from.
57 std::vector<SrcBuffer> Buffers;
59 // This is the list of directories we should search for include files in.
60 std::vector<std::string> IncludeDirectories;
62 /// This is a cache for line number queries, its implementation is really
63 /// private to SourceMgr.cpp.
64 mutable void *LineNoCache;
66 DiagHandlerTy DiagHandler;
69 SourceMgr(const SourceMgr&) LLVM_DELETED_FUNCTION;
70 void operator=(const SourceMgr&) LLVM_DELETED_FUNCTION;
73 : LineNoCache(nullptr), DiagHandler(nullptr), DiagContext(nullptr) {}
76 void setIncludeDirs(const std::vector<std::string> &Dirs) {
77 IncludeDirectories = Dirs;
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) {
87 DiagHandlerTy getDiagHandler() const { return DiagHandler; }
88 void *getDiagContext() const { return DiagContext; }
90 const SrcBuffer &getBufferInfo(unsigned i) const {
91 assert(i < Buffers.size() && "Invalid Buffer ID!");
95 const MemoryBuffer *getMemoryBuffer(unsigned i) const {
96 assert(i < Buffers.size() && "Invalid Buffer ID!");
97 return Buffers[i].Buffer;
100 size_t getNumBuffers() const {
101 return Buffers.size();
104 SMLoc getParentIncludeLoc(unsigned i) const {
105 assert(i < Buffers.size() && "Invalid Buffer ID!");
106 return Buffers[i].IncludeLoc;
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) {
114 NB.IncludeLoc = IncludeLoc;
115 Buffers.push_back(NB);
116 return Buffers.size() - 1;
119 /// Search for a file with the specified name in the current directory or in
120 /// one of the IncludeDirs.
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
125 size_t AddIncludeFile(const std::string &Filename, SMLoc IncludeLoc,
126 std::string &IncludedFile);
128 /// Return the ID of the buffer containing the specified location.
130 /// -1 is returned if the buffer is not found.
131 int FindBufferContainingLoc(SMLoc Loc) const;
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;
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;
144 /// Emit a message about the specified location with the specified string.
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,
150 ArrayRef<SMRange> Ranges = None,
151 ArrayRef<SMFixIt> FixIts = None,
152 bool ShowColors = true) const;
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;
160 /// Emits a manually-constructed diagnostic to the given output stream.
162 /// \param ShowColors Display colored messages if output is a terminal and
163 /// the default error handler is used.
164 void PrintMessage(raw_ostream &OS, const SMDiagnostic &Diagnostic,
165 bool ShowColors = true) const;
167 /// Return an SMDiagnostic at the specified location with the specified
170 /// \param Msg If non-null, the kind of message (e.g., "error") which is
171 /// prefixed to the message.
172 SMDiagnostic GetMessage(SMLoc Loc, DiagKind Kind, const Twine &Msg,
173 ArrayRef<SMRange> Ranges = None,
174 ArrayRef<SMFixIt> FixIts = None) const;
176 /// Prints the names of included files and the line of the file they were
177 /// included from. A diagnostic handler can use this before printing its
178 /// custom formatted message.
180 /// \param IncludeLoc The location of the include.
181 /// \param OS the raw_ostream to print on.
182 void PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const;
186 /// Represents a single fixit, a replacement of one range of text with another.
193 // FIXME: Twine.str() is not very efficient.
194 SMFixIt(SMLoc Loc, const Twine &Insertion)
195 : Range(Loc, Loc), Text(Insertion.str()) {
196 assert(Loc.isValid());
199 // FIXME: Twine.str() is not very efficient.
200 SMFixIt(SMRange R, const Twine &Replacement)
201 : Range(R), Text(Replacement.str()) {
205 StringRef getText() const { return Text; }
206 SMRange getRange() const { return Range; }
208 bool operator<(const SMFixIt &Other) const {
209 if (Range.Start.getPointer() != Other.Range.Start.getPointer())
210 return Range.Start.getPointer() < Other.Range.Start.getPointer();
211 if (Range.End.getPointer() != Other.Range.End.getPointer())
212 return Range.End.getPointer() < Other.Range.End.getPointer();
213 return Text < Other.Text;
218 /// Instances of this class encapsulate one diagnostic report, allowing
219 /// printing to a raw_ostream as a caret diagnostic.
223 std::string Filename;
224 int LineNo, ColumnNo;
225 SourceMgr::DiagKind Kind;
226 std::string Message, LineContents;
227 std::vector<std::pair<unsigned, unsigned> > Ranges;
228 SmallVector<SMFixIt, 4> FixIts;
233 : SM(nullptr), LineNo(0), ColumnNo(0), Kind(SourceMgr::DK_Error) {}
234 // Diagnostic with no location (e.g. file not found, command line arg error).
235 SMDiagnostic(StringRef filename, SourceMgr::DiagKind Knd, StringRef Msg)
236 : SM(nullptr), Filename(filename), LineNo(-1), ColumnNo(-1), Kind(Knd),
239 // Diagnostic with a location.
240 SMDiagnostic(const SourceMgr &sm, SMLoc L, StringRef FN,
241 int Line, int Col, SourceMgr::DiagKind Kind,
242 StringRef Msg, StringRef LineStr,
243 ArrayRef<std::pair<unsigned,unsigned> > Ranges,
244 ArrayRef<SMFixIt> FixIts = None);
246 const SourceMgr *getSourceMgr() const { return SM; }
247 SMLoc getLoc() const { return Loc; }
248 StringRef getFilename() const { return Filename; }
249 int getLineNo() const { return LineNo; }
250 int getColumnNo() const { return ColumnNo; }
251 SourceMgr::DiagKind getKind() const { return Kind; }
252 StringRef getMessage() const { return Message; }
253 StringRef getLineContents() const { return LineContents; }
254 ArrayRef<std::pair<unsigned, unsigned> > getRanges() const {
258 void addFixIt(const SMFixIt &Hint) {
259 FixIts.push_back(Hint);
262 ArrayRef<SMFixIt> getFixIts() const {
266 void print(const char *ProgName, raw_ostream &S,
267 bool ShowColors = true) const;
270 } // end llvm namespace