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);
105 return (1 + (EOL - Offsets->begin()));
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;
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>*>();
124 delete OffsetCache.get<std::vector<uint64_t>*>();
125 OffsetCache =
nullptr;
129 std::pair<unsigned, unsigned>
133 assert(BufferID &&
"Invalid Location!");
138 size_t Sz = SB.Buffer->getBufferSize();
141 LineNo = SB.getLineNumber<uint8_t>(Ptr);
143 LineNo = SB.getLineNumber<uint16_t>(Ptr);
145 LineNo = SB.getLineNumber<
uint32_t>(Ptr);
147 LineNo = SB.getLineNumber<uint64_t>(Ptr);
149 const char *BufStart = SB.Buffer->getBufferStart();
152 return std::make_pair(LineNo, Ptr-BufStart-NewlineOffs);
156 if (IncludeLoc ==
SMLoc())
return;
159 assert(CurBuf &&
"Invalid or unspecified location!");
163 OS <<
"Included from " 175 std::pair<unsigned, unsigned> LineAndCol;
181 assert(CurBuf &&
"Invalid or unspecified location!");
189 while (LineStart != BufStart && LineStart[-1] !=
'\n' &&
190 LineStart[-1] !=
'\r')
196 while (LineEnd != BufEnd && LineEnd[0] !=
'\n' && LineEnd[0] !=
'\r')
198 LineStr = std::string(LineStart, LineEnd);
202 for (
unsigned i = 0, e = Ranges.
size(); i != e; ++i) {
225 return SMDiagnostic(*
this, Loc, BufferID, LineAndCol.first,
226 LineAndCol.second-1, Kind, Msg.
str(),
227 LineStr, ColRanges, FixIts);
231 bool ShowColors)
const {
234 DiagHandler(Diagnostic, DiagContext);
240 assert(CurBuf &&
"Invalid or unspecified location!");
244 Diagnostic.
print(
nullptr, OS, ShowColors);
267 ArrayRef<std::pair<unsigned,unsigned>> Ranges,
269 : SM(&sm), Loc(L), Filename(FN), LineNo(Line), ColumnNo(Col), Kind(Kind),
270 Message(Msg), LineContents(LineStr), Ranges(Ranges.vec()),
271 FixIts(Hints.
begin(), Hints.
end()) {
280 const char *LineStart = SourceLine.
begin();
281 const char *LineEnd = SourceLine.
end();
283 size_t PrevHintEndCol = 0;
313 unsigned HintCol = FirstCol;
314 if (HintCol < PrevHintEndCol)
315 HintCol = PrevHintEndCol + 1;
321 I->getText().size());
324 unsigned LastColumnModified = HintCol +
I->getText().size();
325 if (LastColumnModified > FixItLine.size())
326 FixItLine.resize(LastColumnModified,
' ');
328 std::copy(
I->getText().begin(),
I->getText().end(),
329 FixItLine.begin() + HintCol);
331 PrevHintEndCol = LastColumnModified;
337 LastCol = LineEnd - LineStart;
341 std::fill(&CaretLine[FirstCol], &CaretLine[LastCol],
'~');
347 for (
unsigned i = 0, e = LineContents.
size(), OutCol = 0; i != e; ++i) {
348 if (LineContents[i] !=
'\t') {
349 S << LineContents[i];
358 }
while ((OutCol %
TabStop) != 0);
368 bool ShowKindLabel)
const {
375 if (ProgName && ProgName[0])
376 S << ProgName <<
": ";
378 if (!Filename.empty()) {
387 S <<
':' << (ColumnNo+1);
422 S << Message <<
'\n';
427 if (LineNo == -1 || ColumnNo == -1)
439 size_t NumColumns = LineContents.size();
442 std::string CaretLine(NumColumns+1,
' ');
445 for (
unsigned r = 0, e = Ranges.size(); r != e; ++r) {
446 std::pair<unsigned, unsigned> R = Ranges[r];
447 std::fill(&CaretLine[R.first],
448 &CaretLine[std::min((
size_t)R.second, CaretLine.size())],
454 std::string FixItInsertionLine;
457 LineContents.size()));
460 if (
unsigned(ColumnNo) <= NumColumns)
461 CaretLine[ColumnNo] =
'^';
463 CaretLine[NumColumns] =
'^';
468 CaretLine.erase(CaretLine.find_last_not_of(
' ')+1);
476 for (
unsigned i = 0, e = CaretLine.size(), OutCol = 0; i != e; ++i) {
477 if (i >= LineContents.size() || LineContents[i] !=
'\t') {
487 }
while ((OutCol %
TabStop) != 0);
495 if (FixItInsertionLine.empty())
498 for (
size_t i = 0, e = FixItInsertionLine.size(), OutCol = 0; i < e; ++i) {
499 if (i >= LineContents.size() || LineContents[i] !=
'\t') {
500 S << FixItInsertionLine[i];
507 S << FixItInsertionLine[i];
513 if (FixItInsertionLine[i] !=
' ')
516 }
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.
void push_back(const T &Elt)
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.
Compute iterated dominance frontiers using a linear time algorithm.
void print(const char *ProgName, raw_ostream &S, bool ShowColors=true, bool ShowKindLabel=true) const
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
virtual raw_ostream & changeColor(enum Colors Color, bool Bold=false, bool BG=false)
Changes the foreground color of text that will be output from this point forward. ...
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.
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.
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...
virtual bool has_colors() const
This function determines if this stream is displayed and supports colors.
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)
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...
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())
virtual raw_ostream & resetColor()
Resets the colors to terminal defaults.
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...