www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 1425] New: Feature Request: call stack reflection

reply d-bugmail puremagic.com writes:

           Summary: Feature Request: call stack reflection
           Product: D
           Version: unspecified
          Platform: All
               URL: http://www.digitalmars.com/webnews/newsgroups.php?art_gr
        OS/Version: All
            Status: NEW
          Severity: blocker
          Priority: P2
         Component: Phobos
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: tortoise_74 yahoo.co.uk

For improved support for assertions, logging code and stack traces it would be
useful to have means of identifying the calling frame of reference. This would
also have the advantage of rendering __LINE__ and its ilk redundant.
For example it is not currently possible to implement a cppunit like
assertEqual function.
The best I can achieve is:

void assertEqual(Type)(Type actual_, Type expected_, 
                       char[] file_, 
                       uint line_) {
   if (actual_ != expected_) {
      writefln("Equality assertion failed");
      writefln("actual:   '", actual_, "'");
      writefln("expected: '", expected_, "'");

Which has to be used as:


It should be possible to achieve a more natural syntax like:


Several proposals seem have been made on the newsgroups but none has been
raised as a request here.
I personally think the best approach is to have a library function

stackFrame[] callStack = std.trace();

By default the calling frmae would be something like:

class stackFrame {
   stackFrame* parent;
   object[]    arguments;

When functions request source identification the compiler would have to
include mapping information so its not a pure library issue.

class SourceStackFrame: public StackFrame {
// inherited
//   stackFrame* parent;
//   object[]    arguments;
   uint        parentLine;
   string*     parentFile;
   string*     parentFunction;   

I imagine two ways of creating such a structure.
Use of an analogue to __FILE__ e.g. the so called "context" keyword
would tell the compiler to use the special stack frame for a specific call
A command line switch could tell the compiler to always use the source stack
frame format - with resulting code bloat.

Aug 16 2007
next sibling parent d-bugmail puremagic.com writes:

tortoise_74 yahoo.co.uk changed:

           What    |Removed                     |Added
           Severity|blocker                     |enhancement

------- Comment #1 from tortoise_74 yahoo.co.uk  2007-08-24 19:35 -------
For the case of using __FILE__ & __LINE__ there is a workaround as follows:

void assertEqual (T) (T actual, T expected, char[] file, uint line) {
   if (actual != expected) {
     writeln("Equality assertion failed.");
     writefln("  actual:   '%s'", actual);
     writefln("  expected: '%s'", expected);
     _d_assert(file, line); //or other assert wrapper

template MAssertEqual (alias actual, alias expected) {
   const AssertEqual =
     `assertEqual!(` ~typeof(actual).stringof  ~`)(`
     `, `
     `, cast(char[])(__FILE__), __LINE__);`

unittest {
   int var5 = 1;
   int var6 = 2;

  mixin(MAssertEqual!(var5, var6)); // -> assertEqual(var5, var6, __FILE__,

Aug 24 2007
prev sibling parent d-bugmail puremagic.com writes:

nfxjfg gmail.com changed:

           What    |Removed                     |Added
                 CC|                            |nfxjfg gmail.com

--- Comment #2 from nfxjfg gmail.com 2010-03-29 16:29:47 PDT ---
Tango has this.

It doesn't include the "object[] arguments" thing, though.

Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 29 2010