Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't GCC smart enough to understand a missing return statement?

Tags:

c

llvm

clang

I have this C code:

EaglePage* EaglePage_RealCopy(EaglePage *page)
{
    if(NULL == page) {
        return NULL;
    }

    switch(page->type) {

        case EagleDataTypeUnknown:
            EagleLogger_Log(EagleLoggerSeverityError, "Cannot page of Unknown type.");
            return NULL;

        case EagleDataTypeInteger:
            return EaglePage_RealCopyInt_(page);

        case EagleDataTypeVarchar:
            return EaglePage_RealCopyVarchar_(page);

        case EagleDataTypeFloat:
            return EaglePage_RealCopyFloat_(page);

    }
}

When I compile this on Mac OS X using clang it understands that the switch handles all 'possible' branches and does not warn about a missing return statement (like Java). However if I run the same code through GCC 4.4.5 it always gives a missing return warning.

It's annoying because if I put in return statements then my code coverage breaks because I don't cover those 'impossible' scenarios. Is there a way for GCC to handle this like clang/javac?

like image 865
Elliot Chance Avatar asked Jun 18 '26 09:06

Elliot Chance


1 Answers

This sometimes happens, but it is easy to fix with a default label.

switch(page->type) {

    case EagleDataTypeInteger:
        return EaglePage_RealCopyInt_(page);

    case EagleDataTypeVarchar:
        return EaglePage_RealCopyVarchar_(page);

    case EagleDataTypeFloat:
        return EaglePage_RealCopyFloat_(page);

    case EagleDataTypeUnknown:
    default:
        EagleLogger_Log(EagleLoggerSeverityError, "Cannot page of Unknown type.");
        return NULL;

}
like image 179
jxh Avatar answered Jun 20 '26 23:06

jxh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!