Skip to main content

IAR Embedded Workbench for RX 5.20

MISRAC2012-Rule-5.2_c89

In this section:
Synopsis

(Required) Identifiers declared in the same scope and name space shall be distinct.

Enabled by default

Yes

Severity/Certainty

Low/Medium

lowmedium.png
Full description

Identifier names were found that are not distinct in their first 31 characters from other names in the same scope.

Coding standards
MISRA C:2012 Rule-5.2

(Required) Identifiers declared in the same scope and name space shall be distinct

Code examples

The following code example fails the check and will give a warning:

/*         1234567890123456789012345678901********* */
extern int n01_var_hides_var____________31x;
static int n01_var_hides_var____________31y;

/*         1234567890123456789012345678901********* */
static int n02_function_hides_var_______31x;
void       n02_function_hides_var_______31y (void) {}

void foo(void) {
  int i;
  switch(f1()) {
  case 1: {
      do {
	for(i = 0; i < 10; i++) {
	  if(f3()) {
	    /*     1234567890123456789012345678901********* */
	    int    n03_var_hides_var____________31x;
	    int    n03_var_hides_var____________31y;
	  }
	}
      } while(f2());
    }
  }
}

/*   1234567890123456789012345678901********* */
enum E {
     n04_var_hides_enum_const_____31x,
};

/*   1234567890123456789012345678901********* */
int  n04_var_hides_enum_const_____31y;

/*           1234567890123456789012345678901********* */
void bar(int n05_var_hides_parameter______31x) {
  int        n05_var_hides_parameter______31y;
}

/*      1234567890123456789012345678901********* */
#define n06_var_hides_macro_name_____31x 123
int     n06_var_hides_macro_name_____31y;

/*          1234567890123456789012345678901********* */
int         n07_type_hides_var___________31x;
typedef int n07_type_hides_var___________31y;

/*    1234567890123456789012345678901********* */
union U {
  int n08_field_hides_field________31x;
  int n08_field_hides_field________31y;
};

struct S {
  int n09_field_hides_field________31x;
  int n09_field_hides_field________31y;
};

The following code example passes the check and will not give a warning about this issue:

/*           1234567890123456789012345678901********* */
extern int   n01_var_in_different_scope___31x;
void         n02_different_function_name__31x (void) {
  static int n01_var_in_different_scope___31y;

  switch(fn()) {
  case 1:
    {
      int    n01_var_in_different_scope___31a;
    }
    break;
  case 2:
    {
      int    n01_var_in_different_scope___31b;
    }
    break;
  }
  {
      int    n01_var_in_different_scope___31c;
  }
  {
      int    n01_var_in_different_scope___31d;
  }
}

/* exception for typedef of tag name*/
typedef struct s1 {
  int sf1;
} s1;

typedef union u1 {
  int uf1;
  int uf2;
} u1;

typedef enum e1 {
  ec1, ec2
} e1;

/* identifiers in different name spaces */
/*    1234567890123456789012345678901********* */
union n02_var_hides_union_tag______31x {
  int v1;
  unsigned int v2;
}     n02_var_hides_union_tag______31y;

/*   1234567890123456789012345678901********* */
enum n03_var_hides_enum_tag_______31x {
     n04_tag_hides_enum_const_____31x
};

/*   1234567890123456789012345678901********* */
int  n03_var_hides_enum_tag_______31y;

/*     1234567890123456789012345678901********* */
struct n04_tag_hides_enum_const_____31y {
  int ff2;
};

void foo() {
/*    1234567890123456789012345678901********* */
  int n05_label_hides_var__________31x;
  {
/*1234567890123456789012345678901********* */
  n05_label_hides_var__________31y:
    n05_label_hides_var__________31x = 1;
  }
}

void bar(void) {
  int i;
  switch(f1()) {
  case 1: {
      do {
	for(i = 0; i < 10; i++) {
	  if(f3()) {
	    /*     1234567890123456789012345678901********* */
	    struct n06_var_hides_struct_tag_____31x {
	      int f1;
	    }      n06_var_hides_struct_tag_____31y;
	  }
	}
      } while(f2());
    }
  }
}