1 //===- SourceMgr.cpp - Manager for Simple Source Buffers & Diagnostics ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
14 //===----------------------------------------------------------------------===//
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/Support/SourceMgr.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 #include "llvm/ADT/OwningPtr.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include "llvm/Support/system_error.h"
25 struct LineNoCacheTy
{
26 int LastQueryBufferID
;
27 const char *LastQuery
;
28 unsigned LineNoOfQuery
;
32 static LineNoCacheTy
*getCache(void *Ptr
) {
33 return (LineNoCacheTy
*)Ptr
;
37 SourceMgr::~SourceMgr() {
38 // Delete the line # cache if allocated.
39 if (LineNoCacheTy
*Cache
= getCache(LineNoCache
))
42 while (!Buffers
.empty()) {
43 delete Buffers
.back().Buffer
;
48 /// AddIncludeFile - Search for a file with the specified name in the current
49 /// directory or in one of the IncludeDirs. If no file is found, this returns
50 /// ~0, otherwise it returns the buffer ID of the stacked file.
51 unsigned SourceMgr::AddIncludeFile(const std::string
&Filename
,
53 OwningPtr
<MemoryBuffer
> NewBuf
;
54 MemoryBuffer::getFile(Filename
.c_str(), NewBuf
);
56 // If the file didn't exist directly, see if it's in an include path.
57 for (unsigned i
= 0, e
= IncludeDirectories
.size(); i
!= e
&& !NewBuf
; ++i
) {
58 std::string IncFile
= IncludeDirectories
[i
] + "/" + Filename
;
59 MemoryBuffer::getFile(IncFile
.c_str(), NewBuf
);
62 if (NewBuf
== 0) return ~0U;
64 return AddNewSourceBuffer(NewBuf
.take(), IncludeLoc
);
68 /// FindBufferContainingLoc - Return the ID of the buffer containing the
69 /// specified location, returning -1 if not found.
70 int SourceMgr::FindBufferContainingLoc(SMLoc Loc
) const {
71 for (unsigned i
= 0, e
= Buffers
.size(); i
!= e
; ++i
)
72 if (Loc
.getPointer() >= Buffers
[i
].Buffer
->getBufferStart() &&
73 // Use <= here so that a pointer to the null at the end of the buffer
74 // is included as part of the buffer.
75 Loc
.getPointer() <= Buffers
[i
].Buffer
->getBufferEnd())
80 /// FindLineNumber - Find the line number for the specified location in the
81 /// specified file. This is not a fast method.
82 unsigned SourceMgr::FindLineNumber(SMLoc Loc
, int BufferID
) const {
83 if (BufferID
== -1) BufferID
= FindBufferContainingLoc(Loc
);
84 assert(BufferID
!= -1 && "Invalid Location!");
86 MemoryBuffer
*Buff
= getBufferInfo(BufferID
).Buffer
;
88 // Count the number of \n's between the start of the file and the specified
92 const char *Ptr
= Buff
->getBufferStart();
94 // If we have a line number cache, and if the query is to a later point in the
95 // same file, start searching from the last query location. This optimizes
96 // for the case when multiple diagnostics come out of one file in order.
97 if (LineNoCacheTy
*Cache
= getCache(LineNoCache
))
98 if (Cache
->LastQueryBufferID
== BufferID
&&
99 Cache
->LastQuery
<= Loc
.getPointer()) {
100 Ptr
= Cache
->LastQuery
;
101 LineNo
= Cache
->LineNoOfQuery
;
104 // Scan for the location being queried, keeping track of the number of lines
106 for (; SMLoc::getFromPointer(Ptr
) != Loc
; ++Ptr
)
107 if (*Ptr
== '\n') ++LineNo
;
110 // Allocate the line number cache if it doesn't exist.
111 if (LineNoCache
== 0)
112 LineNoCache
= new LineNoCacheTy();
114 // Update the line # cache.
115 LineNoCacheTy
&Cache
= *getCache(LineNoCache
);
116 Cache
.LastQueryBufferID
= BufferID
;
117 Cache
.LastQuery
= Ptr
;
118 Cache
.LineNoOfQuery
= LineNo
;
122 void SourceMgr::PrintIncludeStack(SMLoc IncludeLoc
, raw_ostream
&OS
) const {
123 if (IncludeLoc
== SMLoc()) return; // Top of stack.
125 int CurBuf
= FindBufferContainingLoc(IncludeLoc
);
126 assert(CurBuf
!= -1 && "Invalid or unspecified location!");
128 PrintIncludeStack(getBufferInfo(CurBuf
).IncludeLoc
, OS
);
130 OS
<< "Included from "
131 << getBufferInfo(CurBuf
).Buffer
->getBufferIdentifier()
132 << ":" << FindLineNumber(IncludeLoc
, CurBuf
) << ":\n";
136 /// GetMessage - Return an SMDiagnostic at the specified location with the
137 /// specified string.
139 /// @param Type - If non-null, the kind of message (e.g., "error") which is
140 /// prefixed to the message.
141 SMDiagnostic
SourceMgr::GetMessage(SMLoc Loc
, const Twine
&Msg
,
142 const char *Type
, bool ShowLine
) const {
144 // First thing to do: find the current buffer containing the specified
146 int CurBuf
= FindBufferContainingLoc(Loc
);
147 assert(CurBuf
!= -1 && "Invalid or unspecified location!");
149 MemoryBuffer
*CurMB
= getBufferInfo(CurBuf
).Buffer
;
151 // Scan backward to find the start of the line.
152 const char *LineStart
= Loc
.getPointer();
153 while (LineStart
!= CurMB
->getBufferStart() &&
154 LineStart
[-1] != '\n' && LineStart
[-1] != '\r')
159 // Get the end of the line.
160 const char *LineEnd
= Loc
.getPointer();
161 while (LineEnd
!= CurMB
->getBufferEnd() &&
162 LineEnd
[0] != '\n' && LineEnd
[0] != '\r')
164 LineStr
= std::string(LineStart
, LineEnd
);
167 std::string PrintedMsg
;
168 raw_string_ostream
OS(PrintedMsg
);
173 return SMDiagnostic(*this, Loc
,
174 CurMB
->getBufferIdentifier(), FindLineNumber(Loc
, CurBuf
),
175 Loc
.getPointer()-LineStart
, OS
.str(),
179 void SourceMgr::PrintMessage(SMLoc Loc
, const Twine
&Msg
,
180 const char *Type
, bool ShowLine
) const {
181 // Report the message with the diagnostic handler if present.
183 DiagHandler(GetMessage(Loc
, Msg
, Type
, ShowLine
), DiagContext
);
187 raw_ostream
&OS
= errs();
189 int CurBuf
= FindBufferContainingLoc(Loc
);
190 assert(CurBuf
!= -1 && "Invalid or unspecified location!");
191 PrintIncludeStack(getBufferInfo(CurBuf
).IncludeLoc
, OS
);
193 GetMessage(Loc
, Msg
, Type
, ShowLine
).Print(0, OS
);
196 //===----------------------------------------------------------------------===//
197 // SMDiagnostic Implementation
198 //===----------------------------------------------------------------------===//
200 void SMDiagnostic::Print(const char *ProgName
, raw_ostream
&S
) const {
201 if (ProgName
&& ProgName
[0])
202 S
<< ProgName
<< ": ";
204 if (!Filename
.empty()) {
213 S
<< ':' << (ColumnNo
+1);
218 S
<< Message
<< '\n';
220 if (LineNo
!= -1 && ColumnNo
!= -1 && ShowLine
) {
221 S
<< LineContents
<< '\n';
223 // Print out spaces/tabs before the caret.
224 for (unsigned i
= 0; i
!= unsigned(ColumnNo
); ++i
)
225 S
<< (LineContents
[i
] == '\t' ? '\t' : ' ');