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