Commit 2a291a73 authored by shengnan hu's avatar shengnan hu
Browse files

main

parents
#include "../include/typedefs.h"
// Example code from MISRA C:2012 begin
extern int32_t array1[ 10 ]; /* Compliant */
extern int32_t array2[ ]; /* Non-compliant */
// Example code from MISRA C:2012 end
// Example code from MISRA C:2012 begin
/* Non-compliant - yellow replicates implicit green */
enum colour_bad { red1 = 3, blue1, green1, yellow1 = 5 };
/* Compliant */
enum colour_food { red2 = 3, blue2, green2 = 5, yellow2 = 5 };
// Example code from MISRA C:2012 end
#include "../include/typedefs.h"
// Example code from MISRA C:2012 begin
/* Non-compliant */
uint16_t f ( uint16_t *p )
{
return *p;
}
/* Compliant */
uint16_t g ( const uint16_t *p );
/* Constraint violation */
void h ( const uint16_t *p )
{
*p = 0;
}
#include <string.h>
/* Non-compliant */
char last_char_bad ( char *const s )
{
return s[ strlen ( s ) - 1u ];
}
/* Compliant */
char last_char_good ( const char *const s );
/* Non-compliant */
uint16_t first_bad ( uint16_t a[ 5 ] )
{
return a[ 0 ];
}
/* Compliant */
uint16_t first_good ( const uint16_t a[ 5 ] );
// Example code from MISRA C:2012 end
char *p1;
char *q1;
int n1;
// Example code from MISRA C:2012 begin
#include <string.h>
/* Compliant */
void f ( void )
{
/* memcpy has restrict-qualified parameters */
memcpy ( p1, q1, n1 );
}
/* Non-compliant */
void user_copy ( void * restrict p2, void * restrict q2, size_t n2 )
{
}
// Example code from MISRA C:2012 end
#include "../include/typedefs.h"
// Example code from MISRA C:2012 begin
/* Compliant */
extern int16_t func1 ( int16_t n );
/* Non-compliant - parameter name not specified */
extern void func2 ( int16_t );
/* Non-compliant - not in prototype form */
static int16_t func3 ( );
/* Compliant */
static int16_t func4 ( void );
/* Compliant */
int16_t func1 ( int16_t n )
{
return n;
}
/* Non-compliant - old style identifier and declaration list */
static int16_t func3 ( vec, n )
int16_t *vec;
int16_t n;
{
return vec[ n - 1 ];
}
/* Non-compliant - no prototype */
int16_t ( *pf1 ) ( );
/* Compliant */
int16_t ( *pf1 ) ( void );
/* Non-compliant - parameter name not specified */
typedef int16_t ( *pf2_t ) ( int16_t );
/* Compliant */
typedef int16_t ( *pf3_t ) ( int16_t n );
// Example code from MISRA C:2012 end
#include "../include/typedefs.h"
// Example code from MISRA C:2012 begin
extern void f ( signed int );
void f ( int ); /* Compliant - Exception */
extern void g ( int * const );
void g ( int * ); /* Non-compliant - type qualifiers */
extern int16_t func ( int16_t num, int16_t den );
/* Non-compliant - parameter names do not match */
int16_t func ( int16_t den, int16_t num )
{
return num / den;
}
typedef uint16_t width_t;
typedef uint16_t height_t;
typedef uint32_t area_t;
extern area_t area ( width_t w, height_t h );
/* Non-compliant */
area_t area ( width_t w, width_t h )
{
return ( area_t ) w * h;
}
/* Compliant */
extern void f1 ( int16_t x );
extern void f2 ( int16_t y );
void f3 ( bool b )
{
void ( *fp1 ) ( int16_t z ) = b ? f1 : f2;
}
// Example code from MISRA C:2012 end
#include "../include/typedefs.h"
// Example code from MISRA C:2012 begin
extern int16_t count;
int16_t count = 0; /* Compliant */
extern uint16_t speed = 6000u; /* Non-compliant - no declaration
* prior to this definition */
uint8_t pressure = 101u; /* Non-compliant - no declaration
* prior to this definition */
extern void func1 ( void );
extern void func2 ( int16_t x, int16_t y );
extern void func3 ( int16_t x, int16_t y );
void func1 ( void )
{
/* Compliant */
}
void func2 ( int16_t x, int16_t y )
{
/* Compliant */
}
void func3 ( int16_t x, uint16_t y )
{
/* Non-compliant - parameter types different */
}
void func4 ( void )
{
/* Non-compliant - no declaration of func4 before this definition */
}
static void func5 ( void )
{
/* Compliant - rule does not apply to object/functions with internal
* linkage */
}
// Example code from MISRA C:2012 end
#include "../include/typedefs.h"
// Example code from MISRA C:2012 begin
int16_t i = 10;
// Example code from MISRA C:2012 end
#include "../include/typedefs.h"
// Example code from MISRA C:2012 begin
int16_t i = 20; /* Non-compliant - two definitions of i */
// Example code from MISRA C:2012 end
#include "../include/typedefs.h"
// Example code from MISRA C:2012 begin
int16_t j; /* Tentative definition */
int16_t j = 1; /* Compliant - external definition */
// Example code from MISRA C:2012 end
#include "../include/typedefs.h"
// Example code from MISRA C:2012 begin
/* Non-compliant */
int16_t k; /* Tentative definition - becomes external */
// Example code from MISRA C:2012 end
#include "../include/typedefs.h"
// Example code from MISRA C:2012 begin
/* Non-compliant */
int16_t k = 0; /* External definition */
// Example code from MISRA C:2012 end
#include "../include/typedefs.h"
// Example code from MISRA C:2012 begin
static int32_t x = 0; /* definition: internal linkage */
extern int32_t x; /* Non-compliant */
static int32_t f ( void ); /* declaration: internal linkage */
int32_t f ( void ) /* Non-compliant */
{
return 1;
}
static int32_t g ( void ); /* declaration: internal linkage */
extern int32_t g ( void ) /* Non-compliant */
{
return 1;
}
// Example code from MISRA C:2012 end
#include "../include/typedefs.h"
// Example code from MISRA C:2012 begin
void f ( bool_t b, uint16_t *p )
{
if ( b )
{
*p = 3U;
}
}
void g ( void )
{
uint16_t u;
f ( false, &u );
if ( u == 3U )
{
/* Non-compliant - u has not been assigned a value */
}
}
// Example code from MISRA C:2012 end
void example ( void )
// Example code from MISRA C:2012 begin
{
goto L1;
uint16_t x = 10u;
L1:
x = x + 1u; /* Non-compliant - x has not been assigned a value */
}
// Example code from MISRA C:2012 end
#include "../include/typedefs.h"
// Example code from MISRA C:2012 begin
int16_t y1[ 3 ][ 2 ] = { 1, 2, 3, 4, 5, 6 }; /* Non-compliant */
int16_t y2[ 3 ][ 2 ] = { { 1, 2 }, { 0 }, { 5, 6 } }; /* Compliant */
int16_t y3[ 3 ][ 2 ] = { { 1, 2 }, { 0, 0 }, { 5, 6 } }; /* Compliant */
int16_t z1[ 2 ][ 2 ] = { { 0 }, [ 1 ][ 1 ] = 1 }; /* Compliant */
int16_t z2[ 2 ][ 2 ] = { { 0 },
[ 1 ][ 1 ] = 1, [ 1 ][ 0 ] = 0
}; /* Compliant */
int16_t z3[ 2 ][ 2 ] = { { 0 }, [ 1 ][ 0 ] = 0, 1 }; /* Non-compliant */
int16_t z4[ 2 ][ 2 ] = { [ 0 ][ 1 ] = 0, { 0, 1 } }; /* Compliant */
float32_t a1[ 3 ][ 2 ] = { 0 }; /* Compliant */
float32_t a2[ 3 ][ 2 ] = { { 0 }, { 0 }, { 0 }}; /* Compliant */
float32_t a3[ 3 ][ 2 ] = { { 0.0f, 0.0f },
{ 0.0f, 0.0f },
{ 0.0f, 0.0f }
}; /* Compliant */
union u1 {
int16_t i;
float32_t f;
} u = { 0 }; /* Compliant */
struct s1 {
uint16_t len;
char buf[ 8 ];
} s[ 3 ] = {
{ 5u, { 'a','b','c','d','e','\0','\0','\0' } },
{ 2u, { 0 } },
{ .len = 0u } /* Compliant - buf initialized implicitly */
}; /* Compliant - s[ ] fully initialized */
// Example code from MISRA C:2012 end
#include "../include/typedefs.h"
#define PI 3.14159265358979323846 /* pi */
// Example code from MISRA C:2012 begin
/* Compliant */
int32_t x[ 3 ] = { 0, 1, 3 };
/* Non-compliant - y[ 2 ] is implicitly initialized */
int32_t y[ 3 ] = { 0, 1 };
/* Non-compliant - t[ 0 ] and t[ 3 ] are implicitly initialized */
float32_t t[ 4 ] = { [ 1 ] = 1.0f, 2.0f };
/* Compliant - designated initializers for sparse matrix */
float32_t z[ 50 ] = { [ 1 ] = 1.0f, [ 25 ] = 2.0f };
/* Compliant */
float32_t arr[ 3 ][ 2 ] =
{
{ 0.0f, 0.0 },
{ PI / 4.0f, -PI / 4.0f },
{ 0 } /* initializes all elements of array subobject arr[ 2 ] */
};
char h[ 10 ] = "hello"; /* Compliant by Exception 3 */
// Example code from MISRA C:2012 end
#include "../include/typedefs.h"
// Example code from MISRA C:2012 begin
/*
* Required behaviour using positional initialization
* Compliant - a1 is -5, -4, -3, -2, -1
*/
int16_t a1[ 5 ] = { -5, -4, -3, -2, -1 };
/*
* Similar behaviour using designated initializers
* Compliant - a2 is -5, -4, -3, -2, -1
*/
int16_t a2[ 5 ] = { [ 0 ] = -5, [ 1 ] = -4, [ 2 ] = -3,
[ 3 ] = -2, [ 4 ] = -1 };
/*
* Repeated designated initializer element values overwrite earlier ones
* Non-compliant - a3 is -5, -4, -2, 0, -1
*/
int16_t a3[ 5 ] = { [ 0 ] = -5, [ 1 ] = -4, [ 2 ] = -3,
[ 2 ] = -2, [ 4 ] = -1 };
/* Non-compliant */
uint16_t *p;
void f ( void )
{
uint16_t a[ 2 ] = { [ 0 ] = *p++, [ 0 ] = 1};
}
struct mystruct
{
int32_t a;
int32_t b;
int32_t c;
int32_t d;
};
/*
* Required behaviour using positional initialization
* Compliant - s1 is 100, -1, 42, 999
*/
struct mystruct s1 = { 100, -1, 42, 999 };
/*
* Similar behaviour using designated initializers
* Compliant - s2 is 100, -1, 42, 999
*/
struct mystruct s2 = { .a = 100, .b = -1, .c = 42, .d = 999 };
/*
* Repeated designated initializer element values overwrite earlier ones
* Non-compliant - s3 is 42, -1, 0, 999
*/
struct mystruct s3 = { .a = 100, .b = -1, .a = 42, .d = 999 };
// Example code from MISRA C:2012 end
// Example code from MISRA C:2012 begin
/* Non-compliant */
int a1[ ] = { [ 0 ] = 1 };
/* Compliant */
int a2[ 10 ] = { [ 0 ] = 1 };
// Example code from MISRA C:2012 end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment