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 operator==(const SMLoc &RHS) const { return RHS.Ptr == Ptr; }
34 bool operator!=(const SMLoc &RHS) const { return RHS.Ptr != Ptr; }
36 const char *getPointer() const { return Ptr; }
38 static SMLoc getFromPointer(const char *Ptr) {
45 /// SourceMgr - This owns the files read by tblgen, handles include stacks,
46 /// and handles printing of diagnostics.
49 /// Buffer - The memory buffer for the file.
52 /// IncludeLoc - This is the location of the parent include, or null if at
57 /// Buffers - This is all of the buffers that we are reading from.
58 std::vector<SrcBuffer> Buffers;
60 // IncludeDirectories - This is the list of directories we should search for
62 std::vector<std::string> IncludeDirectories;
64 SourceMgr(const SourceMgr&); // DO NOT IMPLEMENT
65 void operator=(const SourceMgr&); // DO NOT IMPLEMENT
70 void setIncludeDirs(const std::vector<std::string> &Dirs) {
71 IncludeDirectories = Dirs;
74 const SrcBuffer &getBufferInfo(unsigned i) const {
75 assert(i < Buffers.size() && "Invalid Buffer ID!");
79 const MemoryBuffer *getMemoryBuffer(unsigned i) const {
80 assert(i < Buffers.size() && "Invalid Buffer ID!");
81 return Buffers[i].Buffer;
84 SMLoc getParentIncludeLoc(unsigned i) const {
85 assert(i < Buffers.size() && "Invalid Buffer ID!");
86 return Buffers[i].IncludeLoc;
89 unsigned AddNewSourceBuffer(MemoryBuffer *F, SMLoc IncludeLoc) {
92 NB.IncludeLoc = IncludeLoc;
93 Buffers.push_back(NB);
94 return Buffers.size()-1;
97 /// AddIncludeFile - Search for a file with the specified name in the current
98 /// directory or in one of the IncludeDirs. If no file is found, this returns
99 /// ~0, otherwise it returns the buffer ID of the stacked file.
100 unsigned AddIncludeFile(const std::string &Filename, SMLoc IncludeLoc);
102 /// FindBufferContainingLoc - Return the ID of the buffer containing the
103 /// specified location, returning -1 if not found.
104 int FindBufferContainingLoc(SMLoc Loc) const;
106 /// FindLineNumber - Find the line number for the specified location in the
107 /// specified file. This is not a fast method.
108 unsigned FindLineNumber(SMLoc Loc, int BufferID = -1) const;
111 /// PrintError - Emit an error message about the specified location with the
112 /// specified string.
113 void PrintError(SMLoc ErrorLoc, const std::string &Msg) const;
116 void PrintIncludeStack(SMLoc IncludeLoc) const;
119 } // end llvm namespace