Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:34

0001 //===- SourceMgr.h - Manager for Source Buffers & Diagnostics ---*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file declares the SMDiagnostic and SourceMgr classes.  This
0010 // provides a simple substrate for diagnostics, #include handling, and other low
0011 // level things for simple parsers.
0012 //
0013 //===----------------------------------------------------------------------===//
0014 
0015 #ifndef LLVM_SUPPORT_SOURCEMGR_H
0016 #define LLVM_SUPPORT_SOURCEMGR_H
0017 
0018 #include "llvm/ADT/SmallVector.h"
0019 #include "llvm/Support/MemoryBuffer.h"
0020 #include "llvm/Support/SMLoc.h"
0021 #include <vector>
0022 
0023 namespace llvm {
0024 
0025 class raw_ostream;
0026 class SMDiagnostic;
0027 class SMFixIt;
0028 
0029 /// This owns the files read by a parser, handles include stacks,
0030 /// and handles diagnostic wrangling.
0031 class SourceMgr {
0032 public:
0033   enum DiagKind {
0034     DK_Error,
0035     DK_Warning,
0036     DK_Remark,
0037     DK_Note,
0038   };
0039 
0040   /// Clients that want to handle their own diagnostics in a custom way can
0041   /// register a function pointer+context as a diagnostic handler.
0042   /// It gets called each time PrintMessage is invoked.
0043   using DiagHandlerTy = void (*)(const SMDiagnostic &, void *Context);
0044 
0045 private:
0046   struct SrcBuffer {
0047     /// The memory buffer for the file.
0048     std::unique_ptr<MemoryBuffer> Buffer;
0049 
0050     /// Vector of offsets into Buffer at which there are line-endings
0051     /// (lazily populated). Once populated, the '\n' that marks the end of
0052     /// line number N from [1..] is at Buffer[OffsetCache[N-1]]. Since
0053     /// these offsets are in sorted (ascending) order, they can be
0054     /// binary-searched for the first one after any given offset (eg. an
0055     /// offset corresponding to a particular SMLoc).
0056     ///
0057     /// Since we're storing offsets into relatively small files (often smaller
0058     /// than 2^8 or 2^16 bytes), we select the offset vector element type
0059     /// dynamically based on the size of Buffer.
0060     mutable void *OffsetCache = nullptr;
0061 
0062     /// Look up a given \p Ptr in the buffer, determining which line it came
0063     /// from.
0064     unsigned getLineNumber(const char *Ptr) const;
0065     template <typename T>
0066     unsigned getLineNumberSpecialized(const char *Ptr) const;
0067 
0068     /// Return a pointer to the first character of the specified line number or
0069     /// null if the line number is invalid.
0070     const char *getPointerForLineNumber(unsigned LineNo) const;
0071     template <typename T>
0072     const char *getPointerForLineNumberSpecialized(unsigned LineNo) const;
0073 
0074     /// This is the location of the parent include, or null if at the top level.
0075     SMLoc IncludeLoc;
0076 
0077     SrcBuffer() = default;
0078     SrcBuffer(SrcBuffer &&);
0079     SrcBuffer(const SrcBuffer &) = delete;
0080     SrcBuffer &operator=(const SrcBuffer &) = delete;
0081     ~SrcBuffer();
0082   };
0083 
0084   /// This is all of the buffers that we are reading from.
0085   std::vector<SrcBuffer> Buffers;
0086 
0087   // This is the list of directories we should search for include files in.
0088   std::vector<std::string> IncludeDirectories;
0089 
0090   DiagHandlerTy DiagHandler = nullptr;
0091   void *DiagContext = nullptr;
0092 
0093   bool isValidBufferID(unsigned i) const { return i && i <= Buffers.size(); }
0094 
0095 public:
0096   SourceMgr() = default;
0097   SourceMgr(const SourceMgr &) = delete;
0098   SourceMgr &operator=(const SourceMgr &) = delete;
0099   SourceMgr(SourceMgr &&) = default;
0100   SourceMgr &operator=(SourceMgr &&) = default;
0101   ~SourceMgr() = default;
0102 
0103   /// Return the include directories of this source manager.
0104   ArrayRef<std::string> getIncludeDirs() const { return IncludeDirectories; }
0105 
0106   void setIncludeDirs(const std::vector<std::string> &Dirs) {
0107     IncludeDirectories = Dirs;
0108   }
0109 
0110   /// Specify a diagnostic handler to be invoked every time PrintMessage is
0111   /// called. \p Ctx is passed into the handler when it is invoked.
0112   void setDiagHandler(DiagHandlerTy DH, void *Ctx = nullptr) {
0113     DiagHandler = DH;
0114     DiagContext = Ctx;
0115   }
0116 
0117   DiagHandlerTy getDiagHandler() const { return DiagHandler; }
0118   void *getDiagContext() const { return DiagContext; }
0119 
0120   const SrcBuffer &getBufferInfo(unsigned i) const {
0121     assert(isValidBufferID(i));
0122     return Buffers[i - 1];
0123   }
0124 
0125   const MemoryBuffer *getMemoryBuffer(unsigned i) const {
0126     assert(isValidBufferID(i));
0127     return Buffers[i - 1].Buffer.get();
0128   }
0129 
0130   unsigned getNumBuffers() const { return Buffers.size(); }
0131 
0132   unsigned getMainFileID() const {
0133     assert(getNumBuffers());
0134     return 1;
0135   }
0136 
0137   SMLoc getParentIncludeLoc(unsigned i) const {
0138     assert(isValidBufferID(i));
0139     return Buffers[i - 1].IncludeLoc;
0140   }
0141 
0142   /// Add a new source buffer to this source manager. This takes ownership of
0143   /// the memory buffer.
0144   unsigned AddNewSourceBuffer(std::unique_ptr<MemoryBuffer> F,
0145                               SMLoc IncludeLoc) {
0146     SrcBuffer NB;
0147     NB.Buffer = std::move(F);
0148     NB.IncludeLoc = IncludeLoc;
0149     Buffers.push_back(std::move(NB));
0150     return Buffers.size();
0151   }
0152 
0153   /// Takes the source buffers from the given source manager and append them to
0154   /// the current manager. `MainBufferIncludeLoc` is an optional include
0155   /// location to attach to the main buffer of `SrcMgr` after it gets moved to
0156   /// the current manager.
0157   void takeSourceBuffersFrom(SourceMgr &SrcMgr,
0158                              SMLoc MainBufferIncludeLoc = SMLoc()) {
0159     if (SrcMgr.Buffers.empty())
0160       return;
0161 
0162     size_t OldNumBuffers = getNumBuffers();
0163     std::move(SrcMgr.Buffers.begin(), SrcMgr.Buffers.end(),
0164               std::back_inserter(Buffers));
0165     SrcMgr.Buffers.clear();
0166     Buffers[OldNumBuffers].IncludeLoc = MainBufferIncludeLoc;
0167   }
0168 
0169   /// Search for a file with the specified name in the current directory or in
0170   /// one of the IncludeDirs.
0171   ///
0172   /// If no file is found, this returns 0, otherwise it returns the buffer ID
0173   /// of the stacked file. The full path to the included file can be found in
0174   /// \p IncludedFile.
0175   unsigned AddIncludeFile(const std::string &Filename, SMLoc IncludeLoc,
0176                           std::string &IncludedFile);
0177 
0178   /// Search for a file with the specified name in the current directory or in
0179   /// one of the IncludeDirs, and try to open it **without** adding to the
0180   /// SourceMgr. If the opened file is intended to be added to the source
0181   /// manager, prefer `AddIncludeFile` instead.
0182   ///
0183   /// If no file is found, this returns an Error, otherwise it returns the
0184   /// buffer of the stacked file. The full path to the included file can be
0185   /// found in \p IncludedFile.
0186   ErrorOr<std::unique_ptr<MemoryBuffer>>
0187   OpenIncludeFile(const std::string &Filename, std::string &IncludedFile);
0188 
0189   /// Return the ID of the buffer containing the specified location.
0190   ///
0191   /// 0 is returned if the buffer is not found.
0192   unsigned FindBufferContainingLoc(SMLoc Loc) const;
0193 
0194   /// Find the line number for the specified location in the specified file.
0195   /// This is not a fast method.
0196   unsigned FindLineNumber(SMLoc Loc, unsigned BufferID = 0) const {
0197     return getLineAndColumn(Loc, BufferID).first;
0198   }
0199 
0200   /// Find the line and column number for the specified location in the
0201   /// specified file. This is not a fast method.
0202   std::pair<unsigned, unsigned> getLineAndColumn(SMLoc Loc,
0203                                                  unsigned BufferID = 0) const;
0204 
0205   /// Get a string with the \p SMLoc filename and line number
0206   /// formatted in the standard style.
0207   std::string getFormattedLocationNoOffset(SMLoc Loc,
0208                                            bool IncludePath = false) const;
0209 
0210   /// Given a line and column number in a mapped buffer, turn it into an SMLoc.
0211   /// This will return a null SMLoc if the line/column location is invalid.
0212   SMLoc FindLocForLineAndColumn(unsigned BufferID, unsigned LineNo,
0213                                 unsigned ColNo);
0214 
0215   /// Emit a message about the specified location with the specified string.
0216   ///
0217   /// \param ShowColors Display colored messages if output is a terminal and
0218   /// the default error handler is used.
0219   void PrintMessage(raw_ostream &OS, SMLoc Loc, DiagKind Kind, const Twine &Msg,
0220                     ArrayRef<SMRange> Ranges = {},
0221                     ArrayRef<SMFixIt> FixIts = {},
0222                     bool ShowColors = true) const;
0223 
0224   /// Emits a diagnostic to llvm::errs().
0225   void PrintMessage(SMLoc Loc, DiagKind Kind, const Twine &Msg,
0226                     ArrayRef<SMRange> Ranges = {},
0227                     ArrayRef<SMFixIt> FixIts = {},
0228                     bool ShowColors = true) const;
0229 
0230   /// Emits a manually-constructed diagnostic to the given output stream.
0231   ///
0232   /// \param ShowColors Display colored messages if output is a terminal and
0233   /// the default error handler is used.
0234   void PrintMessage(raw_ostream &OS, const SMDiagnostic &Diagnostic,
0235                     bool ShowColors = true) const;
0236 
0237   /// Return an SMDiagnostic at the specified location with the specified
0238   /// string.
0239   ///
0240   /// \param Msg If non-null, the kind of message (e.g., "error") which is
0241   /// prefixed to the message.
0242   SMDiagnostic GetMessage(SMLoc Loc, DiagKind Kind, const Twine &Msg,
0243                           ArrayRef<SMRange> Ranges = {},
0244                           ArrayRef<SMFixIt> FixIts = {}) const;
0245 
0246   /// Prints the names of included files and the line of the file they were
0247   /// included from. A diagnostic handler can use this before printing its
0248   /// custom formatted message.
0249   ///
0250   /// \param IncludeLoc The location of the include.
0251   /// \param OS the raw_ostream to print on.
0252   void PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const;
0253 };
0254 
0255 /// Represents a single fixit, a replacement of one range of text with another.
0256 class SMFixIt {
0257   SMRange Range;
0258 
0259   std::string Text;
0260 
0261 public:
0262   SMFixIt(SMRange R, const Twine &Replacement);
0263 
0264   SMFixIt(SMLoc Loc, const Twine &Replacement)
0265       : SMFixIt(SMRange(Loc, Loc), Replacement) {}
0266 
0267   StringRef getText() const { return Text; }
0268   SMRange getRange() const { return Range; }
0269 
0270   bool operator<(const SMFixIt &Other) const {
0271     if (Range.Start.getPointer() != Other.Range.Start.getPointer())
0272       return Range.Start.getPointer() < Other.Range.Start.getPointer();
0273     if (Range.End.getPointer() != Other.Range.End.getPointer())
0274       return Range.End.getPointer() < Other.Range.End.getPointer();
0275     return Text < Other.Text;
0276   }
0277 };
0278 
0279 /// Instances of this class encapsulate one diagnostic report, allowing
0280 /// printing to a raw_ostream as a caret diagnostic.
0281 class SMDiagnostic {
0282   const SourceMgr *SM = nullptr;
0283   SMLoc Loc;
0284   std::string Filename;
0285   int LineNo = 0;
0286   int ColumnNo = 0;
0287   SourceMgr::DiagKind Kind = SourceMgr::DK_Error;
0288   std::string Message, LineContents;
0289   std::vector<std::pair<unsigned, unsigned>> Ranges;
0290   SmallVector<SMFixIt, 4> FixIts;
0291 
0292 public:
0293   // Null diagnostic.
0294   SMDiagnostic() = default;
0295   // Diagnostic with no location (e.g. file not found, command line arg error).
0296   SMDiagnostic(StringRef filename, SourceMgr::DiagKind Knd, StringRef Msg)
0297       : Filename(filename), LineNo(-1), ColumnNo(-1), Kind(Knd), Message(Msg) {}
0298 
0299   // Diagnostic with a location.
0300   SMDiagnostic(const SourceMgr &sm, SMLoc L, StringRef FN, int Line, int Col,
0301                SourceMgr::DiagKind Kind, StringRef Msg, StringRef LineStr,
0302                ArrayRef<std::pair<unsigned, unsigned>> Ranges,
0303                ArrayRef<SMFixIt> FixIts = {});
0304 
0305   const SourceMgr *getSourceMgr() const { return SM; }
0306   SMLoc getLoc() const { return Loc; }
0307   StringRef getFilename() const { return Filename; }
0308   int getLineNo() const { return LineNo; }
0309   int getColumnNo() const { return ColumnNo; }
0310   SourceMgr::DiagKind getKind() const { return Kind; }
0311   StringRef getMessage() const { return Message; }
0312   StringRef getLineContents() const { return LineContents; }
0313   ArrayRef<std::pair<unsigned, unsigned>> getRanges() const { return Ranges; }
0314 
0315   void addFixIt(const SMFixIt &Hint) { FixIts.push_back(Hint); }
0316 
0317   ArrayRef<SMFixIt> getFixIts() const { return FixIts; }
0318 
0319   void print(const char *ProgName, raw_ostream &S, bool ShowColors = true,
0320              bool ShowKindLabel = true, bool ShowLocation = true) const;
0321 };
0322 
0323 } // end namespace llvm
0324 
0325 #endif // LLVM_SUPPORT_SOURCEMGR_H