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