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