jacob-carlborg / dstep

A tool for converting C and Objective-C headers to D modules

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Pointer arithmetic and parentheses elision

jblachly opened this issue · 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