Revert "System: Add SwapByteOrder and update Support/MathExtras.h to use it."
[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 Twine;
30   class raw_ostream;
31
32 /// SourceMgr - This owns the files read by a parser, handles include stacks,
33 /// and handles diagnostic wrangling.
34 class SourceMgr {
35 public:
36   /// DiagHandlerTy - Clients that want to handle their own diagnostics in a
37   /// custom way can register a function pointer+context as a diagnostic
38   /// handler.  It gets called each time PrintMessage is invoked.
39   typedef void (*DiagHandlerTy)(const SMDiagnostic&, void *Context,
40                                 unsigned LocCookie);
41 private:
42   struct SrcBuffer {
43     /// Buffer - The memory buffer for the file.
44     MemoryBuffer *Buffer;
45
46     /// IncludeLoc - This is the location of the parent include, or null if at
47     /// the top level.
48     SMLoc IncludeLoc;
49   };
50
51   /// Buffers - This is all of the buffers that we are reading from.
52   std::vector<SrcBuffer> Buffers;
53
54   // IncludeDirectories - This is the list of directories we should search for
55   // include files in.
56   std::vector<std::string> IncludeDirectories;
57
58   /// LineNoCache - This is a cache for line number queries, its implementation
59   /// is really private to SourceMgr.cpp.
60   mutable void *LineNoCache;
61
62   DiagHandlerTy DiagHandler;
63   void *DiagContext;
64   unsigned DiagLocCookie;
65   
66   SourceMgr(const SourceMgr&);    // DO NOT IMPLEMENT
67   void operator=(const SourceMgr&); // DO NOT IMPLEMENT
68 public:
69   SourceMgr() : LineNoCache(0), DiagHandler(0), DiagContext(0) {}
70   ~SourceMgr();
71
72   void setIncludeDirs(const std::vector<std::string> &Dirs) {
73     IncludeDirectories = Dirs;
74   }
75
76   /// setDiagHandler - Specify a diagnostic handler to be invoked every time
77   /// PrintMessage is called.  Ctx and Cookie are passed into the handler when
78   /// it is invoked.
79   void setDiagHandler(DiagHandlerTy DH, void *Ctx = 0, unsigned Cookie = 0) {
80     DiagHandler = DH;
81     DiagContext = Ctx;
82     DiagLocCookie = Cookie;
83   }
84
85   const SrcBuffer &getBufferInfo(unsigned i) const {
86     assert(i < Buffers.size() && "Invalid Buffer ID!");
87     return Buffers[i];
88   }
89
90   const MemoryBuffer *getMemoryBuffer(unsigned i) const {
91     assert(i < Buffers.size() && "Invalid Buffer ID!");
92     return Buffers[i].Buffer;
93   }
94
95   SMLoc getParentIncludeLoc(unsigned i) const {
96     assert(i < Buffers.size() && "Invalid Buffer ID!");
97     return Buffers[i].IncludeLoc;
98   }
99
100   /// AddNewSourceBuffer - Add a new source buffer to this source manager.  This
101   /// takes ownership of the memory buffer.
102   unsigned AddNewSourceBuffer(MemoryBuffer *F, SMLoc IncludeLoc) {
103     SrcBuffer NB;
104     NB.Buffer = F;
105     NB.IncludeLoc = IncludeLoc;
106     Buffers.push_back(NB);
107     return Buffers.size()-1;
108   }
109
110   /// AddIncludeFile - Search for a file with the specified name in the current
111   /// directory or in one of the IncludeDirs.  If no file is found, this returns
112   /// ~0, otherwise it returns the buffer ID of the stacked file.
113   unsigned AddIncludeFile(const std::string &Filename, SMLoc IncludeLoc);
114
115   /// FindBufferContainingLoc - Return the ID of the buffer containing the
116   /// specified location, returning -1 if not found.
117   int FindBufferContainingLoc(SMLoc Loc) const;
118
119   /// FindLineNumber - Find the line number for the specified location in the
120   /// specified file.  This is not a fast method.
121   unsigned FindLineNumber(SMLoc Loc, int BufferID = -1) const;
122
123   /// PrintMessage - Emit a message about the specified location with the
124   /// specified string.
125   ///
126   /// @param Type - If non-null, the kind of message (e.g., "error") which is
127   /// prefixed to the message.
128   /// @param ShowLine - Should the diagnostic show the source line.
129   void PrintMessage(SMLoc Loc, const Twine &Msg, const char *Type,
130                     bool ShowLine = true) const;
131
132
133   /// GetMessage - Return an SMDiagnostic at the specified location with the
134   /// specified string.
135   ///
136   /// @param Type - If non-null, the kind of message (e.g., "error") which is
137   /// prefixed to the message.
138   /// @param ShowLine - Should the diagnostic show the source line.
139   SMDiagnostic GetMessage(SMLoc Loc,
140                           const Twine &Msg, const char *Type,
141                           bool ShowLine = true) const;
142
143
144 private:
145   void PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const;
146 };
147
148
149 /// SMDiagnostic - Instances of this class encapsulate one diagnostic report,
150 /// allowing printing to a raw_ostream as a caret diagnostic.
151 class SMDiagnostic {
152   const SourceMgr *SM;
153   SMLoc Loc;
154   std::string Filename;
155   int LineNo, ColumnNo;
156   std::string Message, LineContents;
157   unsigned ShowLine : 1;
158
159 public:
160   // Null diagnostic.
161   SMDiagnostic() : SM(0), LineNo(0), ColumnNo(0), ShowLine(0) {}
162   // Diagnostic with no location (e.g. file not found, command line arg error).
163   SMDiagnostic(const std::string &filename, const std::string &Msg,
164                bool showline = true)
165     : SM(0), Filename(filename), LineNo(-1), ColumnNo(-1),
166       Message(Msg), ShowLine(showline) {}
167   
168   // Diagnostic with a location.
169   SMDiagnostic(const SourceMgr &sm, SMLoc L, const std::string &FN,
170                int Line, int Col,
171                const std::string &Msg, const std::string &LineStr,
172                bool showline = true)
173     : SM(&sm), Loc(L), Filename(FN), LineNo(Line), ColumnNo(Col), Message(Msg),
174       LineContents(LineStr), ShowLine(showline) {}
175
176   const SourceMgr *getSourceMgr() const { return SM; }
177   SMLoc getLoc() const { return Loc; }
178   const std::string &getFilename() { return Filename; }
179   int getLineNo() const { return LineNo; }
180   int getColumnNo() const { return ColumnNo; }
181   const std::string &getMessage() const { return Message; }
182   const std::string &getLineContents() const { return LineContents; }
183   bool getShowLine() const { return ShowLine; }
184   
185   void Print(const char *ProgName, raw_ostream &S) const;
186 };
187
188 }  // end llvm namespace
189
190 #endif