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