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 | |
Chris Lattner | 7ee5d5f | 2009-06-21 05:06:04 +0000 | [diff] [blame] | 28 | /// AddIncludeFile - Search for a file with the specified name in the current |
| 29 | /// directory or in one of the IncludeDirs. If no file is found, this returns |
| 30 | /// ~0, otherwise it returns the buffer ID of the stacked file. |
| 31 | unsigned SourceMgr::AddIncludeFile(const std::string &Filename, |
| 32 | SMLoc IncludeLoc) { |
| 33 | |
| 34 | MemoryBuffer *NewBuf = MemoryBuffer::getFile(Filename.c_str()); |
| 35 | |
| 36 | // If the file didn't exist directly, see if it's in an include path. |
| 37 | for (unsigned i = 0, e = IncludeDirectories.size(); i != e && !NewBuf; ++i) { |
| 38 | std::string IncFile = IncludeDirectories[i] + "/" + Filename; |
| 39 | NewBuf = MemoryBuffer::getFile(IncFile.c_str()); |
| 40 | } |
| 41 | |
| 42 | if (NewBuf == 0) return ~0U; |
| 43 | |
| 44 | return AddNewSourceBuffer(NewBuf, IncludeLoc); |
| 45 | } |
| 46 | |
| 47 | |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 48 | /// FindBufferContainingLoc - Return the ID of the buffer containing the |
| 49 | /// specified location, returning -1 if not found. |
Chris Lattner | 8070ea3 | 2009-06-21 03:41:50 +0000 | [diff] [blame] | 50 | int SourceMgr::FindBufferContainingLoc(SMLoc Loc) const { |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 51 | for (unsigned i = 0, e = Buffers.size(); i != e; ++i) |
Chris Lattner | 1c8ae59 | 2009-03-13 16:01:53 +0000 | [diff] [blame] | 52 | if (Loc.getPointer() >= Buffers[i].Buffer->getBufferStart() && |
Chris Lattner | d477182 | 2009-03-18 20:36:45 +0000 | [diff] [blame] | 53 | // Use <= here so that a pointer to the null at the end of the buffer |
| 54 | // is included as part of the buffer. |
| 55 | Loc.getPointer() <= Buffers[i].Buffer->getBufferEnd()) |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 56 | return i; |
| 57 | return -1; |
| 58 | } |
| 59 | |
| 60 | /// FindLineNumber - Find the line number for the specified location in the |
| 61 | /// specified file. This is not a fast method. |
Chris Lattner | 8070ea3 | 2009-06-21 03:41:50 +0000 | [diff] [blame] | 62 | unsigned SourceMgr::FindLineNumber(SMLoc Loc, int BufferID) const { |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 63 | if (BufferID == -1) BufferID = FindBufferContainingLoc(Loc); |
| 64 | assert(BufferID != -1 && "Invalid Location!"); |
| 65 | |
| 66 | MemoryBuffer *Buff = getBufferInfo(BufferID).Buffer; |
| 67 | |
| 68 | // Count the number of \n's between the start of the file and the specified |
| 69 | // location. |
| 70 | unsigned LineNo = 1; |
| 71 | |
| 72 | const char *Ptr = Buff->getBufferStart(); |
| 73 | |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 74 | for (; SMLoc::getFromPointer(Ptr) != Loc; ++Ptr) |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 75 | if (*Ptr == '\n') ++LineNo; |
| 76 | return LineNo; |
| 77 | } |
| 78 | |
Chris Lattner | 2f510ae | 2009-07-02 22:24:20 +0000 | [diff] [blame] | 79 | void SourceMgr::PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const { |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 80 | if (IncludeLoc == SMLoc()) return; // Top of stack. |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 81 | |
| 82 | int CurBuf = FindBufferContainingLoc(IncludeLoc); |
| 83 | assert(CurBuf != -1 && "Invalid or unspecified location!"); |
| 84 | |
Chris Lattner | 2f510ae | 2009-07-02 22:24:20 +0000 | [diff] [blame] | 85 | PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS); |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 86 | |
Chris Lattner | 2f510ae | 2009-07-02 22:24:20 +0000 | [diff] [blame] | 87 | OS << "Included from " |
| 88 | << getBufferInfo(CurBuf).Buffer->getBufferIdentifier() |
| 89 | << ":" << FindLineNumber(IncludeLoc, CurBuf) << ":\n"; |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | |
Chris Lattner | eeb4a84 | 2009-07-02 23:08:13 +0000 | [diff] [blame^] | 93 | /// GetMessage - Return an SMDiagnostic at the specified location with the |
| 94 | /// specified string. |
| 95 | /// |
| 96 | /// @param Type - If non-null, the kind of message (e.g., "error") which is |
| 97 | /// prefixed to the message. |
| 98 | SMDiagnostic SourceMgr::GetMessage(SMLoc Loc, const std::string &Msg, |
| 99 | const char *Type) const { |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 100 | |
| 101 | // First thing to do: find the current buffer containing the specified |
| 102 | // location. |
Chris Lattner | 14ee48a | 2009-06-21 21:22:11 +0000 | [diff] [blame] | 103 | int CurBuf = FindBufferContainingLoc(Loc); |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 104 | assert(CurBuf != -1 && "Invalid or unspecified location!"); |
| 105 | |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 106 | MemoryBuffer *CurMB = getBufferInfo(CurBuf).Buffer; |
| 107 | |
| 108 | |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 109 | // Scan backward to find the start of the line. |
Chris Lattner | 14ee48a | 2009-06-21 21:22:11 +0000 | [diff] [blame] | 110 | const char *LineStart = Loc.getPointer(); |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 111 | while (LineStart != CurMB->getBufferStart() && |
| 112 | LineStart[-1] != '\n' && LineStart[-1] != '\r') |
| 113 | --LineStart; |
| 114 | // Get the end of the line. |
Chris Lattner | 14ee48a | 2009-06-21 21:22:11 +0000 | [diff] [blame] | 115 | const char *LineEnd = Loc.getPointer(); |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 116 | while (LineEnd != CurMB->getBufferEnd() && |
| 117 | LineEnd[0] != '\n' && LineEnd[0] != '\r') |
| 118 | ++LineEnd; |
Chris Lattner | 2f510ae | 2009-07-02 22:24:20 +0000 | [diff] [blame] | 119 | |
| 120 | std::string PrintedMsg; |
| 121 | if (Type) { |
| 122 | PrintedMsg = Type; |
| 123 | PrintedMsg += ": "; |
| 124 | } |
| 125 | PrintedMsg += Msg; |
| 126 | |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 127 | // Print out the line. |
Chris Lattner | eeb4a84 | 2009-07-02 23:08:13 +0000 | [diff] [blame^] | 128 | return SMDiagnostic(CurMB->getBufferIdentifier(), FindLineNumber(Loc, CurBuf), |
| 129 | Loc.getPointer()-LineStart, PrintedMsg, |
| 130 | std::string(LineStart, LineEnd)); |
| 131 | } |
| 132 | |
| 133 | void SourceMgr::PrintMessage(SMLoc Loc, const std::string &Msg, |
| 134 | const char *Type) const { |
| 135 | raw_ostream &OS = errs(); |
| 136 | |
| 137 | int CurBuf = FindBufferContainingLoc(Loc); |
| 138 | assert(CurBuf != -1 && "Invalid or unspecified location!"); |
| 139 | PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS); |
| 140 | |
| 141 | GetMessage(Loc, Msg, Type).Print(0, OS); |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 142 | } |
Chris Lattner | 2f510ae | 2009-07-02 22:24:20 +0000 | [diff] [blame] | 143 | |
| 144 | //===----------------------------------------------------------------------===// |
| 145 | // SMDiagnostic Implementation |
| 146 | //===----------------------------------------------------------------------===// |
| 147 | |
| 148 | void SMDiagnostic::Print(const char *ProgName, raw_ostream &S) { |
| 149 | if (ProgName && ProgName[0]) |
| 150 | S << ProgName << ": "; |
| 151 | |
| 152 | if (Filename == "-") |
| 153 | S << "<stdin>"; |
| 154 | else |
| 155 | S << Filename; |
| 156 | |
| 157 | if (LineNo != -1) { |
| 158 | S << ':' << LineNo; |
| 159 | if (ColumnNo != -1) |
| 160 | S << ':' << (ColumnNo+1); |
| 161 | } |
| 162 | |
| 163 | S << ": " << Message << '\n'; |
| 164 | |
| 165 | if (LineNo != -1 && ColumnNo != -1) { |
| 166 | S << LineContents << '\n'; |
| 167 | |
| 168 | // Print out spaces/tabs before the caret. |
| 169 | for (unsigned i = 0; i != unsigned(ColumnNo); ++i) |
| 170 | S << (LineContents[i] == '\t' ? '\t' : ' '); |
| 171 | S << "^\n"; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | |