1
//===- SourceMgr.h - Manager for Source Buffers & Diagnostics ---*- C++ -*-===//
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 declares the SMDiagnostic and SourceMgr classes. This
11
// provides a simple substrate for diagnostics, #include handling, and other low
12
// level things for simple parsers.
14
//===----------------------------------------------------------------------===//
16
#ifndef SUPPORT_SOURCEMGR_H
17
#define SUPPORT_SOURCEMGR_H
19
#include "llvm/Support/SMLoc.h"
31
/// SourceMgr - This owns the files read by a parser, handles include stacks,
32
/// and handles diagnostic wrangling.
35
/// Buffer - The memory buffer for the file.
38
/// IncludeLoc - This is the location of the parent include, or null if at
43
/// Buffers - This is all of the buffers that we are reading from.
44
std::vector<SrcBuffer> Buffers;
46
// IncludeDirectories - This is the list of directories we should search for
48
std::vector<std::string> IncludeDirectories;
50
/// LineNoCache - This is a cache for line number queries, its implementation
51
/// is really private to SourceMgr.cpp.
52
mutable void *LineNoCache;
54
SourceMgr(const SourceMgr&); // DO NOT IMPLEMENT
55
void operator=(const SourceMgr&); // DO NOT IMPLEMENT
57
SourceMgr() : LineNoCache(0) {}
60
void setIncludeDirs(const std::vector<std::string> &Dirs) {
61
IncludeDirectories = Dirs;
64
const SrcBuffer &getBufferInfo(unsigned i) const {
65
assert(i < Buffers.size() && "Invalid Buffer ID!");
69
const MemoryBuffer *getMemoryBuffer(unsigned i) const {
70
assert(i < Buffers.size() && "Invalid Buffer ID!");
71
return Buffers[i].Buffer;
74
SMLoc getParentIncludeLoc(unsigned i) const {
75
assert(i < Buffers.size() && "Invalid Buffer ID!");
76
return Buffers[i].IncludeLoc;
79
unsigned AddNewSourceBuffer(MemoryBuffer *F, SMLoc IncludeLoc) {
82
NB.IncludeLoc = IncludeLoc;
83
Buffers.push_back(NB);
84
return Buffers.size()-1;
87
/// AddIncludeFile - Search for a file with the specified name in the current
88
/// directory or in one of the IncludeDirs. If no file is found, this returns
89
/// ~0, otherwise it returns the buffer ID of the stacked file.
90
unsigned AddIncludeFile(const std::string &Filename, SMLoc IncludeLoc);
92
/// FindBufferContainingLoc - Return the ID of the buffer containing the
93
/// specified location, returning -1 if not found.
94
int FindBufferContainingLoc(SMLoc Loc) const;
96
/// FindLineNumber - Find the line number for the specified location in the
97
/// specified file. This is not a fast method.
98
unsigned FindLineNumber(SMLoc Loc, int BufferID = -1) const;
100
/// PrintMessage - Emit a message about the specified location with the
101
/// specified string.
103
/// @param Type - If non-null, the kind of message (e.g., "error") which is
104
/// prefixed to the message.
105
/// @param ShowLine - Should the diagnostic show the source line.
106
void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type,
107
bool ShowLine = true) const;
110
/// GetMessage - Return an SMDiagnostic at the specified location with the
111
/// specified string.
113
/// @param Type - If non-null, the kind of message (e.g., "error") which is
114
/// prefixed to the message.
115
/// @param ShowLine - Should the diagnostic show the source line.
116
SMDiagnostic GetMessage(SMLoc Loc,
117
const std::string &Msg, const char *Type,
118
bool ShowLine = true) const;
122
void PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const;
126
/// SMDiagnostic - Instances of this class encapsulate one diagnostic report,
127
/// allowing printing to a raw_ostream as a caret diagnostic.
129
std::string Filename;
130
int LineNo, ColumnNo;
131
std::string Message, LineContents;
132
unsigned ShowLine : 1;
135
SMDiagnostic() : LineNo(0), ColumnNo(0), ShowLine(0) {}
136
SMDiagnostic(const std::string &FN, int Line, int Col,
137
const std::string &Msg, const std::string &LineStr,
138
bool showline = true)
139
: Filename(FN), LineNo(Line), ColumnNo(Col), Message(Msg),
140
LineContents(LineStr), ShowLine(showline) {}
142
void Print(const char *ProgName, raw_ostream &S) const;
145
} // end llvm namespace