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 SourceMgr(const SourceMgr&); // DO NOT IMPLEMENT
69 void operator=(const SourceMgr&); // DO NOT IMPLEMENT
74 void setIncludeDirs(const std::vector<std::string> &Dirs) {
75 IncludeDirectories = Dirs;
78 const SrcBuffer &getBufferInfo(unsigned i) const {
79 assert(i < Buffers.size() && "Invalid Buffer ID!");
83 const MemoryBuffer *getMemoryBuffer(unsigned i) const {
84 assert(i < Buffers.size() && "Invalid Buffer ID!");
85 return Buffers[i].Buffer;
88 SMLoc getParentIncludeLoc(unsigned i) const {
89 assert(i < Buffers.size() && "Invalid Buffer ID!");
90 return Buffers[i].IncludeLoc;
93 unsigned AddNewSourceBuffer(MemoryBuffer *F, SMLoc IncludeLoc) {
96 NB.IncludeLoc = IncludeLoc;
97 Buffers.push_back(NB);
98 return Buffers.size()-1;
101 /// AddIncludeFile - Search for a file with the specified name in the current
102 /// directory or in one of the IncludeDirs. If no file is found, this returns
103 /// ~0, otherwise it returns the buffer ID of the stacked file.
104 unsigned AddIncludeFile(const std::string &Filename, SMLoc IncludeLoc);
106 /// FindBufferContainingLoc - Return the ID of the buffer containing the
107 /// specified location, returning -1 if not found.
108 int FindBufferContainingLoc(SMLoc Loc) const;
110 /// FindLineNumber - Find the line number for the specified location in the
111 /// specified file. This is not a fast method.
112 unsigned FindLineNumber(SMLoc Loc, int BufferID = -1) const;
114 /// PrintMessage - Emit a message about the specified location with the
115 /// specified string.
117 /// @param Type - If non-null, the kind of message (e.g., "error") which is
118 /// prefixed to the message.
119 void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const;
122 /// GetMessage - Return an SMDiagnostic at the specified location with the
123 /// specified string.
125 /// @param Type - If non-null, the kind of message (e.g., "error") which is
126 /// prefixed to the message.
127 SMDiagnostic GetMessage(SMLoc Loc,
128 const std::string &Msg, const char *Type) const;
132 void PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const;
136 /// SMDiagnostic - Instances of this class encapsulate one diagnostic report,
137 /// allowing printing to a raw_ostream as a caret diagnostic.
139 std::string Filename;
140 int LineNo, ColumnNo;
141 std::string Message, LineContents;
143 SMDiagnostic() : LineNo(0), ColumnNo(0) {}
144 SMDiagnostic(const std::string &FN, int Line, int Col,
145 const std::string &Msg, const std::string &LineStr)
146 : Filename(FN), LineNo(Line), ColumnNo(Col), Message(Msg),
147 LineContents(LineStr) {}
148 SMDiagnostic(const SMDiagnostic &RHS) {
152 void operator=(const SMDiagnostic &E) {
153 Filename = E.Filename;
155 ColumnNo = E.ColumnNo;
157 LineContents = E.LineContents;
160 void Print(const char *ProgName, raw_ostream &S);
163 } // end llvm namespace