C 代码 : enumeration values not explicitly handled in switch 的 Clang

标签 c enums clang suppress-warnings

我正在尝试使用 clang 3.1 和选项 -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;
}

然后编译器返回以下警告

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 ) {
                        ^

我在其他地方读到过类似的错误消息,人们通过添加 default 案例来解决它,但在这里你可以看到它已经存在了。我真的很想在没有任何警告的情况下编译我的代码,当然这里也不需要放置 229 种可能的情况。

最佳答案

从这个链接:

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

编译(查看注释以了解可能的更改)-Weverything -Wno-switch-enum

GCC 文档: http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html

-Wswitch 每当 switch 语句具有枚举类型的索引并且缺少 该枚举的一个或多个命名代码的情况。 (一个存在 默认标签会阻止此警告。)枚举范围外的案例标签 使用此选项时也会引发警告(即使有默认 标签)。此警告由 -Wall 启用。

-Wswitch-枚举 每当 switch 语句具有枚举类型的索引并且缺少 该枚举的一个或多个命名代码的情况。外壳标签 使用此选项时,超出枚举范围也会引发警告。

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.<<<

关于C 代码 : enumeration values not explicitly handled in switch 的 Clang,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16631713/

相关文章:

swift - 具有静态方法的服务对象的结构、类或枚举?

c++ - 无法捕获自定义 std::runtime_error

powershell - 如何将自定义 COM 枚举指定为 PowerShell 方法参数

c - MKL CBlas 错误

c - 当我知道每个元素的大小时,如何从指向数组的空指针获取项目?

arrays - 使用sizeof将字符附加到字符串形式用户的末尾

java - 实现响应式方向枚举的最简单方法? java

objective-c - Rust bindgen clang 错误,此 __builtin_neon 函数的常量不兼容

c - ndisasm手册中提到的二进制文件和可执行文件有什么区别?

c - 在 R-Pi 上用 C 语言生成随机数