@@ -64,7 +64,7 @@ nothrow:
6464 extern (C++ ) static Loc singleFilename(const char * filename)
6565 {
6666 Loc result;
67- locFileTable ~= new BaseLoc(filename.toDString, locIndex, 0 , [0 ]);
67+ locFileTable ~= new BaseLoc(filename.toDString, null , locIndex, 0 , [0 ]);
6868 result.index = locIndex++ ;
6969 return result;
7070 }
@@ -235,16 +235,20 @@ struct SourceLoc
235235 uint column; // / column number (starts at 1)
236236 uint fileOffset; // / byte index into file
237237
238+ // / Index `fileOffset` into this to to obtain source code context of this location
239+ const (char )[] fileContent;
240+
238241 // aliases for backwards compatibility
239242 alias linnum = line;
240243 alias charnum = column;
241244
242- this (const (char )[] filename, uint line, uint column, uint fileOffset = 0 ) nothrow @nogc pure @safe
245+ this (const (char )[] filename, uint line, uint column, uint fileOffset = 0 , const ( char )[] fileContent = null ) nothrow @nogc pure @safe
243246 {
244247 this .filename = filename;
245248 this .line = line;
246249 this .column = column;
247250 this .fileOffset = fileOffset;
251+ this .fileContent = fileContent;
248252 }
249253
250254 this (Loc loc) nothrow @nogc @trusted
@@ -300,15 +304,15 @@ private size_t fileTableIndex(uint index) nothrow @nogc
300304 * Create a new source location map for a file
301305 * Params:
302306 * filename = source file name
303- * size = space to reserve for locations, equal to the file size in bytes
307+ * fileContent = content of source file
304308 * Returns: new BaseLoc
305309 */
306- BaseLoc* newBaseLoc (const (char )* filename, size_t size ) nothrow
310+ BaseLoc* newBaseLoc (const (char )* filename, const ( char )[] fileContent ) nothrow
307311{
308- locFileTable ~= new BaseLoc(filename.toDString, locIndex, 1 , [0 ]);
312+ locFileTable ~= new BaseLoc(filename.toDString, fileContent, locIndex, 1 , [0 ]);
309313 // Careful: the endloc of a FuncDeclaration can
310314 // point to 1 past the very last byte in the file, so account for that
311- locIndex += size + 1 ;
315+ locIndex += fileContent.length + 1 ;
312316 return locFileTable[$ - 1 ];
313317}
314318
@@ -354,6 +358,7 @@ struct BaseLoc
354358@safe nothrow :
355359
356360 const (char )[] filename; // / Source file name
361+ const (char )[] fileContents; // / Source file contents
357362 uint startIndex; // / Subtract this from Loc.index to get file offset
358363 int startLine = 1 ; // / Line number at index 0
359364 uint [] lines; // / For each line, the file offset at which it starts. At index 0 there's always a 0 entry.
@@ -384,11 +389,11 @@ struct BaseLoc
384389 {
385390 auto fname = filename.toDString;
386391 if (substitutions.length == 0 )
387- substitutions ~= BaseLoc(this .filename, 0 , 0 );
392+ substitutions ~= BaseLoc(this .filename, null , 0 , 0 );
388393
389394 if (fname.length == 0 )
390395 fname = substitutions[$ - 1 ].filename;
391- substitutions ~= BaseLoc(fname, offset, cast (int ) (line - lines.length + startLine - 2 ));
396+ substitutions ~= BaseLoc(fname, null , offset, cast (int ) (line - lines.length + startLine - 2 ));
392397 }
393398
394399 // / Returns: `loc` modified by substitutions from #file / #line directives
@@ -408,7 +413,7 @@ struct BaseLoc
408413 private SourceLoc getSourceLoc (uint offset) @nogc
409414 {
410415 const i = getLineIndex(offset);
411- const sl = SourceLoc(filename, cast (int ) (i + startLine), cast (int ) (1 + offset - lines[i]), offset);
416+ const sl = SourceLoc(filename, cast (int ) (i + startLine), cast (int ) (1 + offset - lines[i]), offset, fileContents );
412417 return substitute (sl);
413418 }
414419
0 commit comments