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 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
19 #include "llvm/Support/SMLoc.h"
32 /// SourceMgr - This owns the files read by a parser, handles include stacks,
33 /// and handles diagnostic wrangling.
36 /// DiagHandlerTy - Clients that want to handle their own diagnostics in a
37 /// custom way can register a function pointer+context as a diagnostic
38 /// handler. It gets called each time PrintMessage is invoked.
39 typedef void (*DiagHandlerTy)(const SMDiagnostic&, void *Context);
42 /// Buffer - The memory buffer for the file.
45 /// IncludeLoc - This is the location of the parent include, or null if at
50 /// Buffers - This is all of the buffers that we are reading from.
51 std::vector<SrcBuffer> Buffers;
53 // IncludeDirectories - This is the list of directories we should search for
55 std::vector<std::string> IncludeDirectories;
57 /// LineNoCache - This is a cache for line number queries, its implementation
58 /// is really private to SourceMgr.cpp.
59 mutable void *LineNoCache;
61 DiagHandlerTy DiagHandler;
64 SourceMgr(const SourceMgr&); // DO NOT IMPLEMENT
65 void operator=(const SourceMgr&); // DO NOT IMPLEMENT
67 SourceMgr() : LineNoCache(0), DiagHandler(0), DiagContext(0) {}
70 void setIncludeDirs(const std::vector<std::string> &Dirs) {
71 IncludeDirectories = Dirs;
74 /// setDiagHandler - Specify a diagnostic handler to be invoked every time
75 /// PrintMessage is called. Ctx is passed into the handler when it is invoked.
76 void setDiagHandler(DiagHandlerTy DH, void *Ctx = 0) {
81 DiagHandlerTy getDiagHandler() const { return DiagHandler; }
82 void *getDiagContext() const { return DiagContext; }
84 const SrcBuffer &getBufferInfo(unsigned i) const {
85 assert(i < Buffers.size() && "Invalid Buffer ID!");
89 const MemoryBuffer *getMemoryBuffer(unsigned i) const {
90 assert(i < Buffers.size() && "Invalid Buffer ID!");
91 return Buffers[i].Buffer;
94 SMLoc getParentIncludeLoc(unsigned i) const {
95 assert(i < Buffers.size() && "Invalid Buffer ID!");
96 return Buffers[i].IncludeLoc;
99 /// AddNewSourceBuffer - Add a new source buffer to this source manager. This
100 /// takes ownership of the memory buffer.
101 unsigned AddNewSourceBuffer(MemoryBuffer *F, SMLoc IncludeLoc) {
104 NB.IncludeLoc = IncludeLoc;
105 Buffers.push_back(NB);
106 return Buffers.size()-1;
109 /// AddIncludeFile - Search for a file with the specified name in the current
110 /// directory or in one of the IncludeDirs. If no file is found, this returns
111 /// ~0, otherwise it returns the buffer ID of the stacked file.
112 /// The full path to the included file can be found in IncludedFile.
113 unsigned AddIncludeFile(const std::string &Filename, SMLoc IncludeLoc,
114 std::string &IncludedFile);
116 /// FindBufferContainingLoc - Return the ID of the buffer containing the
117 /// specified location, returning -1 if not found.
118 int FindBufferContainingLoc(SMLoc Loc) const;
120 /// FindLineNumber - Find the line number for the specified location in the
121 /// specified file. This is not a fast method.
122 unsigned FindLineNumber(SMLoc Loc, int BufferID = -1) const;
124 /// PrintMessage - Emit a message about the specified location with the
125 /// specified string.
127 /// @param Type - If non-null, the kind of message (e.g., "error") which is
128 /// prefixed to the message.
129 /// @param ShowLine - Should the diagnostic show the source line.
130 void PrintMessage(SMLoc Loc, const Twine &Msg, const char *Type,
131 bool ShowLine = true) const;
134 /// GetMessage - Return an SMDiagnostic at the specified location with the
135 /// specified string.
137 /// @param Type - If non-null, the kind of message (e.g., "error") which is
138 /// prefixed to the message.
139 /// @param ShowLine - Should the diagnostic show the source line.
140 SMDiagnostic GetMessage(SMLoc Loc,
141 const Twine &Msg, const char *Type,
142 bool ShowLine = true) const;
144 /// PrintIncludeStack - Prints the names of included files and the line of the
145 /// file they were included from. A diagnostic handler can use this before
146 /// printing its custom formatted message.
148 /// @param IncludeLoc - The line of the include.
149 /// @param OS the raw_ostream to print on.
150 void PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const;
154 /// SMDiagnostic - Instances of this class encapsulate one diagnostic report,
155 /// allowing printing to a raw_ostream as a caret diagnostic.
159 std::string Filename;
160 int LineNo, ColumnNo;
161 std::string Message, LineContents;
162 unsigned ShowLine : 1;
166 SMDiagnostic() : SM(0), LineNo(0), ColumnNo(0), ShowLine(0) {}
167 // Diagnostic with no location (e.g. file not found, command line arg error).
168 SMDiagnostic(const std::string &filename, const std::string &Msg)
169 : SM(0), Filename(filename), LineNo(-1), ColumnNo(-1),
170 Message(Msg), ShowLine(false) {}
172 // Diagnostic with a location.
173 SMDiagnostic(const SourceMgr &sm, SMLoc L, const std::string &FN,
175 const std::string &Msg, const std::string &LineStr,
176 bool showline = true)
177 : SM(&sm), Loc(L), Filename(FN), LineNo(Line), ColumnNo(Col), Message(Msg),
178 LineContents(LineStr), ShowLine(showline) {}
180 const SourceMgr *getSourceMgr() const { return SM; }
181 SMLoc getLoc() const { return Loc; }
182 const std::string &getFilename() const { return Filename; }
183 int getLineNo() const { return LineNo; }
184 int getColumnNo() const { return ColumnNo; }
185 const std::string &getMessage() const { return Message; }
186 const std::string &getLineContents() const { return LineContents; }
187 bool getShowLine() const { return ShowLine; }
189 void Print(const char *ProgName, raw_ostream &S) const;
192 } // end llvm namespace