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 | |
Chris Lattner | 77eafd9 | 2012-05-05 22:17:32 +0000 | [diff] [blame^] | 82 | /// getLineAndColumn - Find the line and column number for the specified |
| 83 | /// location in the specified file. This is not a fast method. |
| 84 | std::pair<unsigned, unsigned> |
| 85 | SourceMgr::getLineAndColumn(SMLoc Loc, int BufferID) const { |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 86 | if (BufferID == -1) BufferID = FindBufferContainingLoc(Loc); |
| 87 | assert(BufferID != -1 && "Invalid Location!"); |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 88 | |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 89 | MemoryBuffer *Buff = getBufferInfo(BufferID).Buffer; |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 90 | |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 91 | // Count the number of \n's between the start of the file and the specified |
| 92 | // location. |
| 93 | unsigned LineNo = 1; |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 94 | |
Chris Lattner | 77eafd9 | 2012-05-05 22:17:32 +0000 | [diff] [blame^] | 95 | const char *BufStart = Buff->getBufferStart(); |
| 96 | const char *Ptr = BufStart; |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 97 | |
Chris Lattner | 1d96ccc | 2009-08-11 17:49:14 +0000 | [diff] [blame] | 98 | // If we have a line number cache, and if the query is to a later point in the |
| 99 | // same file, start searching from the last query location. This optimizes |
| 100 | // for the case when multiple diagnostics come out of one file in order. |
| 101 | if (LineNoCacheTy *Cache = getCache(LineNoCache)) |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 102 | if (Cache->LastQueryBufferID == BufferID && |
Chris Lattner | 1d96ccc | 2009-08-11 17:49:14 +0000 | [diff] [blame] | 103 | Cache->LastQuery <= Loc.getPointer()) { |
| 104 | Ptr = Cache->LastQuery; |
| 105 | LineNo = Cache->LineNoOfQuery; |
| 106 | } |
| 107 | |
| 108 | // Scan for the location being queried, keeping track of the number of lines |
| 109 | // we see. |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 110 | for (; SMLoc::getFromPointer(Ptr) != Loc; ++Ptr) |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 111 | if (*Ptr == '\n') ++LineNo; |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 112 | |
Chris Lattner | 1d96ccc | 2009-08-11 17:49:14 +0000 | [diff] [blame] | 113 | // Allocate the line number cache if it doesn't exist. |
| 114 | if (LineNoCache == 0) |
| 115 | LineNoCache = new LineNoCacheTy(); |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 116 | |
Chris Lattner | 1d96ccc | 2009-08-11 17:49:14 +0000 | [diff] [blame] | 117 | // Update the line # cache. |
| 118 | LineNoCacheTy &Cache = *getCache(LineNoCache); |
| 119 | Cache.LastQueryBufferID = BufferID; |
| 120 | Cache.LastQuery = Ptr; |
| 121 | Cache.LineNoOfQuery = LineNo; |
Chris Lattner | 77eafd9 | 2012-05-05 22:17:32 +0000 | [diff] [blame^] | 122 | |
| 123 | size_t NewlineOffs = StringRef(BufStart, Ptr-BufStart).find_last_of("\n\r"); |
| 124 | if (NewlineOffs == StringRef::npos) NewlineOffs = ~0ULL; |
| 125 | return std::make_pair(LineNo, Ptr-BufStart-NewlineOffs); |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Chris Lattner | 2f510ae | 2009-07-02 22:24:20 +0000 | [diff] [blame] | 128 | void SourceMgr::PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const { |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 129 | if (IncludeLoc == SMLoc()) return; // Top of stack. |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 130 | |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 131 | int CurBuf = FindBufferContainingLoc(IncludeLoc); |
| 132 | assert(CurBuf != -1 && "Invalid or unspecified location!"); |
| 133 | |
Chris Lattner | 2f510ae | 2009-07-02 22:24:20 +0000 | [diff] [blame] | 134 | PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS); |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 135 | |
Chris Lattner | 2f510ae | 2009-07-02 22:24:20 +0000 | [diff] [blame] | 136 | OS << "Included from " |
| 137 | << getBufferInfo(CurBuf).Buffer->getBufferIdentifier() |
| 138 | << ":" << FindLineNumber(IncludeLoc, CurBuf) << ":\n"; |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | |
Chris Lattner | eeb4a84 | 2009-07-02 23:08:13 +0000 | [diff] [blame] | 142 | /// GetMessage - Return an SMDiagnostic at the specified location with the |
| 143 | /// specified string. |
| 144 | /// |
| 145 | /// @param Type - If non-null, the kind of message (e.g., "error") which is |
| 146 | /// prefixed to the message. |
Chris Lattner | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 147 | SMDiagnostic SourceMgr::GetMessage(SMLoc Loc, SourceMgr::DiagKind Kind, |
Chris Lattner | 462b43c | 2011-10-16 05:47:55 +0000 | [diff] [blame] | 148 | const Twine &Msg, |
| 149 | ArrayRef<SMRange> Ranges) const { |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 150 | |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 151 | // First thing to do: find the current buffer containing the specified |
| 152 | // location. |
Chris Lattner | 14ee48a | 2009-06-21 21:22:11 +0000 | [diff] [blame] | 153 | int CurBuf = FindBufferContainingLoc(Loc); |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 154 | assert(CurBuf != -1 && "Invalid or unspecified location!"); |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 155 | |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 156 | MemoryBuffer *CurMB = getBufferInfo(CurBuf).Buffer; |
Daniel Dunbar | 4153982 | 2009-11-22 22:08:00 +0000 | [diff] [blame] | 157 | |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 158 | // Scan backward to find the start of the line. |
Chris Lattner | 14ee48a | 2009-06-21 21:22:11 +0000 | [diff] [blame] | 159 | const char *LineStart = Loc.getPointer(); |
Chris Lattner | 77eafd9 | 2012-05-05 22:17:32 +0000 | [diff] [blame^] | 160 | const char *BufStart = CurMB->getBufferStart(); |
| 161 | while (LineStart != BufStart && LineStart[-1] != '\n' && |
| 162 | LineStart[-1] != '\r') |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 163 | --LineStart; |
Daniel Dunbar | 4153982 | 2009-11-22 22:08:00 +0000 | [diff] [blame] | 164 | |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 165 | // Get the end of the line. |
| 166 | const char *LineEnd = Loc.getPointer(); |
Chris Lattner | 77eafd9 | 2012-05-05 22:17:32 +0000 | [diff] [blame^] | 167 | const char *BufEnd = CurMB->getBufferEnd(); |
| 168 | while (LineEnd != BufEnd && LineEnd[0] != '\n' && LineEnd[0] != '\r') |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 169 | ++LineEnd; |
| 170 | std::string LineStr(LineStart, LineEnd); |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 171 | |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 172 | // Convert any ranges to column ranges that only intersect the line of the |
| 173 | // location. |
| 174 | SmallVector<std::pair<unsigned, unsigned>, 4> ColRanges; |
| 175 | for (unsigned i = 0, e = Ranges.size(); i != e; ++i) { |
| 176 | SMRange R = Ranges[i]; |
| 177 | if (!R.isValid()) continue; |
| 178 | |
| 179 | // If the line doesn't contain any part of the range, then ignore it. |
| 180 | if (R.Start.getPointer() > LineEnd || R.End.getPointer() < LineStart) |
| 181 | continue; |
| 182 | |
| 183 | // Ignore pieces of the range that go onto other lines. |
| 184 | if (R.Start.getPointer() < LineStart) |
| 185 | R.Start = SMLoc::getFromPointer(LineStart); |
| 186 | if (R.End.getPointer() > LineEnd) |
| 187 | R.End = SMLoc::getFromPointer(LineEnd); |
| 188 | |
| 189 | // Translate from SMLoc ranges to column ranges. |
| 190 | ColRanges.push_back(std::make_pair(R.Start.getPointer()-LineStart, |
| 191 | R.End.getPointer()-LineStart)); |
| 192 | } |
| 193 | |
Chris Lattner | 77eafd9 | 2012-05-05 22:17:32 +0000 | [diff] [blame^] | 194 | std::pair<unsigned, unsigned> LineAndCol = getLineAndColumn(Loc, CurBuf); |
Chris Lattner | b019491 | 2010-04-06 18:06:18 +0000 | [diff] [blame] | 195 | return SMDiagnostic(*this, Loc, |
Chris Lattner | 77eafd9 | 2012-05-05 22:17:32 +0000 | [diff] [blame^] | 196 | CurMB->getBufferIdentifier(), LineAndCol.first, |
| 197 | LineAndCol.second-1, Kind, Msg.str(), |
Chris Lattner | 462b43c | 2011-10-16 05:47:55 +0000 | [diff] [blame] | 198 | LineStr, ColRanges); |
Chris Lattner | eeb4a84 | 2009-07-02 23:08:13 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Chris Lattner | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 201 | void SourceMgr::PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind, |
Benjamin Kramer | 89f33fd | 2012-04-18 19:04:15 +0000 | [diff] [blame] | 202 | const Twine &Msg, ArrayRef<SMRange> Ranges, |
| 203 | bool ShowColors) const { |
Chris Lattner | 462b43c | 2011-10-16 05:47:55 +0000 | [diff] [blame] | 204 | SMDiagnostic Diagnostic = GetMessage(Loc, Kind, Msg, Ranges); |
Chris Lattner | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 205 | |
Chris Lattner | 8f0f480 | 2010-04-06 00:26:48 +0000 | [diff] [blame] | 206 | // Report the message with the diagnostic handler if present. |
| 207 | if (DiagHandler) { |
Chris Lattner | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 208 | DiagHandler(Diagnostic, DiagContext); |
Chris Lattner | 8f0f480 | 2010-04-06 00:26:48 +0000 | [diff] [blame] | 209 | return; |
| 210 | } |
Michael J. Spencer | 333ad3f | 2010-12-09 17:37:32 +0000 | [diff] [blame] | 211 | |
Chris Lattner | eeb4a84 | 2009-07-02 23:08:13 +0000 | [diff] [blame] | 212 | raw_ostream &OS = errs(); |
| 213 | |
| 214 | int CurBuf = FindBufferContainingLoc(Loc); |
| 215 | assert(CurBuf != -1 && "Invalid or unspecified location!"); |
| 216 | PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS); |
| 217 | |
Benjamin Kramer | 89f33fd | 2012-04-18 19:04:15 +0000 | [diff] [blame] | 218 | Diagnostic.print(0, OS, ShowColors); |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 219 | } |
Chris Lattner | 2f510ae | 2009-07-02 22:24:20 +0000 | [diff] [blame] | 220 | |
| 221 | //===----------------------------------------------------------------------===// |
| 222 | // SMDiagnostic Implementation |
| 223 | //===----------------------------------------------------------------------===// |
| 224 | |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 225 | SMDiagnostic::SMDiagnostic(const SourceMgr &sm, SMLoc L, const std::string &FN, |
Chris Lattner | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 226 | int Line, int Col, SourceMgr::DiagKind Kind, |
| 227 | const std::string &Msg, |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 228 | const std::string &LineStr, |
Chris Lattner | 462b43c | 2011-10-16 05:47:55 +0000 | [diff] [blame] | 229 | ArrayRef<std::pair<unsigned,unsigned> > Ranges) |
Chris Lattner | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 230 | : SM(&sm), Loc(L), Filename(FN), LineNo(Line), ColumnNo(Col), Kind(Kind), |
Chris Lattner | 462b43c | 2011-10-16 05:47:55 +0000 | [diff] [blame] | 231 | Message(Msg), LineContents(LineStr), Ranges(Ranges.vec()) { |
Chris Lattner | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 232 | } |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 233 | |
| 234 | |
Benjamin Kramer | 89f33fd | 2012-04-18 19:04:15 +0000 | [diff] [blame] | 235 | void SMDiagnostic::print(const char *ProgName, raw_ostream &S, |
| 236 | bool ShowColors) const { |
| 237 | // Display colors only if OS goes to a tty. |
| 238 | ShowColors &= S.is_displayed(); |
| 239 | |
| 240 | if (ShowColors) |
| 241 | S.changeColor(raw_ostream::SAVEDCOLOR, true); |
| 242 | |
Chris Lattner | 2f510ae | 2009-07-02 22:24:20 +0000 | [diff] [blame] | 243 | if (ProgName && ProgName[0]) |
| 244 | S << ProgName << ": "; |
| 245 | |
Dan Gohman | 5e5442c | 2010-01-21 10:13:27 +0000 | [diff] [blame] | 246 | if (!Filename.empty()) { |
| 247 | if (Filename == "-") |
| 248 | S << "<stdin>"; |
| 249 | else |
| 250 | S << Filename; |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 251 | |
Dan Gohman | 5e5442c | 2010-01-21 10:13:27 +0000 | [diff] [blame] | 252 | if (LineNo != -1) { |
| 253 | S << ':' << LineNo; |
| 254 | if (ColumnNo != -1) |
| 255 | S << ':' << (ColumnNo+1); |
| 256 | } |
| 257 | S << ": "; |
Chris Lattner | 2f510ae | 2009-07-02 22:24:20 +0000 | [diff] [blame] | 258 | } |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 259 | |
Chris Lattner | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 260 | switch (Kind) { |
Benjamin Kramer | 89f33fd | 2012-04-18 19:04:15 +0000 | [diff] [blame] | 261 | case SourceMgr::DK_Error: |
| 262 | if (ShowColors) |
| 263 | S.changeColor(raw_ostream::RED, true); |
| 264 | S << "error: "; |
| 265 | break; |
| 266 | case SourceMgr::DK_Warning: |
| 267 | if (ShowColors) |
| 268 | S.changeColor(raw_ostream::MAGENTA, true); |
| 269 | S << "warning: "; |
| 270 | break; |
| 271 | case SourceMgr::DK_Note: |
| 272 | if (ShowColors) |
| 273 | S.changeColor(raw_ostream::BLACK, true); |
| 274 | S << "note: "; |
| 275 | break; |
Chris Lattner | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 276 | } |
Benjamin Kramer | 89f33fd | 2012-04-18 19:04:15 +0000 | [diff] [blame] | 277 | |
| 278 | if (ShowColors) { |
| 279 | S.resetColor(); |
| 280 | S.changeColor(raw_ostream::SAVEDCOLOR, true); |
| 281 | } |
| 282 | |
Dan Gohman | 5e5442c | 2010-01-21 10:13:27 +0000 | [diff] [blame] | 283 | S << Message << '\n'; |
Daniel Dunbar | 4153982 | 2009-11-22 22:08:00 +0000 | [diff] [blame] | 284 | |
Benjamin Kramer | 89f33fd | 2012-04-18 19:04:15 +0000 | [diff] [blame] | 285 | if (ShowColors) |
| 286 | S.resetColor(); |
| 287 | |
Chris Lattner | 462b43c | 2011-10-16 05:47:55 +0000 | [diff] [blame] | 288 | if (LineNo == -1 || ColumnNo == -1) |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 289 | return; |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 290 | |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 291 | // Build the line with the caret and ranges. |
| 292 | std::string CaretLine(LineContents.size()+1, ' '); |
| 293 | |
| 294 | // Expand any ranges. |
| 295 | for (unsigned r = 0, e = Ranges.size(); r != e; ++r) { |
| 296 | std::pair<unsigned, unsigned> R = Ranges[r]; |
| 297 | for (unsigned i = R.first, |
| 298 | e = std::min(R.second, (unsigned)LineContents.size())+1; i != e; ++i) |
| 299 | CaretLine[i] = '~'; |
Chris Lattner | 2f510ae | 2009-07-02 22:24:20 +0000 | [diff] [blame] | 300 | } |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 301 | |
| 302 | // Finally, plop on the caret. |
| 303 | if (unsigned(ColumnNo) <= LineContents.size()) |
| 304 | CaretLine[ColumnNo] = '^'; |
| 305 | else |
| 306 | CaretLine[LineContents.size()] = '^'; |
| 307 | |
| 308 | // ... and remove trailing whitespace so the output doesn't wrap for it. We |
| 309 | // know that the line isn't completely empty because it has the caret in it at |
| 310 | // least. |
| 311 | CaretLine.erase(CaretLine.find_last_not_of(' ')+1); |
| 312 | |
| 313 | // Print out the source line one character at a time, so we can expand tabs. |
| 314 | for (unsigned i = 0, e = LineContents.size(), OutCol = 0; i != e; ++i) { |
| 315 | if (LineContents[i] != '\t') { |
| 316 | S << LineContents[i]; |
| 317 | ++OutCol; |
| 318 | continue; |
| 319 | } |
| 320 | |
| 321 | // If we have a tab, emit at least one space, then round up to 8 columns. |
| 322 | do { |
| 323 | S << ' '; |
| 324 | ++OutCol; |
| 325 | } while (OutCol & 7); |
| 326 | } |
| 327 | S << '\n'; |
| 328 | |
Benjamin Kramer | 89f33fd | 2012-04-18 19:04:15 +0000 | [diff] [blame] | 329 | if (ShowColors) |
| 330 | S.changeColor(raw_ostream::GREEN, true); |
| 331 | |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 332 | // Print out the caret line, matching tabs in the source line. |
| 333 | for (unsigned i = 0, e = CaretLine.size(), OutCol = 0; i != e; ++i) { |
| 334 | if (i >= LineContents.size() || LineContents[i] != '\t') { |
| 335 | S << CaretLine[i]; |
| 336 | ++OutCol; |
| 337 | continue; |
| 338 | } |
| 339 | |
| 340 | // Okay, we have a tab. Insert the appropriate number of characters. |
| 341 | do { |
| 342 | S << CaretLine[i]; |
| 343 | ++OutCol; |
| 344 | } while (OutCol & 7); |
| 345 | } |
Benjamin Kramer | 89f33fd | 2012-04-18 19:04:15 +0000 | [diff] [blame] | 346 | |
| 347 | if (ShowColors) |
| 348 | S.resetColor(); |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 349 | |
| 350 | S << '\n'; |
Chris Lattner | 2f510ae | 2009-07-02 22:24:20 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | |