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