LLVM 23.0.0git
SourceMgr.h
Go to the documentation of this file.
1//===- SourceMgr.h - Manager for Source Buffers & Diagnostics ---*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file declares the SMDiagnostic and SourceMgr classes. This
10// provides a simple substrate for diagnostics, #include handling, and other low
11// level things for simple parsers.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_SUPPORT_SOURCEMGR_H
16#define LLVM_SUPPORT_SOURCEMGR_H
17
22#include "llvm/Support/SMLoc.h"
23#include <vector>
24
25namespace llvm {
26
27namespace vfs {
28class FileSystem;
29} // end namespace vfs
30
31class raw_ostream;
32class SMDiagnostic;
33class SMFixIt;
34
35/// This owns the files read by a parser, handles include stacks,
36/// and handles diagnostic wrangling.
37class SourceMgr {
38public:
45
46 /// Clients that want to handle their own diagnostics in a custom way can
47 /// register a function pointer+context as a diagnostic handler.
48 /// It gets called each time PrintMessage is invoked.
49 using DiagHandlerTy = void (*)(const SMDiagnostic &, void *Context);
50
51private:
52 struct SrcBuffer {
53 /// The memory buffer for the file.
54 std::unique_ptr<MemoryBuffer> Buffer;
55
56 /// Vector of offsets into Buffer at which there are line-endings
57 /// (lazily populated). Once populated, the '\n' that marks the end of
58 /// line number N from [1..] is at Buffer[OffsetCache[N-1]]. Since
59 /// these offsets are in sorted (ascending) order, they can be
60 /// binary-searched for the first one after any given offset (eg. an
61 /// offset corresponding to a particular SMLoc).
62 ///
63 /// Since we're storing offsets into relatively small files (often smaller
64 /// than 2^8 or 2^16 bytes), we select the offset vector element type
65 /// dynamically based on the size of Buffer.
66 mutable void *OffsetCache = nullptr;
67
68 /// Look up a given \p Ptr in the buffer, determining which line and column
69 /// it came from. This method has O(log n) complexity, where n is the number
70 /// of lines in the buffer.
71 LLVM_ABI std::pair<unsigned, unsigned>
72 getLineAndColumn(const char *Ptr) const;
73 template <typename T>
74 std::pair<unsigned, unsigned>
75 getLineAndColumnSpecialized(const char *Ptr) const;
76
77 /// Return a pointer to the first character of the specified line number or
78 /// null if the line number is invalid.
79 LLVM_ABI const char *getPointerForLineNumber(unsigned LineNo) const;
80 template <typename T>
81 const char *getPointerForLineNumberSpecialized(unsigned LineNo) const;
82
83 /// This is the location of the parent include, or null if at the top level.
84 SMLoc IncludeLoc;
85
86 SrcBuffer() = default;
87 LLVM_ABI SrcBuffer(SrcBuffer &&);
88 SrcBuffer(const SrcBuffer &) = delete;
89 SrcBuffer &operator=(const SrcBuffer &) = delete;
90 LLVM_ABI ~SrcBuffer();
91 };
92
93 /// This is all of the buffers that we are reading from.
94 std::vector<SrcBuffer> Buffers;
95
96 // This is the list of directories we should search for include files in.
97 std::vector<std::string> IncludeDirectories;
98
99 DiagHandlerTy DiagHandler = nullptr;
100 void *DiagContext = nullptr;
101
102 // Optional file system for finding include files.
104
105 bool isValidBufferID(unsigned i) const { return i && i <= Buffers.size(); }
106
107public:
108 /// Create new source manager without support for include files.
110 /// Create new source manager with the capability of finding include files
111 /// via the provided file system.
113 SourceMgr(const SourceMgr &) = delete;
114 SourceMgr &operator=(const SourceMgr &) = delete;
118
121
122 /// Return the include directories of this source manager.
123 ArrayRef<std::string> getIncludeDirs() const { return IncludeDirectories; }
124
125 void setIncludeDirs(const std::vector<std::string> &Dirs) {
126 IncludeDirectories = Dirs;
127 }
128
129 /// Specify a diagnostic handler to be invoked every time PrintMessage is
130 /// called. \p Ctx is passed into the handler when it is invoked.
131 void setDiagHandler(DiagHandlerTy DH, void *Ctx = nullptr) {
132 DiagHandler = DH;
133 DiagContext = Ctx;
134 }
135
136 DiagHandlerTy getDiagHandler() const { return DiagHandler; }
137 void *getDiagContext() const { return DiagContext; }
138
139 const SrcBuffer &getBufferInfo(unsigned i) const {
140 assert(isValidBufferID(i));
141 return Buffers[i - 1];
142 }
143
144 const MemoryBuffer *getMemoryBuffer(unsigned i) const {
145 assert(isValidBufferID(i));
146 return Buffers[i - 1].Buffer.get();
147 }
148
149 unsigned getNumBuffers() const { return Buffers.size(); }
150
151 unsigned getMainFileID() const {
153 return 1;
154 }
155
156 SMLoc getParentIncludeLoc(unsigned i) const {
157 assert(isValidBufferID(i));
158 return Buffers[i - 1].IncludeLoc;
159 }
160
161 /// Add a new source buffer to this source manager. This takes ownership of
162 /// the memory buffer.
163 unsigned AddNewSourceBuffer(std::unique_ptr<MemoryBuffer> F,
164 SMLoc IncludeLoc) {
165 SrcBuffer NB;
166 NB.Buffer = std::move(F);
167 NB.IncludeLoc = IncludeLoc;
168 Buffers.push_back(std::move(NB));
169 return Buffers.size();
170 }
171
172 /// Takes the source buffers from the given source manager and append them to
173 /// the current manager. `MainBufferIncludeLoc` is an optional include
174 /// location to attach to the main buffer of `SrcMgr` after it gets moved to
175 /// the current manager.
177 SMLoc MainBufferIncludeLoc = SMLoc()) {
178 if (SrcMgr.Buffers.empty())
179 return;
180
181 size_t OldNumBuffers = getNumBuffers();
182 std::move(SrcMgr.Buffers.begin(), SrcMgr.Buffers.end(),
183 std::back_inserter(Buffers));
184 SrcMgr.Buffers.clear();
185 Buffers[OldNumBuffers].IncludeLoc = MainBufferIncludeLoc;
186 }
187
188 /// Search for a file with the specified name in the current directory or in
189 /// one of the IncludeDirs.
190 ///
191 /// If no file is found, this returns 0, otherwise it returns the buffer ID
192 /// of the stacked file. The full path to the included file can be found in
193 /// \p IncludedFile.
194 LLVM_ABI unsigned AddIncludeFile(const std::string &Filename,
195 SMLoc IncludeLoc, std::string &IncludedFile);
196
197 /// Search for a file with the specified name in the current directory or in
198 /// one of the IncludeDirs, and try to open it **without** adding to the
199 /// SourceMgr. If the opened file is intended to be added to the source
200 /// manager, prefer `AddIncludeFile` instead.
201 ///
202 /// If no file is found, this returns an Error, otherwise it returns the
203 /// buffer of the stacked file. The full path to the included file can be
204 /// found in \p IncludedFile.
206 OpenIncludeFile(const std::string &Filename, std::string &IncludedFile,
207 bool RequiresNullTerminator = true);
208
209 /// Return the ID of the buffer containing the specified location.
210 ///
211 /// 0 is returned if the buffer is not found.
213
214 /// Find the line number for the specified location in the specified file.
215 /// This method has O(log n) complexity, where n is the number of lines in the
216 /// buffer.
217 unsigned FindLineNumber(SMLoc Loc, unsigned BufferID = 0) const {
218 return getLineAndColumn(Loc, BufferID).first;
219 }
220
221 /// Find the line and column number for the specified location in the
222 /// specified file. This method has O(log n) complexity, where n is the number
223 /// of lines in the buffer.
224 LLVM_ABI std::pair<unsigned, unsigned>
225 getLineAndColumn(SMLoc Loc, unsigned BufferID = 0) const;
226
227 /// Get a string with the \p SMLoc filename and line number
228 /// formatted in the standard style.
229 LLVM_ABI std::string
230 getFormattedLocationNoOffset(SMLoc Loc, bool IncludePath = false) const;
231
232 /// Given a line and column number in a mapped buffer, turn it into an SMLoc.
233 /// This will return a null SMLoc if the line/column location is invalid.
234 LLVM_ABI SMLoc FindLocForLineAndColumn(unsigned BufferID, unsigned LineNo,
235 unsigned ColNo);
236
237 /// Emit a message about the specified location with the specified string.
238 ///
239 /// \param ShowColors Display colored messages if output is a terminal and
240 /// the default error handler is used.
241 LLVM_ABI void PrintMessage(raw_ostream &OS, SMLoc Loc, DiagKind Kind,
242 const Twine &Msg, ArrayRef<SMRange> Ranges = {},
243 ArrayRef<SMFixIt> FixIts = {},
244 bool ShowColors = true) const;
245
246 /// Emits a diagnostic to llvm::errs().
247 LLVM_ABI void PrintMessage(SMLoc Loc, DiagKind Kind, const Twine &Msg,
248 ArrayRef<SMRange> Ranges = {},
249 ArrayRef<SMFixIt> FixIts = {},
250 bool ShowColors = true) const;
251
252 /// Emits a manually-constructed diagnostic to the given output stream.
253 ///
254 /// \param ShowColors Display colored messages if output is a terminal and
255 /// the default error handler is used.
256 LLVM_ABI void PrintMessage(raw_ostream &OS, const SMDiagnostic &Diagnostic,
257 bool ShowColors = true) const;
258
259 /// Return an SMDiagnostic at the specified location with the specified
260 /// string.
261 ///
262 /// \param Msg If non-null, the kind of message (e.g., "error") which is
263 /// prefixed to the message.
264 LLVM_ABI SMDiagnostic GetMessage(SMLoc Loc, DiagKind Kind, const Twine &Msg,
265 ArrayRef<SMRange> Ranges = {},
266 ArrayRef<SMFixIt> FixIts = {}) const;
267
268 /// Prints the names of included files and the line of the file they were
269 /// included from. A diagnostic handler can use this before printing its
270 /// custom formatted message.
271 ///
272 /// \param IncludeLoc The location of the include.
273 /// \param OS the raw_ostream to print on.
274 LLVM_ABI void PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const;
275};
276
277/// Represents a single fixit, a replacement of one range of text with another.
278class SMFixIt {
279 SMRange Range;
280
281 std::string Text;
282
283public:
284 LLVM_ABI SMFixIt(SMRange R, const Twine &Replacement);
285
286 SMFixIt(SMLoc Loc, const Twine &Replacement)
287 : SMFixIt(SMRange(Loc, Loc), Replacement) {}
288
289 StringRef getText() const { return Text; }
290 SMRange getRange() const { return Range; }
291
292 bool operator<(const SMFixIt &Other) const {
293 if (Range.Start.getPointer() != Other.Range.Start.getPointer())
294 return Range.Start.getPointer() < Other.Range.Start.getPointer();
295 if (Range.End.getPointer() != Other.Range.End.getPointer())
296 return Range.End.getPointer() < Other.Range.End.getPointer();
297 return Text < Other.Text;
298 }
299};
300
301/// Instances of this class encapsulate one diagnostic report, allowing
302/// printing to a raw_ostream as a caret diagnostic.
304 const SourceMgr *SM = nullptr;
305 SMLoc Loc;
306 std::string Filename;
307 int LineNo = 0;
308 int ColumnNo = 0;
310 std::string Message, LineContents;
311 std::vector<std::pair<unsigned, unsigned>> Ranges;
313
314public:
315 // Null diagnostic.
316 SMDiagnostic() = default;
317 // Diagnostic with no location (e.g. file not found, command line arg error).
319 : Filename(filename), LineNo(-1), ColumnNo(-1), Kind(Knd), Message(Msg) {}
320
321 // Diagnostic with a location.
322 LLVM_ABI SMDiagnostic(const SourceMgr &sm, SMLoc L, StringRef FN, int Line,
323 int Col, SourceMgr::DiagKind Kind, StringRef Msg,
324 StringRef LineStr,
325 ArrayRef<std::pair<unsigned, unsigned>> Ranges,
326 ArrayRef<SMFixIt> FixIts = {});
327
328 const SourceMgr *getSourceMgr() const { return SM; }
329 SMLoc getLoc() const { return Loc; }
330 StringRef getFilename() const { return Filename; }
331 int getLineNo() const { return LineNo; }
332 int getColumnNo() const { return ColumnNo; }
333 SourceMgr::DiagKind getKind() const { return Kind; }
334 StringRef getMessage() const { return Message; }
335 StringRef getLineContents() const { return LineContents; }
337
338 void addFixIt(const SMFixIt &Hint) { FixIts.push_back(Hint); }
339
340 ArrayRef<SMFixIt> getFixIts() const { return FixIts; }
341
342 LLVM_ABI void print(const char *ProgName, raw_ostream &S,
343 bool ShowColors = true, bool ShowKindLabel = true,
344 bool ShowLocation = true) const;
345};
346
347} // end namespace llvm
348
349#endif // LLVM_SUPPORT_SOURCEMGR_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition Compiler.h:213
This file defines the RefCountedBase, ThreadSafeRefCountedBase, and IntrusiveRefCntPtr classes.
#define F(x, y, z)
Definition MD5.cpp:54
static constexpr StringLiteral Filename
This file defines the SmallVector class.
static void DiagHandler(const SMDiagnostic &Diag, void *Context)
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Represents either an error or a value T.
Definition ErrorOr.h:56
A smart pointer to a reference-counted object that inherits from RefCountedBase or ThreadSafeRefCount...
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
Definition SourceMgr.h:303
LLVM_ABI void print(const char *ProgName, raw_ostream &S, bool ShowColors=true, bool ShowKindLabel=true, bool ShowLocation=true) const
SourceMgr::DiagKind getKind() const
Definition SourceMgr.h:333
StringRef getFilename() const
Definition SourceMgr.h:330
SMDiagnostic()=default
int getLineNo() const
Definition SourceMgr.h:331
SMDiagnostic(StringRef filename, SourceMgr::DiagKind Knd, StringRef Msg)
Definition SourceMgr.h:318
StringRef getLineContents() const
Definition SourceMgr.h:335
SMLoc getLoc() const
Definition SourceMgr.h:329
StringRef getMessage() const
Definition SourceMgr.h:334
ArrayRef< SMFixIt > getFixIts() const
Definition SourceMgr.h:340
ArrayRef< std::pair< unsigned, unsigned > > getRanges() const
Definition SourceMgr.h:336
void addFixIt(const SMFixIt &Hint)
Definition SourceMgr.h:338
const SourceMgr * getSourceMgr() const
Definition SourceMgr.h:328
int getColumnNo() const
Definition SourceMgr.h:332
Represents a single fixit, a replacement of one range of text with another.
Definition SourceMgr.h:278
bool operator<(const SMFixIt &Other) const
Definition SourceMgr.h:292
StringRef getText() const
Definition SourceMgr.h:289
LLVM_ABI SMFixIt(SMRange R, const Twine &Replacement)
SMFixIt(SMLoc Loc, const Twine &Replacement)
Definition SourceMgr.h:286
SMRange getRange() const
Definition SourceMgr.h:290
Represents a location in source code.
Definition SMLoc.h:22
Represents a range in source code.
Definition SMLoc.h:47
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.
Definition SourceMgr.h:37
void * getDiagContext() const
Definition SourceMgr.h:137
ArrayRef< std::string > getIncludeDirs() const
Return the include directories of this source manager.
Definition SourceMgr.h:123
unsigned getMainFileID() const
Definition SourceMgr.h:151
DiagHandlerTy getDiagHandler() const
Definition SourceMgr.h:136
SourceMgr & operator=(const SourceMgr &)=delete
void setIncludeDirs(const std::vector< std::string > &Dirs)
Definition SourceMgr.h:125
LLVM_ABI 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.
LLVM_ABI void setVirtualFileSystem(IntrusiveRefCntPtr< vfs::FileSystem > FS)
Definition SourceMgr.cpp:54
const MemoryBuffer * getMemoryBuffer(unsigned i) const
Definition SourceMgr.h:144
unsigned getNumBuffers() const
Definition SourceMgr.h:149
LLVM_ABI void PrintMessage(raw_ostream &OS, SMLoc Loc, DiagKind Kind, const Twine &Msg, ArrayRef< SMRange > Ranges={}, ArrayRef< SMFixIt > FixIts={}, bool ShowColors=true) const
Emit a message about the specified location with the specified string.
SMLoc getParentIncludeLoc(unsigned i) const
Definition SourceMgr.h:156
LLVM_ABI void PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const
Prints the names of included files and the line of the file they were included from.
LLVM_ABI SourceMgr()
Create new source manager without support for include files.
LLVM_ABI unsigned FindBufferContainingLoc(SMLoc Loc) const
Return the ID of the buffer containing the specified location.
Definition SourceMgr.cpp:97
LLVM_ABI IntrusiveRefCntPtr< vfs::FileSystem > getVirtualFileSystem() const
Definition SourceMgr.cpp:50
LLVM_ABI SourceMgr & operator=(SourceMgr &&)
LLVM_ABI ErrorOr< std::unique_ptr< MemoryBuffer > > OpenIncludeFile(const std::string &Filename, std::string &IncludedFile, bool RequiresNullTerminator=true)
Search for a file with the specified name in the current directory or in one of the IncludeDirs,...
Definition SourceMgr.cpp:70
LLVM_ABI SMDiagnostic GetMessage(SMLoc Loc, DiagKind Kind, const Twine &Msg, ArrayRef< SMRange > Ranges={}, ArrayRef< SMFixIt > FixIts={}) const
Return an SMDiagnostic at the specified location with the specified string.
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:49
void setDiagHandler(DiagHandlerTy DH, void *Ctx=nullptr)
Specify a diagnostic handler to be invoked every time PrintMessage is called.
Definition SourceMgr.h:131
LLVM_ABI 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:58
SourceMgr(const SourceMgr &)=delete
unsigned FindLineNumber(SMLoc Loc, unsigned BufferID=0) const
Find the line number for the specified location in the specified file.
Definition SourceMgr.h:217
LLVM_ABI std::string getFormattedLocationNoOffset(SMLoc Loc, bool IncludePath=false) const
Get a string with the SMLoc filename and line number formatted in the standard style.
void takeSourceBuffersFrom(SourceMgr &SrcMgr, SMLoc MainBufferIncludeLoc=SMLoc())
Takes the source buffers from the given source manager and append them to the current manager.
Definition SourceMgr.h:176
LLVM_ABI ~SourceMgr()
LLVM_ABI SourceMgr(SourceMgr &&)
unsigned AddNewSourceBuffer(std::unique_ptr< MemoryBuffer > F, SMLoc IncludeLoc)
Add a new source buffer to this source manager.
Definition SourceMgr.h:163
LLVM_ABI SMLoc FindLocForLineAndColumn(unsigned BufferID, unsigned LineNo, unsigned ColNo)
Given a line and column number in a mapped buffer, turn it into an SMLoc.
const SrcBuffer & getBufferInfo(unsigned i) const
Definition SourceMgr.h:139
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
The virtual file system interface.
This is an optimization pass for GlobalISel generic memory operations.
SourceMgr SrcMgr
Definition Error.cpp:24
@ Other
Any other memory.
Definition ModRef.h:68
ArrayRef(const T &OneElt) -> ArrayRef< T >