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" |
Michael J. Spencer | 3ff9563 | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/OwningPtr.h" |
Jordan Rose | 9b1f44b | 2013-01-10 18:50:15 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallString.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/Twine.h" |
Jordan Rose | 9b1f44b | 2013-01-10 18:50:15 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Locale.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 21 | #include "llvm/Support/MemoryBuffer.h" |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | 333fb04 | 2010-12-09 17:36:48 +0000 | [diff] [blame] | 23 | #include "llvm/Support/system_error.h" |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
Jordan Rose | 9b1f44b | 2013-01-10 18:50:15 +0000 | [diff] [blame] | 26 | static const size_t TabStop = 8; |
| 27 | |
Chris Lattner | 1d96ccc | 2009-08-11 17:49:14 +0000 | [diff] [blame] | 28 | namespace { |
| 29 | struct LineNoCacheTy { |
| 30 | int LastQueryBufferID; |
| 31 | const char *LastQuery; |
| 32 | unsigned LineNoOfQuery; |
| 33 | }; |
| 34 | } |
| 35 | |
| 36 | static LineNoCacheTy *getCache(void *Ptr) { |
| 37 | return (LineNoCacheTy*)Ptr; |
| 38 | } |
| 39 | |
| 40 | |
Chris Lattner | 8070ea3 | 2009-06-21 03:41:50 +0000 | [diff] [blame] | 41 | SourceMgr::~SourceMgr() { |
Chris Lattner | 1d96ccc | 2009-08-11 17:49:14 +0000 | [diff] [blame] | 42 | // Delete the line # cache if allocated. |
| 43 | if (LineNoCacheTy *Cache = getCache(LineNoCache)) |
| 44 | delete Cache; |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 45 | |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 46 | while (!Buffers.empty()) { |
| 47 | delete Buffers.back().Buffer; |
| 48 | Buffers.pop_back(); |
| 49 | } |
| 50 | } |
| 51 | |
Chris Lattner | 7ee5d5f | 2009-06-21 05:06:04 +0000 | [diff] [blame] | 52 | /// AddIncludeFile - Search for a file with the specified name in the current |
| 53 | /// directory or in one of the IncludeDirs. If no file is found, this returns |
| 54 | /// ~0, otherwise it returns the buffer ID of the stacked file. |
Matt Arsenault | ac9e819 | 2013-07-20 00:20:10 +0000 | [diff] [blame] | 55 | size_t SourceMgr::AddIncludeFile(const std::string &Filename, |
| 56 | SMLoc IncludeLoc, |
| 57 | std::string &IncludedFile) { |
Michael J. Spencer | 3ff9563 | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 58 | OwningPtr<MemoryBuffer> NewBuf; |
Joerg Sonnenberger | dd13790 | 2011-06-01 13:10:15 +0000 | [diff] [blame] | 59 | IncludedFile = Filename; |
| 60 | MemoryBuffer::getFile(IncludedFile.c_str(), NewBuf); |
Chris Lattner | 7ee5d5f | 2009-06-21 05:06:04 +0000 | [diff] [blame] | 61 | |
| 62 | // If the file didn't exist directly, see if it's in an include path. |
| 63 | for (unsigned i = 0, e = IncludeDirectories.size(); i != e && !NewBuf; ++i) { |
Joerg Sonnenberger | dd13790 | 2011-06-01 13:10:15 +0000 | [diff] [blame] | 64 | IncludedFile = IncludeDirectories[i] + "/" + Filename; |
| 65 | MemoryBuffer::getFile(IncludedFile.c_str(), NewBuf); |
Chris Lattner | 7ee5d5f | 2009-06-21 05:06:04 +0000 | [diff] [blame] | 66 | } |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 67 | |
David Blaikie | 453f4f0 | 2013-05-15 07:36:59 +0000 | [diff] [blame] | 68 | if (!NewBuf) return ~0U; |
Chris Lattner | 7ee5d5f | 2009-06-21 05:06:04 +0000 | [diff] [blame] | 69 | |
Michael J. Spencer | 3ff9563 | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 70 | return AddNewSourceBuffer(NewBuf.take(), IncludeLoc); |
Chris Lattner | 7ee5d5f | 2009-06-21 05:06:04 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 74 | /// FindBufferContainingLoc - Return the ID of the buffer containing the |
| 75 | /// specified location, returning -1 if not found. |
Chris Lattner | 8070ea3 | 2009-06-21 03:41:50 +0000 | [diff] [blame] | 76 | int SourceMgr::FindBufferContainingLoc(SMLoc Loc) const { |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 77 | for (unsigned i = 0, e = Buffers.size(); i != e; ++i) |
Chris Lattner | 1c8ae59 | 2009-03-13 16:01:53 +0000 | [diff] [blame] | 78 | if (Loc.getPointer() >= Buffers[i].Buffer->getBufferStart() && |
Chris Lattner | d477182 | 2009-03-18 20:36:45 +0000 | [diff] [blame] | 79 | // Use <= here so that a pointer to the null at the end of the buffer |
| 80 | // is included as part of the buffer. |
| 81 | Loc.getPointer() <= Buffers[i].Buffer->getBufferEnd()) |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 82 | return i; |
| 83 | return -1; |
| 84 | } |
| 85 | |
Chris Lattner | 77eafd9 | 2012-05-05 22:17:32 +0000 | [diff] [blame] | 86 | /// getLineAndColumn - Find the line and column number for the specified |
| 87 | /// location in the specified file. This is not a fast method. |
| 88 | std::pair<unsigned, unsigned> |
| 89 | SourceMgr::getLineAndColumn(SMLoc Loc, int BufferID) const { |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 90 | if (BufferID == -1) BufferID = FindBufferContainingLoc(Loc); |
| 91 | assert(BufferID != -1 && "Invalid Location!"); |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 92 | |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 93 | MemoryBuffer *Buff = getBufferInfo(BufferID).Buffer; |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 94 | |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 95 | // Count the number of \n's between the start of the file and the specified |
| 96 | // location. |
| 97 | unsigned LineNo = 1; |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 98 | |
Chris Lattner | 77eafd9 | 2012-05-05 22:17:32 +0000 | [diff] [blame] | 99 | const char *BufStart = Buff->getBufferStart(); |
| 100 | const char *Ptr = BufStart; |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 101 | |
Chris Lattner | 1d96ccc | 2009-08-11 17:49:14 +0000 | [diff] [blame] | 102 | // If we have a line number cache, and if the query is to a later point in the |
| 103 | // same file, start searching from the last query location. This optimizes |
| 104 | // for the case when multiple diagnostics come out of one file in order. |
| 105 | if (LineNoCacheTy *Cache = getCache(LineNoCache)) |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 106 | if (Cache->LastQueryBufferID == BufferID && |
Chris Lattner | 1d96ccc | 2009-08-11 17:49:14 +0000 | [diff] [blame] | 107 | Cache->LastQuery <= Loc.getPointer()) { |
| 108 | Ptr = Cache->LastQuery; |
| 109 | LineNo = Cache->LineNoOfQuery; |
| 110 | } |
| 111 | |
| 112 | // Scan for the location being queried, keeping track of the number of lines |
| 113 | // we see. |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 114 | for (; SMLoc::getFromPointer(Ptr) != Loc; ++Ptr) |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 115 | if (*Ptr == '\n') ++LineNo; |
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 | // Allocate the line number cache if it doesn't exist. |
| 118 | if (LineNoCache == 0) |
| 119 | LineNoCache = new LineNoCacheTy(); |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 120 | |
Chris Lattner | 1d96ccc | 2009-08-11 17:49:14 +0000 | [diff] [blame] | 121 | // Update the line # cache. |
| 122 | LineNoCacheTy &Cache = *getCache(LineNoCache); |
| 123 | Cache.LastQueryBufferID = BufferID; |
| 124 | Cache.LastQuery = Ptr; |
| 125 | Cache.LineNoOfQuery = LineNo; |
Chris Lattner | 77eafd9 | 2012-05-05 22:17:32 +0000 | [diff] [blame] | 126 | |
| 127 | size_t NewlineOffs = StringRef(BufStart, Ptr-BufStart).find_last_of("\n\r"); |
Matt Beaumont-Gay | d58518a | 2012-05-07 18:12:42 +0000 | [diff] [blame] | 128 | if (NewlineOffs == StringRef::npos) NewlineOffs = ~(size_t)0; |
Chris Lattner | 77eafd9 | 2012-05-05 22:17:32 +0000 | [diff] [blame] | 129 | return std::make_pair(LineNo, Ptr-BufStart-NewlineOffs); |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Chris Lattner | 2f510ae | 2009-07-02 22:24:20 +0000 | [diff] [blame] | 132 | void SourceMgr::PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const { |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 133 | if (IncludeLoc == SMLoc()) return; // Top of stack. |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 134 | |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 135 | int CurBuf = FindBufferContainingLoc(IncludeLoc); |
| 136 | assert(CurBuf != -1 && "Invalid or unspecified location!"); |
| 137 | |
Chris Lattner | 2f510ae | 2009-07-02 22:24:20 +0000 | [diff] [blame] | 138 | PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS); |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 139 | |
Chris Lattner | 2f510ae | 2009-07-02 22:24:20 +0000 | [diff] [blame] | 140 | OS << "Included from " |
| 141 | << getBufferInfo(CurBuf).Buffer->getBufferIdentifier() |
| 142 | << ":" << FindLineNumber(IncludeLoc, CurBuf) << ":\n"; |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | |
Chris Lattner | eeb4a84 | 2009-07-02 23:08:13 +0000 | [diff] [blame] | 146 | /// GetMessage - Return an SMDiagnostic at the specified location with the |
| 147 | /// specified string. |
| 148 | /// |
| 149 | /// @param Type - If non-null, the kind of message (e.g., "error") which is |
| 150 | /// prefixed to the message. |
Chris Lattner | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 151 | SMDiagnostic SourceMgr::GetMessage(SMLoc Loc, SourceMgr::DiagKind Kind, |
Chris Lattner | 462b43c | 2011-10-16 05:47:55 +0000 | [diff] [blame] | 152 | const Twine &Msg, |
Jordan Rose | 9b1f44b | 2013-01-10 18:50:15 +0000 | [diff] [blame] | 153 | ArrayRef<SMRange> Ranges, |
| 154 | ArrayRef<SMFixIt> FixIts) const { |
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 | // First thing to do: find the current buffer containing the specified |
Chris Lattner | eb034f4 | 2012-05-06 16:20:49 +0000 | [diff] [blame] | 157 | // location to pull out the source line. |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 158 | SmallVector<std::pair<unsigned, unsigned>, 4> ColRanges; |
Chris Lattner | eb034f4 | 2012-05-06 16:20:49 +0000 | [diff] [blame] | 159 | std::pair<unsigned, unsigned> LineAndCol; |
| 160 | const char *BufferID = "<unknown>"; |
| 161 | std::string LineStr; |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 162 | |
Chris Lattner | eb034f4 | 2012-05-06 16:20:49 +0000 | [diff] [blame] | 163 | if (Loc.isValid()) { |
| 164 | int CurBuf = FindBufferContainingLoc(Loc); |
| 165 | assert(CurBuf != -1 && "Invalid or unspecified location!"); |
| 166 | |
| 167 | MemoryBuffer *CurMB = getBufferInfo(CurBuf).Buffer; |
| 168 | BufferID = CurMB->getBufferIdentifier(); |
| 169 | |
| 170 | // Scan backward to find the start of the line. |
| 171 | const char *LineStart = Loc.getPointer(); |
| 172 | const char *BufStart = CurMB->getBufferStart(); |
| 173 | while (LineStart != BufStart && LineStart[-1] != '\n' && |
| 174 | LineStart[-1] != '\r') |
| 175 | --LineStart; |
| 176 | |
| 177 | // Get the end of the line. |
| 178 | const char *LineEnd = Loc.getPointer(); |
| 179 | const char *BufEnd = CurMB->getBufferEnd(); |
| 180 | while (LineEnd != BufEnd && LineEnd[0] != '\n' && LineEnd[0] != '\r') |
| 181 | ++LineEnd; |
| 182 | LineStr = std::string(LineStart, LineEnd); |
| 183 | |
| 184 | // Convert any ranges to column ranges that only intersect the line of the |
| 185 | // location. |
| 186 | for (unsigned i = 0, e = Ranges.size(); i != e; ++i) { |
| 187 | SMRange R = Ranges[i]; |
| 188 | if (!R.isValid()) continue; |
| 189 | |
| 190 | // If the line doesn't contain any part of the range, then ignore it. |
| 191 | if (R.Start.getPointer() > LineEnd || R.End.getPointer() < LineStart) |
| 192 | continue; |
| 193 | |
| 194 | // Ignore pieces of the range that go onto other lines. |
| 195 | if (R.Start.getPointer() < LineStart) |
| 196 | R.Start = SMLoc::getFromPointer(LineStart); |
| 197 | if (R.End.getPointer() > LineEnd) |
| 198 | R.End = SMLoc::getFromPointer(LineEnd); |
| 199 | |
| 200 | // Translate from SMLoc ranges to column ranges. |
Jordan Rose | 9b1f44b | 2013-01-10 18:50:15 +0000 | [diff] [blame] | 201 | // FIXME: Handle multibyte characters. |
Chris Lattner | eb034f4 | 2012-05-06 16:20:49 +0000 | [diff] [blame] | 202 | ColRanges.push_back(std::make_pair(R.Start.getPointer()-LineStart, |
| 203 | R.End.getPointer()-LineStart)); |
| 204 | } |
| 205 | |
| 206 | LineAndCol = getLineAndColumn(Loc, CurBuf); |
| 207 | } |
| 208 | |
| 209 | return SMDiagnostic(*this, Loc, BufferID, LineAndCol.first, |
Chris Lattner | 77eafd9 | 2012-05-05 22:17:32 +0000 | [diff] [blame] | 210 | LineAndCol.second-1, Kind, Msg.str(), |
Jordan Rose | 9b1f44b | 2013-01-10 18:50:15 +0000 | [diff] [blame] | 211 | LineStr, ColRanges, FixIts); |
Chris Lattner | eeb4a84 | 2009-07-02 23:08:13 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Chris Lattner | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 214 | void SourceMgr::PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind, |
Benjamin Kramer | 89f33fd | 2012-04-18 19:04:15 +0000 | [diff] [blame] | 215 | const Twine &Msg, ArrayRef<SMRange> Ranges, |
Jordan Rose | 9b1f44b | 2013-01-10 18:50:15 +0000 | [diff] [blame] | 216 | ArrayRef<SMFixIt> FixIts, bool ShowColors) const { |
| 217 | SMDiagnostic Diagnostic = GetMessage(Loc, Kind, Msg, Ranges, FixIts); |
Chris Lattner | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 218 | |
Chris Lattner | 8f0f480 | 2010-04-06 00:26:48 +0000 | [diff] [blame] | 219 | // Report the message with the diagnostic handler if present. |
| 220 | if (DiagHandler) { |
Chris Lattner | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 221 | DiagHandler(Diagnostic, DiagContext); |
Chris Lattner | 8f0f480 | 2010-04-06 00:26:48 +0000 | [diff] [blame] | 222 | return; |
| 223 | } |
Michael J. Spencer | 333ad3f | 2010-12-09 17:37:32 +0000 | [diff] [blame] | 224 | |
Chris Lattner | eeb4a84 | 2009-07-02 23:08:13 +0000 | [diff] [blame] | 225 | raw_ostream &OS = errs(); |
| 226 | |
Chris Lattner | eb034f4 | 2012-05-06 16:20:49 +0000 | [diff] [blame] | 227 | if (Loc != SMLoc()) { |
| 228 | int CurBuf = FindBufferContainingLoc(Loc); |
| 229 | assert(CurBuf != -1 && "Invalid or unspecified location!"); |
| 230 | PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS); |
| 231 | } |
Chris Lattner | eeb4a84 | 2009-07-02 23:08:13 +0000 | [diff] [blame] | 232 | |
Benjamin Kramer | 89f33fd | 2012-04-18 19:04:15 +0000 | [diff] [blame] | 233 | Diagnostic.print(0, OS, ShowColors); |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 234 | } |
Chris Lattner | 2f510ae | 2009-07-02 22:24:20 +0000 | [diff] [blame] | 235 | |
| 236 | //===----------------------------------------------------------------------===// |
| 237 | // SMDiagnostic Implementation |
| 238 | //===----------------------------------------------------------------------===// |
| 239 | |
Jordan Rose | 9b1f44b | 2013-01-10 18:50:15 +0000 | [diff] [blame] | 240 | SMDiagnostic::SMDiagnostic(const SourceMgr &sm, SMLoc L, StringRef FN, |
Chris Lattner | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 241 | int Line, int Col, SourceMgr::DiagKind Kind, |
Jordan Rose | 9b1f44b | 2013-01-10 18:50:15 +0000 | [diff] [blame] | 242 | StringRef Msg, StringRef LineStr, |
| 243 | ArrayRef<std::pair<unsigned,unsigned> > Ranges, |
| 244 | ArrayRef<SMFixIt> Hints) |
Chris Lattner | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 245 | : SM(&sm), Loc(L), Filename(FN), LineNo(Line), ColumnNo(Col), Kind(Kind), |
Jordan Rose | 9b1f44b | 2013-01-10 18:50:15 +0000 | [diff] [blame] | 246 | Message(Msg), LineContents(LineStr), Ranges(Ranges.vec()), |
| 247 | FixIts(Hints.begin(), Hints.end()) { |
| 248 | std::sort(FixIts.begin(), FixIts.end()); |
Chris Lattner | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 249 | } |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 250 | |
Benjamin Kramer | 74b3c8d | 2013-02-15 12:30:38 +0000 | [diff] [blame] | 251 | static void buildFixItLine(std::string &CaretLine, std::string &FixItLine, |
| 252 | ArrayRef<SMFixIt> FixIts, ArrayRef<char> SourceLine){ |
Jordan Rose | 9b1f44b | 2013-01-10 18:50:15 +0000 | [diff] [blame] | 253 | if (FixIts.empty()) |
| 254 | return; |
| 255 | |
| 256 | const char *LineStart = SourceLine.begin(); |
| 257 | const char *LineEnd = SourceLine.end(); |
| 258 | |
| 259 | size_t PrevHintEndCol = 0; |
| 260 | |
| 261 | for (ArrayRef<SMFixIt>::iterator I = FixIts.begin(), E = FixIts.end(); |
| 262 | I != E; ++I) { |
| 263 | // If the fixit contains a newline or tab, ignore it. |
| 264 | if (I->getText().find_first_of("\n\r\t") != StringRef::npos) |
| 265 | continue; |
| 266 | |
| 267 | SMRange R = I->getRange(); |
| 268 | |
| 269 | // If the line doesn't contain any part of the range, then ignore it. |
| 270 | if (R.Start.getPointer() > LineEnd || R.End.getPointer() < LineStart) |
| 271 | continue; |
| 272 | |
| 273 | // Translate from SMLoc to column. |
| 274 | // Ignore pieces of the range that go onto other lines. |
| 275 | // FIXME: Handle multibyte characters in the source line. |
| 276 | unsigned FirstCol; |
| 277 | if (R.Start.getPointer() < LineStart) |
| 278 | FirstCol = 0; |
| 279 | else |
| 280 | FirstCol = R.Start.getPointer() - LineStart; |
| 281 | |
| 282 | // If we inserted a long previous hint, push this one forwards, and add |
| 283 | // an extra space to show that this is not part of the previous |
| 284 | // completion. This is sort of the best we can do when two hints appear |
| 285 | // to overlap. |
| 286 | // |
| 287 | // Note that if this hint is located immediately after the previous |
| 288 | // hint, no space will be added, since the location is more important. |
| 289 | unsigned HintCol = FirstCol; |
| 290 | if (HintCol < PrevHintEndCol) |
| 291 | HintCol = PrevHintEndCol + 1; |
| 292 | |
| 293 | // FIXME: This assertion is intended to catch unintended use of multibyte |
| 294 | // characters in fixits. If we decide to do this, we'll have to track |
| 295 | // separate byte widths for the source and fixit lines. |
| 296 | assert((size_t)llvm::sys::locale::columnWidth(I->getText()) == |
| 297 | I->getText().size()); |
| 298 | |
| 299 | // This relies on one byte per column in our fixit hints. |
| 300 | unsigned LastColumnModified = HintCol + I->getText().size(); |
| 301 | if (LastColumnModified > FixItLine.size()) |
| 302 | FixItLine.resize(LastColumnModified, ' '); |
| 303 | |
| 304 | std::copy(I->getText().begin(), I->getText().end(), |
| 305 | FixItLine.begin() + HintCol); |
| 306 | |
| 307 | PrevHintEndCol = LastColumnModified; |
| 308 | |
| 309 | // For replacements, mark the removal range with '~'. |
| 310 | // FIXME: Handle multibyte characters in the source line. |
| 311 | unsigned LastCol; |
| 312 | if (R.End.getPointer() >= LineEnd) |
| 313 | LastCol = LineEnd - LineStart; |
| 314 | else |
| 315 | LastCol = R.End.getPointer() - LineStart; |
| 316 | |
| 317 | std::fill(&CaretLine[FirstCol], &CaretLine[LastCol], '~'); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | static void printSourceLine(raw_ostream &S, StringRef LineContents) { |
| 322 | // Print out the source line one character at a time, so we can expand tabs. |
| 323 | for (unsigned i = 0, e = LineContents.size(), OutCol = 0; i != e; ++i) { |
| 324 | if (LineContents[i] != '\t') { |
| 325 | S << LineContents[i]; |
| 326 | ++OutCol; |
| 327 | continue; |
| 328 | } |
| 329 | |
| 330 | // If we have a tab, emit at least one space, then round up to 8 columns. |
| 331 | do { |
| 332 | S << ' '; |
| 333 | ++OutCol; |
| 334 | } while ((OutCol % TabStop) != 0); |
| 335 | } |
| 336 | S << '\n'; |
| 337 | } |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 338 | |
Jordan Rose | 9b1e854 | 2013-01-11 02:37:55 +0000 | [diff] [blame] | 339 | static bool isNonASCII(char c) { |
| 340 | return c & 0x80; |
| 341 | } |
| 342 | |
Benjamin Kramer | 89f33fd | 2012-04-18 19:04:15 +0000 | [diff] [blame] | 343 | void SMDiagnostic::print(const char *ProgName, raw_ostream &S, |
| 344 | bool ShowColors) const { |
Daniel Dunbar | a7eb583 | 2012-07-20 18:29:44 +0000 | [diff] [blame] | 345 | // Display colors only if OS supports colors. |
| 346 | ShowColors &= S.has_colors(); |
Benjamin Kramer | 89f33fd | 2012-04-18 19:04:15 +0000 | [diff] [blame] | 347 | |
| 348 | if (ShowColors) |
| 349 | S.changeColor(raw_ostream::SAVEDCOLOR, true); |
| 350 | |
Chris Lattner | 2f510ae | 2009-07-02 22:24:20 +0000 | [diff] [blame] | 351 | if (ProgName && ProgName[0]) |
| 352 | S << ProgName << ": "; |
| 353 | |
Dan Gohman | 5e5442c | 2010-01-21 10:13:27 +0000 | [diff] [blame] | 354 | if (!Filename.empty()) { |
| 355 | if (Filename == "-") |
| 356 | S << "<stdin>"; |
| 357 | else |
| 358 | S << Filename; |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 359 | |
Dan Gohman | 5e5442c | 2010-01-21 10:13:27 +0000 | [diff] [blame] | 360 | if (LineNo != -1) { |
| 361 | S << ':' << LineNo; |
| 362 | if (ColumnNo != -1) |
| 363 | S << ':' << (ColumnNo+1); |
| 364 | } |
| 365 | S << ": "; |
Chris Lattner | 2f510ae | 2009-07-02 22:24:20 +0000 | [diff] [blame] | 366 | } |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 367 | |
Chris Lattner | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 368 | switch (Kind) { |
Benjamin Kramer | 89f33fd | 2012-04-18 19:04:15 +0000 | [diff] [blame] | 369 | case SourceMgr::DK_Error: |
| 370 | if (ShowColors) |
| 371 | S.changeColor(raw_ostream::RED, true); |
| 372 | S << "error: "; |
| 373 | break; |
| 374 | case SourceMgr::DK_Warning: |
| 375 | if (ShowColors) |
| 376 | S.changeColor(raw_ostream::MAGENTA, true); |
| 377 | S << "warning: "; |
| 378 | break; |
| 379 | case SourceMgr::DK_Note: |
| 380 | if (ShowColors) |
| 381 | S.changeColor(raw_ostream::BLACK, true); |
| 382 | S << "note: "; |
| 383 | break; |
Chris Lattner | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 384 | } |
Benjamin Kramer | 89f33fd | 2012-04-18 19:04:15 +0000 | [diff] [blame] | 385 | |
| 386 | if (ShowColors) { |
| 387 | S.resetColor(); |
| 388 | S.changeColor(raw_ostream::SAVEDCOLOR, true); |
| 389 | } |
| 390 | |
Dan Gohman | 5e5442c | 2010-01-21 10:13:27 +0000 | [diff] [blame] | 391 | S << Message << '\n'; |
Daniel Dunbar | 4153982 | 2009-11-22 22:08:00 +0000 | [diff] [blame] | 392 | |
Benjamin Kramer | 89f33fd | 2012-04-18 19:04:15 +0000 | [diff] [blame] | 393 | if (ShowColors) |
| 394 | S.resetColor(); |
| 395 | |
Chris Lattner | 462b43c | 2011-10-16 05:47:55 +0000 | [diff] [blame] | 396 | if (LineNo == -1 || ColumnNo == -1) |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 397 | return; |
Mikhail Glushenkov | e690ffb | 2010-01-27 10:13:11 +0000 | [diff] [blame] | 398 | |
Jordan Rose | 9b1e854 | 2013-01-11 02:37:55 +0000 | [diff] [blame] | 399 | // FIXME: If there are multibyte or multi-column characters in the source, all |
| 400 | // our ranges will be wrong. To do this properly, we'll need a byte-to-column |
| 401 | // map like Clang's TextDiagnostic. For now, we'll just handle tabs by |
| 402 | // expanding them later, and bail out rather than show incorrect ranges and |
| 403 | // misaligned fixits for any other odd characters. |
| 404 | if (std::find_if(LineContents.begin(), LineContents.end(), isNonASCII) != |
| 405 | LineContents.end()) { |
Jordan Rose | 9b1f44b | 2013-01-10 18:50:15 +0000 | [diff] [blame] | 406 | printSourceLine(S, LineContents); |
| 407 | return; |
| 408 | } |
Jordan Rose | 9b1e854 | 2013-01-11 02:37:55 +0000 | [diff] [blame] | 409 | size_t NumColumns = LineContents.size(); |
Jordan Rose | 9b1f44b | 2013-01-10 18:50:15 +0000 | [diff] [blame] | 410 | |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 411 | // Build the line with the caret and ranges. |
Jordan Rose | 9b1f44b | 2013-01-10 18:50:15 +0000 | [diff] [blame] | 412 | std::string CaretLine(NumColumns+1, ' '); |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 413 | |
| 414 | // Expand any ranges. |
| 415 | for (unsigned r = 0, e = Ranges.size(); r != e; ++r) { |
| 416 | std::pair<unsigned, unsigned> R = Ranges[r]; |
Jordan Rose | 9b1f44b | 2013-01-10 18:50:15 +0000 | [diff] [blame] | 417 | std::fill(&CaretLine[R.first], |
| 418 | &CaretLine[std::min((size_t)R.second, CaretLine.size())], |
| 419 | '~'); |
Chris Lattner | 2f510ae | 2009-07-02 22:24:20 +0000 | [diff] [blame] | 420 | } |
Jordan Rose | 9b1f44b | 2013-01-10 18:50:15 +0000 | [diff] [blame] | 421 | |
| 422 | // Add any fix-its. |
| 423 | // FIXME: Find the beginning of the line properly for multibyte characters. |
| 424 | std::string FixItInsertionLine; |
| 425 | buildFixItLine(CaretLine, FixItInsertionLine, FixIts, |
| 426 | makeArrayRef(Loc.getPointer() - ColumnNo, |
| 427 | LineContents.size())); |
| 428 | |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 429 | // Finally, plop on the caret. |
Jordan Rose | 9b1f44b | 2013-01-10 18:50:15 +0000 | [diff] [blame] | 430 | if (unsigned(ColumnNo) <= NumColumns) |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 431 | CaretLine[ColumnNo] = '^'; |
| 432 | else |
Jordan Rose | 9b1f44b | 2013-01-10 18:50:15 +0000 | [diff] [blame] | 433 | CaretLine[NumColumns] = '^'; |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 434 | |
| 435 | // ... and remove trailing whitespace so the output doesn't wrap for it. We |
| 436 | // know that the line isn't completely empty because it has the caret in it at |
| 437 | // least. |
| 438 | CaretLine.erase(CaretLine.find_last_not_of(' ')+1); |
| 439 | |
Jordan Rose | 9b1f44b | 2013-01-10 18:50:15 +0000 | [diff] [blame] | 440 | printSourceLine(S, LineContents); |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 441 | |
Benjamin Kramer | 89f33fd | 2012-04-18 19:04:15 +0000 | [diff] [blame] | 442 | if (ShowColors) |
| 443 | S.changeColor(raw_ostream::GREEN, true); |
| 444 | |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 445 | // Print out the caret line, matching tabs in the source line. |
| 446 | for (unsigned i = 0, e = CaretLine.size(), OutCol = 0; i != e; ++i) { |
| 447 | if (i >= LineContents.size() || LineContents[i] != '\t') { |
| 448 | S << CaretLine[i]; |
| 449 | ++OutCol; |
| 450 | continue; |
| 451 | } |
| 452 | |
| 453 | // Okay, we have a tab. Insert the appropriate number of characters. |
| 454 | do { |
| 455 | S << CaretLine[i]; |
| 456 | ++OutCol; |
Jordan Rose | 9b1f44b | 2013-01-10 18:50:15 +0000 | [diff] [blame] | 457 | } while ((OutCol % TabStop) != 0); |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 458 | } |
Jordan Rose | 9b1f44b | 2013-01-10 18:50:15 +0000 | [diff] [blame] | 459 | S << '\n'; |
Benjamin Kramer | 89f33fd | 2012-04-18 19:04:15 +0000 | [diff] [blame] | 460 | |
| 461 | if (ShowColors) |
| 462 | S.resetColor(); |
Jordan Rose | 9b1f44b | 2013-01-10 18:50:15 +0000 | [diff] [blame] | 463 | |
| 464 | // Print out the replacement line, matching tabs in the source line. |
| 465 | if (FixItInsertionLine.empty()) |
| 466 | return; |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 467 | |
Jordan Rose | 9b1f44b | 2013-01-10 18:50:15 +0000 | [diff] [blame] | 468 | for (size_t i = 0, e = FixItInsertionLine.size(), OutCol = 0; i != e; ++i) { |
| 469 | if (i >= LineContents.size() || LineContents[i] != '\t') { |
| 470 | S << FixItInsertionLine[i]; |
| 471 | ++OutCol; |
| 472 | continue; |
| 473 | } |
| 474 | |
| 475 | // Okay, we have a tab. Insert the appropriate number of characters. |
| 476 | do { |
| 477 | S << FixItInsertionLine[i]; |
| 478 | // FIXME: This is trying not to break up replacements, but then to re-sync |
| 479 | // with the tabs between replacements. This will fail, though, if two |
| 480 | // fix-it replacements are exactly adjacent, or if a fix-it contains a |
| 481 | // space. Really we should be precomputing column widths, which we'll |
| 482 | // need anyway for multibyte chars. |
| 483 | if (FixItInsertionLine[i] != ' ') |
| 484 | ++i; |
| 485 | ++OutCol; |
| 486 | } while (((OutCol % TabStop) != 0) && i != e); |
| 487 | } |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 488 | S << '\n'; |
Chris Lattner | 2f510ae | 2009-07-02 22:24:20 +0000 | [diff] [blame] | 489 | } |