View source code
Display the source code in dmd/location.d from which thispage was generated on github.
Report a bug
If you spot a problem with this page, click here to create aBugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.Requires a signed-in GitHub account. This works well for small changes.If you'd like to make larger changes you may want to consider usinglocal clone.

Struct dmd.location.BaseLoc

Mapping from byte offset into source file to line/column numbers

struct BaseLoc;

Consider this 4-line 24 byte source file:

app.d
1 struct S
2 {
3     int y;
4 }

Loc(0) is reserved for null locations, so the first BaseLoc gets startIndex = 1 and reserves 25 possible positions. Loc(1) represents the very start of this source file, and every next byte gets the next Loc, up to Loc(25) which represents the location right past the very last } character (hence it's 1 more than the file size of 24, classic fence post problem!).

The next source file will get Loc(26) .. Loc(26 + fileSize + 1) etc.

Now say we know that int y has a Loc(20) and we want to know the line and column number.

First we find the corresponding BaseLoc in locFileTable. Since 20 < 26, the first BaseLoc contains this location. Since startIndex = 1, we subtract that to get a file offset 19.

To get the line number from the file offset, we binary search into the lines array, which contains file offsets where each line starts:

locFileTable[0].lines == [0, 9, 11, 22, 24]

We see 14 would be inserted right after 11 at lines[2], so it's line 3 (+1 for 1-indexing). Since line 3 starts at file offset 11, and 14 - 11 = 3, it's column 4 (again, accounting for 1-indexing)

#line and #file directives are handled with a separate array substitutions because they're rare, and we don't want to penalize memory usage in their absence.

Fields

NameTypeDescription
filename const(char)[]Source file name
lines uint[]For each line, the file offset at which it starts. At index 0 there's always a 0 entry.
startIndex uintSubtract this from Loc.index to get file offset
startLine intLine number at index 0
substitutions BaseLoc[]Substitutions from #line / #file directives

Methods

NameDescription
addSubstitution (offset, filename, line) Register a new file/line mapping from #file and #line directives
getLoc (offset) Construct a Loc entry for the start of the source file + offset bytes
newLine (offset) Register that a new line starts at offset bytes from the start of the source file
substitute (loc)

Authors

Walter Bright

License

Boost License 1.0