digitalmars.D.learn - template debuging
- JS (26/26) Jul 18 2013 Anyone successful at debugging templates and ctfe?
- John Colvin (16/43) Jul 18 2013 If I understand you correctly, this might be useful to you:
- JS (12/67) Jul 18 2013 ? It is still global.
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (9/12) Jul 18 2013 debug can have tags:
- bearophile (4/6) Jul 18 2013 Aren't debug and version = id or version = number enough?
- JS (17/23) Jul 18 2013 No, unless I litter the code with them(if I get what your
- John Colvin (46/73) Jul 18 2013 I had another go:
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.
Jul 18 2013
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.If I understand you correctly, this might be useful to you: void Msg(msgs ...)() { debug { foreach(m; msgs) { pragma(msg, m); } } } which is a no-op if you don't pass -debug on the command line. or alternatively you could wrap your pragma(msg, ...) statments in version(TemplateNameDebug), meaning you can turn them on individually with -version switches
Jul 18 2013
On Thursday, 18 July 2013 at 11:46:39 UTC, John Colvin wrote:On Thursday, 18 July 2013 at 11:16:26 UTC, JS wrote:? It is still global. T!("x"); T!("y"); // I want to see the pragma outputs of only this template T!("z"); I also want "library" level debugging instead of whole program debugging. (although I suppose one could compile each module separately each with it's own -debug). For example immutable bool _MyLibrary_DEBUG_ = true; is a library level debug toggle, not whole program... it won't effect other libraries.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.If I understand you correctly, this might be useful to you: void Msg(msgs ...)() { debug { foreach(m; msgs) { pragma(msg, m); } } } which is a no-op if you don't pass -debug on the command line. or alternatively you could wrap your pragma(msg, ...) statments in version(TemplateNameDebug), meaning you can turn them on individually with -version switches
Jul 18 2013
On 07/18/2013 05:32 AM, JS wrote:immutable bool _MyLibrary_DEBUG_ = true; is a library level debug toggle, not whole program... it won't effect other libraries.debug can have tags: debug (myLibrary) { // ... } debug (myLibrary) writeln("it worked!"); Enabled by the -debug=myLibrary compiler switch. Ali
Jul 18 2013
JS:Right now I have to use an immutable bool for debug which is a global toggle...Aren't debug and version = id or version = number enough? Bye, bearophile
Jul 18 2013
On Thursday, 18 July 2013 at 11:51:57 UTC, bearophile wrote:JS:No, unless I litter the code with them(if I get what your saying). I don't want to whole program pragma output, just local pragma output. I use static if (_MyLibrary_DEBUG) pragma(msg, txt) in my templates for debug output. But I can only set _MyLibrary_DEBUG but not toggle it in code. e.g., immutable _MyLibrary_DEBUG = false; template T(string x) { enum T = x; static if (_MyLibrary_DEBUG) pragma(msg, T); } T!("A"); _MyLibrary_DEBUG = true; T!("B"); _myLibrary_DEBUG = false; T!("C"); Only the pragma output of T!("B") should output.Right now I have to use an immutable bool for debug which is a global toggle...Aren't debug and version = id or version = number enough? Bye, bearophile
Jul 18 2013
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.
Jul 18 2013