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 SMLoc, SMDiagnostic and SourceMgr classes. This
11 // provides a simple substrate for diagnostics, #include handling, and other low
12 // level things for simple parsers.
14 //===----------------------------------------------------------------------===//
16 #ifndef SUPPORT_SOURCEMGR_H
17 #define SUPPORT_SOURCEMGR_H
33 SMLoc(const SMLoc &RHS) : Ptr(RHS.Ptr) {}
35 bool isValid() const { return Ptr != 0; }
37 bool operator==(const SMLoc &RHS) const { return RHS.Ptr == Ptr; }
38 bool operator!=(const SMLoc &RHS) const { return RHS.Ptr != Ptr; }
40 const char *getPointer() const { return Ptr; }
42 static SMLoc getFromPointer(const char *Ptr) {
49 /// SourceMgr - This owns the files read by a parser, handles include stacks,
50 /// and handles diagnostic wrangling.
53 /// Buffer - The memory buffer for the file.
56 /// IncludeLoc - This is the location of the parent include, or null if at
61 /// Buffers - This is all of the buffers that we are reading from.
62 std::vector<SrcBuffer> Buffers;
64 // IncludeDirectories - This is the list of directories we should search for
66 std::vector<std::string> IncludeDirectories;
68 /// LineNoCache - This is a cache for line number queries, its implementation
69 /// is really private to SourceMgr.cpp.
70 mutable void *LineNoCache;
72 SourceMgr(const SourceMgr&); // DO NOT IMPLEMENT
73 void operator=(const SourceMgr&); // DO NOT IMPLEMENT
75 SourceMgr() : LineNoCache(0) {}
78 void setIncludeDirs(const std::vector<std::string> &Dirs) {
79 IncludeDirectories = Dirs;
82 const SrcBuffer &getBufferInfo(unsigned i) const {
83 assert(i < Buffers.size() && "Invalid Buffer ID!");
87 const MemoryBuffer *getMemoryBuffer(unsigned i) const {
88 assert(i < Buffers.size() && "Invalid Buffer ID!");
89 return Buffers[i].Buffer;
92 SMLoc getParentIncludeLoc(unsigned i) const {
93 assert(i < Buffers.size() && "Invalid Buffer ID!");
94 return Buffers[i].IncludeLoc;
97 unsigned AddNewSourceBuffer(MemoryBuffer *F, SMLoc IncludeLoc) {
100 NB.IncludeLoc = IncludeLoc;
101 Buffers.push_back(NB);
102 return Buffers.size()-1;
105 /// AddIncludeFile - Search for a file with the specified name in the current
106 /// directory or in one of the IncludeDirs. If no file is found, this returns
107 /// ~0, otherwise it returns the buffer ID of the stacked file.
108 unsigned AddIncludeFile(const std::string &Filename, SMLoc IncludeLoc);
110 /// FindBufferContainingLoc - Return the ID of the buffer containing the
111 /// specified location, returning -1 if not found.
112 int FindBufferContainingLoc(SMLoc Loc) const;
114 /// FindLineNumber - Find the line number for the specified location in the
115 /// specified file. This is not a fast method.
116 unsigned FindLineNumber(SMLoc Loc, int BufferID = -1) const;
118 /// PrintMessage - Emit a message about the specified location with the
119 /// specified string.
121 /// @param Type - If non-null, the kind of message (e.g., "error") which is
122 /// prefixed to the message.
123 void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const;
126 /// GetMessage - Return an SMDiagnostic at the specified location with the
127 /// specified string.
129 /// @param Type - If non-null, the kind of message (e.g., "error") which is
130 /// prefixed to the message.
131 SMDiagnostic GetMessage(SMLoc Loc,
132 const std::string &Msg, const char *Type) const;
136 void PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const;
140 /// SMDiagnostic - Instances of this class encapsulate one diagnostic report,
141 /// allowing printing to a raw_ostream as a caret diagnostic.
143 std::string Filename;
144 int LineNo, ColumnNo;
145 std::string Message, LineContents;
147 SMDiagnostic() : LineNo(0), ColumnNo(0) {}
148 SMDiagnostic(const std::string &FN, int Line, int Col,
149 const std::string &Msg, const std::string &LineStr)
150 : Filename(FN), LineNo(Line), ColumnNo(Col), Message(Msg),
151 LineContents(LineStr) {}
153 void Print(const char *ProgName, raw_ostream &S);
156 } // end llvm namespace