blob: e4d1c40dc6ca4d6435b22725c187f1c6337eae55 [file] [log] [blame]
Chris Lattneraa739d22009-03-13 07:05:43 +00001//===- TGSourceMgr.cpp - Manager for 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 TGSourceMgr class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "TGSourceMgr.h"
15#include "llvm/Support/MemoryBuffer.h"
16#include "llvm/Support/raw_ostream.h"
17using namespace llvm;
18
19TGSourceMgr::~TGSourceMgr() {
20 while (!Buffers.empty()) {
21 delete Buffers.back().Buffer;
22 Buffers.pop_back();
23 }
24}
25
26/// FindBufferContainingLoc - Return the ID of the buffer containing the
27/// specified location, returning -1 if not found.
Chris Lattner1c8ae592009-03-13 16:01:53 +000028int TGSourceMgr::FindBufferContainingLoc(TGLoc Loc) const {
Chris Lattneraa739d22009-03-13 07:05:43 +000029 for (unsigned i = 0, e = Buffers.size(); i != e; ++i)
Chris Lattner1c8ae592009-03-13 16:01:53 +000030 if (Loc.getPointer() >= Buffers[i].Buffer->getBufferStart() &&
Chris Lattnerd4771822009-03-18 20:36:45 +000031 // Use <= here so that a pointer to the null at the end of the buffer
32 // is included as part of the buffer.
33 Loc.getPointer() <= Buffers[i].Buffer->getBufferEnd())
Chris Lattneraa739d22009-03-13 07:05:43 +000034 return i;
35 return -1;
36}
37
38/// FindLineNumber - Find the line number for the specified location in the
39/// specified file. This is not a fast method.
Chris Lattner1c8ae592009-03-13 16:01:53 +000040unsigned TGSourceMgr::FindLineNumber(TGLoc Loc, int BufferID) const {
Chris Lattneraa739d22009-03-13 07:05:43 +000041 if (BufferID == -1) BufferID = FindBufferContainingLoc(Loc);
42 assert(BufferID != -1 && "Invalid Location!");
43
44 MemoryBuffer *Buff = getBufferInfo(BufferID).Buffer;
45
46 // Count the number of \n's between the start of the file and the specified
47 // location.
48 unsigned LineNo = 1;
49
50 const char *Ptr = Buff->getBufferStart();
51
Chris Lattner1c8ae592009-03-13 16:01:53 +000052 for (; TGLoc::getFromPointer(Ptr) != Loc; ++Ptr)
Chris Lattneraa739d22009-03-13 07:05:43 +000053 if (*Ptr == '\n') ++LineNo;
54 return LineNo;
55}
56
Chris Lattner1c8ae592009-03-13 16:01:53 +000057void TGSourceMgr::PrintIncludeStack(TGLoc IncludeLoc) const {
58 if (IncludeLoc == TGLoc()) return; // Top of stack.
Chris Lattneraa739d22009-03-13 07:05:43 +000059
60 int CurBuf = FindBufferContainingLoc(IncludeLoc);
61 assert(CurBuf != -1 && "Invalid or unspecified location!");
62
63 PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc);
64
65 errs() << "Included from "
66 << getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
67 << ":" << FindLineNumber(IncludeLoc, CurBuf) << ":\n";
68}
69
70
Chris Lattner1c8ae592009-03-13 16:01:53 +000071void TGSourceMgr::PrintError(TGLoc ErrorLoc, const std::string &Msg) const {
Chris Lattneraa739d22009-03-13 07:05:43 +000072 raw_ostream &OS = errs();
73
74 // First thing to do: find the current buffer containing the specified
75 // location.
76 int CurBuf = FindBufferContainingLoc(ErrorLoc);
77 assert(CurBuf != -1 && "Invalid or unspecified location!");
78
79 PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc);
80
81 MemoryBuffer *CurMB = getBufferInfo(CurBuf).Buffer;
82
83
84 OS << "Parsing " << CurMB->getBufferIdentifier() << ":"
85 << FindLineNumber(ErrorLoc, CurBuf) << ": ";
86
87 OS << Msg << "\n";
Chris Lattneraa739d22009-03-13 07:05:43 +000088
89 // Scan backward to find the start of the line.
Chris Lattner1c8ae592009-03-13 16:01:53 +000090 const char *LineStart = ErrorLoc.getPointer();
Chris Lattneraa739d22009-03-13 07:05:43 +000091 while (LineStart != CurMB->getBufferStart() &&
92 LineStart[-1] != '\n' && LineStart[-1] != '\r')
93 --LineStart;
94 // Get the end of the line.
Chris Lattner1c8ae592009-03-13 16:01:53 +000095 const char *LineEnd = ErrorLoc.getPointer();
Chris Lattneraa739d22009-03-13 07:05:43 +000096 while (LineEnd != CurMB->getBufferEnd() &&
97 LineEnd[0] != '\n' && LineEnd[0] != '\r')
98 ++LineEnd;
99 // Print out the line.
100 OS << std::string(LineStart, LineEnd) << "\n";
101 // Print out spaces before the carat.
Chris Lattner1c8ae592009-03-13 16:01:53 +0000102 for (const char *Pos = LineStart; Pos != ErrorLoc.getPointer(); ++Pos)
Chris Lattneraa739d22009-03-13 07:05:43 +0000103 OS << (*Pos == '\t' ? '\t' : ' ');
104 OS << "^\n";
105}