SourceMgr.h revision 203954
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 {
34  struct SrcBuffer {
35    /// Buffer - The memory buffer for the file.
36    MemoryBuffer *Buffer;
37
38    /// IncludeLoc - This is the location of the parent include, or null if at
39    /// the top level.
40    SMLoc IncludeLoc;
41  };
42
43  /// Buffers - This is all of the buffers that we are reading from.
44  std::vector<SrcBuffer> Buffers;
45
46  // IncludeDirectories - This is the list of directories we should search for
47  // include files in.
48  std::vector<std::string> IncludeDirectories;
49
50  /// LineNoCache - This is a cache for line number queries, its implementation
51  /// is really private to SourceMgr.cpp.
52  mutable void *LineNoCache;
53
54  SourceMgr(const SourceMgr&);    // DO NOT IMPLEMENT
55  void operator=(const SourceMgr&); // DO NOT IMPLEMENT
56public:
57  SourceMgr() : LineNoCache(0) {}
58  ~SourceMgr();
59
60  void setIncludeDirs(const std::vector<std::string> &Dirs) {
61    IncludeDirectories = Dirs;
62  }
63
64  const SrcBuffer &getBufferInfo(unsigned i) const {
65    assert(i < Buffers.size() && "Invalid Buffer ID!");
66    return Buffers[i];
67  }
68
69  const MemoryBuffer *getMemoryBuffer(unsigned i) const {
70    assert(i < Buffers.size() && "Invalid Buffer ID!");
71    return Buffers[i].Buffer;
72  }
73
74  SMLoc getParentIncludeLoc(unsigned i) const {
75    assert(i < Buffers.size() && "Invalid Buffer ID!");
76    return Buffers[i].IncludeLoc;
77  }
78
79  unsigned AddNewSourceBuffer(MemoryBuffer *F, SMLoc IncludeLoc) {
80    SrcBuffer NB;
81    NB.Buffer = F;
82    NB.IncludeLoc = IncludeLoc;
83    Buffers.push_back(NB);
84    return Buffers.size()-1;
85  }
86
87  /// AddIncludeFile - Search for a file with the specified name in the current
88  /// directory or in one of the IncludeDirs.  If no file is found, this returns
89  /// ~0, otherwise it returns the buffer ID of the stacked file.
90  unsigned AddIncludeFile(const std::string &Filename, SMLoc IncludeLoc);
91
92  /// FindBufferContainingLoc - Return the ID of the buffer containing the
93  /// specified location, returning -1 if not found.
94  int FindBufferContainingLoc(SMLoc Loc) const;
95
96  /// FindLineNumber - Find the line number for the specified location in the
97  /// specified file.  This is not a fast method.
98  unsigned FindLineNumber(SMLoc Loc, int BufferID = -1) const;
99
100  /// PrintMessage - Emit a message about the specified location with the
101  /// specified string.
102  ///
103  /// @param Type - If non-null, the kind of message (e.g., "error") which is
104  /// prefixed to the message.
105  /// @param ShowLine - Should the diagnostic show the source line.
106  void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type,
107                    bool ShowLine = true) const;
108
109
110  /// GetMessage - Return an SMDiagnostic at the specified location with the
111  /// specified string.
112  ///
113  /// @param Type - If non-null, the kind of message (e.g., "error") which is
114  /// prefixed to the message.
115  /// @param ShowLine - Should the diagnostic show the source line.
116  SMDiagnostic GetMessage(SMLoc Loc,
117                          const std::string &Msg, const char *Type,
118                          bool ShowLine = true) const;
119
120
121private:
122  void PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const;
123};
124
125
126/// SMDiagnostic - Instances of this class encapsulate one diagnostic report,
127/// allowing printing to a raw_ostream as a caret diagnostic.
128class SMDiagnostic {
129  std::string Filename;
130  int LineNo, ColumnNo;
131  std::string Message, LineContents;
132  unsigned ShowLine : 1;
133
134public:
135  SMDiagnostic() : LineNo(0), ColumnNo(0), ShowLine(0) {}
136  SMDiagnostic(const std::string &FN, int Line, int Col,
137               const std::string &Msg, const std::string &LineStr,
138               bool showline = true)
139    : Filename(FN), LineNo(Line), ColumnNo(Col), Message(Msg),
140      LineContents(LineStr), ShowLine(showline) {}
141
142  void Print(const char *ProgName, raw_ostream &S) const;
143};
144
145}  // end llvm namespace
146
147#endif
148