This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

compiler fails to detect error when comparing pointer to enum to enum value

Using CCS version 4.0.1.01001, which uses cl2000 version 5.2.1, a major error is not detected.

In the following code sequence, on the second line of the function, the compiler should detect that a pointer to enum is being compared with an enum value:

typedef enum {
    NVMTYPES_DRIVER_UNEXECUTED,
    NVMTYPES_DRIVER_IN_PROGRESS,
    NVMTYPES_DRIVER_COMPLETE
} NvmTypes_TypeDriverStatus;

void EepromDrvr_GetWriteStatus(NvmTypes_TypeDriverStatus* EepromDrvr_OpStatus, int *ptr) {
    int val = 1;
    if (EepromDrvr_OpStatus != NVMTYPES_DRIVER_UNEXECUTED) {
        return;
    }
    if (ptr == val) {
        return;
    }
}

The instruction should dereference the pointer to get its value.

Please correct the compiler to detect and report this error. Note that g++ does detect this.

 

  • When I compile that code, this is what I see ...

    % cl2000 try1.c
    "try1.c", line 12: error: operand types are incompatible ("int *" and "int")
    1 error detected in the compilation of "try1.c".
    
    >> Compilation failure
    
    

    What do you see?

    Thanks and regards,

    -George

  • clinthelton said:

    Using CCS version 4.0.1.01001, which uses cl2000 version 5.2.1, a major error is not detected.

    In the following code sequence, on the second line of the function, the compiler should detect that a pointer to enum is being compared with an enum value:

    typedef enum {
        NVMTYPES_DRIVER_UNEXECUTED,
        NVMTYPES_DRIVER_IN_PROGRESS,
        NVMTYPES_DRIVER_COMPLETE
    } NvmTypes_TypeDriverStatus;

    void EepromDrvr_GetWriteStatus(NvmTypes_TypeDriverStatus* EepromDrvr_OpStatus, int *ptr) {
        int val = 1;
        if (EepromDrvr_OpStatus != NVMTYPES_DRIVER_UNEXECUTED) {
            return;
        }
        if (ptr == val) {
            return;
        }
    }

     

    When I compile that code, this is what I see ...

    % cl2000 try1.c
    "try1.c", line 12: error: operand types are incompatible ("int *" and "int")
    1 error detected in the compilation of "try1.c".
    
    >> Compilation failure
    
    

    What do you see?

    Thanks and regards,

    -George

    Just an observation but isn't that error referring to the line:

        if (ptr == val) {

    which is comparing a pointer to a value?

    Tim

  • Tim King said:

    Just an observation but isn't that error referring to the line:

        if (ptr == val) {

    which is comparing a pointer to a value?

    Oops.  Sorry about that.  I focused on the wrong thing.  You are wondering why this line

    Tim King said:
    if (EepromDrvr_OpStatus != NVMTYPES_DRIVER_UNEXECUTED) {

    does not get an error diagnostic.  It turns out that line is equivalent to something like this ...

    if (ptr_to_enum != ENUM_VALUE_OF_ZERO)

    In C, it is OK to compare a pointer to 0, even if the value 0 comes from an enumeration type.  C++ is more paranoid about such things, and complains.  The g++ compiler complains about this because it is a C++ compiler.  If you run cl2000 in C++ mode, it complains too.  To do that, either change the file extension to .cpp, or add the build option --cpp_default.  Another change that results in an error is to compare against some enum value other than NVMTYPES_DRIVER_UNEXECUTED.

    Thanks and regards,

    -George