为什么gcc会引发隐式穿透警告?

时间:2020-07-21 08:06:00

标签: c gcc gcc-warning

给出代码:

#include <stdlib.h> 

enum one {
    A, B
};

enum two {
    AA
};

int main(int argc, char *argv[])
{
    enum one one = atoi(argv[1]);
    enum two two = atoi(argv[2]);
    
    if ((one != A && one != B) || two != AA)
        return 1;
    
    switch (one) {
    case A:
        switch (two) {
        case AA:
            return 2;
        }
    case B:
        return 3;
    }
    return 0;
}

使用gcc -Wimplicit-fallthrough test_fallthrough.c进行编译时,收到以下警告

test_fallthrough.c: In function 'main':
test_fallthrough.c:21:3: warning: this statement may fall through [-Wimplicit-fallthrough=]
   21 |   switch (two) {
      |   ^~~~~~
test_fallthrough.c:25:2: note: here
   25 |  case B:
      |  ^~~~

它试图警告什么,我该怎么做以使其不警告(我宁愿避免添加诸如/* Falls through. */之类的评论)

3 个答案:

答案 0 :(得分:0)

通常,编译器会在每个break主体之后检查case语句,以确保程序流(穿透)不会出错。

在您的情况下,case A主体没有break,因此当case B语句与{{1 }}。

switch

答案 1 :(得分:0)

您在第一个switch语句中缺少onCameraClickHandler () { if(cc.sys.OS_IOS == cc.sys.os) { var ret = jsb.reflection.callStaticMethod("TestingLogging", "testingIntegration", "Hello iOS") } } ,它可能会陷入第二种情况,它可以执行Please find use objectId type db.collection.find({ "content.creator._id": ObjectId("5f166ce0c6b1744fee6b28e7") }) ,然后执行break,因此发出警告。

case A

旁注:

  • 使用Case B检查//... switch (one) { case A: switch (two) { case AA: return 2; } break; //breaking case A removes the warning. case B: return 3; } //... argc是否存在总是一个好主意。

答案 2 :(得分:0)

为什么gcc会引发隐式掉线警告?

好吧,因为它可能会掉线。

要警告的是什么

case A时从case B进入two != AA

我该怎么办,以便它不会发出警告

在低于7的gcc上使用注释,即。 one of the markers that disable the warning

/* falls through */

在7以上的gcc上,您可以use a attribute

__attribute__((__fallthrough__));

在10以上的gcc上,您可以使用the attribute from C2x

[[fallthrough]];

-

请注意,if (one != A || one != B || two != AA)并没有真正检查任何内容,因为one != A || one != B将始终为true。我想您是想做if ((one != A && one != B) || two != AA)。尽管如此,-Wimplicit-falthrough=警告仍然没有考虑到if

相关问题