jacob-carlborg/dstep

Pointer arithmetic and parentheses elision

jblachly opened this issue · 0 comments

This was caught by our unit tests. Parentheses elision in #define macro to template conversion can lead to incorrect results when dealing with pointers.

This line:

#define bam_get_cigar(b) ((uint32_t*)((b)->data + (b)->core.l_qname))

Was translated by dstep to:

extern (D) auto bam_get_cigar(T)(auto ref T b)
{
    return cast(uint*) b.data + b.core.l_qname;
}

Pointer arithmetic rules cause this to give wrong result. Our original hand translation preserves parentheses, giving correct results:

extern (D) auto bam_get_cigar(bam1_t * b)
{
    return cast(uint*) ((*b).data + (*b).core.l_qname);
}

(b->data here is a ubyte *)

noted by @charlesgregory