SourceMgr.cpp revision 4afa12890f679034e9741a687a6ce33f2846f129
1//===- SourceMgr.cpp - Manager for Simple Source Buffers & Diagnostics ----===// 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 implements the SourceMgr class. This class is used as a simple 11// substrate for diagnostics, #include handling, and other low level things for 12// simple parsers. 13// 14//===----------------------------------------------------------------------===// 15 16#include "llvm/ADT/Twine.h" 17#include "llvm/Support/SourceMgr.h" 18#include "llvm/Support/MemoryBuffer.h" 19#include "llvm/Support/raw_ostream.h" 20using namespace llvm; 21 22namespace { 23 struct LineNoCacheTy { 24 int LastQueryBufferID; 25 const char *LastQuery; 26 unsigned LineNoOfQuery; 27 }; 28} 29 30static LineNoCacheTy *getCache(void *Ptr) { 31 return (LineNoCacheTy*)Ptr; 32} 33 34 35SourceMgr::~SourceMgr() { 36 // Delete the line # cache if allocated. 37 if (LineNoCacheTy *Cache = getCache(LineNoCache)) 38 delete Cache; 39 40 while (!Buffers.empty()) { 41 delete Buffers.back().Buffer; 42 Buffers.pop_back(); 43 } 44} 45 46/// AddIncludeFile - Search for a file with the specified name in the current 47/// directory or in one of the IncludeDirs. If no file is found, this returns 48/// ~0, otherwise it returns the buffer ID of the stacked file. 49unsigned SourceMgr::AddIncludeFile(const std::string &Filename, 50 SMLoc IncludeLoc) { 51 52 MemoryBuffer *NewBuf = MemoryBuffer::getFile(Filename.c_str()); 53 54 // If the file didn't exist directly, see if it's in an include path. 55 for (unsigned i = 0, e = IncludeDirectories.size(); i != e && !NewBuf; ++i) { 56 std::string IncFile = IncludeDirectories[i] + "/" + Filename; 57 NewBuf = MemoryBuffer::getFile(IncFile.c_str()); 58 } 59 60 if (NewBuf == 0) return ~0U; 61 62 return AddNewSourceBuffer(NewBuf, IncludeLoc); 63} 64 65 66/// FindBufferContainingLoc - Return the ID of the buffer containing the 67/// specified location, returning -1 if not found. 68int SourceMgr::FindBufferContainingLoc(SMLoc Loc) const { 69 for (unsigned i = 0, e = Buffers.size(); i != e; ++i) 70 if (Loc.getPointer() >= Buffers[i].Buffer->getBufferStart() && 71 // Use <= here so that a pointer to the null at the end of the buffer 72 // is included as part of the buffer. 73 Loc.getPointer() <= Buffers[i].Buffer->getBufferEnd()) 74 return i; 75 return -1; 76} 77 78/// FindLineNumber - Find the line number for the specified location in the 79/// specified file. This is not a fast method. 80unsigned SourceMgr::FindLineNumber(SMLoc Loc, int BufferID) const { 81 if (BufferID == -1) BufferID = FindBufferContainingLoc(Loc); 82 assert(BufferID != -1 && "Invalid Location!"); 83 84 MemoryBuffer *Buff = getBufferInfo(BufferID).Buffer; 85 86 // Count the number of \n's between the start of the file and the specified 87 // location. 88 unsigned LineNo = 1; 89 90 const char *Ptr = Buff->getBufferStart(); 91 92 // If we have a line number cache, and if the query is to a later point in the 93 // same file, start searching from the last query location. This optimizes 94 // for the case when multiple diagnostics come out of one file in order. 95 if (LineNoCacheTy *Cache = getCache(LineNoCache)) 96 if (Cache->LastQueryBufferID == BufferID && 97 Cache->LastQuery <= Loc.getPointer()) { 98 Ptr = Cache->LastQuery; 99 LineNo = Cache->LineNoOfQuery; 100 } 101 102 // Scan for the location being queried, keeping track of the number of lines 103 // we see. 104 for (; SMLoc::getFromPointer(Ptr) != Loc; ++Ptr) 105 if (*Ptr == '\n') ++LineNo; 106 107 108 // Allocate the line number cache if it doesn't exist. 109 if (LineNoCache == 0) 110 LineNoCache = new LineNoCacheTy(); 111 112 // Update the line # cache. 113 LineNoCacheTy &Cache = *getCache(LineNoCache); 114 Cache.LastQueryBufferID = BufferID; 115 Cache.LastQuery = Ptr; 116 Cache.LineNoOfQuery = LineNo; 117 return LineNo; 118} 119 120void SourceMgr::PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const { 121 if (IncludeLoc == SMLoc()) return; // Top of stack. 122 123 int CurBuf = FindBufferContainingLoc(IncludeLoc); 124 assert(CurBuf != -1 && "Invalid or unspecified location!"); 125 126 PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS); 127 128 OS << "Included from " 129 << getBufferInfo(CurBuf).Buffer->getBufferIdentifier() 130 << ":" << FindLineNumber(IncludeLoc, CurBuf) << ":\n"; 131} 132 133 134/// GetMessage - Return an SMDiagnostic at the specified location with the 135/// specified string. 136/// 137/// @param Type - If non-null, the kind of message (e.g., "error") which is 138/// prefixed to the message. 139SMDiagnostic SourceMgr::GetMessage(SMLoc Loc, const Twine &Msg, 140 const char *Type, bool ShowLine) const { 141 142 // First thing to do: find the current buffer containing the specified 143 // location. 144 int CurBuf = FindBufferContainingLoc(Loc); 145 assert(CurBuf != -1 && "Invalid or unspecified location!"); 146 147 MemoryBuffer *CurMB = getBufferInfo(CurBuf).Buffer; 148 149 // Scan backward to find the start of the line. 150 const char *LineStart = Loc.getPointer(); 151 while (LineStart != CurMB->getBufferStart() && 152 LineStart[-1] != '\n' && LineStart[-1] != '\r') 153 --LineStart; 154 155 std::string LineStr; 156 if (ShowLine) { 157 // Get the end of the line. 158 const char *LineEnd = Loc.getPointer(); 159 while (LineEnd != CurMB->getBufferEnd() && 160 LineEnd[0] != '\n' && LineEnd[0] != '\r') 161 ++LineEnd; 162 LineStr = std::string(LineStart, LineEnd); 163 } 164 165 std::string PrintedMsg; 166 raw_string_ostream OS(PrintedMsg); 167 if (Type) 168 OS << Type << ": "; 169 OS << Msg; 170 171 return SMDiagnostic(*this, Loc, 172 CurMB->getBufferIdentifier(), FindLineNumber(Loc, CurBuf), 173 Loc.getPointer()-LineStart, OS.str(), 174 LineStr, ShowLine); 175} 176 177void SourceMgr::PrintMessage(SMLoc Loc, const Twine &Msg, 178 const char *Type, bool ShowLine) const { 179 // Report the message with the diagnostic handler if present. 180 if (DiagHandler) { 181 DiagHandler(GetMessage(Loc, Msg, Type, ShowLine), DiagContext); 182 return; 183 } 184 185 raw_ostream &OS = errs(); 186 187 int CurBuf = FindBufferContainingLoc(Loc); 188 assert(CurBuf != -1 && "Invalid or unspecified location!"); 189 PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS); 190 191 GetMessage(Loc, Msg, Type, ShowLine).Print(0, OS); 192} 193 194//===----------------------------------------------------------------------===// 195// SMDiagnostic Implementation 196//===----------------------------------------------------------------------===// 197 198void SMDiagnostic::Print(const char *ProgName, raw_ostream &S) const { 199 if (ProgName && ProgName[0]) 200 S << ProgName << ": "; 201 202 if (!Filename.empty()) { 203 if (Filename == "-") 204 S << "<stdin>"; 205 else 206 S << Filename; 207 208 if (LineNo != -1) { 209 S << ':' << LineNo; 210 if (ColumnNo != -1) 211 S << ':' << (ColumnNo+1); 212 } 213 S << ": "; 214 } 215 216 S << Message << '\n'; 217 218 if (LineNo != -1 && ColumnNo != -1 && ShowLine) { 219 S << LineContents << '\n'; 220 221 // Print out spaces/tabs before the caret. 222 for (unsigned i = 0; i != unsigned(ColumnNo); ++i) 223 S << (LineContents[i] == '\t' ? '\t' : ' '); 224 S << "^\n"; 225 } 226} 227 228 229