__FILE__ and __LINE__ in case of import expression

Steven Schveighoffer schveiguy at gmail.com
Sun Aug 23 14:42:19 UTC 2020


On 8/23/20 8:42 AM, Andrey Zherikov wrote:
> On Saturday, 22 August 2020 at 03:43:10 UTC, Steven Schveighoffer wrote:
>> On 8/21/20 6:34 PM, Adam D. Ruppe wrote:
>>> On Friday, 21 August 2020 at 22:12:48 UTC, Steven Schveighoffer wrote:
>>>> And honestly, if it says the source is "mixin-50, line 1", I think 
>>>> people will get it.
>>>
>>> I could probably live with that too, but the status quo is pretty 
>>> useful as-is.
>>
>> I wonder if the compiler could detect when you are using a string 
>> literal vs. a generated or imported string, and change the behavior 
>> accordingly.
> 
> As far as I understand behavior of this is that mixin() changes __FILE__ 
> to point to the location of this mixin. But import expression doesn't do 
> so. So if import("file") could change __FILE__ to "file" and __LINE__ to 
> 1 internally that will make sense IMHO.
> I mean something like this:
>                  //__FILE__='test', __LINE__=1
> mixin(          //__FILE__='test-mixin-2', __LINE__=2
>                  //__FILE__='test-mixin-2', __LINE__=3
> import("file")  //__FILE__='file', __LINE__=1           - only inside 
> import()  !!!
>                  //__FILE__='test-mixin-2', __LINE__=5
> )
>                  //__FILE__='test', __LINE__=7
> 

import("file") returns a string, the source of the string is no longer 
available for the compiler after it's done importing it as a string.

However, you can easily use the #line directive to automate this...

string getImport(string file)()
{
    return "#line 1 " ~ file ~ "\n" ~ import(file);
}

mixin(getImport!"file");

-Steve


More information about the Digitalmars-d-learn mailing list