add a comment.
[oota-llvm.git] / include / llvm / Support / SourceMgr.h
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
25 namespace 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.
33 class 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
56 public:
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   /// AddNewSourceBuffer - Add a new source buffer to this source manager.  This
80   /// takes ownership of the memory buffer.
81   unsigned AddNewSourceBuffer(MemoryBuffer *F, SMLoc IncludeLoc) {
82     SrcBuffer NB;
83     NB.Buffer = F;
84     NB.IncludeLoc = IncludeLoc;
85     Buffers.push_back(NB);
86     return Buffers.size()-1;
87   }
88
89   /// AddIncludeFile - Search for a file with the specified name in the current
90   /// directory or in one of the IncludeDirs.  If no file is found, this returns
91   /// ~0, otherwise it returns the buffer ID of the stacked file.
92   unsigned AddIncludeFile(const std::string &Filename, SMLoc IncludeLoc);
93
94   /// FindBufferContainingLoc - Return the ID of the buffer containing the
95   /// specified location, returning -1 if not found.
96   int FindBufferContainingLoc(SMLoc Loc) const;
97
98   /// FindLineNumber - Find the line number for the specified location in the
99   /// specified file.  This is not a fast method.
100   unsigned FindLineNumber(SMLoc Loc, int BufferID = -1) const;
101
102   /// PrintMessage - Emit a message about the specified location with the
103   /// specified string.
104   ///
105   /// @param Type - If non-null, the kind of message (e.g., "error") which is
106   /// prefixed to the message.
107   /// @param ShowLine - Should the diagnostic show the source line.
108   void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type,
109                     bool ShowLine = true) const;
110
111
112   /// GetMessage - Return an SMDiagnostic at the specified location with the
113   /// specified string.
114   ///
115   /// @param Type - If non-null, the kind of message (e.g., "error") which is
116   /// prefixed to the message.
117   /// @param ShowLine - Should the diagnostic show the source line.
118   SMDiagnostic GetMessage(SMLoc Loc,
119                           const std::string &Msg, const char *Type,
120                           bool ShowLine = true) const;
121
122
123 private:
124   void PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const;
125 };
126
127
128 /// SMDiagnostic - Instances of this class encapsulate one diagnostic report,
129 /// allowing printing to a raw_ostream as a caret diagnostic.
130 class SMDiagnostic {
131   std::string Filename;
132   int LineNo, ColumnNo;
133   std::string Message, LineContents;
134   unsigned ShowLine : 1;
135
136 public:
137   SMDiagnostic() : LineNo(0), ColumnNo(0), ShowLine(0) {}
138   SMDiagnostic(const std::string &FN, int Line, int Col,
139                const std::string &Msg, const std::string &LineStr,
140                bool showline = true)
141     : Filename(FN), LineNo(Line), ColumnNo(Col), Message(Msg),
142       LineContents(LineStr), ShowLine(showline) {}
143
144   void Print(const char *ProgName, raw_ostream &S) const;
145 };
146
147 }  // end llvm namespace
148
149 #endif