Finding Dips in C Code

The product I'm working on has several unpleasant bugs related to unintentional "failures" in switch statements.

Now I would like to go one step further - I would like to detect the switch statement falling in the large body of C code. I can only use Linux and gcc 5.6 to compile (so not clang or new gcc; this is because for the target architecture for our project does not exist newer gcc).

This is the code without omissions:

    switch(m_loadAnimSubCt){
        case 0: 
        case 1: 
            // Do something
            break;
        case 2:
        case 3:
        case 4:
            // Do something
            break;
   }

This is the pass code:

   switch(m_loadAnimSubCt){
        case 0: 
        case 1: 
            // Do something but fall through to the other cases 
            // after doing it.
        case 2:
        case 3:
        case 4:
            // Do something else.
            break;
   }
+4
source share
2 answers

GCC, Wimplicit-fallthrough , . , -Werror (.. -Werror=implicit-fallthrough .

-Wextra ( ), , . , , -Wextra -Wpedantic -Werror.

, , , case, case, , , , /.

, - ( demo):

(?# match any 'case' following 'break;', ':' or '{' into a non-capturing group)
(?# then match the remaining 'case' into a named "fallthrough" group)
(?:(break;|:|{)[\r\n\s]*case) | (?<fallthrough>case)

, perl ( python - ) script , "".

+3

, ( , , , - , ).

, , - GCC (), "" clang , .

+2

Source: https://habr.com/ru/post/1684895/


All Articles