SourceMgr.h revision 195340
1102447Sjhb//===- SourceMgr.h - Manager for Source Buffers & Diagnostics ---*- C++ -*-===//
2102447Sjhb//
3102447Sjhb//                     The LLVM Compiler Infrastructure
4102447Sjhb//
5102447Sjhb// This file is distributed under the University of Illinois Open Source
6102447Sjhb// License. See LICENSE.TXT for details.
7102447Sjhb//
8102447Sjhb//===----------------------------------------------------------------------===//
9102447Sjhb//
10102447Sjhb// This file declares the SMLoc, SMDiagnostic and SourceMgr classes.  This
11102447Sjhb// provides a simple substrate for diagnostics, #include handling, and other low
12102447Sjhb// level things for simple parsers.
13102447Sjhb//
14102447Sjhb//===----------------------------------------------------------------------===//
15102447Sjhb
16102447Sjhb#ifndef SUPPORT_SOURCEMGR_H
17102447Sjhb#define SUPPORT_SOURCEMGR_H
18102447Sjhb
19102447Sjhb#include <string>
20102447Sjhb#include <vector>
21102447Sjhb#include <cassert>
22102447Sjhb
23102447Sjhbnamespace llvm {
24102447Sjhb  class MemoryBuffer;
25102447Sjhb  class SourceMgr;
26102447Sjhb  class SMDiagnostic;
27102447Sjhb  class raw_ostream;
28102447Sjhb
29102447Sjhbclass SMLoc {
30102447Sjhb  const char *Ptr;
31102447Sjhbpublic:
32102447Sjhb  SMLoc() : Ptr(0) {}
33142753Snjl  SMLoc(const SMLoc &RHS) : Ptr(RHS.Ptr) {}
34210864Sjhb
35138035Sjhb  bool isValid() const { return Ptr != 0; }
36138035Sjhb
37138035Sjhb  bool operator==(const SMLoc &RHS) const { return RHS.Ptr == Ptr; }
38102447Sjhb  bool operator!=(const SMLoc &RHS) const { return RHS.Ptr != Ptr; }
39138033Sjhb
40138033Sjhb  const char *getPointer() const { return Ptr; }
41211430Sjhb
42211430Sjhb  static SMLoc getFromPointer(const char *Ptr) {
43210864Sjhb    SMLoc L;
44142753Snjl    L.Ptr = Ptr;
45102447Sjhb    return L;
46142753Snjl  }
47};
48
49/// SourceMgr - This owns the files read by a parser, handles include stacks,
50/// and handles diagnostic wrangling.
51class SourceMgr {
52  struct SrcBuffer {
53    /// Buffer - The memory buffer for the file.
54    MemoryBuffer *Buffer;
55
56    /// IncludeLoc - This is the location of the parent include, or null if at
57    /// the top level.
58    SMLoc IncludeLoc;
59  };
60
61  /// Buffers - This is all of the buffers that we are reading from.
62  std::vector<SrcBuffer> Buffers;
63
64  // IncludeDirectories - This is the list of directories we should search for
65  // include files in.
66  std::vector<std::string> IncludeDirectories;
67
68  SourceMgr(const SourceMgr&);    // DO NOT IMPLEMENT
69  void operator=(const SourceMgr&); // DO NOT IMPLEMENT
70public:
71  SourceMgr() {}
72  ~SourceMgr();
73
74  void setIncludeDirs(const std::vector<std::string> &Dirs) {
75    IncludeDirectories = Dirs;
76  }
77
78  const SrcBuffer &getBufferInfo(unsigned i) const {
79    assert(i < Buffers.size() && "Invalid Buffer ID!");
80    return Buffers[i];
81  }
82
83  const MemoryBuffer *getMemoryBuffer(unsigned i) const {
84    assert(i < Buffers.size() && "Invalid Buffer ID!");
85    return Buffers[i].Buffer;
86  }
87
88  SMLoc getParentIncludeLoc(unsigned i) const {
89    assert(i < Buffers.size() && "Invalid Buffer ID!");
90    return Buffers[i].IncludeLoc;
91  }
92
93  unsigned AddNewSourceBuffer(MemoryBuffer *F, SMLoc IncludeLoc) {
94    SrcBuffer NB;
95    NB.Buffer = F;
96    NB.IncludeLoc = IncludeLoc;
97    Buffers.push_back(NB);
98    return Buffers.size()-1;
99  }
100
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);
105
106  /// FindBufferContainingLoc - Return the ID of the buffer containing the
107  /// specified location, returning -1 if not found.
108  int FindBufferContainingLoc(SMLoc Loc) const;
109
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;
113
114  /// PrintMessage - Emit a message about the specified location with the
115  /// specified string.
116  ///
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;
120
121
122  /// GetMessage - Return an SMDiagnostic at 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  SMDiagnostic GetMessage(SMLoc Loc,
128                          const std::string &Msg, const char *Type) const;
129
130
131private:
132  void PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const;
133};
134
135
136/// SMDiagnostic - Instances of this class encapsulate one diagnostic report,
137/// allowing printing to a raw_ostream as a caret diagnostic.
138class SMDiagnostic {
139  std::string Filename;
140  int LineNo, ColumnNo;
141  std::string Message, LineContents;
142public:
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) {
149    operator=(RHS);
150  }
151
152  void operator=(const SMDiagnostic &E) {
153    Filename = E.Filename;
154    LineNo = E.LineNo;
155    ColumnNo = E.ColumnNo;
156    Message = E.Message;
157    LineContents = E.LineContents;
158  }
159
160  void Print(const char *ProgName, raw_ostream &S);
161};
162
163}  // end llvm namespace
164
165#endif
166