Don't analyze block if it's not considered for ifcvt anymore.
[llvm/stm8.git] / lib / Support / SourceMgr.cpp
blobde042a9f53c8262b551f25caf1bed8edd8a76aa1
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.
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"
22 using namespace llvm;
24 namespace {
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))
40 delete Cache;
42 while (!Buffers.empty()) {
43 delete Buffers.back().Buffer;
44 Buffers.pop_back();
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,
52 SMLoc IncludeLoc,
53 std::string &IncludedFile) {
54 OwningPtr<MemoryBuffer> NewBuf;
55 IncludedFile = Filename;
56 MemoryBuffer::getFile(IncludedFile.c_str(), NewBuf);
58 // If the file didn't exist directly, see if it's in an include path.
59 for (unsigned i = 0, e = IncludeDirectories.size(); i != e && !NewBuf; ++i) {
60 IncludedFile = IncludeDirectories[i] + "/" + Filename;
61 MemoryBuffer::getFile(IncludedFile.c_str(), NewBuf);
64 if (NewBuf == 0) return ~0U;
66 return AddNewSourceBuffer(NewBuf.take(), IncludeLoc);
70 /// FindBufferContainingLoc - Return the ID of the buffer containing the
71 /// specified location, returning -1 if not found.
72 int SourceMgr::FindBufferContainingLoc(SMLoc Loc) const {
73 for (unsigned i = 0, e = Buffers.size(); i != e; ++i)
74 if (Loc.getPointer() >= Buffers[i].Buffer->getBufferStart() &&
75 // Use <= here so that a pointer to the null at the end of the buffer
76 // is included as part of the buffer.
77 Loc.getPointer() <= Buffers[i].Buffer->getBufferEnd())
78 return i;
79 return -1;
82 /// FindLineNumber - Find the line number for the specified location in the
83 /// specified file. This is not a fast method.
84 unsigned SourceMgr::FindLineNumber(SMLoc Loc, int BufferID) const {
85 if (BufferID == -1) BufferID = FindBufferContainingLoc(Loc);
86 assert(BufferID != -1 && "Invalid Location!");
88 MemoryBuffer *Buff = getBufferInfo(BufferID).Buffer;
90 // Count the number of \n's between the start of the file and the specified
91 // location.
92 unsigned LineNo = 1;
94 const char *Ptr = Buff->getBufferStart();
96 // If we have a line number cache, and if the query is to a later point in the
97 // same file, start searching from the last query location. This optimizes
98 // for the case when multiple diagnostics come out of one file in order.
99 if (LineNoCacheTy *Cache = getCache(LineNoCache))
100 if (Cache->LastQueryBufferID == BufferID &&
101 Cache->LastQuery <= Loc.getPointer()) {
102 Ptr = Cache->LastQuery;
103 LineNo = Cache->LineNoOfQuery;
106 // Scan for the location being queried, keeping track of the number of lines
107 // we see.
108 for (; SMLoc::getFromPointer(Ptr) != Loc; ++Ptr)
109 if (*Ptr == '\n') ++LineNo;
112 // Allocate the line number cache if it doesn't exist.
113 if (LineNoCache == 0)
114 LineNoCache = new LineNoCacheTy();
116 // Update the line # cache.
117 LineNoCacheTy &Cache = *getCache(LineNoCache);
118 Cache.LastQueryBufferID = BufferID;
119 Cache.LastQuery = Ptr;
120 Cache.LineNoOfQuery = LineNo;
121 return LineNo;
124 void SourceMgr::PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const {
125 if (IncludeLoc == SMLoc()) return; // Top of stack.
127 int CurBuf = FindBufferContainingLoc(IncludeLoc);
128 assert(CurBuf != -1 && "Invalid or unspecified location!");
130 PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS);
132 OS << "Included from "
133 << getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
134 << ":" << FindLineNumber(IncludeLoc, CurBuf) << ":\n";
138 /// GetMessage - Return an SMDiagnostic at the specified location with the
139 /// specified string.
141 /// @param Type - If non-null, the kind of message (e.g., "error") which is
142 /// prefixed to the message.
143 SMDiagnostic SourceMgr::GetMessage(SMLoc Loc, const Twine &Msg,
144 const char *Type, bool ShowLine) const {
146 // First thing to do: find the current buffer containing the specified
147 // location.
148 int CurBuf = FindBufferContainingLoc(Loc);
149 assert(CurBuf != -1 && "Invalid or unspecified location!");
151 MemoryBuffer *CurMB = getBufferInfo(CurBuf).Buffer;
153 // Scan backward to find the start of the line.
154 const char *LineStart = Loc.getPointer();
155 while (LineStart != CurMB->getBufferStart() &&
156 LineStart[-1] != '\n' && LineStart[-1] != '\r')
157 --LineStart;
159 std::string LineStr;
160 if (ShowLine) {
161 // Get the end of the line.
162 const char *LineEnd = Loc.getPointer();
163 while (LineEnd != CurMB->getBufferEnd() &&
164 LineEnd[0] != '\n' && LineEnd[0] != '\r')
165 ++LineEnd;
166 LineStr = std::string(LineStart, LineEnd);
169 std::string PrintedMsg;
170 raw_string_ostream OS(PrintedMsg);
171 if (Type)
172 OS << Type << ": ";
173 OS << Msg;
175 return SMDiagnostic(*this, Loc,
176 CurMB->getBufferIdentifier(), FindLineNumber(Loc, CurBuf),
177 Loc.getPointer()-LineStart, OS.str(),
178 LineStr, ShowLine);
181 void SourceMgr::PrintMessage(SMLoc Loc, const Twine &Msg,
182 const char *Type, bool ShowLine) const {
183 // Report the message with the diagnostic handler if present.
184 if (DiagHandler) {
185 DiagHandler(GetMessage(Loc, Msg, Type, ShowLine), DiagContext);
186 return;
189 raw_ostream &OS = errs();
191 int CurBuf = FindBufferContainingLoc(Loc);
192 assert(CurBuf != -1 && "Invalid or unspecified location!");
193 PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS);
195 GetMessage(Loc, Msg, Type, ShowLine).Print(0, OS);
198 //===----------------------------------------------------------------------===//
199 // SMDiagnostic Implementation
200 //===----------------------------------------------------------------------===//
202 void SMDiagnostic::Print(const char *ProgName, raw_ostream &S) const {
203 if (ProgName && ProgName[0])
204 S << ProgName << ": ";
206 if (!Filename.empty()) {
207 if (Filename == "-")
208 S << "<stdin>";
209 else
210 S << Filename;
212 if (LineNo != -1) {
213 S << ':' << LineNo;
214 if (ColumnNo != -1)
215 S << ':' << (ColumnNo+1);
217 S << ": ";
220 S << Message << '\n';
222 if (LineNo != -1 && ColumnNo != -1 && ShowLine) {
223 S << LineContents << '\n';
225 // Print out spaces/tabs before the caret.
226 for (unsigned i = 0; i != unsigned(ColumnNo); ++i)
227 S << (LineContents[i] == '\t' ? '\t' : ' ');
228 S << "^\n";