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 LLVM_SUPPORT_SOURCEMGR_H
17 #define LLVM_SUPPORT_SOURCEMGR_H
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/Support/SMLoc.h"
30 /// SourceMgr - This owns the files read by a parser, handles include stacks,
31 /// and handles diagnostic wrangling.
40 /// DiagHandlerTy - Clients that want to handle their own diagnostics in a
41 /// custom way can register a function pointer+context as a diagnostic
42 /// handler. It gets called each time PrintMessage is invoked.
43 typedef void (*DiagHandlerTy)(const SMDiagnostic &, void *Context);
46 /// Buffer - The memory buffer for the file.
49 /// IncludeLoc - This is the location of the parent include, or null if at
54 /// Buffers - This is all of the buffers that we are reading from.
55 std::vector<SrcBuffer> Buffers;
57 // IncludeDirectories - This is the list of directories we should search for
59 std::vector<std::string> IncludeDirectories;
61 /// LineNoCache - This is a cache for line number queries, its implementation
62 /// is really private to SourceMgr.cpp.
63 mutable void *LineNoCache;
65 DiagHandlerTy DiagHandler;
68 SourceMgr(const SourceMgr&) LLVM_DELETED_FUNCTION;
69 void operator=(const SourceMgr&) LLVM_DELETED_FUNCTION;
71 SourceMgr() : LineNoCache(0), DiagHandler(0), DiagContext(0) {}
74 void setIncludeDirs(const std::vector<std::string> &Dirs) {
75 IncludeDirectories = Dirs;
78 /// setDiagHandler - Specify a diagnostic handler to be invoked every time
79 /// PrintMessage is called. Ctx is passed into the handler when it is invoked.
80 void setDiagHandler(DiagHandlerTy DH, void *Ctx = 0) {
85 DiagHandlerTy getDiagHandler() const { return DiagHandler; }
86 void *getDiagContext() const { return DiagContext; }
88 const SrcBuffer &getBufferInfo(unsigned i) const {
89 assert(i < Buffers.size() && "Invalid Buffer ID!");
93 const MemoryBuffer *getMemoryBuffer(unsigned i) const {
94 assert(i < Buffers.size() && "Invalid Buffer ID!");
95 return Buffers[i].Buffer;
98 unsigned getNumBuffers() const {
99 return Buffers.size();
102 SMLoc getParentIncludeLoc(unsigned i) const {
103 assert(i < Buffers.size() && "Invalid Buffer ID!");
104 return Buffers[i].IncludeLoc;
107 /// AddNewSourceBuffer - Add a new source buffer to this source manager. This
108 /// takes ownership of the memory buffer.
109 unsigned AddNewSourceBuffer(MemoryBuffer *F, SMLoc IncludeLoc) {
112 NB.IncludeLoc = IncludeLoc;
113 Buffers.push_back(NB);
114 return Buffers.size()-1;
117 /// AddIncludeFile - Search for a file with the specified name in the current
118 /// directory or in one of the IncludeDirs. If no file is found, this returns
119 /// ~0, otherwise it returns the buffer ID of the stacked file.
120 /// The full path to the included file can be found in IncludedFile.
121 unsigned AddIncludeFile(const std::string &Filename, SMLoc IncludeLoc,
122 std::string &IncludedFile);
124 /// FindBufferContainingLoc - Return the ID of the buffer containing the
125 /// specified location, returning -1 if not found.
126 int FindBufferContainingLoc(SMLoc Loc) const;
128 /// FindLineNumber - Find the line number for the specified location in the
129 /// specified file. This is not a fast method.
130 unsigned FindLineNumber(SMLoc Loc, int BufferID = -1) const {
131 return getLineAndColumn(Loc, BufferID).first;
134 /// getLineAndColumn - Find the line and column number for the specified
135 /// location in the specified file. This is not a fast method.
136 std::pair<unsigned, unsigned>
137 getLineAndColumn(SMLoc Loc, int BufferID = -1) const;
139 /// PrintMessage - Emit a message about the specified location with the
140 /// specified string.
142 /// @param ShowColors - Display colored messages if output is a terminal and
143 /// the default error handler is used.
144 void PrintMessage(SMLoc Loc, DiagKind Kind, const Twine &Msg,
145 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>(),
146 bool ShowColors = true) const;
149 /// GetMessage - Return an SMDiagnostic at the specified location with the
150 /// specified string.
152 /// @param Msg If non-null, the kind of message (e.g., "error") which is
153 /// prefixed to the message.
154 SMDiagnostic GetMessage(SMLoc Loc, DiagKind Kind, const Twine &Msg,
155 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) const;
157 /// PrintIncludeStack - Prints the names of included files and the line of the
158 /// file they were included from. A diagnostic handler can use this before
159 /// printing its custom formatted message.
161 /// @param IncludeLoc - The line of the include.
162 /// @param OS the raw_ostream to print on.
163 void PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const;
167 /// SMDiagnostic - Instances of this class encapsulate one diagnostic report,
168 /// allowing printing to a raw_ostream as a caret diagnostic.
172 std::string Filename;
173 int LineNo, ColumnNo;
174 SourceMgr::DiagKind Kind;
175 std::string Message, LineContents;
176 std::vector<std::pair<unsigned, unsigned> > Ranges;
181 : SM(0), LineNo(0), ColumnNo(0), Kind(SourceMgr::DK_Error) {}
182 // Diagnostic with no location (e.g. file not found, command line arg error).
183 SMDiagnostic(const std::string &filename, SourceMgr::DiagKind Knd,
184 const std::string &Msg)
185 : SM(0), Filename(filename), LineNo(-1), ColumnNo(-1), Kind(Knd),
188 // Diagnostic with a location.
189 SMDiagnostic(const SourceMgr &sm, SMLoc L, const std::string &FN,
190 int Line, int Col, SourceMgr::DiagKind Kind,
191 const std::string &Msg, const std::string &LineStr,
192 ArrayRef<std::pair<unsigned,unsigned> > Ranges);
194 const SourceMgr *getSourceMgr() const { return SM; }
195 SMLoc getLoc() const { return Loc; }
196 const std::string &getFilename() const { return Filename; }
197 int getLineNo() const { return LineNo; }
198 int getColumnNo() const { return ColumnNo; }
199 SourceMgr::DiagKind getKind() const { return Kind; }
200 const std::string &getMessage() const { return Message; }
201 const std::string &getLineContents() const { return LineContents; }
202 const std::vector<std::pair<unsigned, unsigned> > &getRanges() const {
205 void print(const char *ProgName, raw_ostream &S, bool ShowColors = true) const;
208 } // end llvm namespace