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