42 std::string &IncludedFile) {
43 IncludedFile = Filename;
48 for (
unsigned i = 0,
e = IncludeDirectories.size(); i !=
e && !NewBufOrErr;
62 for (
unsigned i = 0,
e = Buffers.size(); i !=
e; ++i)
63 if (Loc.
getPointer() >= Buffers[i].Buffer->getBufferStart() &&
66 Loc.
getPointer() <= Buffers[i].Buffer->getBufferEnd())
72 unsigned SourceMgr::SrcBuffer::getLineNumber(
const char *Ptr)
const {
76 std::vector<T> *
Offsets =
nullptr;
77 if (OffsetCache.isNull()) {
78 Offsets =
new std::vector<T>();
80 size_t Sz = Buffer->getBufferSize();
83 for (
size_t N = 0;
N < Sz; ++
N) {
85 Offsets->push_back(static_cast<T>(
N));
89 Offsets = OffsetCache.get<std::vector<T> *>();
92 const char *BufStart = Buffer->getBufferStart();
93 assert(Ptr >= BufStart && Ptr <= Buffer->getBufferEnd());
96 T PtrOffset =
static_cast<T>(PtrDiff);
103 SourceMgr::SrcBuffer::SrcBuffer(SourceMgr::SrcBuffer &&
Other)
104 : Buffer(std::move(
Other.Buffer)),
105 OffsetCache(
Other.OffsetCache),
106 IncludeLoc(
Other.IncludeLoc) {
107 Other.OffsetCache =
nullptr;
110 SourceMgr::SrcBuffer::~SrcBuffer() {
111 if (!OffsetCache.isNull()) {
112 if (OffsetCache.is<std::vector<uint8_t>*>())
113 delete OffsetCache.get<std::vector<uint8_t>*>();
114 else if (OffsetCache.is<std::vector<uint16_t>*>())
115 delete OffsetCache.get<std::vector<uint16_t>*>();
116 else if (OffsetCache.is<std::vector<uint32_t>*>())
117 delete OffsetCache.get<std::vector<uint32_t>*>();
119 delete OffsetCache.get<std::vector<uint64_t>*>();
120 OffsetCache =
nullptr;
124 std::pair<unsigned, unsigned>
128 assert(BufferID &&
"Invalid Location!");
133 size_t Sz = SB.Buffer->getBufferSize();
136 LineNo = SB.getLineNumber<uint8_t>(Ptr);
138 LineNo = SB.getLineNumber<
uint16_t>(Ptr);
140 LineNo = SB.getLineNumber<
uint32_t>(Ptr);
142 LineNo = SB.getLineNumber<uint64_t>(Ptr);
144 const char *BufStart = SB.Buffer->getBufferStart();
147 return std::make_pair(LineNo, Ptr-BufStart-NewlineOffs);
151 if (IncludeLoc ==
SMLoc())
return;
154 assert(CurBuf &&
"Invalid or unspecified location!");
158 OS <<
"Included from " 170 std::pair<unsigned, unsigned> LineAndCol;
176 assert(CurBuf &&
"Invalid or unspecified location!");
184 while (LineStart != BufStart && LineStart[-1] !=
'\n' &&
185 LineStart[-1] !=
'\r')
191 while (LineEnd != BufEnd && LineEnd[0] !=
'\n' && LineEnd[0] !=
'\r')
193 LineStr = std::string(LineStart, LineEnd);
197 for (
unsigned i = 0,
e = Ranges.
size(); i !=
e; ++i) {
220 return SMDiagnostic(*
this, Loc, BufferID, LineAndCol.first,
221 LineAndCol.second-1, Kind, Msg.
str(),
222 LineStr, ColRanges, FixIts);
226 bool ShowColors)
const {
229 DiagHandler(Diagnostic, DiagContext);
235 assert(CurBuf &&
"Invalid or unspecified location!");
239 Diagnostic.
print(
nullptr, OS, ShowColors);
262 ArrayRef<std::pair<unsigned,unsigned>> Ranges,
264 : SM(&sm), Loc(L), Filename(FN), LineNo(Line), ColumnNo(Col), Kind(Kind),
265 Message(Msg), LineContents(LineStr), Ranges(Ranges.vec()),
266 FixIts(Hints.
begin(), Hints.
end()) {
275 const char *LineStart = SourceLine.
begin();
276 const char *LineEnd = SourceLine.
end();
278 size_t PrevHintEndCol = 0;
308 unsigned HintCol = FirstCol;
309 if (HintCol < PrevHintEndCol)
310 HintCol = PrevHintEndCol + 1;
316 I->getText().size());
319 unsigned LastColumnModified = HintCol +
I->getText().size();
320 if (LastColumnModified > FixItLine.size())
321 FixItLine.resize(LastColumnModified,
' ');
323 std::copy(
I->getText().begin(),
I->getText().end(),
324 FixItLine.begin() + HintCol);
326 PrevHintEndCol = LastColumnModified;
332 LastCol = LineEnd - LineStart;
336 std::fill(&CaretLine[FirstCol], &CaretLine[LastCol],
'~');
342 for (
unsigned i = 0,
e = LineContents.
size(), OutCol = 0; i !=
e; ++i) {
343 size_t NextTab = LineContents.
find(
'\t', i);
351 S << LineContents.
slice(i, NextTab);
352 OutCol += NextTab - i;
359 }
while ((OutCol %
TabStop) != 0);
369 bool ShowColors,
bool ShowKindLabel)
const {
373 if (ProgName && ProgName[0])
374 S << ProgName <<
": ";
376 if (!Filename.empty()) {
385 S <<
':' << (ColumnNo + 1);
411 if (LineNo == -1 || ColumnNo == -1)
423 size_t NumColumns = LineContents.size();
426 std::string CaretLine(NumColumns+1,
' ');
429 for (
unsigned r = 0,
e = Ranges.size(); r !=
e; ++r) {
430 std::pair<unsigned, unsigned> R = Ranges[r];
431 std::fill(&CaretLine[R.first],
432 &CaretLine[std::min((
size_t)R.second, CaretLine.size())],
438 std::string FixItInsertionLine;
441 LineContents.size()));
444 if (
unsigned(ColumnNo) <= NumColumns)
445 CaretLine[ColumnNo] =
'^';
447 CaretLine[NumColumns] =
'^';
452 CaretLine.erase(CaretLine.find_last_not_of(
' ')+1);
460 for (
unsigned i = 0,
e = CaretLine.size(), OutCol = 0; i !=
e; ++i) {
461 if (i >= LineContents.size() || LineContents[i] !=
'\t') {
471 }
while ((OutCol %
TabStop) != 0);
477 if (FixItInsertionLine.empty())
480 for (
size_t i = 0,
e = FixItInsertionLine.size(), OutCol = 0; i <
e; ++i) {
481 if (i >= LineContents.size() || LineContents[i] !=
'\t') {
482 OS << FixItInsertionLine[i];
489 OS << FixItInsertionLine[i];
495 if (FixItInsertionLine[i] !=
' ')
498 }
while (((OutCol %
TabStop) != 0) && i !=
e);
auto lower_bound(R &&Range, T &&Value) -> decltype(adl_begin(Range))
Provide wrappers to std::lower_bound which take ranges instead of having to pass begin/end explicitly...
virtual StringRef getBufferIdentifier() const
Return an identifier for this buffer, typically the filename it was read from.
Represents a range in source code.
const_iterator end(StringRef path)
Get end iterator over path.
Represents either an error or a value T.
unsigned FindBufferContainingLoc(SMLoc Loc) const
Return the ID of the buffer containing the specified location.
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
This class represents lattice values for constants.
void print(const char *ProgName, raw_ostream &S, bool ShowColors=true, bool ShowKindLabel=true) const
An RAII object that temporarily switches an output stream to a specific color.
void push_back(const T &Elt)
static raw_ostream & error()
Convenience method for printing "error: " to stderr.
void PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const
Prints the names of included files and the line of the file they were included from.
LLVM_NODISCARD size_t find_last_of(char C, size_t From=npos) const
Find the last character in the string that is C, or npos if not found.
static raw_ostream & warning()
Convenience method for printing "warning: " to stderr.
std::pair< unsigned, unsigned > getLineAndColumn(SMLoc Loc, unsigned BufferID=0) const
Find the line and column number for the specified location in the specified file. ...
void PrintMessage(raw_ostream &OS, SMLoc Loc, DiagKind Kind, const Twine &Msg, ArrayRef< SMRange > Ranges=None, ArrayRef< SMFixIt > FixIts=None, bool ShowColors=true) const
Emit a message about the specified location with the specified string.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
LLVM_NODISCARD StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
LLVM_NODISCARD StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
static raw_ostream & note()
Convenience method for printing "note: " to stderr.
const MemoryBuffer * getMemoryBuffer(unsigned i) const
static const size_t TabStop
unsigned AddNewSourceBuffer(std::unique_ptr< MemoryBuffer > F, SMLoc IncludeLoc)
Add a new source buffer to this source manager.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
LLVM_NODISCARD size_t size() const
size - Get the string size.
int columnWidth(StringRef s)
const char * getPointer() const
static const Colors GREEN
size_t size() const
size - Get the array size.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
LLVM_NODISCARD size_t find(char C, size_t From=0) const
Search for the first character C in the string.
auto find_if(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range))
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly...
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling...
StringRef get_separator(Style style=Style::native)
Return the preferred separator for this platform.
SMDiagnostic GetMessage(SMLoc Loc, DiagKind Kind, const Twine &Msg, ArrayRef< SMRange > Ranges=None, ArrayRef< SMFixIt > FixIts=None) const
Return an SMDiagnostic at the specified location with the specified string.
const SrcBuffer & getBufferInfo(unsigned i) const
void sort(IteratorTy Start, IteratorTy End)
Align max(MaybeAlign Lhs, Align Rhs)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
static void printSourceLine(raw_ostream &S, StringRef LineContents)
This interface provides simple read-only access to a block of memory, and provides simple methods for...
static raw_ostream & remark()
Convenience method for printing "remark: " to stderr.
unsigned FindLineNumber(SMLoc Loc, unsigned BufferID=0) const
Find the line number for the specified location in the specified file.
static bool isNonASCII(char c)
static SMLoc getFromPointer(const char *Ptr)
unsigned AddIncludeFile(const std::string &Filename, SMLoc IncludeLoc, std::string &IncludedFile)
Search for a file with the specified name in the current directory or in one of the IncludeDirs...
const char * getBufferEnd() const
static void buildFixItLine(std::string &CaretLine, std::string &FixItLine, ArrayRef< SMFixIt > FixIts, ArrayRef< char > SourceLine)
const char * getBufferStart() const
std::string str() const
Return the twine contents as a std::string.
Provides ErrorOr<T> smart pointer.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true, bool IsVolatile=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static const Colors SAVEDCOLOR
This class implements an extremely fast bulk output stream that can only output to a stream...
StringRef - Represent a constant reference to a string, i.e.
Represents a location in source code.
OutputIt copy(R &&Range, OutputIt Out)
bool empty() const
empty - Check if the array is empty.
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...