LLVM: include/llvm/Support/SourceMgr.h Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15#ifndef LLVM_SUPPORT_SOURCEMGR_H

16#define LLVM_SUPPORT_SOURCEMGR_H

17

23#include

24

25namespace llvm {

26

27namespace vfs {

29}

30

34

35

36

38public:

45

46

47

48

50

51private:

52 struct SrcBuffer {

53

54 std::unique_ptr Buffer;

55

56

57

58

59

60

61

62

63

64

65

66 mutable void *OffsetCache = nullptr;

67

68

69

70 LLVM_ABI unsigned getLineNumber(const char *Ptr) const;

71 template

72 unsigned getLineNumberSpecialized(const char *Ptr) const;

73

74

75

76 LLVM_ABI const char *getPointerForLineNumber(unsigned LineNo) const;

77 template

78 const char *getPointerForLineNumberSpecialized(unsigned LineNo) const;

79

80

82

83 SrcBuffer() = default;

84 LLVM_ABI SrcBuffer(SrcBuffer &&);

85 SrcBuffer(const SrcBuffer &) = delete;

86 SrcBuffer &operator=(const SrcBuffer &) = delete;

88 };

89

90

91 std::vector Buffers;

92

93

94 std::vectorstd::string IncludeDirectories;

95

97 void *DiagContext = nullptr;

98

99

101

102 bool isValidBufferID(unsigned i) const { return i && i <= Buffers.size(); }

103

104public:

105

107

108

115

118

119

121

123 IncludeDirectories = Dirs;

124 }

125

126

127

129 DiagHandler = DH;

130 DiagContext = Ctx;

131 }

132

135

137 assert(isValidBufferID(i));

138 return Buffers[i - 1];

139 }

140

142 assert(isValidBufferID(i));

143 return Buffers[i - 1].Buffer.get();

144 }

145

147

152

154 assert(isValidBufferID(i));

155 return Buffers[i - 1].IncludeLoc;

156 }

157

158

159

161 SMLoc IncludeLoc) {

162 SrcBuffer NB;

163 NB.Buffer = std::move(F);

164 NB.IncludeLoc = IncludeLoc;

165 Buffers.push_back(std::move(NB));

166 return Buffers.size();

167 }

168

169

170

171

172

174 SMLoc MainBufferIncludeLoc = SMLoc()) {

175 if (SrcMgr.Buffers.empty())

176 return;

177

179 std::move(SrcMgr.Buffers.begin(), SrcMgr.Buffers.end(),

180 std::back_inserter(Buffers));

181 SrcMgr.Buffers.clear();

182 Buffers[OldNumBuffers].IncludeLoc = MainBufferIncludeLoc;

183 }

184

185

186

187

188

189

190

192 SMLoc IncludeLoc, std::string &IncludedFile);

193

194

195

196

197

198

199

200

201

203 OpenIncludeFile(const std::string &Filename, std::string &IncludedFile);

204

205

206

207

209

210

211

215

216

217

218 LLVM_ABI std::pair<unsigned, unsigned>

220

221

222

225

226

227

229 unsigned ColNo);

230

231

232

233

234

238 bool ShowColors = true) const;

239

240

244 bool ShowColors = true) const;

245

246

247

248

249

251 bool ShowColors = true) const;

252

253

254

255

256

257

261

262

263

264

265

266

267

269};

270

271

274

275 std::string Text;

276

277public:

279

282

285

287 if (Range.Start.getPointer() != Other.Range.Start.getPointer())

288 return Range.Start.getPointer() < Other.Range.Start.getPointer();

289 if (Range.End.getPointer() != Other.Range.End.getPointer())

290 return Range.End.getPointer() < Other.Range.End.getPointer();

291 return Text < Other.Text;

292 }

293};

294

295

296

300 std::string Filename;

301 int LineNo = 0;

302 int ColumnNo = 0;

304 std::string Message, LineContents;

305 std::vector<std::pair<unsigned, unsigned>> Ranges;

307

308public:

309

311

313 : Filename(filename), LineNo(-1), ColumnNo(-1), Kind(Knd), Message(Msg) {}

314

315

319 ArrayRef<std::pair<unsigned, unsigned>> Ranges,

321

331

333

335

337 bool ShowColors = true, bool ShowKindLabel = true,

338 bool ShowLocation = true) const;

339};

340

341}

342

343#endif

assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")

This file defines the RefCountedBase, ThreadSafeRefCountedBase, and IntrusiveRefCntPtr classes.

This file defines the SmallVector class.

static void DiagHandler(const SMDiagnostic &Diag, void *Context)

ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...

Represents either an error or a value T.

A smart pointer to a reference-counted object that inherits from RefCountedBase or ThreadSafeRefCount...

This interface provides simple read-only access to a block of memory, and provides simple methods for...

Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...

Definition SourceMgr.h:297

LLVM_ABI void print(const char *ProgName, raw_ostream &S, bool ShowColors=true, bool ShowKindLabel=true, bool ShowLocation=true) const

SourceMgr::DiagKind getKind() const

Definition SourceMgr.h:327

StringRef getFilename() const

Definition SourceMgr.h:324

int getLineNo() const

Definition SourceMgr.h:325

SMDiagnostic(StringRef filename, SourceMgr::DiagKind Knd, StringRef Msg)

Definition SourceMgr.h:312

StringRef getLineContents() const

Definition SourceMgr.h:329

SMLoc getLoc() const

Definition SourceMgr.h:323

StringRef getMessage() const

Definition SourceMgr.h:328

ArrayRef< SMFixIt > getFixIts() const

Definition SourceMgr.h:334

ArrayRef< std::pair< unsigned, unsigned > > getRanges() const

Definition SourceMgr.h:330

void addFixIt(const SMFixIt &Hint)

Definition SourceMgr.h:332

const SourceMgr * getSourceMgr() const

Definition SourceMgr.h:322

int getColumnNo() const

Definition SourceMgr.h:326

Represents a single fixit, a replacement of one range of text with another.

Definition SourceMgr.h:272

bool operator<(const SMFixIt &Other) const

Definition SourceMgr.h:286

StringRef getText() const

Definition SourceMgr.h:283

LLVM_ABI SMFixIt(SMRange R, const Twine &Replacement)

SMFixIt(SMLoc Loc, const Twine &Replacement)

Definition SourceMgr.h:280

SMRange getRange() const

Definition SourceMgr.h:284

Represents a location in source code.

Represents a range in source code.

This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.

This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.

Definition SourceMgr.h:37

LLVM_ABI ErrorOr< std::unique_ptr< MemoryBuffer > > OpenIncludeFile(const std::string &Filename, std::string &IncludedFile)

Search for a file with the specified name in the current directory or in one of the IncludeDirs,...

void * getDiagContext() const

Definition SourceMgr.h:134

ArrayRef< std::string > getIncludeDirs() const

Return the include directories of this source manager.

Definition SourceMgr.h:120

unsigned getMainFileID() const

Definition SourceMgr.h:148

DiagHandlerTy getDiagHandler() const

Definition SourceMgr.h:133

DiagKind

Definition SourceMgr.h:39

@ DK_Warning

Definition SourceMgr.h:41

@ DK_Note

Definition SourceMgr.h:43

@ DK_Error

Definition SourceMgr.h:40

@ DK_Remark

Definition SourceMgr.h:42

SourceMgr & operator=(const SourceMgr &)=delete

void setIncludeDirs(const std::vector< std::string > &Dirs)

Definition SourceMgr.h:122

LLVM_ABI std::pair< unsigned, unsigned > getLineAndColumn(SMLoc Loc, unsigned BufferID=0) const

Find the line and column number for the specified location in the specified file.

LLVM_ABI void setVirtualFileSystem(IntrusiveRefCntPtr< vfs::FileSystem > FS)

const MemoryBuffer * getMemoryBuffer(unsigned i) const

Definition SourceMgr.h:141

unsigned getNumBuffers() const

Definition SourceMgr.h:146

LLVM_ABI void PrintMessage(raw_ostream &OS, SMLoc Loc, DiagKind Kind, const Twine &Msg, ArrayRef< SMRange > Ranges={}, ArrayRef< SMFixIt > FixIts={}, bool ShowColors=true) const

Emit a message about the specified location with the specified string.

SMLoc getParentIncludeLoc(unsigned i) const

Definition SourceMgr.h:153

LLVM_ABI void PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const

Prints the names of included files and the line of the file they were included from.

LLVM_ABI SourceMgr()

Create new source manager without support for include files.

LLVM_ABI unsigned FindBufferContainingLoc(SMLoc Loc) const

Return the ID of the buffer containing the specified location.

IntrusiveRefCntPtr< vfs::FileSystem > getVirtualFileSystem() const

SourceMgr & operator=(SourceMgr &&)

LLVM_ABI SMDiagnostic GetMessage(SMLoc Loc, DiagKind Kind, const Twine &Msg, ArrayRef< SMRange > Ranges={}, ArrayRef< SMFixIt > FixIts={}) const

Return an SMDiagnostic at the specified location with the specified string.

void(*)(const SMDiagnostic &, void *Context) DiagHandlerTy

Clients that want to handle their own diagnostics in a custom way can register a function pointer+con...

Definition SourceMgr.h:49

void setDiagHandler(DiagHandlerTy DH, void *Ctx=nullptr)

Specify a diagnostic handler to be invoked every time PrintMessage is called.

Definition SourceMgr.h:128

LLVM_ABI unsigned AddIncludeFile(const std::string &Filename, SMLoc IncludeLoc, std::string &IncludedFile)

Search for a file with the specified name in the current directory or in one of the IncludeDirs.

SourceMgr(const SourceMgr &)=delete

unsigned FindLineNumber(SMLoc Loc, unsigned BufferID=0) const

Find the line number for the specified location in the specified file.

Definition SourceMgr.h:212

LLVM_ABI std::string getFormattedLocationNoOffset(SMLoc Loc, bool IncludePath=false) const

Get a string with the SMLoc filename and line number formatted in the standard style.

void takeSourceBuffersFrom(SourceMgr &SrcMgr, SMLoc MainBufferIncludeLoc=SMLoc())

Takes the source buffers from the given source manager and append them to the current manager.

Definition SourceMgr.h:173

unsigned AddNewSourceBuffer(std::unique_ptr< MemoryBuffer > F, SMLoc IncludeLoc)

Add a new source buffer to this source manager.

Definition SourceMgr.h:160

LLVM_ABI SMLoc FindLocForLineAndColumn(unsigned BufferID, unsigned LineNo, unsigned ColNo)

Given a line and column number in a mapped buffer, turn it into an SMLoc.

const SrcBuffer & getBufferInfo(unsigned i) const

Definition SourceMgr.h:136

StringRef - Represent a constant reference to a string, i.e.

Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...

This class implements an extremely fast bulk output stream that can only output to a stream.

The virtual file system interface.

This is an optimization pass for GlobalISel generic memory operations.

ArrayRef(const T &OneElt) -> ArrayRef< T >