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 | |
Benjamin Kramer | d1e1703 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Twine.h" |
Chris Lattner | 099e198 | 2009-06-21 03:36:54 +0000 | [diff] [blame] | 17 | #include "llvm/Support/SourceMgr.h" |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 18 | #include "llvm/Support/MemoryBuffer.h" |
Michael J. Spencer | 3ff9563 | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/OwningPtr.h" |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | 333fb04 | 2010-12-09 17:36:48 +0000 | [diff] [blame] | 21 | #include "llvm/Support/system_error.h" |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | |
Chris Lattner | 1d96ccc | 2009-08-11 17:49:14 +0000 | [diff] [blame] | 24 | namespace { |
| 25 | struct LineNoCacheTy { |
| 26 | int LastQueryBufferID; |
| 27 | const char *LastQuery; |
| 28 | unsigned LineNoOfQuery; |
| 29 | }; |
| 30 | } |
| 31 | |
| 32 | static LineNoCacheTy *getCache(void *Ptr) { |
| 33 | return (LineNoCacheTy*)Ptr; |
| 34 | } |
| 35 | |
| 36 | |
Chris Lattner | 8070ea3 | 2009-06-21 03:41:50 +0000 | [diff] [blame] | 37 | SourceMgr::~SourceMgr() { |
Chris Lattner | 1d96ccc | 2009-08-11 17:49:14 +0000 | [diff] [blame] | 38 | // Delete the line # cache if allocated. |
| 39 | if (LineNoCacheTy *Cache = getCache(LineNoCache)) |
| 40 | delete Cache; |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 41 | |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 42 | while (!Buffers.empty()) { |
| 43 | delete Buffers.back().Buffer; |
| 44 | Buffers.pop_back(); |
| 45 | } |
| 46 | } |
| 47 | |
Chris Lattner | 7ee5d5f | 2009-06-21 05:06:04 +0000 | [diff] [blame] | 48 | /// AddIncludeFile - Search for a file with the specified name in the current |
| 49 | /// directory or in one of the IncludeDirs. If no file is found, this returns |
| 50 | /// ~0, otherwise it returns the buffer ID of the stacked file. |
| 51 | unsigned SourceMgr::AddIncludeFile(const std::string &Filename, |
Joerg Sonnenberger | dd13790 | 2011-06-01 13:10:15 +0000 | [diff] [blame] | 52 | SMLoc IncludeLoc, |
| 53 | std::string &IncludedFile) { |
Michael J. Spencer | 3ff9563 | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 54 | OwningPtr<MemoryBuffer> NewBuf; |
Joerg Sonnenberger | dd13790 | 2011-06-01 13:10:15 +0000 | [diff] [blame] | 55 | IncludedFile = Filename; |
| 56 | MemoryBuffer::getFile(IncludedFile.c_str(), NewBuf); |
Chris Lattner | 7ee5d5f | 2009-06-21 05:06:04 +0000 | [diff] [blame] | 57 | |
| 58 | // If the file didn't exist directly, see if it's in an include path. |
| 59 | for (unsigned i = 0, e = IncludeDirectories.size(); i != e && !NewBuf; ++i) { |
Joerg Sonnenberger | dd13790 | 2011-06-01 13:10:15 +0000 | [diff] [blame] | 60 | IncludedFile = IncludeDirectories[i] + "/" + Filename; |
| 61 | MemoryBuffer::getFile(IncludedFile.c_str(), NewBuf); |
Chris Lattner | 7ee5d5f | 2009-06-21 05:06:04 +0000 | [diff] [blame] | 62 | } |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 63 | |
Chris Lattner | 7ee5d5f | 2009-06-21 05:06:04 +0000 | [diff] [blame] | 64 | if (NewBuf == 0) return ~0U; |
| 65 | |
Michael J. Spencer | 3ff9563 | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 66 | return AddNewSourceBuffer(NewBuf.take(), IncludeLoc); |
Chris Lattner | 7ee5d5f | 2009-06-21 05:06:04 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 70 | /// FindBufferContainingLoc - Return the ID of the buffer containing the |
| 71 | /// specified location, returning -1 if not found. |
Chris Lattner | 8070ea3 | 2009-06-21 03:41:50 +0000 | [diff] [blame] | 72 | int SourceMgr::FindBufferContainingLoc(SMLoc Loc) const { |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 73 | for (unsigned i = 0, e = Buffers.size(); i != e; ++i) |
Chris Lattner | 1c8ae59 | 2009-03-13 16:01:53 +0000 | [diff] [blame] | 74 | if (Loc.getPointer() >= Buffers[i].Buffer->getBufferStart() && |
Chris Lattner | d477182 | 2009-03-18 20:36:45 +0000 | [diff] [blame] | 75 | // Use <= here so that a pointer to the null at the end of the buffer |
| 76 | // is included as part of the buffer. |
| 77 | Loc.getPointer() <= Buffers[i].Buffer->getBufferEnd()) |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 78 | return i; |
| 79 | return -1; |
| 80 | } |
| 81 | |
| 82 | /// FindLineNumber - Find the line number for the specified location in the |
| 83 | /// specified file. This is not a fast method. |
Chris Lattner | 8070ea3 | 2009-06-21 03:41:50 +0000 | [diff] [blame] | 84 | unsigned SourceMgr::FindLineNumber(SMLoc Loc, int BufferID) const { |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 85 | if (BufferID == -1) BufferID = FindBufferContainingLoc(Loc); |
| 86 | assert(BufferID != -1 && "Invalid Location!"); |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 87 | |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 88 | MemoryBuffer *Buff = getBufferInfo(BufferID).Buffer; |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 89 | |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 90 | // Count the number of \n's between the start of the file and the specified |
| 91 | // location. |
| 92 | unsigned LineNo = 1; |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 93 | |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 94 | const char *Ptr = Buff->getBufferStart(); |
| 95 | |
Chris Lattner | 1d96ccc | 2009-08-11 17:49:14 +0000 | [diff] [blame] | 96 | // If we have a line number cache, and if the query is to a later point in the |
| 97 | // same file, start searching from the last query location. This optimizes |
| 98 | // for the case when multiple diagnostics come out of one file in order. |
| 99 | if (LineNoCacheTy *Cache = getCache(LineNoCache)) |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 100 | if (Cache->LastQueryBufferID == BufferID && |
Chris Lattner | 1d96ccc | 2009-08-11 17:49:14 +0000 | [diff] [blame] | 101 | Cache->LastQuery <= Loc.getPointer()) { |
| 102 | Ptr = Cache->LastQuery; |
| 103 | LineNo = Cache->LineNoOfQuery; |
| 104 | } |
| 105 | |
| 106 | // Scan for the location being queried, keeping track of the number of lines |
| 107 | // we see. |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 108 | for (; SMLoc::getFromPointer(Ptr) != Loc; ++Ptr) |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 109 | if (*Ptr == '\n') ++LineNo; |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 110 | |
| 111 | |
Chris Lattner | 1d96ccc | 2009-08-11 17:49:14 +0000 | [diff] [blame] | 112 | // Allocate the line number cache if it doesn't exist. |
| 113 | if (LineNoCache == 0) |
| 114 | LineNoCache = new LineNoCacheTy(); |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 115 | |
Chris Lattner | 1d96ccc | 2009-08-11 17:49:14 +0000 | [diff] [blame] | 116 | // Update the line # cache. |
| 117 | LineNoCacheTy &Cache = *getCache(LineNoCache); |
| 118 | Cache.LastQueryBufferID = BufferID; |
| 119 | Cache.LastQuery = Ptr; |
| 120 | Cache.LineNoOfQuery = LineNo; |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 121 | return LineNo; |
| 122 | } |
| 123 | |
Chris Lattner | 2f510ae | 2009-07-02 22:24:20 +0000 | [diff] [blame] | 124 | void SourceMgr::PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const { |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 125 | if (IncludeLoc == SMLoc()) return; // Top of stack. |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 126 | |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 127 | int CurBuf = FindBufferContainingLoc(IncludeLoc); |
| 128 | assert(CurBuf != -1 && "Invalid or unspecified location!"); |
| 129 | |
Chris Lattner | 2f510ae | 2009-07-02 22:24:20 +0000 | [diff] [blame] | 130 | PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS); |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 131 | |
Chris Lattner | 2f510ae | 2009-07-02 22:24:20 +0000 | [diff] [blame] | 132 | OS << "Included from " |
| 133 | << getBufferInfo(CurBuf).Buffer->getBufferIdentifier() |
| 134 | << ":" << FindLineNumber(IncludeLoc, CurBuf) << ":\n"; |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | |
Chris Lattner | eeb4a84 | 2009-07-02 23:08:13 +0000 | [diff] [blame] | 138 | /// GetMessage - Return an SMDiagnostic at the specified location with the |
| 139 | /// specified string. |
| 140 | /// |
| 141 | /// @param Type - If non-null, the kind of message (e.g., "error") which is |
| 142 | /// prefixed to the message. |
Benjamin Kramer | d1e1703 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 143 | SMDiagnostic SourceMgr::GetMessage(SMLoc Loc, const Twine &Msg, |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame^] | 144 | const char *Type, ArrayRef<SMRange> Ranges, |
| 145 | bool ShowLine) const { |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 146 | |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 147 | // First thing to do: find the current buffer containing the specified |
| 148 | // location. |
Chris Lattner | 14ee48a | 2009-06-21 21:22:11 +0000 | [diff] [blame] | 149 | int CurBuf = FindBufferContainingLoc(Loc); |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 150 | assert(CurBuf != -1 && "Invalid or unspecified location!"); |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 151 | |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 152 | MemoryBuffer *CurMB = getBufferInfo(CurBuf).Buffer; |
Daniel Dunbar | 4153982 | 2009-11-22 22:08:00 +0000 | [diff] [blame] | 153 | |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 154 | // Scan backward to find the start of the line. |
Chris Lattner | 14ee48a | 2009-06-21 21:22:11 +0000 | [diff] [blame] | 155 | const char *LineStart = Loc.getPointer(); |
Daniel Dunbar | 4153982 | 2009-11-22 22:08:00 +0000 | [diff] [blame] | 156 | while (LineStart != CurMB->getBufferStart() && |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 157 | LineStart[-1] != '\n' && LineStart[-1] != '\r') |
| 158 | --LineStart; |
Daniel Dunbar | 4153982 | 2009-11-22 22:08:00 +0000 | [diff] [blame] | 159 | |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame^] | 160 | // Get the end of the line. |
| 161 | const char *LineEnd = Loc.getPointer(); |
| 162 | while (LineEnd != CurMB->getBufferEnd() && |
| 163 | LineEnd[0] != '\n' && LineEnd[0] != '\r') |
| 164 | ++LineEnd; |
| 165 | std::string LineStr(LineStart, LineEnd); |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 166 | |
Chris Lattner | 2f510ae | 2009-07-02 22:24:20 +0000 | [diff] [blame] | 167 | std::string PrintedMsg; |
Benjamin Kramer | d1e1703 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 168 | raw_string_ostream OS(PrintedMsg); |
| 169 | if (Type) |
| 170 | OS << Type << ": "; |
| 171 | OS << Msg; |
Daniel Dunbar | 4153982 | 2009-11-22 22:08:00 +0000 | [diff] [blame] | 172 | |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame^] | 173 | // Convert any ranges to column ranges that only intersect the line of the |
| 174 | // location. |
| 175 | SmallVector<std::pair<unsigned, unsigned>, 4> ColRanges; |
| 176 | for (unsigned i = 0, e = Ranges.size(); i != e; ++i) { |
| 177 | SMRange R = Ranges[i]; |
| 178 | if (!R.isValid()) continue; |
| 179 | |
| 180 | // If the line doesn't contain any part of the range, then ignore it. |
| 181 | if (R.Start.getPointer() > LineEnd || R.End.getPointer() < LineStart) |
| 182 | continue; |
| 183 | |
| 184 | // Ignore pieces of the range that go onto other lines. |
| 185 | if (R.Start.getPointer() < LineStart) |
| 186 | R.Start = SMLoc::getFromPointer(LineStart); |
| 187 | if (R.End.getPointer() > LineEnd) |
| 188 | R.End = SMLoc::getFromPointer(LineEnd); |
| 189 | |
| 190 | // Translate from SMLoc ranges to column ranges. |
| 191 | ColRanges.push_back(std::make_pair(R.Start.getPointer()-LineStart, |
| 192 | R.End.getPointer()-LineStart)); |
| 193 | } |
| 194 | |
Chris Lattner | b019491 | 2010-04-06 18:06:18 +0000 | [diff] [blame] | 195 | return SMDiagnostic(*this, Loc, |
Chris Lattner | 8f0f480 | 2010-04-06 00:26:48 +0000 | [diff] [blame] | 196 | CurMB->getBufferIdentifier(), FindLineNumber(Loc, CurBuf), |
Benjamin Kramer | d1e1703 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 197 | Loc.getPointer()-LineStart, OS.str(), |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame^] | 198 | LineStr, ColRanges, ShowLine); |
Chris Lattner | eeb4a84 | 2009-07-02 23:08:13 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Benjamin Kramer | d1e1703 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 201 | void SourceMgr::PrintMessage(SMLoc Loc, const Twine &Msg, |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame^] | 202 | const char *Type, ArrayRef<SMRange> Ranges, |
| 203 | bool ShowLine) const { |
Chris Lattner | 8f0f480 | 2010-04-06 00:26:48 +0000 | [diff] [blame] | 204 | // Report the message with the diagnostic handler if present. |
| 205 | if (DiagHandler) { |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame^] | 206 | DiagHandler(GetMessage(Loc, Msg, Type, Ranges, ShowLine), DiagContext); |
Chris Lattner | 8f0f480 | 2010-04-06 00:26:48 +0000 | [diff] [blame] | 207 | return; |
| 208 | } |
Michael J. Spencer | 333ad3f | 2010-12-09 17:37:32 +0000 | [diff] [blame] | 209 | |
Chris Lattner | eeb4a84 | 2009-07-02 23:08:13 +0000 | [diff] [blame] | 210 | raw_ostream &OS = errs(); |
| 211 | |
| 212 | int CurBuf = FindBufferContainingLoc(Loc); |
| 213 | assert(CurBuf != -1 && "Invalid or unspecified location!"); |
| 214 | PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS); |
| 215 | |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame^] | 216 | GetMessage(Loc, Msg, Type, Ranges, ShowLine).print(0, OS); |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 217 | } |
Chris Lattner | 2f510ae | 2009-07-02 22:24:20 +0000 | [diff] [blame] | 218 | |
| 219 | //===----------------------------------------------------------------------===// |
| 220 | // SMDiagnostic Implementation |
| 221 | //===----------------------------------------------------------------------===// |
| 222 | |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame^] | 223 | SMDiagnostic::SMDiagnostic(const SourceMgr &sm, SMLoc L, const std::string &FN, |
| 224 | int Line, int Col, const std::string &Msg, |
| 225 | const std::string &LineStr, |
| 226 | ArrayRef<std::pair<unsigned,unsigned> > Ranges, |
| 227 | bool showline) |
| 228 | : SM(&sm), Loc(L), Filename(FN), LineNo(Line), ColumnNo(Col), Message(Msg), |
| 229 | LineContents(LineStr), ShowLine(showline), Ranges(Ranges.vec()) {} |
| 230 | |
| 231 | |
| 232 | void SMDiagnostic::print(const char *ProgName, raw_ostream &S) const { |
Chris Lattner | 2f510ae | 2009-07-02 22:24:20 +0000 | [diff] [blame] | 233 | if (ProgName && ProgName[0]) |
| 234 | S << ProgName << ": "; |
| 235 | |
Dan Gohman | 5e5442c | 2010-01-21 10:13:27 +0000 | [diff] [blame] | 236 | if (!Filename.empty()) { |
| 237 | if (Filename == "-") |
| 238 | S << "<stdin>"; |
| 239 | else |
| 240 | S << Filename; |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 241 | |
Dan Gohman | 5e5442c | 2010-01-21 10:13:27 +0000 | [diff] [blame] | 242 | if (LineNo != -1) { |
| 243 | S << ':' << LineNo; |
| 244 | if (ColumnNo != -1) |
| 245 | S << ':' << (ColumnNo+1); |
| 246 | } |
| 247 | S << ": "; |
Chris Lattner | 2f510ae | 2009-07-02 22:24:20 +0000 | [diff] [blame] | 248 | } |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 249 | |
Dan Gohman | 5e5442c | 2010-01-21 10:13:27 +0000 | [diff] [blame] | 250 | S << Message << '\n'; |
Daniel Dunbar | 4153982 | 2009-11-22 22:08:00 +0000 | [diff] [blame] | 251 | |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame^] | 252 | if (LineNo == -1 || ColumnNo == -1 || !ShowLine) |
| 253 | return; |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 254 | |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame^] | 255 | // Build the line with the caret and ranges. |
| 256 | std::string CaretLine(LineContents.size()+1, ' '); |
| 257 | |
| 258 | // Expand any ranges. |
| 259 | for (unsigned r = 0, e = Ranges.size(); r != e; ++r) { |
| 260 | std::pair<unsigned, unsigned> R = Ranges[r]; |
| 261 | for (unsigned i = R.first, |
| 262 | e = std::min(R.second, (unsigned)LineContents.size())+1; i != e; ++i) |
| 263 | CaretLine[i] = '~'; |
Chris Lattner | 2f510ae | 2009-07-02 22:24:20 +0000 | [diff] [blame] | 264 | } |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame^] | 265 | |
| 266 | // Finally, plop on the caret. |
| 267 | if (unsigned(ColumnNo) <= LineContents.size()) |
| 268 | CaretLine[ColumnNo] = '^'; |
| 269 | else |
| 270 | CaretLine[LineContents.size()] = '^'; |
| 271 | |
| 272 | // ... and remove trailing whitespace so the output doesn't wrap for it. We |
| 273 | // know that the line isn't completely empty because it has the caret in it at |
| 274 | // least. |
| 275 | CaretLine.erase(CaretLine.find_last_not_of(' ')+1); |
| 276 | |
| 277 | // Print out the source line one character at a time, so we can expand tabs. |
| 278 | for (unsigned i = 0, e = LineContents.size(), OutCol = 0; i != e; ++i) { |
| 279 | if (LineContents[i] != '\t') { |
| 280 | S << LineContents[i]; |
| 281 | ++OutCol; |
| 282 | continue; |
| 283 | } |
| 284 | |
| 285 | // If we have a tab, emit at least one space, then round up to 8 columns. |
| 286 | do { |
| 287 | S << ' '; |
| 288 | ++OutCol; |
| 289 | } while (OutCol & 7); |
| 290 | } |
| 291 | S << '\n'; |
| 292 | |
| 293 | // Print out the caret line, matching tabs in the source line. |
| 294 | for (unsigned i = 0, e = CaretLine.size(), OutCol = 0; i != e; ++i) { |
| 295 | if (i >= LineContents.size() || LineContents[i] != '\t') { |
| 296 | S << CaretLine[i]; |
| 297 | ++OutCol; |
| 298 | continue; |
| 299 | } |
| 300 | |
| 301 | // Okay, we have a tab. Insert the appropriate number of characters. |
| 302 | do { |
| 303 | S << CaretLine[i]; |
| 304 | ++OutCol; |
| 305 | } while (OutCol & 7); |
| 306 | } |
| 307 | |
| 308 | S << '\n'; |
Chris Lattner | 2f510ae | 2009-07-02 22:24:20 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | |