Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clang with C code: enumeration values not explicitly handled in switch

I'm trying to compile this code with clang 3.1 and the option -Weverything:

#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>

SDL_Surface* init(SDL_Surface* screen);


int main() {
    SDL_Event event;
    SDL_Surface* screen = NULL;
    int quit = 0;

    screen = init(screen);
    if (screen == NULL) {
        return EXIT_FAILURE;
    }
    while(quit == 0) {
        while(SDL_PollEvent(&event)) {
            if( event.type == SDL_QUIT ) {
                quit = 1;
            } else if( event.type == SDL_KEYDOWN ) {
                switch( event.key.keysym.sym ) {
                    case SDLK_UP: printf("up\n"); break;
                    case SDLK_DOWN: printf("down\n"); break;
                    case SDLK_LEFT: printf("left\n"); break;
                    case SDLK_RIGHT: printf("right\n"); break;
                    default: break;
                }
            }
        }
    }
    SDL_FreeSurface(screen);
    SDL_Quit();
    return 0;
}

SDL_Surface* init(SDL_Surface* screen) {
    if( SDL_Init(SDL_INIT_EVERYTHING) == -1) {
        return NULL;
    }
    screen=SDL_SetVideoMode(100,100,32,SDL_SWSURFACE);
    return screen;
}

The compiler then returns me the following warning

main.c:22:25: warning: 229 enumeration values not explicitly handled in switch: 'SDLK_UNKNOWN', 'SDLK_BACKSPACE',
      'SDLK_TAB'... [-Wswitch-enum]
                switch( event.key.keysym.sym ) {
                        ^

Elsewhere I've read similar error messages and people solved it by adding the default case, but here as you can see it's already there. I would really like to compile my code without a single warning, and of course here without needing to put the 229 possible cases.

like image 712
sysko Avatar asked May 19 '13 05:05

sysko


1 Answers

From this link:

http://clang-developers.42468.n3.nabble.com/Question-on-Wswitch-enum-td4025927.html

compile with (see comments for possible change) -Weverything -Wno-switch-enum

Docs for GCC: http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html

-Wswitch Warn whenever a switch statement has an index of enumerated type and lacks a case for one or more of the named codes of that enumeration. (The presence of a default label prevents this warning.) case labels outside the enumeration range also provoke warnings when this option is used (even if there is a default label). This warning is enabled by -Wall.

-Wswitch-enum Warn whenever a switch statement has an index of enumerated type and lacks a case for one or more of the named codes of that enumeration. case labels outside the enumeration range also provoke warnings when this option is used.

The only difference between -Wswitch and this option is that this option gives a warning about an omitted enumeration code even if there is a default label.<<<

like image 71
xaxxon Avatar answered Oct 19 '22 04:10

xaxxon