SourceMgr.cpp revision 198090
122347Spst//===- SourceMgr.cpp - Manager for Simple Source Buffers & Diagnostics ----===// 222347Spst// 322347Spst// The LLVM Compiler Infrastructure 422347Spst// 522347Spst// This file is distributed under the University of Illinois Open Source 622347Spst// License. See LICENSE.TXT for details. 722347Spst// 822347Spst//===----------------------------------------------------------------------===// 922347Spst// 1022347Spst// This file implements the SourceMgr class. This class is used as a simple 1122347Spst// substrate for diagnostics, #include handling, and other low level things for 1222347Spst// simple parsers. 1322347Spst// 1422347Spst//===----------------------------------------------------------------------===// 1522347Spst 1622347Spst#include "llvm/Support/SourceMgr.h" 1722347Spst#include "llvm/Support/MemoryBuffer.h" 1822347Spst#include "llvm/Support/raw_ostream.h" 1922347Spstusing namespace llvm; 2022347Spst 2122347Spstnamespace { 2222347Spst struct LineNoCacheTy { 2322347Spst int LastQueryBufferID; 2422347Spst const char *LastQuery; 2522347Spst unsigned LineNoOfQuery; 2622347Spst }; 2722347Spst} 2822347Spst 2922347Spststatic LineNoCacheTy *getCache(void *Ptr) { 3022347Spst return (LineNoCacheTy*)Ptr; 3122347Spst} 3222347Spst 3322347Spst 3422347SpstSourceMgr::~SourceMgr() { 3522347Spst // Delete the line # cache if allocated. 3622347Spst if (LineNoCacheTy *Cache = getCache(LineNoCache)) 3722347Spst delete Cache; 3822347Spst 3922347Spst while (!Buffers.empty()) { 4022347Spst delete Buffers.back().Buffer; 4122347Spst Buffers.pop_back(); 4222347Spst } 4322347Spst} 4422347Spst 4522347Spst/// AddIncludeFile - Search for a file with the specified name in the current 4622347Spst/// directory or in one of the IncludeDirs. If no file is found, this returns 4722347Spst/// ~0, otherwise it returns the buffer ID of the stacked file. 4822347Spstunsigned SourceMgr::AddIncludeFile(const std::string &Filename, 4922347Spst SMLoc IncludeLoc) { 5022347Spst 5122347Spst MemoryBuffer *NewBuf = MemoryBuffer::getFile(Filename.c_str()); 5222347Spst 5322347Spst // If the file didn't exist directly, see if it's in an include path. 5422347Spst for (unsigned i = 0, e = IncludeDirectories.size(); i != e && !NewBuf; ++i) { 5522347Spst std::string IncFile = IncludeDirectories[i] + "/" + Filename; 5622347Spst NewBuf = MemoryBuffer::getFile(IncFile.c_str()); 5722347Spst } 5822347Spst 5922347Spst if (NewBuf == 0) return ~0U; 6022347Spst 6122347Spst return AddNewSourceBuffer(NewBuf, IncludeLoc); 6222347Spst} 6322347Spst 6422347Spst 6522347Spst/// FindBufferContainingLoc - Return the ID of the buffer containing the 6622347Spst/// specified location, returning -1 if not found. 6722347Spstint SourceMgr::FindBufferContainingLoc(SMLoc Loc) const { 6822347Spst for (unsigned i = 0, e = Buffers.size(); i != e; ++i) 6922347Spst if (Loc.getPointer() >= Buffers[i].Buffer->getBufferStart() && 7022347Spst // Use <= here so that a pointer to the null at the end of the buffer 7122347Spst // is included as part of the buffer. 7222347Spst Loc.getPointer() <= Buffers[i].Buffer->getBufferEnd()) 7322347Spst return i; 7422347Spst return -1; 7522347Spst} 7622347Spst 7722347Spst/// FindLineNumber - Find the line number for the specified location in the 7822347Spst/// specified file. This is not a fast method. 7922347Spstunsigned SourceMgr::FindLineNumber(SMLoc Loc, int BufferID) const { 8022347Spst if (BufferID == -1) BufferID = FindBufferContainingLoc(Loc); 8122347Spst assert(BufferID != -1 && "Invalid Location!"); 8222347Spst 8322347Spst MemoryBuffer *Buff = getBufferInfo(BufferID).Buffer; 8422347Spst 8522347Spst // Count the number of \n's between the start of the file and the specified 8622347Spst // location. 8722347Spst unsigned LineNo = 1; 8822347Spst 89 const char *Ptr = Buff->getBufferStart(); 90 91 // If we have a line number cache, and if the query is to a later point in the 92 // same file, start searching from the last query location. This optimizes 93 // for the case when multiple diagnostics come out of one file in order. 94 if (LineNoCacheTy *Cache = getCache(LineNoCache)) 95 if (Cache->LastQueryBufferID == BufferID && 96 Cache->LastQuery <= Loc.getPointer()) { 97 Ptr = Cache->LastQuery; 98 LineNo = Cache->LineNoOfQuery; 99 } 100 101 // Scan for the location being queried, keeping track of the number of lines 102 // we see. 103 for (; SMLoc::getFromPointer(Ptr) != Loc; ++Ptr) 104 if (*Ptr == '\n') ++LineNo; 105 106 107 // Allocate the line number cache if it doesn't exist. 108 if (LineNoCache == 0) 109 LineNoCache = new LineNoCacheTy(); 110 111 // Update the line # cache. 112 LineNoCacheTy &Cache = *getCache(LineNoCache); 113 Cache.LastQueryBufferID = BufferID; 114 Cache.LastQuery = Ptr; 115 Cache.LineNoOfQuery = LineNo; 116 return LineNo; 117} 118 119void SourceMgr::PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const { 120 if (IncludeLoc == SMLoc()) return; // Top of stack. 121 122 int CurBuf = FindBufferContainingLoc(IncludeLoc); 123 assert(CurBuf != -1 && "Invalid or unspecified location!"); 124 125 PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS); 126 127 OS << "Included from " 128 << getBufferInfo(CurBuf).Buffer->getBufferIdentifier() 129 << ":" << FindLineNumber(IncludeLoc, CurBuf) << ":\n"; 130} 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. 138SMDiagnostic SourceMgr::GetMessage(SMLoc Loc, const std::string &Msg, 139 const char *Type) const { 140 141 // First thing to do: find the current buffer containing the specified 142 // location. 143 int CurBuf = FindBufferContainingLoc(Loc); 144 assert(CurBuf != -1 && "Invalid or unspecified location!"); 145 146 MemoryBuffer *CurMB = getBufferInfo(CurBuf).Buffer; 147 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 // Get the end of the line. 155 const char *LineEnd = Loc.getPointer(); 156 while (LineEnd != CurMB->getBufferEnd() && 157 LineEnd[0] != '\n' && LineEnd[0] != '\r') 158 ++LineEnd; 159 160 std::string PrintedMsg; 161 if (Type) { 162 PrintedMsg = Type; 163 PrintedMsg += ": "; 164 } 165 PrintedMsg += Msg; 166 167 // Print out the line. 168 return SMDiagnostic(CurMB->getBufferIdentifier(), FindLineNumber(Loc, CurBuf), 169 Loc.getPointer()-LineStart, PrintedMsg, 170 std::string(LineStart, LineEnd)); 171} 172 173void SourceMgr::PrintMessage(SMLoc Loc, const std::string &Msg, 174 const char *Type) const { 175 raw_ostream &OS = errs(); 176 177 int CurBuf = FindBufferContainingLoc(Loc); 178 assert(CurBuf != -1 && "Invalid or unspecified location!"); 179 PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS); 180 181 GetMessage(Loc, Msg, Type).Print(0, OS); 182} 183 184//===----------------------------------------------------------------------===// 185// SMDiagnostic Implementation 186//===----------------------------------------------------------------------===// 187 188void SMDiagnostic::Print(const char *ProgName, raw_ostream &S) { 189 if (ProgName && ProgName[0]) 190 S << ProgName << ": "; 191 192 if (Filename == "-") 193 S << "<stdin>"; 194 else 195 S << Filename; 196 197 if (LineNo != -1) { 198 S << ':' << LineNo; 199 if (ColumnNo != -1) 200 S << ':' << (ColumnNo+1); 201 } 202 203 S << ": " << Message << '\n'; 204 205 if (LineNo != -1 && ColumnNo != -1) { 206 S << LineContents << '\n'; 207 208 // Print out spaces/tabs before the caret. 209 for (unsigned i = 0; i != unsigned(ColumnNo); ++i) 210 S << (LineContents[i] == '\t' ? '\t' : ' '); 211 S << "^\n"; 212 } 213} 214 215 216