SourceMgr.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. #ifndef LLVM_SUPPORT_SOURCEMGR_H
  16. #define LLVM_SUPPORT_SOURCEMGR_H
  17. #include "llvm/ADT/ArrayRef.h"
  18. #include "llvm/ADT/StringRef.h"
  19. #include "llvm/ADT/Twine.h"
  20. #include "llvm/Support/MemoryBuffer.h"
  21. #include "llvm/Support/SMLoc.h"
  22. #include <string>
  23. namespace llvm {
  24. class SourceMgr;
  25. class SMDiagnostic;
  26. class SMFixIt;
  27. class Twine;
  28. class raw_ostream;
  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 {
  34. DK_Error,
  35. DK_Warning,
  36. DK_Note
  37. };
  38. /// Clients that want to handle their own diagnostics in a custom way can
  39. /// register a function pointer+context as a diagnostic handler.
  40. /// It gets called each time PrintMessage is invoked.
  41. typedef void (*DiagHandlerTy)(const SMDiagnostic &, void *Context);
  42. private:
  43. struct SrcBuffer {
  44. /// The memory buffer for the file.
  45. std::unique_ptr<MemoryBuffer> Buffer;
  46. /// This is the location of the parent include, or null if at the top level.
  47. SMLoc IncludeLoc;
  48. SrcBuffer() {}
  49. SrcBuffer(SrcBuffer &&O)
  50. : Buffer(std::move(O.Buffer)), IncludeLoc(O.IncludeLoc) {}
  51. };
  52. /// This is all of the buffers that we are reading from.
  53. std::vector<SrcBuffer> Buffers;
  54. // This is the list of directories we should search for include files in.
  55. std::vector<std::string> IncludeDirectories;
  56. /// This is a cache for line number queries, its implementation is really
  57. /// private to SourceMgr.cpp.
  58. mutable void *LineNoCache;
  59. DiagHandlerTy DiagHandler;
  60. void *DiagContext;
  61. bool isValidBufferID(unsigned i) const { return i && i <= Buffers.size(); }
  62. SourceMgr(const SourceMgr&) = delete;
  63. void operator=(const SourceMgr&) = delete;
  64. public:
  65. SourceMgr()
  66. : LineNoCache(nullptr), DiagHandler(nullptr), DiagContext(nullptr) {}
  67. ~SourceMgr();
  68. void Reset(); // HLSL Change - add a Reset version to clean up explicitly
  69. void setIncludeDirs(const std::vector<std::string> &Dirs) {
  70. IncludeDirectories = Dirs;
  71. }
  72. /// Specify a diagnostic handler to be invoked every time PrintMessage is
  73. /// called. \p Ctx is passed into the handler when it is invoked.
  74. void setDiagHandler(DiagHandlerTy DH, void *Ctx = nullptr) {
  75. DiagHandler = DH;
  76. DiagContext = Ctx;
  77. }
  78. DiagHandlerTy getDiagHandler() const { return DiagHandler; }
  79. void *getDiagContext() const { return DiagContext; }
  80. const SrcBuffer &getBufferInfo(unsigned i) const {
  81. assert(isValidBufferID(i));
  82. return Buffers[i - 1];
  83. }
  84. const MemoryBuffer *getMemoryBuffer(unsigned i) const {
  85. assert(isValidBufferID(i));
  86. return Buffers[i - 1].Buffer.get();
  87. }
  88. unsigned getNumBuffers() const {
  89. return Buffers.size();
  90. }
  91. unsigned getMainFileID() const {
  92. assert(getNumBuffers());
  93. return 1;
  94. }
  95. SMLoc getParentIncludeLoc(unsigned i) const {
  96. assert(isValidBufferID(i));
  97. return Buffers[i - 1].IncludeLoc;
  98. }
  99. /// Add a new source buffer to this source manager. This takes ownership of
  100. /// the memory buffer.
  101. unsigned AddNewSourceBuffer(std::unique_ptr<MemoryBuffer> F,
  102. SMLoc IncludeLoc) {
  103. SrcBuffer NB;
  104. NB.Buffer = std::move(F);
  105. NB.IncludeLoc = IncludeLoc;
  106. Buffers.push_back(std::move(NB));
  107. return Buffers.size();
  108. }
  109. /// Search for a file with the specified name in the current directory or in
  110. /// one of the IncludeDirs.
  111. ///
  112. /// If no file is found, this returns 0, otherwise it returns the buffer ID
  113. /// of the stacked file. The full path to the included file can be found in
  114. /// \p IncludedFile.
  115. unsigned AddIncludeFile(const std::string &Filename, SMLoc IncludeLoc,
  116. std::string &IncludedFile);
  117. /// Return the ID of the buffer containing the specified location.
  118. ///
  119. /// 0 is returned if the buffer is not found.
  120. unsigned FindBufferContainingLoc(SMLoc Loc) const;
  121. /// Find the line number for the specified location in the specified file.
  122. /// This is not a fast method.
  123. unsigned FindLineNumber(SMLoc Loc, unsigned BufferID = 0) const {
  124. return getLineAndColumn(Loc, BufferID).first;
  125. }
  126. /// Find the line and column number for the specified location in the
  127. /// specified file. This is not a fast method.
  128. std::pair<unsigned, unsigned> getLineAndColumn(SMLoc Loc,
  129. unsigned BufferID = 0) const;
  130. /// Emit a message about the specified location with the specified string.
  131. ///
  132. /// \param ShowColors Display colored messages if output is a terminal and
  133. /// the default error handler is used.
  134. void PrintMessage(raw_ostream &OS, SMLoc Loc, DiagKind Kind,
  135. const Twine &Msg,
  136. ArrayRef<SMRange> Ranges = None,
  137. ArrayRef<SMFixIt> FixIts = None,
  138. bool ShowColors = true) const;
  139. /// Emits a diagnostic to llvm::errs().
  140. void PrintMessage(SMLoc Loc, DiagKind Kind, const Twine &Msg,
  141. ArrayRef<SMRange> Ranges = None,
  142. ArrayRef<SMFixIt> FixIts = None,
  143. bool ShowColors = true) const;
  144. /// Emits a manually-constructed diagnostic to the given output stream.
  145. ///
  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, const SMDiagnostic &Diagnostic,
  149. bool ShowColors = true) const;
  150. /// Return an SMDiagnostic at the specified location with the specified
  151. /// string.
  152. ///
  153. /// \param Msg If non-null, the kind of message (e.g., "error") which is
  154. /// prefixed to the message.
  155. SMDiagnostic GetMessage(SMLoc Loc, DiagKind Kind, const Twine &Msg,
  156. ArrayRef<SMRange> Ranges = None,
  157. ArrayRef<SMFixIt> FixIts = None) const;
  158. /// Prints the names of included files and the line of the file they were
  159. /// included from. A diagnostic handler can use this before printing its
  160. /// custom formatted message.
  161. ///
  162. /// \param IncludeLoc The location of the include.
  163. /// \param OS the raw_ostream to print on.
  164. void PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const;
  165. };
  166. /// Represents a single fixit, a replacement of one range of text with another.
  167. class SMFixIt {
  168. SMRange Range;
  169. std::string Text;
  170. public:
  171. // FIXME: Twine.str() is not very efficient.
  172. SMFixIt(SMLoc Loc, const Twine &Insertion)
  173. : Range(Loc, Loc), Text(Insertion.str()) {
  174. assert(Loc.isValid());
  175. }
  176. // FIXME: Twine.str() is not very efficient.
  177. SMFixIt(SMRange R, const Twine &Replacement)
  178. : Range(R), Text(Replacement.str()) {
  179. assert(R.isValid());
  180. }
  181. StringRef getText() const { return Text; }
  182. SMRange getRange() const { return Range; }
  183. bool operator<(const SMFixIt &Other) const {
  184. if (Range.Start.getPointer() != Other.Range.Start.getPointer())
  185. return Range.Start.getPointer() < Other.Range.Start.getPointer();
  186. if (Range.End.getPointer() != Other.Range.End.getPointer())
  187. return Range.End.getPointer() < Other.Range.End.getPointer();
  188. return Text < Other.Text;
  189. }
  190. };
  191. /// Instances of this class encapsulate one diagnostic report, allowing
  192. /// printing to a raw_ostream as a caret diagnostic.
  193. class SMDiagnostic {
  194. const SourceMgr *SM;
  195. SMLoc Loc;
  196. std::string Filename;
  197. int LineNo, ColumnNo;
  198. SourceMgr::DiagKind Kind;
  199. std::string Message, LineContents;
  200. std::vector<std::pair<unsigned, unsigned> > Ranges;
  201. SmallVector<SMFixIt, 4> FixIts;
  202. public:
  203. // Null diagnostic.
  204. SMDiagnostic()
  205. : SM(nullptr), LineNo(0), ColumnNo(0), Kind(SourceMgr::DK_Error) {}
  206. // Diagnostic with no location (e.g. file not found, command line arg error).
  207. SMDiagnostic(StringRef filename, SourceMgr::DiagKind Knd, StringRef Msg)
  208. : SM(nullptr), Filename(filename), LineNo(-1), ColumnNo(-1), Kind(Knd),
  209. Message(Msg) {}
  210. // Diagnostic with a location.
  211. SMDiagnostic(const SourceMgr &sm, SMLoc L, StringRef FN,
  212. int Line, int Col, SourceMgr::DiagKind Kind,
  213. StringRef Msg, StringRef LineStr,
  214. ArrayRef<std::pair<unsigned,unsigned> > Ranges,
  215. ArrayRef<SMFixIt> FixIts = None);
  216. const SourceMgr *getSourceMgr() const { return SM; }
  217. SMLoc getLoc() const { return Loc; }
  218. StringRef getFilename() const { return Filename; }
  219. int getLineNo() const { return LineNo; }
  220. int getColumnNo() const { return ColumnNo; }
  221. SourceMgr::DiagKind getKind() const { return Kind; }
  222. StringRef getMessage() const { return Message; }
  223. StringRef getLineContents() const { return LineContents; }
  224. ArrayRef<std::pair<unsigned, unsigned> > getRanges() const {
  225. return Ranges;
  226. }
  227. void addFixIt(const SMFixIt &Hint) {
  228. FixIts.push_back(Hint);
  229. }
  230. ArrayRef<SMFixIt> getFixIts() const {
  231. return FixIts;
  232. }
  233. void print(const char *ProgName, raw_ostream &S, bool ShowColors = true,
  234. bool ShowKindLabel = true) const;
  235. };
  236. } // end llvm namespace
  237. #endif