Lockedswitch case issue with XC8 compiler

Author
lcn
Junior Member
  • Total Posts : 111
  • Reward points : 0
  • Joined: 2012/02/05 06:28:55
  • Location: 0
  • Status: offline
2013/04/14 22:58:28 (permalink)
0

switch case issue with XC8 compiler

Hello,
 
I'm still trying to port a working code from C18 to XC8 compiler, but i getting trouble with the following code snippet :
 
XC8 compiler reports warning: unreachable code for on each break;
Can someone tell me what is wrong please ?

#define H_Error        0x02
#define M_Error        0x04
 
    if (CRC != CRC_Image)
        switch(mode)
            {
            case Temperature :
                return(T_Error);
                break;

            case Humidity :
                return(H_Error);
                break;
            }
    return(0);

 
#1

6 Replies Related Threads

    1and0
    Access is Denied
    • Total Posts : 7114
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re:switch case issue with XC8 compiler 2013/04/14 23:20:57 (permalink)
    0
    lcn
    XC8 compiler reports warning: unreachable code for on each break;
    Can someone tell me what is wrong please ?

           switch(mode)
               {
               case Temperature :
                   return(T_Error);
                   break;

               case Humidity :
                   return(H_Error);
                   break;
               }
       return(0);

    The warning is self explanatory.
    #2
    newfound
    Super Member
    • Total Posts : 1803
    • Reward points : 0
    • Joined: 2003/11/07 12:35:49
    • Status: online
    Re:switch case issue with XC8 compiler 2013/04/14 23:24:14 (permalink)
    0
    Well it seems to me that it is a valid warning. The break line will never be reached because of the unconditional return before it. As the break generates a BRA / GOTO instruction that will never be executed it seems reasonable to me to be advised of that.  There is no need for the break instructions in the above case statements.
     
     
    #3
    lcn
    Junior Member
    • Total Posts : 111
    • Reward points : 0
    • Joined: 2012/02/05 06:28:55
    • Location: 0
    • Status: offline
    Re:switch case issue with XC8 compiler 2013/04/15 01:17:43 (permalink)
    0
    Will it be correct to simply remove the break; at the end of each case because the return(); will exit from it ?
     
        switch(mode) 
               { 
               case Temperature : 
                   return(T_Error);  
                     
               case Humidity : 
                   return(H_Error);  
                } 
       return(0);
     
    post edited by lcn - 2013/04/15 01:29:53
    #4
    DarioG
    leaving this planet
    • Total Posts : 53071
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: porcodioland
    • Status: offline
    Re:switch case issue with XC8 compiler 2013/04/15 01:55:53 (permalink)
    0
    Funny, I never saw that warning on any platform Smile
    But indeed, I prefer to set a return variable and goto the end of the function.

    Dario Greggio
    --
    how does it feel, never having been loved? as life fades away, day after day, on this planet made of s h i t...

    #5
    andreic
    Super Member
    • Total Posts : 162
    • Reward points : 0
    • Joined: 2011/05/12 08:16:45
    • Location: Great White North
    • Status: offline
    Re:switch case issue with XC8 compiler 2013/04/15 08:42:14 (permalink)
    +2 (2)
    In either of the switch statements you return before you hit the break, unconditionally. Therefore the break statements can never be executed.
     
    My preferred style is to never have a block, like your if, that doesn't have {}s, always have a default case, and only one exit. I find that this makes my milage vary less than others.
    int retcode;
    retcode = 0;
    if (CRC != CRC_Image) {
      switch(mode)  {
               case Temperature :  
                   retcode = T_Error;
               break;            
     
               case Humidity :   
                   retcode = H_Error;
               break;
     
               default:
                   retcode = some_other_Error;
               break;    
         }  
    }
    return(retcode);  
    post edited by andreic - 2013/04/15 08:43:40
    #6
    DarioG
    leaving this planet
    • Total Posts : 53071
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: porcodioland
    • Status: offline
    Re:switch case issue with XC8 compiler 2013/04/15 10:20:00 (permalink)
    0
    Yeah grin

    Dario Greggio
    --
    how does it feel, never having been loved? as life fades away, day after day, on this planet made of s h i t...

    #7
    Jump to:
    © 2017 APG vNext Commercial Version 4.5