blob: 123531e21db53aa7047344cec3f49a9fb878c55c [file] [log] [blame]
Chris Lattner099e1982009-06-21 03:36:54 +00001//===- SourceMgr.cpp - Manager for Simple Source Buffers & Diagnostics ----===//
Chris Lattneraa739d22009-03-13 07:05:43 +00002//
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//
Chris Lattner099e1982009-06-21 03:36:54 +000010// 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.
Chris Lattneraa739d22009-03-13 07:05:43 +000013//
14//===----------------------------------------------------------------------===//
15
Chris Lattner099e1982009-06-21 03:36:54 +000016#include "llvm/Support/SourceMgr.h"
Chris Lattneraa739d22009-03-13 07:05:43 +000017#include "llvm/Support/MemoryBuffer.h"
18#include "llvm/Support/raw_ostream.h"
19using namespace llvm;
20
Chris Lattner8070ea32009-06-21 03:41:50 +000021SourceMgr::~SourceMgr() {
Chris Lattneraa739d22009-03-13 07:05:43 +000022 while (!Buffers.empty()) {
23 delete Buffers.back().Buffer;
24 Buffers.pop_back();
25 }
26}
27
Chris Lattner7ee5d5f2009-06-21 05:06:04 +000028/// 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.
31unsigned 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
Chris Lattneraa739d22009-03-13 07:05:43 +000048/// FindBufferContainingLoc - Return the ID of the buffer containing the
49/// specified location, returning -1 if not found.
Chris Lattner8070ea32009-06-21 03:41:50 +000050int SourceMgr::FindBufferContainingLoc(SMLoc Loc) const {
Chris Lattneraa739d22009-03-13 07:05:43 +000051 for (unsigned i = 0, e = Buffers.size(); i != e; ++i)
Chris Lattner1c8ae592009-03-13 16:01:53 +000052 if (Loc.getPointer() >= Buffers[i].Buffer->getBufferStart() &&
Chris Lattnerd4771822009-03-18 20:36:45 +000053 // 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())
Chris Lattneraa739d22009-03-13 07:05:43 +000056 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.
Chris Lattner8070ea32009-06-21 03:41:50 +000062unsigned SourceMgr::FindLineNumber(SMLoc Loc, int BufferID) const {
Chris Lattneraa739d22009-03-13 07:05:43 +000063 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
Chris Lattner1e3a8a42009-06-21 03:39:35 +000074 for (; SMLoc::getFromPointer(Ptr) != Loc; ++Ptr)
Chris Lattneraa739d22009-03-13 07:05:43 +000075 if (*Ptr == '\n') ++LineNo;
76 return LineNo;
77}
78
Chris Lattner2f510ae2009-07-02 22:24:20 +000079void SourceMgr::PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const {
Chris Lattner1e3a8a42009-06-21 03:39:35 +000080 if (IncludeLoc == SMLoc()) return; // Top of stack.
Chris Lattneraa739d22009-03-13 07:05:43 +000081
82 int CurBuf = FindBufferContainingLoc(IncludeLoc);
83 assert(CurBuf != -1 && "Invalid or unspecified location!");
84
Chris Lattner2f510ae2009-07-02 22:24:20 +000085 PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS);
Chris Lattneraa739d22009-03-13 07:05:43 +000086
Chris Lattner2f510ae2009-07-02 22:24:20 +000087 OS << "Included from "
88 << getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
89 << ":" << FindLineNumber(IncludeLoc, CurBuf) << ":\n";
Chris Lattneraa739d22009-03-13 07:05:43 +000090}
91
92
Daniel Dunbar3fb76832009-06-30 00:49:23 +000093void SourceMgr::PrintMessage(SMLoc Loc, const std::string &Msg,
94 const char *Type) const {
Chris Lattneraa739d22009-03-13 07:05:43 +000095 raw_ostream &OS = errs();
96
97 // First thing to do: find the current buffer containing the specified
98 // location.
Chris Lattner14ee48a2009-06-21 21:22:11 +000099 int CurBuf = FindBufferContainingLoc(Loc);
Chris Lattneraa739d22009-03-13 07:05:43 +0000100 assert(CurBuf != -1 && "Invalid or unspecified location!");
101
Chris Lattner2f510ae2009-07-02 22:24:20 +0000102 PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS);
Chris Lattneraa739d22009-03-13 07:05:43 +0000103
104 MemoryBuffer *CurMB = getBufferInfo(CurBuf).Buffer;
105
106
Chris Lattneraa739d22009-03-13 07:05:43 +0000107 // Scan backward to find the start of the line.
Chris Lattner14ee48a2009-06-21 21:22:11 +0000108 const char *LineStart = Loc.getPointer();
Chris Lattneraa739d22009-03-13 07:05:43 +0000109 while (LineStart != CurMB->getBufferStart() &&
110 LineStart[-1] != '\n' && LineStart[-1] != '\r')
111 --LineStart;
112 // Get the end of the line.
Chris Lattner14ee48a2009-06-21 21:22:11 +0000113 const char *LineEnd = Loc.getPointer();
Chris Lattneraa739d22009-03-13 07:05:43 +0000114 while (LineEnd != CurMB->getBufferEnd() &&
115 LineEnd[0] != '\n' && LineEnd[0] != '\r')
116 ++LineEnd;
Chris Lattner2f510ae2009-07-02 22:24:20 +0000117
118 std::string PrintedMsg;
119 if (Type) {
120 PrintedMsg = Type;
121 PrintedMsg += ": ";
122 }
123 PrintedMsg += Msg;
124
125
Chris Lattneraa739d22009-03-13 07:05:43 +0000126 // Print out the line.
Chris Lattner2f510ae2009-07-02 22:24:20 +0000127 SMDiagnostic(CurMB->getBufferIdentifier(), FindLineNumber(Loc, CurBuf),
128 Loc.getPointer()-LineStart, PrintedMsg,
129 std::string(LineStart, LineEnd)).Print(0, OS);
130
Chris Lattneraa739d22009-03-13 07:05:43 +0000131}
Chris Lattner2f510ae2009-07-02 22:24:20 +0000132
133//===----------------------------------------------------------------------===//
134// SMDiagnostic Implementation
135//===----------------------------------------------------------------------===//
136
137void SMDiagnostic::Print(const char *ProgName, raw_ostream &S) {
138 if (ProgName && ProgName[0])
139 S << ProgName << ": ";
140
141 if (Filename == "-")
142 S << "<stdin>";
143 else
144 S << Filename;
145
146 if (LineNo != -1) {
147 S << ':' << LineNo;
148 if (ColumnNo != -1)
149 S << ':' << (ColumnNo+1);
150 }
151
152 S << ": " << Message << '\n';
153
154 if (LineNo != -1 && ColumnNo != -1) {
155 S << LineContents << '\n';
156
157 // Print out spaces/tabs before the caret.
158 for (unsigned i = 0; i != unsigned(ColumnNo); ++i)
159 S << (LineContents[i] == '\t' ? '\t' : ' ');
160 S << "^\n";
161 }
162}
163
164