1 //===- SourceMgr.h - Manager for Source Buffers & Diagnostics ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the SourceMgr class. This class is used as a simple
11 // substrate for diagnostics, #include handling, and other low level things for
14 //===----------------------------------------------------------------------===//
16 #ifndef SUPPORT_SOURCEMGR_H
17 #define SUPPORT_SOURCEMGR_H
31 SMLoc(const SMLoc &RHS) : Ptr(RHS.Ptr) {}
33 bool isValid() const { return Ptr != 0; }
35 bool operator==(const SMLoc &RHS) const { return RHS.Ptr == Ptr; }
36 bool operator!=(const SMLoc &RHS) const { return RHS.Ptr != Ptr; }
38 const char *getPointer() const { return Ptr; }
40 static SMLoc getFromPointer(const char *Ptr) {
47 /// SourceMgr - This owns the files read by tblgen, handles include stacks,
48 /// and handles printing of diagnostics.
51 /// Buffer - The memory buffer for the file.
54 /// IncludeLoc - This is the location of the parent include, or null if at
59 /// Buffers - This is all of the buffers that we are reading from.
60 std::vector<SrcBuffer> Buffers;
62 // IncludeDirectories - This is the list of directories we should search for
64 std::vector<std::string> IncludeDirectories;
66 SourceMgr(const SourceMgr&); // DO NOT IMPLEMENT
67 void operator=(const SourceMgr&); // DO NOT IMPLEMENT
72 void setIncludeDirs(const std::vector<std::string> &Dirs) {
73 IncludeDirectories = Dirs;
76 const SrcBuffer &getBufferInfo(unsigned i) const {
77 assert(i < Buffers.size() && "Invalid Buffer ID!");
81 const MemoryBuffer *getMemoryBuffer(unsigned i) const {
82 assert(i < Buffers.size() && "Invalid Buffer ID!");
83 return Buffers[i].Buffer;
86 SMLoc getParentIncludeLoc(unsigned i) const {
87 assert(i < Buffers.size() && "Invalid Buffer ID!");
88 return Buffers[i].IncludeLoc;
91 unsigned AddNewSourceBuffer(MemoryBuffer *F, SMLoc IncludeLoc) {
94 NB.IncludeLoc = IncludeLoc;
95 Buffers.push_back(NB);
96 return Buffers.size()-1;
99 /// AddIncludeFile - Search for a file with the specified name in the current
100 /// directory or in one of the IncludeDirs. If no file is found, this returns
101 /// ~0, otherwise it returns the buffer ID of the stacked file.
102 unsigned AddIncludeFile(const std::string &Filename, SMLoc IncludeLoc);
104 /// FindBufferContainingLoc - Return the ID of the buffer containing the
105 /// specified location, returning -1 if not found.
106 int FindBufferContainingLoc(SMLoc Loc) const;
108 /// FindLineNumber - Find the line number for the specified location in the
109 /// specified file. This is not a fast method.
110 unsigned FindLineNumber(SMLoc Loc, int BufferID = -1) const;
112 /// PrintMessage - Emit a message about the specified location with the
113 /// specified string.
114 void PrintMessage(SMLoc Loc, const std::string &Msg) const;
117 void PrintIncludeStack(SMLoc IncludeLoc) const;
120 } // end llvm namespace