On Thursday, 18 July 2013 at 11:16:26 UTC, JS wrote:
Anyone successful at debugging templates and ctfe?
Also, I am using pragma a lot because it seems to be the most
direct way to get ctfe state information.
I would like to be able to toggle when to show pragma so I can
conditionally enable it during compilation.
e.g., suppose I have a template T that I has pragma debug
messaging and I want to enable it only for a call to T:
___DEBUG___ = true;
T!()
___DEBUG___ = false;
(Not sure if this will produce the correct result depending on
how the compiler analyzes the code)
Right now I have to use an immutable bool for debug which is a
global toggle... I don't see any thing around it because I
can't change an immutable at compile time(or can I?)... which,
technically should be valid because an immutable is a
declaration about the state of a variable at run time, so I
should be able to change it at compile time without changing
that fact.
e.g.,
immutable bool x = true;
x $= false; // $= means static assignment, a compile time
assignment.. not sure if x should be true or false for the
whole program at run time(probably true, site of definition).
Or
ctfe bool x = true; defines a ctfe variable at compile time. x
doesn't exist a run time.
I had another go:
mixin template DbgMsg(alias T)
{
mixin("
mixin template Msg(msgs ...)
{
version(" ~ T.stringof ~ "Dbg)
{
mixin(MsgString!(msgs)());
}
}"
);
}
string MsgString(msgs...)()
{
static if(msgs.length == 0)
{
return "pragma(msg,\"\");";
}
else
{
string s = "pragma(msg, " ~ msgs[0].stringof;
static if(msgs.length > 1)
{
foreach(m; msgs[1 .. $])
{
s ~= ", " ~ m.stringof;
}
}
return s ~ ");";
}
}
unittest
{
struct A
{
mixin DbgMsg!A;
mixin Msg!("hello world from ", typeof(this));
mixin Msg!();
mixin Msg!("one arg");
}
}
When compiled with -version=ADbg you get the messages printed,
otherwise not.
There are probably some improvements to be made, but it works.