Coding Rule Subsets Checked Early in Analysis

In the initial compilation phase of the analysis, Polyspace® checks those coding rules that do not require the run-time error detection part of the analysis. If you want only those rules checked, you can perform a much quicker analysis.

The software provides two predefined subsets of rules that it checks earlier in the analysis. The subsets are available with the options Check MISRA C:2004 (-misra2), Check MISRA AC AGC (-misra-ac-agc), and Check MISRA C:2012 (-misra3).

ArgumentPurpose
single-unit-rules

Check rules that apply only to single translation units.

If you detect only coding rule violations and select this subset, a Bug Finder analysis stops after the compilation phase.

system-decidable-rules

Check rules in the single-unit-rules subset and some rules that apply to the collective set of program files. The additional rules are the less complex rules that apply at the integration level. These rules can be checked only at the integration level because the rules involve more than one translation unit.

If you detect only coding rule violations and select this subset, a Bug Finder analysis stops after the linking phase.

See also Check for Coding Rule Violations.

MISRA C:2004 and MISRA AC AGC Rules

The software checks the following rules early in the analysis. The rules that are checked at a system level and appear only in the system-decidable-rules subset are indicated by an asterisk.

Environment

RuleDescription
1.1*

All code shall conform to ISO® 9899:1990 "Programming languages - C", amended and corrected by ISO/IEC 9899/COR1:1995, ISO/IEC 9899/AMD1:1995, and ISO/IEC 9899/COR2:1996.

Language Extensions

RuleDescription
2.1

Assembly language shall be encapsulated and isolated.

2.2

Source code shall only use /* */ style comments.

2.3

The character sequence /* shall not be used within a comment.

Documentation

RuleDescription
3.4

All uses of the #pragma directive shall be documented and explained.

Character Sets

RuleDescription
4.1

Only those escape sequences which are defined in the ISO C standard shall be used.

4.2

Trigraphs shall not be used.

Identifiers

RuleDescription
5.1*

Identifiers (internal and external) shall not rely on the significance of more than 31 characters.

5.2

Identifiers in an inner scope shall not use the same name as an identifier in an outer scope, and therefore hide that identifier.

5.3*

A typedef name shall be a unique identifier.

5.4*

A tag name shall be a unique identifier.

5.5*

No object or function identifier with a static storage duration should be reused.

5.6*

No identifier in one name space should have the same spelling as an identifier in another name space, with the exception of structure and union member names.

5.7*

No identifier name should be reused.

Types

RuleDescription
6.1

The plain char type shall be used only for the storage and use of character values.

6.2

Signed and unsigned char type shall be used only for the storage and use of numeric values.

6.3

typedefs that indicate size and signedness should be used in place of the basic types.

6.4

Bit fields shall only be defined to be of type unsigned int or signed int.

6.5

Bit fields of type signed int shall be at least 2 bits long.

Constants

RuleDescription
7.1

Octal constants (other than zero) and octal escape sequences shall not be used.

Declarations and Definitions

RuleDescription
8.1

Functions shall have prototype declarations and the prototype shall be visible at both the function definition and call.

8.2

Whenever an object or function is declared or defined, its type shall be explicitly stated.

8.3

For each function parameter the type given in the declaration and definition shall be identical, and the return types shall also be identical.

8.4*

If objects or functions are declared more than once their types shall be compatible.

8.5

There shall be no definitions of objects or functions in a header file.

8.6

Functions shall always be declared at file scope.

8.7

Objects shall be defined at block scope if they are only accessed from within a single function.

8.8*

An external object or function shall be declared in one file and only one file.

8.9*

An identifier with external linkage shall have exactly one external definition.

8.10*

All declarations and definitions of objects or functions at file scope shall have internal linkage unless external linkage is required.

8.11

The static storage class specifier shall be used in definitions and  declarations of objects and functions that have internal linkage

8.12

When an array is declared with external linkage, its size shall be stated explicitly or defined implicitly by initialization.

Initialization

RuleDescription
9.2

Braces shall be used to indicate and match the structure in the nonzero initialization of arrays and structures.

9.3

In an enumerator list, the = construct shall not be used to explicitly initialize members other than the first, unless all items are explicitly initialized.

Arithmetic Type Conversion

RuleDescription
10.1

The value of an expression of integer type shall not be implicitly converted to a different underlying type if:

  • It is not a conversion to a wider integer type of the same signedness, or

  • The expression is complex, or

  • The expression is not constant and is a function argument, or

  • The expression is not constant and is a return expression

10.2

The value of an expression of floating type shall not be implicitly converted to a different type if

  • It is not a conversion to a wider floating type, or

  • The expression is complex, or

  • The expression is a function argument, or

  • The expression is a return expression

10.3

The value of a complex expression of integer type may only be cast to a type that is narrower and of the same signedness as the underlying type of the expression.

10.4

The value of a complex expression of float type may only be cast to narrower floating type.

10.5

If the bitwise operator ~ and << are applied to an operand of underlying type unsigned char or unsigned short, the result shall be immediately cast to the underlying type of the operand

10.6

The "U" suffix shall be applied to all constants of unsigned types.

Pointer Type Conversion

RuleDescription
11.1

Conversion shall not be performed between a pointer to a function and any type other than an integral type.

11.2

Conversion shall not be performed between a pointer to an object and any type other than an integral type, another pointer to a object type or a pointer to void.

11.3

A cast should not be performed between a pointer type and an integral type.

11.4

A cast should not be performed between a pointer to object type and a different pointer to object type.

11.5

A cast shall not be performed that removes any const or volatile qualification from the type addressed by a pointer

Expressions

RuleDescription
12.1

Limited dependence should be placed on C's operator precedence rules in expressions.

12.3

The sizeof operator should not be used on expressions that contain side effects.

12.5

The operands of a logical && or || shall be primary-expressions.

12.6

Operands of logical operators (&&, || and !) should be effectively Boolean. Expression that are effectively Boolean should not be used as operands to operators other than (&&, || or !).

12.7

Bitwise operators shall not be applied to operands whose underlying type is signed.

12.9

The unary minus operator shall not be applied to an expression whose underlying type is unsigned.

12.10

The comma operator shall not be used.

12.11

Evaluation of constant unsigned expression should not lead to wraparound.

12.12

The underlying bit representations of floating-point values shall not be used.

12.13

The increment (++) and decrement (--) operators should not be mixed with other operators in an expression

Control Statement Expressions

RuleDescription
13.1

Assignment operators shall not be used in expressions that yield Boolean values.

13.2

Tests of a value against zero should be made explicit, unless the operand is effectively Boolean.

13.3

Floating-point expressions shall not be tested for equality or inequality.

13.4

The controlling expression of a for statement shall not contain any objects of floating type.

13.5

The three expressions of a for statement shall be concerned only with loop control.

13.6

Numeric variables being used within a for loop for iteration counting should not be modified in the body of the loop.

Control Flow

RuleDescription
14.3

All non-null statements shall either

  • have at least one side effect however executed, or

  • cause control flow to change.

14.4

The goto statement shall not be used.

14.5

The continue statement shall not be used.

14.6

For any iteration statement, there shall be at most one break statement used for loop termination.

14.7

A function shall have a single point of exit at the end of the function.

14.8

The statement forming the body of a switch, while, do while or for statement shall be a compound statement.

14.9

An if (expression) construct shall be followed by a compound statement. The else keyword shall be followed by either a compound statement, or another if statement.

14.10

All if else if constructs should contain a final else clause.

Switch Statements

RuleDescription
15.0

Unreachable code is detected between switch statement and first case.

15.1

A switch label shall only be used when the most closely-enclosing compound statement is the body of a switch statement

15.2

An unconditional break statement shall terminate every non-empty switch clause.

15.3

The final clause of a switch statement shall be the default clause.

15.4

A switch expression should not represent a value that is effectively Boolean.

15.5

Every switch statement shall have at least one case clause.

Functions

RuleDescription
16.1

Functions shall not be defined with variable numbers of arguments.

16.3

Identifiers shall be given for all of the parameters in a function prototype declaration.

16.4*

The identifiers used in the declaration and definition of a function shall be identical.

16.5

Functions with no parameters shall be declared with parameter type void.

16.6

The number of arguments passed to a function shall match the number of parameters.

16.8

All exit paths from a function with non-void return type shall have an explicit return statement with an expression.

16.9

A function identifier shall only be used with either a preceding &, or with a parenthesized parameter list, which may be empty.

Pointers and Arrays

RuleDescription
17.4

Array indexing shall be the only allowed form of pointer arithmetic.

17.5

A type should not contain more than 2 levels of pointer indirection.

Structures and Unions

RuleDescription
18.1

All structure or union types shall be complete at the end of a translation unit.

18.4

Unions shall not be used.

Preprocessing Directives

RuleDescription
19.1

#include statements in a file shall only be preceded by other preprocessors directives or comments.

19.2

Nonstandard characters should not occur in header file names in #include directives.

19.3

The #include directive shall be followed by either a <filename> or "filename" sequence.

19.4

C macros shall only expand to a braced initializer, a constant, a parenthesized expression, a type qualifier, a storage class specifier, or a do-while-zero construct.

19.5

Macros shall not be #defined and #undefd within a block.

19.6

#undef shall not be used.

19.7

A function should be used in preference to a function like-macro.

19.8

A function-like macro shall not be invoked without all of its arguments.

19.9

Arguments to a function-like macro shall not contain tokens that look like preprocessing directives.

19.10

In the definition of a function-like macro, each instance of a parameter shall be enclosed in parentheses unless it is used as the operand of # or ##.

19.11

All macro identifiers in preprocessor directives shall be defined before use, except in #ifdef and #ifndef preprocessor directives and the defined() operator.

19.12

There shall be at most one occurrence of the # or ## preprocessor operators in a single macro definition.

19.13

The # and ## preprocessor operators should not be used.

19.14

The defined preprocessor operator shall only be used in one of the two standard forms.

19.15

Precautions shall be taken in order to prevent the contents of a header file being included twice.

19.16

Preprocessing directives shall be syntactically meaningful even when excluded by the preprocessor.

19.17

All #else, #elif and #endif preprocessor directives shall reside in the same file as the #if or #ifdef directive to which they are related.

Standard Libraries

RuleDescription
20.1

Reserved identifiers, macros and functions in the standard library, shall not be defined, redefined or undefined.

20.2

The names of standard library macros, objects and functions shall not be reused.

20.4

Dynamic heap memory allocation shall not be used.

20.5

The error indicator errno shall not be used.

20.6

The macro offsetof, in library <stddef.h>, shall not be used.

20.7

The setjmp macro and the longjmp function shall not be used.

20.8

The signal handling facilities of <signal.h> shall not be used.

20.9

The input/output library <stdio.h> shall not be used in production code.

20.10

The library functions atof, atoi and atoll from library <stdlib.h> shall not be used.

20.11

The library functions abort, exit, getenv and system from library <stdlib.h> shall not be used.

20.12

The time handling functions of library <time.h> shall not be used.

The rules that are checked at a system level and appear only in the system-decidable-rules subset are indicated by an asterisk.

MISRA C:2012 Rules

The software checks the following rules early in the analysis. The rules that are checked at a system level and appear only in the system-decidable-rules subset are indicated by an asterisk.

Standard C Environment

RuleDescription
1.1

The program shall contain no violations of the standard C syntax and constraints, and shall not exceed the implementation's translation limits.

1.2

Language extensions should not be used.

Unused Code

RuleDescription
2.3*

A project should not contain unused type declarations.

2.4*

A project should not contain unused tag declarations.

2.5*

A project should not contain unused macro declarations.

2.6

A function should not contain unused label declarations.

2.7

There should be no unused parameters in functions.

Comments

RuleDescription
3.1

The character sequences /* and // shall not be used within a comment.

3.2

Line-splicing shall not be used in // comments.

Character Sets and Lexical Conventions

RuleDescription
4.1

Octal and hexadecimal escape sequences shall be terminated.

4.2

Trigraphs should not be used.

Identifiers

RuleDescription
5.1*

External identifiers shall be distinct.

5.2

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

5.3

An identifier declared in an inner scope shall not hide an identifier declared in an outer scope.

5.4

Macro identifiers shall be distinct.

5.5

Identifiers shall be distinct from macro names.

5.6*

A typedef name shall be a unique identifier.

5.7*

A tag name shall be a unique identifier.

5.8*

Identifiers that define objects or functions with external linkage shall be unique.

5.9*

Identifiers that define objects or functions with internal linkage should be unique.

Types

RuleDescription
6.1

Bit-fields shall only be declared with an appropriate type.

6.2

Single-bit named bit fields shall not be of a signed type.

Literals and Constants

RuleDescription
7.1

Octal constants shall not be used.

7.2

A "u" or "U" suffix shall be applied to all integer constants that are represented in an unsigned type.

7.3

The lowercase character "l" shall not be used in a literal suffix.

7.4

A string literal shall not be assigned to an object unless the object's type is "pointer to const-qualified char".

Declarations and Definitions

RuleDescription
8.1

Types shall be explicitly specified.

8.2

Function types shall be in prototype form with named parameters.

8.3*

All declarations of an object or function shall use the same names and type qualifiers.

8.4

A compatible declaration shall be visible when an object or function with external linkage is defined.

8.5*

An external object or function shall be declared once in one and only one file.

8.6*

An identifier with external linkage shall have exactly one external definition.

8.7*

Functions and objects should not be defined with external linkage if they are referenced in only one translation unit.

8.8

The static storage class specifier shall be used in all declarations of objects and functions that have internal linkage.

8.9*

An object should be defined at block scope if its identifier only appears in a single function.

8.10

An inline function shall be declared with the static storage class.

8.11

When an array with external linkage is declared, its size should be explicitly specified.

8.12

Within an enumerator list, the value of an implicitly-specified enumeration constant shall be unique.

8.14

The restrict type qualifier shall not be used.

Initialization

RuleDescription
9.2

The initializer for an aggregate or union shall be enclosed in braces.

9.3

Arrays shall not be partially initialized.

9.4

An element of an object shall not be initialized more than once.

9.5

Where designated initializers are used to initialize an array object the size of the array shall be specified explicitly.

The Essential Type Model

RuleDescription
10.1

Operands shall not be of an inappropriate essential type.

10.2

Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations.

10.3

The value of an expression shall not be assigned to an object with a narrower essential type or of a different essential type category.

10.4

Both operands of an operator in which the usual arithmetic conversions are performed shall have the same essential type category.

10.5

The value of an expression should not be cast to an inappropriate essential type.

10.6

The value of a composite expression shall not be assigned to an object with wider essential type.

10.7

If a composite expression is used as one operand of an operator in which the usual arithmetic conversions are performed then the other operand shall not have wider essential type.

10.8

The value of a composite expression shall not be cast to a different essential type category or a wider essential type.

Pointer Type Conversion

RuleDescription
11.1

Conversions shall not be performed between a pointer to a function and any other type.

11.2

Conversions shall not be performed between a pointer to an incomplete type and any other type.

11.3

A cast shall not be performed between a pointer to object type and a pointer to a different object type.

11.4

A conversion should not be performed between a pointer to object and an integer type.

11.5

A conversion should not be performed from pointer to void into pointer to object.

11.6

A cast shall not be performed between pointer to void and an arithmetic type.

11.7

A cast shall not be performed between pointer to object and a non-integer arithmetic type.

11.8

A cast shall not remove any const or volatile qualification from the type pointed to by a pointer.

11.9

The macro NULL shall be the only permitted form of integer null pointer constant.

Expressions

RuleDescription
12.1

The precedence of operators within expressions should be made explicit.

12.3

The comma operator should not be used.

12.4

Evaluation of constant expressions should not lead to unsigned integer wrap-around.

Side Effects

RuleDescription
13.3

A full expression containing an increment (++) or decrement (--) operator should have no other potential side effects other than that caused by the increment or decrement operator.

13.4

The result of an assignment operator should not be used.

13.6

The operand of the sizeof operator shall not contain any expression which has potential side effects.

Control Statement Expressions

RuleDescription
14.4

The controlling expression of an if statement and the controlling expression of an iteration-statement shall have essentially Boolean type.

Control Flow

RuleDescription
15.1

The goto statement should not be used.

15.2

The goto statement shall jump to a label declared later in the same function.

15.3

Any label referenced by a goto statement shall be declared in the same block, or in any block enclosing the goto statement.

15.4

There should be no more than one break or goto statement used to terminate any iteration statement.

15.5

A function should have a single point of exit at the end

15.6

The body of an iteration-statement or a selection-statement shall be a compound statement.

15.7

All if … else if constructs shall be terminated with an else statement.

Switch Statements

RuleDescription
16.1

All switch statements shall be well-formed.

16.2

A switch label shall only be used when the most closely-enclosing compound statement is the body of a switch statement.

16.3

An unconditional break statement shall terminate every switch-clause.

16.4

Every switch statement shall have a default label.

16.5

A default label shall appear as either the first or the last switch label of a switch statement.

16.6

Every switch statement shall have at least two switch-clauses.

16.7

A switch-expression shall not have essentially Boolean type.

Functions

RuleDescription
17.1

The features of <starg.h> shall not be used.

17.3

A function shall not be declared implicitly.

17.4

All exit paths from a function with non-void return type shall have an explicit return statement with an expression.

17.6

The declaration of an array parameter shall not contain the static keyword between the [ ].

17.7

The value returned by a function having non-void return type shall be used.

Pointers and Arrays

RuleDescription
18.4

The +, -, += and -= operators should not be applied to an expression of pointer type.

18.5

Declarations should contain no more than two levels of pointer nesting.

18.7

Flexible array members shall not be declared.

18.8

Variable-length array types shall not be used.

Overlapping Storage

RuleDescription
19.2

The union keyword should not be used.

Preprocessing Directives

RuleDescription
20.1

#include directives should only be preceded by preprocessor directives or comments.

20.2

The ', ", or \ characters and the /* or // character sequences shall not occur in a header file name.

20.3

The #include directive shall be followed by either a <filename> or \"filename\" sequence.

20.4

A macro shall not be defined with the same name as a keyword.

20.5

#undef should not be used.

20.6

Tokens that look like a preprocessing directive shall not occur within a macro argument.

20.7

Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses.

20.8

The controlling expression of a #if or #elif preprocessing directive shall evaluate to 0 or 1.

20.9

All identifiers used in the controlling expression of #if or #elif preprocessing directives shall be #define'd before evaluation.

20.10

The # and ## preprocessor operators should not be used.

20.11

A macro parameter immediately following a # operator shall not immediately be followed by a ## operator.

20.12

A macro parameter used as an operand to the # or ## operators, which is itself subject to further macro replacement, shall only be used as an operand to these operators.

20.13

A line whose first token is # shall be a valid preprocessing directive.

20.14

All #else, #elif and #endif preprocessor directives shall reside in the same file as the #if, #ifdef or #ifndef directive to which they are related.

Standard Libraries

RuleDescription
21.1

#define and #undef shall not be used on a reserved identifier or reserved macro name.

21.2

A reserved identifier or macro name shall not be declared.

21.3

The memory allocation and deallocation functions of <stdlib.h> shall not be used.

21.4

The standard header file <setjmp.h> shall not be used.

21.5

The standard header file <signal.h> shall not be used.

21.6

The Standard Library input/output functions shall not be used.

21.7

The atof, atoi, atol, and atoll functions of <stdlib.h> shall not be used.

21.8

The library functions abort, exit, getenv and system of <stdlib.h> shall not be used.

21.9

The library functions bsearch and qsort of <stdlib.h> shall not be used.

21.10

The Standard Library time and date functions shall not be used.

21.11

The standard header file <tgmath.h> shall not be used.

21.12

The exception handling features of <fenv.h> should not be used.

The rules that are checked at a system level and appear only in the system-decidable-rules subset are indicated by an asterisk.

See Also

| |

Related Topics