]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - include/llvm/Support/SourceMgr.h
Update llvm to r84119.
[FreeBSD/FreeBSD.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 SMLoc, 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 <string>
20 #include <vector>
21 #include <cassert>
22
23 namespace llvm {
24   class MemoryBuffer;
25   class SourceMgr;
26   class SMDiagnostic;
27   class raw_ostream;
28   
29 class SMLoc {
30   const char *Ptr;
31 public:
32   SMLoc() : Ptr(0) {}
33   SMLoc(const SMLoc &RHS) : Ptr(RHS.Ptr) {}
34   
35   bool isValid() const { return Ptr != 0; }
36
37   bool operator==(const SMLoc &RHS) const { return RHS.Ptr == Ptr; }
38   bool operator!=(const SMLoc &RHS) const { return RHS.Ptr != Ptr; }
39
40   const char *getPointer() const { return Ptr; }
41   
42   static SMLoc getFromPointer(const char *Ptr) {
43     SMLoc L;
44     L.Ptr = Ptr;
45     return L;
46   }
47 };
48
49 /// SourceMgr - This owns the files read by a parser, handles include stacks,
50 /// and handles diagnostic wrangling.
51 class 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   /// LineNoCache - This is a cache for line number queries, its implementation
69   /// is really private to SourceMgr.cpp.
70   mutable void *LineNoCache;
71   
72   SourceMgr(const SourceMgr&);    // DO NOT IMPLEMENT
73   void operator=(const SourceMgr&); // DO NOT IMPLEMENT
74 public:
75   SourceMgr() : LineNoCache(0) {}
76   ~SourceMgr();
77   
78   void setIncludeDirs(const std::vector<std::string> &Dirs) {
79     IncludeDirectories = Dirs;
80   }
81   
82   const SrcBuffer &getBufferInfo(unsigned i) const {
83     assert(i < Buffers.size() && "Invalid Buffer ID!");
84     return Buffers[i];
85   }
86
87   const MemoryBuffer *getMemoryBuffer(unsigned i) const {
88     assert(i < Buffers.size() && "Invalid Buffer ID!");
89     return Buffers[i].Buffer;
90   }
91   
92   SMLoc getParentIncludeLoc(unsigned i) const {
93     assert(i < Buffers.size() && "Invalid Buffer ID!");
94     return Buffers[i].IncludeLoc;
95   }
96   
97   unsigned AddNewSourceBuffer(MemoryBuffer *F, SMLoc IncludeLoc) {
98     SrcBuffer NB;
99     NB.Buffer = F;
100     NB.IncludeLoc = IncludeLoc;
101     Buffers.push_back(NB);
102     return Buffers.size()-1;
103   }
104   
105   /// AddIncludeFile - Search for a file with the specified name in the current
106   /// directory or in one of the IncludeDirs.  If no file is found, this returns
107   /// ~0, otherwise it returns the buffer ID of the stacked file.
108   unsigned AddIncludeFile(const std::string &Filename, SMLoc IncludeLoc);
109   
110   /// FindBufferContainingLoc - Return the ID of the buffer containing the
111   /// specified location, returning -1 if not found.
112   int FindBufferContainingLoc(SMLoc Loc) const;
113   
114   /// FindLineNumber - Find the line number for the specified location in the
115   /// specified file.  This is not a fast method.
116   unsigned FindLineNumber(SMLoc Loc, int BufferID = -1) const;
117   
118   /// PrintMessage - Emit a message about the specified location with the
119   /// specified string.
120   ///
121   /// @param Type - If non-null, the kind of message (e.g., "error") which is
122   /// prefixed to the message.
123   void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const;
124   
125   
126   /// GetMessage - Return an SMDiagnostic at the specified location with the
127   /// specified string.
128   ///
129   /// @param Type - If non-null, the kind of message (e.g., "error") which is
130   /// prefixed to the message.
131   SMDiagnostic GetMessage(SMLoc Loc,
132                           const std::string &Msg, const char *Type) const;
133   
134   
135 private:
136   void PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const;
137 };
138
139   
140 /// SMDiagnostic - Instances of this class encapsulate one diagnostic report,
141 /// allowing printing to a raw_ostream as a caret diagnostic.
142 class SMDiagnostic {
143   std::string Filename;
144   int LineNo, ColumnNo;
145   std::string Message, LineContents;
146 public:
147   SMDiagnostic() : LineNo(0), ColumnNo(0) {}
148   SMDiagnostic(const std::string &FN, int Line, int Col,
149                const std::string &Msg, const std::string &LineStr)
150     : Filename(FN), LineNo(Line), ColumnNo(Col), Message(Msg),
151       LineContents(LineStr) {}
152
153   void Print(const char *ProgName, raw_ostream &S);
154 };
155   
156 }  // end llvm namespace
157
158 #endif