Compiler optimization? what's going on?

02 Jan 2015

The following code fragment reports 2 warnings - both dx and dy are reported {Variable 'dy' was set but never used "loc_t dy = 0;"}. Before I added the "= 0;" to these the warnings were reporting that the values might be used before initialized, but now if I remove the "= 0" is remains "set but never used". Quite confusing to me. I also changed from my typedefs (loc_t) back to int and this did not affect the warnings. And point_t is another typedef of a pair of loc_t.

The code runs correctly, but I always aim for a zero-warnings.

void ThickLine(point_t origin, point_t p)
{
    double angleN = 0;
    loc_t dy = 0;
    loc_t dx = 0;
    int thickness = 0;
    point_t s = { 0, 0 };
    point_t e = { 0, 0 };

    lcd.line(origin,p, rgb);
    INFO("   End @ (%3d,%3d) - (%3d,%3d) [%d]", origin.x, origin.y, p.x, p.y, pensize);
    #define PI 3.14159
    dy = p.y - origin.y;
    dx = p.x - origin.x;
    INFO("delta (%+3d,%+3d)", dx,dy);
    angleN = atan2((double)(p.y-origin.y), (double)(p.x-origin.x));
    thickness = pensize;
    for (int l=-thickness; l<=thickness; l++) {
        s.x = origin.x + l * cos(angleN+PI/2);
        s.y = origin.y + l * sin(angleN+PI/2);
        e.x = p.x      + l * cos(angleN+PI/2);
        e.y = p.y      + l * sin(angleN+PI/2);
        lcd.line(s, e, rgb);
        INFO("     %+d @ (%3d,%3d) - (%3d,%3d) a:%+3.2f:%+3.2f",
                  l, s.x,s.y, e.x,e.y, angleN, angleN+PI/2);
    }
}

Any suggestions?

02 Jan 2015

dx and dy are given values and then used only in what seems to be a macro #define named INFO which is probably for debugging. Check the macro. It may be disabled currently and not use dx and dy at all. That would explain the warnings.

02 Jan 2015

As you are calculating dx and dy, you might as well use them here:

angleN = atan2((double)(dy), (double)(dx));
03 Jan 2015

Thanks Wim and Chris. Some form of code-blindness apparently had set in. INFO was indeed a disabled macro, and placing dy,dx into the atan2 was something I apparently thought I already did - no matter how many times I looked directly at it I couldn't see it... One of those forehead slapping moments. This also supports the other warning when they were not initialized.