Apple's clang compiler has problems discerning type safeness from runtime safeness
Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn) will compile this
#include <stdlib.h>
typedef enum
{
MulleKVCValueForKeyIndex = 0,
MulleKVCTakeValueForKeyIndex = 1,
MulleKVCStoredValueForKeyIndex = 2,
MulleKVCTakeStoredValueForKeyIndex = 3,
} MulleKVCMethodType;
#define MulleKVCNumberOfMethodIndexes (MulleKVCTakeStoredValueForKeyIndex + 1)
void foo( MulleKVCMethodType type)
{
if( type < 0 || type >= MulleKVCNumberOfMethodIndexes)
abort();
}
int main(int argc, const char * argv[])
{
foo( atoi( argv[ 1]));
return 0;
}
to produce the following warnings:
/Volumes/Source/srcM/Tests/compiler-doesnt-understand-runtime/compiler-doesnt-understand-runtime/main.c:17:13: warning: comparison of unsigned enum expression < 0 is always false [-Wtautological-compare]
if( type < 0 || type >= MulleKVCNumberOfMethodIndexes)
~~~~ ^ ~
/Volumes/Source/srcM/Tests/compiler-doesnt-understand-runtime/compiler-doesnt-understand-runtime/main.c:17:25: warning: comparison of constant 4 with expression of type 'MulleKVCMethodType' is always false [-Wtautological-constant-out-of-range-compare]
if( type < 0 || type >= MulleKVCNumberOfMethodIndexes)
~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2 warnings generated.
The first warning surprised me, because in my little mental world enums are still int. Guess they aren’t now, if there are no negative constants.
The second warning shows the trust this compiler has in the safety of static typing :)
I am “fixing” code like this now by casting the enum to int. I also do this now for
switch
statements, where I more often than not, do not handle all enum cases. I notice that in my coding there is a definite trend of more time being spent in code bureaucracy (or meta code) and less in actually getting shit done. And I don’t like it.
Post a comment
All comments are held for moderation; basic HTML formatting accepted.