43 std::string &IncludedFile) {
44 IncludedFile = Filename;
49 for (
unsigned i = 0, e = IncludeDirectories.size(); i != e && !NewBufOrErr;
63 for (
unsigned i = 0, e = Buffers.size(); i != e; ++i)
64 if (Loc.
getPointer() >= Buffers[i].Buffer->getBufferStart() &&
67 Loc.
getPointer() <= Buffers[i].Buffer->getBufferEnd())
73 unsigned SourceMgr::SrcBuffer::getLineNumber(
const char *Ptr)
const {
77 std::vector<T> *
Offsets =
nullptr;
78 if (OffsetCache.isNull()) {
79 Offsets =
new std::vector<T>();
81 size_t Sz = Buffer->getBufferSize();
84 for (
size_t N = 0;
N < Sz; ++
N) {
86 Offsets->push_back(static_cast<T>(
N));
90 Offsets = OffsetCache.get<std::vector<T> *>();
93 const char *BufStart = Buffer->getBufferStart();
94 assert(Ptr >= BufStart && Ptr <= Buffer->getBufferEnd());
97 T PtrOffset =
static_cast<T>(PtrDiff);
106 return (1 + (EOL - Offsets->begin()));
109 SourceMgr::SrcBuffer::SrcBuffer(SourceMgr::SrcBuffer &&
Other)
110 : Buffer(std::move(
Other.Buffer)),
111 OffsetCache(
Other.OffsetCache),
112 IncludeLoc(
Other.IncludeLoc) {
113 Other.OffsetCache =
nullptr;
116 SourceMgr::SrcBuffer::~SrcBuffer() {
117 if (!OffsetCache.isNull()) {
118 if (OffsetCache.is<std::vector<uint8_t>*>())
119 delete OffsetCache.get<std::vector<uint8_t>*>();
120 else if (OffsetCache.is<std::vector<uint16_t>*>())
121 delete OffsetCache.get<std::vector<uint16_t>*>();
122 else if (OffsetCache.is<std::vector<uint32_t>*>())
123 delete OffsetCache.get<std::vector<uint32_t>*>();
125 delete OffsetCache.get<std::vector<uint64_t>*>();
126 OffsetCache =
nullptr;
130 std::pair<unsigned, unsigned>
134 assert(BufferID &&
"Invalid Location!");
139 size_t Sz = SB.Buffer->getBufferSize();
142 LineNo = SB.getLineNumber<uint8_t>(Ptr);
144 LineNo = SB.getLineNumber<uint16_t>(Ptr);
146 LineNo = SB.getLineNumber<
uint32_t>(Ptr);
148 LineNo = SB.getLineNumber<uint64_t>(Ptr);
150 const char *BufStart = SB.Buffer->getBufferStart();
153 return std::make_pair(LineNo, Ptr-BufStart-NewlineOffs);
157 if (IncludeLoc ==
SMLoc())
return;
160 assert(CurBuf &&
"Invalid or unspecified location!");
164 OS <<
"Included from " 176 std::pair<unsigned, unsigned> LineAndCol;
182 assert(CurBuf &&
"Invalid or unspecified location!");
190 while (LineStart != BufStart && LineStart[-1] !=
'\n' &&
191 LineStart[-1] !=
'\r')
197 while (LineEnd != BufEnd && LineEnd[0] !=
'\n' && LineEnd[0] !=
'\r')
199 LineStr = std::string(LineStart, LineEnd);
203 for (
unsigned i = 0, e = Ranges.
size(); i != e; ++i) {
226 return SMDiagnostic(*
this, Loc, BufferID, LineAndCol.first,
227 LineAndCol.second-1, Kind, Msg.
str(),
228 LineStr, ColRanges, FixIts);
232 bool ShowColors)
const {
235 DiagHandler(Diagnostic, DiagContext);
241 assert(CurBuf &&
"Invalid or unspecified location!");
245 Diagnostic.
print(
nullptr, OS, ShowColors);
268 ArrayRef<std::pair<unsigned,unsigned>> Ranges,
270 : SM(&sm), Loc(L), Filename(FN), LineNo(Line), ColumnNo(Col), Kind(Kind),
271 Message(Msg), LineContents(LineStr), Ranges(Ranges.vec()),
272 FixIts(Hints.
begin(), Hints.
end()) {
281 const char *LineStart = SourceLine.
begin();
282 const char *LineEnd = SourceLine.
end();
284 size_t PrevHintEndCol = 0;
314 unsigned HintCol = FirstCol;
315 if (HintCol < PrevHintEndCol)
316 HintCol = PrevHintEndCol + 1;
322 I->getText().size());
325 unsigned LastColumnModified = HintCol +
I->getText().size();
326 if (LastColumnModified > FixItLine.size())
327 FixItLine.resize(LastColumnModified,
' ');
329 std::copy(
I->getText().begin(),
I->getText().end(),
330 FixItLine.begin() + HintCol);
332 PrevHintEndCol = LastColumnModified;
338 LastCol = LineEnd - LineStart;
342 std::fill(&CaretLine[FirstCol], &CaretLine[LastCol],
'~');
348 for (
unsigned i = 0, e = LineContents.
size(), OutCol = 0; i != e; ++i) {
349 size_t NextTab = LineContents.
find(
'\t', i);
357 S << LineContents.
slice(i, NextTab);
358 OutCol += NextTab - i;
365 }
while ((OutCol %
TabStop) != 0);
375 bool ShowColors,
bool ShowKindLabel)
const {
379 if (ProgName && ProgName[0])
380 S << ProgName <<
": ";
382 if (!Filename.empty()) {
391 S <<
':' << (ColumnNo + 1);
417 if (LineNo == -1 || ColumnNo == -1)
429 size_t NumColumns = LineContents.size();
432 std::string CaretLine(NumColumns+1,
' ');
435 for (
unsigned r = 0, e = Ranges.size(); r != e; ++r) {
436 std::pair<unsigned, unsigned> R = Ranges[r];
437 std::fill(&CaretLine[R.first],
438 &CaretLine[std::min((
size_t)R.second, CaretLine.size())],
444 std::string FixItInsertionLine;
447 LineContents.size()));
450 if (
unsigned(ColumnNo) <= NumColumns)
451 CaretLine[ColumnNo] =
'^';
453 CaretLine[NumColumns] =
'^';
458 CaretLine.erase(CaretLine.find_last_not_of(
' ')+1);
466 for (
unsigned i = 0, e = CaretLine.size(), OutCol = 0; i != e; ++i) {
467 if (i >= LineContents.size() || LineContents[i] !=
'\t') {
477 }
while ((OutCol %
TabStop) != 0);
483 if (FixItInsertionLine.empty())
486 for (
size_t i = 0, e = FixItInsertionLine.size(), OutCol = 0; i < e; ++i) {
487 if (i >= LineContents.size() || LineContents[i] !=
'\t') {
488 OS << FixItInsertionLine[i];
495 OS << FixItInsertionLine[i];
501 if (FixItInsertionLine[i] !=
' ')
504 }
while (((OutCol %
TabStop) != 0) && i != e);
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.
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
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)
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
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. ...
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
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.
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)...
auto lower_bound(R &&Range, ForwardIt I) -> decltype(adl_begin(Range))
Provide wrappers to std::lower_bound which take ranges instead of having to pass begin/end explicitly...
int columnWidth(StringRef s)
const char * getPointer() const
size_t size() const
size - Get the array size.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
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)
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
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.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t find(char C, size_t From=0) const
Search for the first character C in the string.
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...