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