blob: 40a6f43f5e0f33c6f70500de541a8b440fc17a89 [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
28/// FindBufferContainingLoc - Return the ID of the buffer containing the
29/// specified location, returning -1 if not found.
Chris Lattner8070ea32009-06-21 03:41:50 +000030int SourceMgr::FindBufferContainingLoc(SMLoc Loc) const {
Chris Lattneraa739d22009-03-13 07:05:43 +000031 for (unsigned i = 0, e = Buffers.size(); i != e; ++i)
Chris Lattner1c8ae592009-03-13 16:01:53 +000032 if (Loc.getPointer() >= Buffers[i].Buffer->getBufferStart() &&
Chris Lattnerd4771822009-03-18 20:36:45 +000033 // Use <= here so that a pointer to the null at the end of the buffer
34 // is included as part of the buffer.
35 Loc.getPointer() <= Buffers[i].Buffer->getBufferEnd())
Chris Lattneraa739d22009-03-13 07:05:43 +000036 return i;
37 return -1;
38}
39
40/// FindLineNumber - Find the line number for the specified location in the
41/// specified file. This is not a fast method.
Chris Lattner8070ea32009-06-21 03:41:50 +000042unsigned SourceMgr::FindLineNumber(SMLoc Loc, int BufferID) const {
Chris Lattneraa739d22009-03-13 07:05:43 +000043 if (BufferID == -1) BufferID = FindBufferContainingLoc(Loc);
44 assert(BufferID != -1 && "Invalid Location!");
45
46 MemoryBuffer *Buff = getBufferInfo(BufferID).Buffer;
47
48 // Count the number of \n's between the start of the file and the specified
49 // location.
50 unsigned LineNo = 1;
51
52 const char *Ptr = Buff->getBufferStart();
53
Chris Lattner1e3a8a42009-06-21 03:39:35 +000054 for (; SMLoc::getFromPointer(Ptr) != Loc; ++Ptr)
Chris Lattneraa739d22009-03-13 07:05:43 +000055 if (*Ptr == '\n') ++LineNo;
56 return LineNo;
57}
58
Chris Lattner8070ea32009-06-21 03:41:50 +000059void SourceMgr::PrintIncludeStack(SMLoc IncludeLoc) const {
Chris Lattner1e3a8a42009-06-21 03:39:35 +000060 if (IncludeLoc == SMLoc()) return; // Top of stack.
Chris Lattneraa739d22009-03-13 07:05:43 +000061
62 int CurBuf = FindBufferContainingLoc(IncludeLoc);
63 assert(CurBuf != -1 && "Invalid or unspecified location!");
64
65 PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc);
66
67 errs() << "Included from "
68 << getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
69 << ":" << FindLineNumber(IncludeLoc, CurBuf) << ":\n";
70}
71
72
Chris Lattner8070ea32009-06-21 03:41:50 +000073void SourceMgr::PrintError(SMLoc ErrorLoc, const std::string &Msg) const {
Chris Lattneraa739d22009-03-13 07:05:43 +000074 raw_ostream &OS = errs();
75
76 // First thing to do: find the current buffer containing the specified
77 // location.
78 int CurBuf = FindBufferContainingLoc(ErrorLoc);
79 assert(CurBuf != -1 && "Invalid or unspecified location!");
80
81 PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc);
82
83 MemoryBuffer *CurMB = getBufferInfo(CurBuf).Buffer;
84
85
86 OS << "Parsing " << CurMB->getBufferIdentifier() << ":"
87 << FindLineNumber(ErrorLoc, CurBuf) << ": ";
88
89 OS << Msg << "\n";
Chris Lattneraa739d22009-03-13 07:05:43 +000090
91 // Scan backward to find the start of the line.
Chris Lattner1c8ae592009-03-13 16:01:53 +000092 const char *LineStart = ErrorLoc.getPointer();
Chris Lattneraa739d22009-03-13 07:05:43 +000093 while (LineStart != CurMB->getBufferStart() &&
94 LineStart[-1] != '\n' && LineStart[-1] != '\r')
95 --LineStart;
96 // Get the end of the line.
Chris Lattner1c8ae592009-03-13 16:01:53 +000097 const char *LineEnd = ErrorLoc.getPointer();
Chris Lattneraa739d22009-03-13 07:05:43 +000098 while (LineEnd != CurMB->getBufferEnd() &&
99 LineEnd[0] != '\n' && LineEnd[0] != '\r')
100 ++LineEnd;
101 // Print out the line.
102 OS << std::string(LineStart, LineEnd) << "\n";
Jim Grosbachda4231f2009-03-26 16:17:51 +0000103 // Print out spaces before the caret.
Chris Lattner1c8ae592009-03-13 16:01:53 +0000104 for (const char *Pos = LineStart; Pos != ErrorLoc.getPointer(); ++Pos)
Chris Lattneraa739d22009-03-13 07:05:43 +0000105 OS << (*Pos == '\t' ? '\t' : ' ');
106 OS << "^\n";
107}