Normalize SourceMgr messages.
[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/Support/MemoryBuffer.h"
18 #include "llvm/Support/raw_ostream.h"
19 using namespace llvm;
20
21 SourceMgr::~SourceMgr() {
22   while (!Buffers.empty()) {
23     delete Buffers.back().Buffer;
24     Buffers.pop_back();
25   }
26 }
27
28 /// AddIncludeFile - Search for a file with the specified name in the current
29 /// directory or in one of the IncludeDirs.  If no file is found, this returns
30 /// ~0, otherwise it returns the buffer ID of the stacked file.
31 unsigned SourceMgr::AddIncludeFile(const std::string &Filename,
32                                    SMLoc IncludeLoc) {
33   
34   MemoryBuffer *NewBuf = MemoryBuffer::getFile(Filename.c_str());
35
36   // If the file didn't exist directly, see if it's in an include path.
37   for (unsigned i = 0, e = IncludeDirectories.size(); i != e && !NewBuf; ++i) {
38     std::string IncFile = IncludeDirectories[i] + "/" + Filename;
39     NewBuf = MemoryBuffer::getFile(IncFile.c_str());
40   }
41  
42   if (NewBuf == 0) return ~0U;
43
44   return AddNewSourceBuffer(NewBuf, IncludeLoc);
45 }
46
47
48 /// FindBufferContainingLoc - Return the ID of the buffer containing the
49 /// specified location, returning -1 if not found.
50 int SourceMgr::FindBufferContainingLoc(SMLoc Loc) const {
51   for (unsigned i = 0, e = Buffers.size(); i != e; ++i)
52     if (Loc.getPointer() >= Buffers[i].Buffer->getBufferStart() &&
53         // Use <= here so that a pointer to the null at the end of the buffer
54         // is included as part of the buffer.
55         Loc.getPointer() <= Buffers[i].Buffer->getBufferEnd())
56       return i;
57   return -1;
58 }
59
60 /// FindLineNumber - Find the line number for the specified location in the
61 /// specified file.  This is not a fast method.
62 unsigned SourceMgr::FindLineNumber(SMLoc Loc, int BufferID) const {
63   if (BufferID == -1) BufferID = FindBufferContainingLoc(Loc);
64   assert(BufferID != -1 && "Invalid Location!");
65   
66   MemoryBuffer *Buff = getBufferInfo(BufferID).Buffer;
67   
68   // Count the number of \n's between the start of the file and the specified
69   // location.
70   unsigned LineNo = 1;
71   
72   const char *Ptr = Buff->getBufferStart();
73
74   for (; SMLoc::getFromPointer(Ptr) != Loc; ++Ptr)
75     if (*Ptr == '\n') ++LineNo;
76   return LineNo;
77 }
78
79 void SourceMgr::PrintIncludeStack(SMLoc IncludeLoc) const {
80   if (IncludeLoc == SMLoc()) return;  // Top of stack.
81   
82   int CurBuf = FindBufferContainingLoc(IncludeLoc);
83   assert(CurBuf != -1 && "Invalid or unspecified location!");
84
85   PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc);
86   
87   errs() << "Included from "
88          << getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
89          << ":" << FindLineNumber(IncludeLoc, CurBuf) << ":\n";
90 }
91
92
93 void SourceMgr::PrintMessage(SMLoc Loc, const std::string &Msg, 
94                              const char *Type) const {
95   raw_ostream &OS = errs();
96   
97   // First thing to do: find the current buffer containing the specified
98   // location.
99   int CurBuf = FindBufferContainingLoc(Loc);
100   assert(CurBuf != -1 && "Invalid or unspecified location!");
101   
102   PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc);
103   
104   MemoryBuffer *CurMB = getBufferInfo(CurBuf).Buffer;
105   
106   
107   OS << CurMB->getBufferIdentifier() << ":"
108      << FindLineNumber(Loc, CurBuf) << ": ";
109
110   if (Type)
111     OS << Type << ": ";
112
113   OS << Msg << "\n";
114   
115   // Scan backward to find the start of the line.
116   const char *LineStart = Loc.getPointer();
117   while (LineStart != CurMB->getBufferStart() && 
118          LineStart[-1] != '\n' && LineStart[-1] != '\r')
119     --LineStart;
120   // Get the end of the line.
121   const char *LineEnd = Loc.getPointer();
122   while (LineEnd != CurMB->getBufferEnd() && 
123          LineEnd[0] != '\n' && LineEnd[0] != '\r')
124     ++LineEnd;
125   // Print out the line.
126   OS << std::string(LineStart, LineEnd) << "\n";
127   // Print out spaces before the caret.
128   for (const char *Pos = LineStart; Pos != Loc.getPointer(); ++Pos)
129     OS << (*Pos == '\t' ? '\t' : ' ');
130   OS << "^\n";
131 }