Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 1 | //===- TGSourceMgr.cpp - Manager for 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 TGSourceMgr class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "TGSourceMgr.h" |
| 15 | #include "llvm/Support/MemoryBuffer.h" |
| 16 | #include "llvm/Support/raw_ostream.h" |
| 17 | using namespace llvm; |
| 18 | |
| 19 | TGSourceMgr::~TGSourceMgr() { |
| 20 | while (!Buffers.empty()) { |
| 21 | delete Buffers.back().Buffer; |
| 22 | Buffers.pop_back(); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | /// FindBufferContainingLoc - Return the ID of the buffer containing the |
| 27 | /// specified location, returning -1 if not found. |
Chris Lattner | 1c8ae59 | 2009-03-13 16:01:53 +0000 | [diff] [blame^] | 28 | int TGSourceMgr::FindBufferContainingLoc(TGLoc Loc) const { |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 29 | for (unsigned i = 0, e = Buffers.size(); i != e; ++i) |
Chris Lattner | 1c8ae59 | 2009-03-13 16:01:53 +0000 | [diff] [blame^] | 30 | if (Loc.getPointer() >= Buffers[i].Buffer->getBufferStart() && |
| 31 | Loc.getPointer() < Buffers[i].Buffer->getBufferEnd()) |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 32 | return i; |
| 33 | return -1; |
| 34 | } |
| 35 | |
| 36 | /// FindLineNumber - Find the line number for the specified location in the |
| 37 | /// specified file. This is not a fast method. |
Chris Lattner | 1c8ae59 | 2009-03-13 16:01:53 +0000 | [diff] [blame^] | 38 | unsigned TGSourceMgr::FindLineNumber(TGLoc Loc, int BufferID) const { |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 39 | if (BufferID == -1) BufferID = FindBufferContainingLoc(Loc); |
| 40 | assert(BufferID != -1 && "Invalid Location!"); |
| 41 | |
| 42 | MemoryBuffer *Buff = getBufferInfo(BufferID).Buffer; |
| 43 | |
| 44 | // Count the number of \n's between the start of the file and the specified |
| 45 | // location. |
| 46 | unsigned LineNo = 1; |
| 47 | |
| 48 | const char *Ptr = Buff->getBufferStart(); |
| 49 | |
Chris Lattner | 1c8ae59 | 2009-03-13 16:01:53 +0000 | [diff] [blame^] | 50 | for (; TGLoc::getFromPointer(Ptr) != Loc; ++Ptr) |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 51 | if (*Ptr == '\n') ++LineNo; |
| 52 | return LineNo; |
| 53 | } |
| 54 | |
Chris Lattner | 1c8ae59 | 2009-03-13 16:01:53 +0000 | [diff] [blame^] | 55 | void TGSourceMgr::PrintIncludeStack(TGLoc IncludeLoc) const { |
| 56 | if (IncludeLoc == TGLoc()) return; // Top of stack. |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 57 | |
| 58 | int CurBuf = FindBufferContainingLoc(IncludeLoc); |
| 59 | assert(CurBuf != -1 && "Invalid or unspecified location!"); |
| 60 | |
| 61 | PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc); |
| 62 | |
| 63 | errs() << "Included from " |
| 64 | << getBufferInfo(CurBuf).Buffer->getBufferIdentifier() |
| 65 | << ":" << FindLineNumber(IncludeLoc, CurBuf) << ":\n"; |
| 66 | } |
| 67 | |
| 68 | |
Chris Lattner | 1c8ae59 | 2009-03-13 16:01:53 +0000 | [diff] [blame^] | 69 | void TGSourceMgr::PrintError(TGLoc ErrorLoc, const std::string &Msg) const { |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 70 | raw_ostream &OS = errs(); |
| 71 | |
| 72 | // First thing to do: find the current buffer containing the specified |
| 73 | // location. |
| 74 | int CurBuf = FindBufferContainingLoc(ErrorLoc); |
| 75 | assert(CurBuf != -1 && "Invalid or unspecified location!"); |
| 76 | |
| 77 | PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc); |
| 78 | |
| 79 | MemoryBuffer *CurMB = getBufferInfo(CurBuf).Buffer; |
| 80 | |
| 81 | |
| 82 | OS << "Parsing " << CurMB->getBufferIdentifier() << ":" |
| 83 | << FindLineNumber(ErrorLoc, CurBuf) << ": "; |
| 84 | |
| 85 | OS << Msg << "\n"; |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 86 | |
| 87 | // Scan backward to find the start of the line. |
Chris Lattner | 1c8ae59 | 2009-03-13 16:01:53 +0000 | [diff] [blame^] | 88 | const char *LineStart = ErrorLoc.getPointer(); |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 89 | while (LineStart != CurMB->getBufferStart() && |
| 90 | LineStart[-1] != '\n' && LineStart[-1] != '\r') |
| 91 | --LineStart; |
| 92 | // Get the end of the line. |
Chris Lattner | 1c8ae59 | 2009-03-13 16:01:53 +0000 | [diff] [blame^] | 93 | const char *LineEnd = ErrorLoc.getPointer(); |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 94 | while (LineEnd != CurMB->getBufferEnd() && |
| 95 | LineEnd[0] != '\n' && LineEnd[0] != '\r') |
| 96 | ++LineEnd; |
| 97 | // Print out the line. |
| 98 | OS << std::string(LineStart, LineEnd) << "\n"; |
| 99 | // Print out spaces before the carat. |
Chris Lattner | 1c8ae59 | 2009-03-13 16:01:53 +0000 | [diff] [blame^] | 100 | for (const char *Pos = LineStart; Pos != ErrorLoc.getPointer(); ++Pos) |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 101 | OS << (*Pos == '\t' ? '\t' : ' '); |
| 102 | OS << "^\n"; |
| 103 | } |