Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

MISRA.CT.UNIQUE.ID

Identifier clashes with type name.

MISRA C 2012 Rule 5.7: A tag name shall be a unique identifier

Category: Required

Analysis: Decidable, System

Applies to: C90, C99

Amplification

The tag shall be unique across all name spaces and translation units.

All declarations of the tag shall specify the same type.

Multiple complete declarations of the same tag are only permitted by this rule if the tag is declared in a header file and that header file is included in multiple source files.

Example

Reusing a tag name may lead to developer confusion. There is also undefined behaviour associated with reuse of tag names in C90 although this is not listed in The Standard’s Annex. This undefined behaviour was recognized in C99 as a constraint in Section 6.7.2.3.

Exception

The tag name may be the same as the typedef name with which it is associated.

Example

struct stag 
{
  uint16_t a; 
  uint16_t b; 
};

struct stag a1 = { 0, 0 };   /* Compliant - compatible with above           */ 
union  stag a2 = { 0, 0 };   /* Non-compliant - declares different type
                              * from struct stag. 
                              * Constraint violation in C99                 */

The following example also violates Rule 5.3

struct deer 
{
  uint16_t a; 
  uint16_t b; 
};

void foo ( void ) 
{
  struct deer 
  {
    uint16_t a; 
  };                       /* Non-compliant - tag "deer" reused              */ 
}

typedef struct coord 
{
  uint16_t x; 
  uint16_t y; 
} coord;                   /* Compliant by Exception                         */

struct elk 
{
  uint16_t x; 
};

struct elk                 /* Non-compliant - declaration of different type
                            * Constraint violation in C99                    */ 

{
  uint32_t x; 
};

See also

Rule 5.6

MISRA-C 2004 Rule 5.4 (required): A tag name shall be a unique identifier.

No tag name shall be reused either to define a different tag or for any other purpose within the program. ISO/IEC 9899:1990 [2] does not define the behaviour when an aggregate declaration uses a tag in different forms of type specifier (struct or union). Either all uses of the tag should be in structure type specifiers, or all uses should be in union type specifiers.

Example

struct stag { uint16_t a; uint16_t b; };

struct stag a1 = { 0, 0 };      /* Compliant - compatible with above  */
union stag a2 = { 0, 0 };       /* Not compliant - not compatible with
                                   previous declarations              */
void foo(void)
{
   struct stag { uint16_t a; }; /* Not compliant - tag stag redefined */
}

The same tag definition shall not be duplicated anywhere in the source code files even if the definitions are identical. Where the tag definition is made in a header file, and that header file is included in multiple source files, this rule is not violated.

MISRA-C++ 2008 Rule 2-10-4 (required): A class, union or enum name (including qualification, if any) shall be a unique identifier.

Rationale

Reusing a class, union or enum name, either as another type or for any other purpose, may lead to developer confusion.

The class, union or enum name shall not be duplicated anywhere in the project, even if the declarations are identical.

This rule is not violated when the definition is made in a header file, and that header file is included in multiple source files.

Example

void f1 ( )
{
   class TYPE { };
}
void f2 ( )
{
   float32_t TYPE;  // Non-compliant
}