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