LLVM: include/llvm/Support/SourceMgr.h Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#ifndef LLVM_SUPPORT_SOURCEMGR_H
16#define LLVM_SUPPORT_SOURCEMGR_H
17
21#include
22
23namespace llvm {
24
25class raw_ostream;
26class SMDiagnostic;
27class SMFixIt;
28
29
30
32public:
38 };
39
40
41
42
44
45private:
46 struct SrcBuffer {
47
48 std::unique_ptr Buffer;
49
50
51
52
53
54
55
56
57
58
59
60 mutable void *OffsetCache = nullptr;
61
62
63
64 unsigned getLineNumber(const char *Ptr) const;
65 template
66 unsigned getLineNumberSpecialized(const char *Ptr) const;
67
68
69
70 const char *getPointerForLineNumber(unsigned LineNo) const;
71 template
72 const char *getPointerForLineNumberSpecialized(unsigned LineNo) const;
73
74
76
77 SrcBuffer() = default;
78 SrcBuffer(SrcBuffer &&);
79 SrcBuffer(const SrcBuffer &) = delete;
80 SrcBuffer &operator=(const SrcBuffer &) = delete;
81 ~SrcBuffer();
82 };
83
84
85 std::vector Buffers;
86
87
88 std::vectorstd::string IncludeDirectories;
89
91 void *DiagContext = nullptr;
92
93 bool isValidBufferID(unsigned i) const { return i && i <= Buffers.size(); }
94
95public:
102
103
105
107 IncludeDirectories = Dirs;
108 }
109
110
111
113 DiagHandler = DH;
114 DiagContext = Ctx;
115 }
116
119
121 assert(isValidBufferID(i));
122 return Buffers[i - 1];
123 }
124
126 assert(isValidBufferID(i));
127 return Buffers[i - 1].Buffer.get();
128 }
129
131
134 return 1;
135 }
136
138 assert(isValidBufferID(i));
139 return Buffers[i - 1].IncludeLoc;
140 }
141
142
143
145 SMLoc IncludeLoc) {
146 SrcBuffer NB;
147 NB.Buffer = std::move(F);
148 NB.IncludeLoc = IncludeLoc;
149 Buffers.push_back(std::move(NB));
150 return Buffers.size();
151 }
152
153
154
155
156
158 SMLoc MainBufferIncludeLoc = SMLoc()) {
159 if (SrcMgr.Buffers.empty())
160 return;
161
163 std::move(SrcMgr.Buffers.begin(), SrcMgr.Buffers.end(),
164 std::back_inserter(Buffers));
165 SrcMgr.Buffers.clear();
166 Buffers[OldNumBuffers].IncludeLoc = MainBufferIncludeLoc;
167 }
168
169
170
171
172
173
174
176 std::string &IncludedFile);
177
178
179
180
181
182
183
184
185
187 OpenIncludeFile(const std::string &Filename, std::string &IncludedFile);
188
189
190
191
193
194
195
198 }
199
200
201
203 unsigned BufferID = 0) const;
204
205
206
208 bool IncludePath = false) const;
209
210
211
213 unsigned ColNo);
214
215
216
217
218
221 ArrayRef FixIts = {},
222 bool ShowColors = true) const;
223
224
226 ArrayRef Ranges = {},
227 ArrayRef FixIts = {},
228 bool ShowColors = true) const;
229
230
231
232
233
234 void PrintMessage(raw_ostream &OS, const SMDiagnostic &Diagnostic,
235 bool ShowColors = true) const;
236
237
238
239
240
241
243 ArrayRef Ranges = {},
244 ArrayRef FixIts = {}) const;
245
246
247
248
249
250
251
253};
254
255
258
259 std::string Text;
260
261public:
263
266
269
271 if (Range.Start.getPointer() != Other.Range.Start.getPointer())
272 return Range.Start.getPointer() < Other.Range.Start.getPointer();
273 if (Range.End.getPointer() != Other.Range.End.getPointer())
274 return Range.End.getPointer() < Other.Range.End.getPointer();
275 return Text < Other.Text;
276 }
277};
278
279
280
284 std::string Filename;
285 int LineNo = 0;
286 int ColumnNo = 0;
288 std::string Message, LineContents;
289 std::vector<std::pair<unsigned, unsigned>> Ranges;
291
292public:
293
295
297 : Filename(filename), LineNo(-1), ColumnNo(-1), Kind(Knd), Message(Msg) {}
298
299
302 ArrayRef<std::pair<unsigned, unsigned>> Ranges,
304
314
316
318
319 void print(const char *ProgName, raw_ostream &S, bool ShowColors = true,
320 bool ShowKindLabel = true, bool ShowLocation = true) const;
321};
322
323}
324
325#endif
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallVector class.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Represents either an error or a value T.
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...
void print(const char *ProgName, raw_ostream &S, bool ShowColors=true, bool ShowKindLabel=true, bool ShowLocation=true) const
SourceMgr::DiagKind getKind() const
StringRef getFilename() const
SMDiagnostic(StringRef filename, SourceMgr::DiagKind Knd, StringRef Msg)
StringRef getLineContents() const
StringRef getMessage() const
ArrayRef< SMFixIt > getFixIts() const
ArrayRef< std::pair< unsigned, unsigned > > getRanges() const
void addFixIt(const SMFixIt &Hint)
const SourceMgr * getSourceMgr() const
Represents a single fixit, a replacement of one range of text with another.
bool operator<(const SMFixIt &Other) const
StringRef getText() const
SMFixIt(SMLoc Loc, const Twine &Replacement)
Represents a location in source code.
Represents a range in source code.
void push_back(const T &Elt)
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.
ErrorOr< std::unique_ptr< MemoryBuffer > > OpenIncludeFile(const std::string &Filename, std::string &IncludedFile)
Search for a file with the specified name in the current directory or in one of the IncludeDirs,...
SourceMgr & operator=(SourceMgr &&)=default
void * getDiagContext() const
SourceMgr(SourceMgr &&)=default
ArrayRef< std::string > getIncludeDirs() const
Return the include directories of this source manager.
unsigned getMainFileID() const
DiagHandlerTy getDiagHandler() const
SourceMgr & operator=(const SourceMgr &)=delete
void setIncludeDirs(const std::vector< std::string > &Dirs)
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.
const MemoryBuffer * getMemoryBuffer(unsigned i) const
unsigned getNumBuffers() const
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
void PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const
Prints the names of included files and the line of the file they were included from.
unsigned FindBufferContainingLoc(SMLoc Loc) const
Return the ID of the buffer containing the specified location.
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...
void setDiagHandler(DiagHandlerTy DH, void *Ctx=nullptr)
Specify a diagnostic handler to be invoked every time PrintMessage is called.
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.
SourceMgr(const SourceMgr &)=delete
unsigned FindLineNumber(SMLoc Loc, unsigned BufferID=0) const
Find the line number for the specified location in the specified file.
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.
unsigned AddNewSourceBuffer(std::unique_ptr< MemoryBuffer > F, SMLoc IncludeLoc)
Add a new source buffer to this source manager.
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
StringRef - Represent a constant reference to a string, i.e.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
This class implements an extremely fast bulk output stream that can only output to a stream.
This is an optimization pass for GlobalISel generic memory operations.