Created
March 21, 2013 13:34
-
-
Save pstef/5213034 to your computer and use it in GitHub Desktop.
Why I didn't like enum types in C until I learned about clang -Weverything -fsanitize=enum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string.h> | |
#include <stdio.h> | |
enum car { honda }; | |
enum number { ZERO, ONE }; | |
enum number function_returning_number(char const *keyword) { | |
if (!strcmp(keyword, "zero")) | |
return ZERO; | |
if (!strcmp(keyword, "one")) | |
return ONE; | |
return -1; /* gcc and clang silent */ | |
} | |
int main(void) { | |
enum number test = function_returning_number("one"); | |
test = 1500; /* gcc and clang silent */ | |
if (test == 1500) /* gcc silent, clang says this is always false */ | |
puts("test == 1500"); | |
test = honda; /* gcc silent, clang warns about implicit conversion */ | |
if (test == honda) /* clang warns and gcc suddenly does too! */ | |
puts("test == honda"); | |
return test; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
clang -Weverything -fsanitize=enum does the job!