SourceMgr.h revision 207618
1//===- SourceMgr.h - Manager for Source Buffers & Diagnostics ---*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
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.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef SUPPORT_SOURCEMGR_H
17#define SUPPORT_SOURCEMGR_H
18
19#include "llvm/Support/SMLoc.h"
20
21#include <string>
22#include <vector>
23#include <cassert>
24
25namespace llvm {
26  class MemoryBuffer;
27  class SourceMgr;
28  class SMDiagnostic;
29  class raw_ostream;
30
31/// SourceMgr - This owns the files read by a parser, handles include stacks,
32/// and handles diagnostic wrangling.
33class SourceMgr {
34public:
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,
39                                unsigned LocCookie);
40private:
41  struct SrcBuffer {
42    /// Buffer - The memory buffer for the file.
43    MemoryBuffer *Buffer;
44
45    /// IncludeLoc - This is the location of the parent include, or null if at
46    /// the top level.
47    SMLoc IncludeLoc;
48  };
49
50  /// Buffers - This is all of the buffers that we are reading from.
51  std::vector<SrcBuffer> Buffers;
52
53  // IncludeDirectories - This is the list of directories we should search for
54  // include files in.
55  std::vector<std::string> IncludeDirectories;
56
57  /// LineNoCache - This is a cache for line number queries, its implementation
58  /// is really private to SourceMgr.cpp.
59  mutable void *LineNoCache;
60
61  DiagHandlerTy DiagHandler;
62  void *DiagContext;
63  unsigned DiagLocCookie;
64
65  SourceMgr(const SourceMgr&);    // DO NOT IMPLEMENT
66  void operator=(const SourceMgr&); // DO NOT IMPLEMENT
67public:
68  SourceMgr() : LineNoCache(0), DiagHandler(0), DiagContext(0) {}
69  ~SourceMgr();
70
71  void setIncludeDirs(const std::vector<std::string> &Dirs) {
72    IncludeDirectories = Dirs;
73  }
74
75  /// setDiagHandler - Specify a diagnostic handler to be invoked every time
76  /// PrintMessage is called.  Ctx and Cookie are passed into the handler when
77  /// it is invoked.
78  void setDiagHandler(DiagHandlerTy DH, void *Ctx = 0, unsigned Cookie = 0) {
79    DiagHandler = DH;
80    DiagContext = Ctx;
81    DiagLocCookie = Cookie;
82  }
83
84  const SrcBuffer &getBufferInfo(unsigned i) const {
85    assert(i < Buffers.size() && "Invalid Buffer ID!");
86    return Buffers[i];
87  }
88
89  const MemoryBuffer *getMemoryBuffer(unsigned i) const {
90    assert(i < Buffers.size() && "Invalid Buffer ID!");
91    return Buffers[i].Buffer;
92  }
93
94  SMLoc getParentIncludeLoc(unsigned i) const {
95    assert(i < Buffers.size() && "Invalid Buffer ID!");
96    return Buffers[i].IncludeLoc;
97  }
98
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) {
102    SrcBuffer NB;
103    NB.Buffer = F;
104    NB.IncludeLoc = IncludeLoc;
105    Buffers.push_back(NB);
106    return Buffers.size()-1;
107  }
108
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  unsigned AddIncludeFile(const std::string &Filename, SMLoc IncludeLoc);
113
114  /// FindBufferContainingLoc - Return the ID of the buffer containing the
115  /// specified location, returning -1 if not found.
116  int FindBufferContainingLoc(SMLoc Loc) const;
117
118  /// FindLineNumber - Find the line number for the specified location in the
119  /// specified file.  This is not a fast method.
120  unsigned FindLineNumber(SMLoc Loc, int BufferID = -1) const;
121
122  /// PrintMessage - Emit a message about the specified location with the
123  /// specified string.
124  ///
125  /// @param Type - If non-null, the kind of message (e.g., "error") which is
126  /// prefixed to the message.
127  /// @param ShowLine - Should the diagnostic show the source line.
128  void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type,
129                    bool ShowLine = true) const;
130
131
132  /// GetMessage - Return an SMDiagnostic at the specified location with the
133  /// specified string.
134  ///
135  /// @param Type - If non-null, the kind of message (e.g., "error") which is
136  /// prefixed to the message.
137  /// @param ShowLine - Should the diagnostic show the source line.
138  SMDiagnostic GetMessage(SMLoc Loc,
139                          const std::string &Msg, const char *Type,
140                          bool ShowLine = true) const;
141
142
143private:
144  void PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const;
145};
146
147
148/// SMDiagnostic - Instances of this class encapsulate one diagnostic report,
149/// allowing printing to a raw_ostream as a caret diagnostic.
150class SMDiagnostic {
151  const SourceMgr *SM;
152  SMLoc Loc;
153  std::string Filename;
154  int LineNo, ColumnNo;
155  std::string Message, LineContents;
156  unsigned ShowLine : 1;
157
158public:
159  // Null diagnostic.
160  SMDiagnostic() : SM(0), LineNo(0), ColumnNo(0), ShowLine(0) {}
161  // Diagnostic with no location (e.g. file not found, command line arg error).
162  SMDiagnostic(const std::string &filename, const std::string &Msg,
163               bool showline = true)
164    : SM(0), Loc(), Filename(filename), LineNo(-1), ColumnNo(-1),
165      Message(Msg), LineContents(""), ShowLine(showline) {}
166
167  // Diagnostic with a location.
168  SMDiagnostic(const SourceMgr &sm, SMLoc L, const std::string &FN,
169               int Line, int Col,
170               const std::string &Msg, const std::string &LineStr,
171               bool showline = true)
172    : SM(&sm), Loc(L), Filename(FN), LineNo(Line), ColumnNo(Col), Message(Msg),
173      LineContents(LineStr), ShowLine(showline) {}
174
175  const SourceMgr *getSourceMgr() const { return SM; }
176  SMLoc getLoc() const { return Loc; }
177  const std::string &getFilename() { return Filename; }
178  int getLineNo() const { return LineNo; }
179  int getColumnNo() const { return ColumnNo; }
180  const std::string &getMessage() const { return Message; }
181  const std::string &getLineContents() const { return LineContents; }
182  bool getShowLine() const { return ShowLine; }
183
184  void Print(const char *ProgName, raw_ostream &S) const;
185};
186
187}  // end llvm namespace
188
189#endif
190