[previous] MISRA C:2012  Rule-16.1:  (Required) [next] All switch statements shall be well-formed

Amplification:

A switch statement shall be considered to be well-formed if it conforms to the subset of C switch statements that is specified by the following syntax rules. If a syntax rule given here has the same name as one defined in The Standard then it replaces the standard version for the scope of the switch statement; otherwise, all syntax rules given in The Standard are unchanged.

switch-statement:
   switch ( switch-expression ) { case-label-clause-list final-default-clause-list }
   switch ( switch-expression ) { initial-default-clause-list case-label-clause-list }
case-label-clause-list:
   case-clause-list
   case-label-clause-list case-clause-list
case-clause-list:
   case-label switch-clause
   case-label case-clause-list
case-label:
   case constant-expression:
final-default-clause-list:
   default: switch-clause
   case-label final-default-clause-list
initial-default-clause-list:
   default: switch-clause
   default: case-clause-list
switch-clause:
   statement-listopt break;
   C90: { declaration-listopt statement-listopt break; }
   C99: { block-item-listopt break; }

Except where explicitly permitted by this syntax, the case and default keywords may not appear anywhere within a switch statement body.

Note: some of the restrictions imposed on switch statements by this rule are expounded in the rules referenced in the "See also" section. It is therefore possible for code to violate both this rule and one of the more specific rules.

Note: the term switch label is used within the text of the specific switch statement rules to denote either a case label or a default label.

Example Code:


#include "misra.h"
#include "m3cmex.h"

extern int16_t rule_1601( void )
{
   switch ( s16a )
   {
      static int16_t xxxx;              /* 3234      */
      ++s16b;                           /* 2008 2880 */

      case 1:
         xxxx = s16a;
         break;

      case 2:
         s16b = xxxx;
         break;

      default:
         break;
   }
   return 1;
}


QAC messages that encompass this guideline:

2008 Code statements precede the first label in this 'switch' construct.
3234 Declarations precede the first label in this 'switch' construct.



(c) The Motor Industry Research Association, 2012
QA C Source Code Analyser 8.1.2
MISRA C:2012 Compliance Module 1.0
© 2013 Programming Research
www.programmingresearch.com
Personality Groups | Glossary | Message Index | MISRA C:2012 Rule Index Contents