Feature discussion: __traits(getSource, function)
Nick Sabalausky
a at a.a
Sat Oct 16 15:03:36 PDT 2010
"Adam D. Ruppe" <destructionator at gmail.com> wrote in message
news:i9cud5$1um2$1 at digitalmars.com...
> Well, it sounds like I'm not the only one who'd find this useful, so I
> took a look
> at the compiler.
>
> It seems to only store file and line internally (I might have missed
> something
> though) so the path of least resistance seems to be exposing that, and
> then
> pulling the source with a library function.
>
> But I'll keep looking, since avoiding the library function would be nice
> (no -J
> and no bugs!).
I've been dealing with ddmd's source a bit lately. Unless it's different in
dmd, the entirety of the source file should be available here:
// Relevent snippets from ddmd, probably similar in dmd:
class Module // in Module.d
{
File srcfile;
}
class File // in File.d
{
ubyte* buffer; // data for our file
uint len; // amount of data in buffer[]
}
So if you have a "Module myModule", then you can get the full original
source code of the module with (D-ified):
myModule.srcfile.buffer[0..myModule.srcfile.len];
You're right that aside from the Module, dmd only keeps track of filename
and line number (via the "Loc" struct, which is a member of most classes in
the AST). You should be able to add members to Loc for "starting index in
source file" and "ending index", and then just use those to index into
Module.srcfile.buffer. (Although at that point, it's not really a "Line Of
Code", more like "Location Of Code"...and for all I know there might be
places that are reliant on it actually having the semantic meaning of "line
of code", expecialy since Loc appears to have an "equals(Loc)" member).
I'd probably start by adding those members to Loc's ctors, searching all the
files for all Loc instantiations, and updating them as relevent. You'll
probably also have to find wherever dmd keeps track of the current line
number (probably in the lexer or parser somewere, but I've never looked into
those, so I don't even know where they are), and keep track of the current
srcIndex, etc. along with it.
More information about the Digitalmars-d
mailing list