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"
31 /// SourceMgr - This owns the files read by a parser, handles include stacks,
32 /// and handles diagnostic wrangling.
35 /// DiagHandlerTy - Clients that want to handle their own diagnostics in a
36 /// custom way can register a function pointer+context as a diagnostic
37 /// handler. It gets called each time PrintMessage is invoked.
38 typedef void (*DiagHandlerTy)(const SMDiagnostic&, void *Context);
41 /// Buffer - The memory buffer for the file.
44 /// IncludeLoc - This is the location of the parent include, or null if at
49 /// Buffers - This is all of the buffers that we are reading from.
50 std::vector<SrcBuffer> Buffers;
52 // IncludeDirectories - This is the list of directories we should search for
54 std::vector<std::string> IncludeDirectories;
56 /// LineNoCache - This is a cache for line number queries, its implementation
57 /// is really private to SourceMgr.cpp.
58 mutable void *LineNoCache;
60 DiagHandlerTy DiagHandler;
63 SourceMgr(const SourceMgr&); // DO NOT IMPLEMENT
64 void operator=(const SourceMgr&); // DO NOT IMPLEMENT
66 SourceMgr() : LineNoCache(0), DiagHandler(0), DiagContext(0) {}
69 void setIncludeDirs(const std::vector<std::string> &Dirs) {
70 IncludeDirectories = Dirs;
73 /// setDiagHandler - Specify a diagnostic handler to be invoked every time
74 /// PrintMessage is called.
75 void setDiagHandler(DiagHandlerTy DH, void *Ctx = 0) {
80 const SrcBuffer &getBufferInfo(unsigned i) const {
81 assert(i < Buffers.size() && "Invalid Buffer ID!");
85 const MemoryBuffer *getMemoryBuffer(unsigned i) const {
86 assert(i < Buffers.size() && "Invalid Buffer ID!");
87 return Buffers[i].Buffer;
90 SMLoc getParentIncludeLoc(unsigned i) const {
91 assert(i < Buffers.size() && "Invalid Buffer ID!");
92 return Buffers[i].IncludeLoc;
95 /// AddNewSourceBuffer - Add a new source buffer to this source manager. This
96 /// takes ownership of the memory buffer.
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 /// @param ShowLine - Should the diagnostic show the source line.
124 void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type,
125 bool ShowLine = true) const;
128 /// GetMessage - Return an SMDiagnostic at the specified location with the
129 /// specified string.
131 /// @param Type - If non-null, the kind of message (e.g., "error") which is
132 /// prefixed to the message.
133 /// @param ShowLine - Should the diagnostic show the source line.
134 SMDiagnostic GetMessage(SMLoc Loc,
135 const std::string &Msg, const char *Type,
136 bool ShowLine = true) const;
140 void PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const;
144 /// SMDiagnostic - Instances of this class encapsulate one diagnostic report,
145 /// allowing printing to a raw_ostream as a caret diagnostic.
148 std::string Filename;
149 int LineNo, ColumnNo;
150 std::string Message, LineContents;
151 unsigned ShowLine : 1;
154 SMDiagnostic() : LineNo(0), ColumnNo(0), ShowLine(0) {}
155 SMDiagnostic(SMLoc L, const std::string &FN, int Line, int Col,
156 const std::string &Msg, const std::string &LineStr,
157 bool showline = true)
158 : Loc(L), Filename(FN), LineNo(Line), ColumnNo(Col), Message(Msg),
159 LineContents(LineStr), ShowLine(showline) {}
161 SMLoc getLoc() const { return Loc; }
162 const std::string getFilename() { return Filename; }
163 int getLineNo() const { return LineNo; }
164 int getColumnNo() const { return ColumnNo; }
165 const std::string &getMessage() const { return Message; }
166 const std::string &getLineContents() const { return LineContents; }
167 bool getShowLine() const { return ShowLine; }
169 void Print(const char *ProgName, raw_ostream &S) const;
172 } // end llvm namespace