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

main

parents
#include <stdint.h>
void example ( void )
{
uint16_t u16a;
uint8_t u8a;
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
u8a = u8a << 7; /* Compliant */
u8a = u8a << 8; /* Non-Compliant */
u16a = ( uint16_t ) u8a << 9; /* Compliant */
1u << 10u; /* Non-compliant */
( uint16_t ) 1u << 10u; /* Compliant */
1UL << 10u; /* Compliant */
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
#include <stdint.h>
#define N 5
extern void f ( int16_t para1, int16_t para2 );
void test ( void )
{
int16_t a[ N ], i, *p;
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
f ( ( 1, 2 ), 3 ); /* Non-compliant */
for ( i = 0, p = &a[ 0 ]; i < N; ++i, ++p ) /* Non-compliant */
{
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
#include <stdint.h>
typedef _Bool bool_t;
extern void f ( void );
extern void g ( void );
extern void h ( void );
void wrapAround ( void )
{
uint16_t x;
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
#define BASE 65024u
switch ( x )
{
case BASE + 0u:
f ( );
break;
case BASE + 1u:
g ( );
break;
case BASE + 512u: /* Non-compliant */
h ( );
break;
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
#if 1u + ( 0u - 10u ) /* Non-compliant */
/* ------------------ Example code from MISRA C:2012 end ------------------ */
#endif
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
#define DELAY 10000u
#define WIDTH 60000u
void fixed_pulse ( void )
{
uint16_t off_time16 = DELAY + WIDTH; /* Non-compliant */
}
const uint16_t c = 0xffffu;
void f ( void )
{
uint16_t y = c + 1u; /* Compliant */
}
bool_t b;
void g ( void )
{
uint16_t x = ( 0u == 0u ) ? 0u : ( 0u - 1u ); /* Compliant */
uint16_t y = b ? 0u : ( 0u - 1u ); /* Non-compliant */
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
#include <stdint.h>
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
int32_t glbA[ ] = { 1, 2, 3, 4, 5 };
void f ( int32_t A[ 4 ] )
{
/* Non-compliant */
uint32_t numElements = sizeof ( A ) / sizeof ( int32_t );
/* Compliant */
uint32_t numElements_glbA = sizeof ( glbA ) / sizeof ( glbA[ 0 ] );
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
#include <stdint.h>
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
volatile uint16_t v1;
void f ( void )
{
uint16_t a[ 2 ] = { v1, 0 }; /* Non-compliant */
}
void g ( uint16_t x, uint16_t y )
{
uint16_t a[ 2 ] = { x + y, x - y }; /* Compliant */
}
uint16_t x = 0u;
extern void p ( uint16_t a[ 2 ] );
void h ( void )
{
p ( ( uint16_t[ 2 ] ) { x++, x++ } ); /* Non-compliant */
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
#include <stdint.h>
extern void f ( uint16_t para1, uint16_t para2 );
extern uint16_t g ( struct str **strp );
struct str
{
void ( *f ) ( uint16_t para );
};
void example ( void )
{
struct str *p;
uint16_t a[ 3 ], b[ 3 ], i;
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
#define COPY_ELEMENT( index ) ( a[ ( index ) ] = b[ ( index ) ] )
COPY_ELEMENT ( i++ ); /* Non-compliant */
extern volatile uint16_t v1, v2;
uint16_t t;
t = v1 + v2; /* Non-compliant */
extern volatile uint8_t PORT;
PORT = PORT & 0x80u; /* Compliant */
i = 0;
f ( i++, i ); /* Non-compliant */
p->f ( g ( &p ) ); /* Non-compliant */
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
#include <stdint.h>
uint8_t f ( void );
void g ( uint8_t );
struct str
{
uint8_t x;
};
void example ( void )
{
uint8_t u8a, u8b, u8c, x, a[ 3 ], i, *p;
struct str b, *c;
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
u8a = u8b++; /* Non-compliant */
u8a = ++u8b + u8c--; /* Non-compliant */
++u8b;
u8a = u8b + u8c;
u8c--;
x++; /* Compliant */
a[ i ]++; /* Compliant */
b.x++; /* Compliant */
c->x++; /* Compliant */
++( *p ); /* Compliant */
*p++; /* Compliant */
( *p )++; /* Compliant */
if ( ( f ( ) + --u8a ) == 0u ) /* Non-compliant */
{
}
g ( u8b++ ); /* Non-compliant */
u8a = ( 1u == 1u ) ? 0u : u8b++; /* Non-compliant */
if ( u8a++ == ( ( 1u == 1u ) ? 0u : f ( ) ) ) /* Non-compliant */
{
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
#include <stdint.h>
#define false 0
#define true 1
typedef _Bool bool_t;
uint8_t f ( void );
void example ( void )
{
uint8_t x, y, a[ 3 ], b, c;
bool_t bool_var;
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
x = y; /* Compliant */
a[ x ] = a[ x = y ]; /* Non-compliant */
if ( bool_var = false ) /* Non-compliant */
{
}
if ( ( 0u == 0u ) || ( bool_var = true ) ) /* Non-compliant */
{
}
if ( ( x = f ( ) ) != 0 ) /* Non-compliant */
{
}
a[ b += c ] = a[ b ]; /* Non-compliant */
x = b = c = 0; /* Non-compliant */
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
#include <stddef.h>
#include <stdint.h>
typedef _Bool bool_t;
bool_t ishigh;
uint16_t a, x;
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
int16_t f ( uint16_t y )
{
uint16_t temp = y;
temp = y + 0x8080U;
return temp;
}
uint16_t h ( uint16_t y )
{
static uint16_t temp = 0;
temp = y + temp;
return temp;
}
void g ( void )
{
if ( ishigh && ( a == f ( x ) ) ) /* Compliant */
{
}
if ( ishigh && ( a == h ( x ) ) ) /* Non-compliant */
{
}
}
volatile uint16_t v;
uint16_t x;
/* ------------------ Example code from MISRA C:2012 end ------------------ */
void example ( void )
{
uint16_t ( *fp ) ( uint16_t para ) = h;
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
if ( ( x == 0u ) || ( v == 1u ) ) /* Non-compliant */
{
}
( fp != NULL ) && ( *fp ) ( 0 ); /* Non-compliant */
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
#include <stdint.h>
void example ( void )
{
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
volatile int32_t i;
int32_t j;
size_t s;
s = sizeof ( j ); /* Compliant */
s = sizeof ( j++ ); /* Non-compliant */
s = sizeof ( i ); /* Compliant */
s = sizeof ( int32_t ); /* Compliant */
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
volatile uint32_t v;
void f ( int32_t n )
{
size_t s;
s = sizeof ( int32_t[ n ] ); /* Compliant */
s = sizeof ( int32_t[ n++ ] ); /* Non-compliant */
s = sizeof ( void ( *[ n ] ) ( int32_t a[ v ] ) ); /* Non-compliant */
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
#include <stdint.h>
typedef float float32_t;
void example ( void )
{
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
uint32_t counter = 0u;
for ( float32_t f = 0.0f; f < 1.0f; f += 0.001f ) /* Non-compliant */
{
++counter;
}
float32_t f;
for ( counter = 0u; counter < 1000u; ++counter ) /* Compliant */
{
f = ( float32_t ) counter * 0.001f;
}
f = 0.0f;
while ( f < 1.0f ) /* Non-compliant */
{
f += 0.001f;
}
uint32_t u32a;
f = read_float32 ( );
do
{
u32a = read_u32 ( );
} while ( ( ( float32_t ) u32a - f ) > 10.0f ); /* Compliant */
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
#include <stdint.h>
#define false 0
#define true 1
typedef _Bool bool_t;
#define C 1
void example ( void )
{
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
bool_t flag = false;
for ( int16_t i = 0; ( i < 5 ) && !flag; i++ )
{
if ( C )
{
flag = true; /* Compliant */
}
i = i + 3; /* Non-compliant */
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
#include <stdint.h>
#define true 1
void example ( void )
{
uint16_t u16a;
int8_t s8a;
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
s8a = ( u16a < 0u ) ? 0 : 1; /* Non-compliant */
if ( u16a < 0xffffu ) /* Non-compliant */
{
}
if ( 2 > 3 ) /* Non-compliant */
{
}
for ( s8a = 0; s8a < 130; ++s8a ) /* Non-compliant */
{
}
if ( ( s8a < 10 ) && ( s8a > 20 ) ) /* Non-compliant */
{
}
if ( ( s8a < 10 ) || ( s8a > 5 ) ) /* Non-compliant */
{
}
while ( s8a > 10 )
{
if ( s8a > 5 ) /* Non-compliant */
{
}
}
while ( true ) /* Compliant */
{
}
do
{
} while ( 0u == 1u ); /* Compliant */
const uint8_t numcyl = 4u;
if ( numcyl == 4u ) /* Non-compliant */
{
}
const volatile uint8_t numcyl_cal = 4u;
if ( numcyl_cal == 4u ) /* Compliant */
{
}
uint16_t n;
uint16_t sum;
sum = 0;
for ( uint16_t i = ( n - 6u ); i < n; ++i )
{
sum += i;
}
if ( ( sum % 2u ) == 0u ) /* Non-compliant */
{
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
#include <stddef.h>
#include <stdint.h>
#define false 0
#define true 1
typedef _Bool bool_t;
void example ( void )
{
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
int32_t *p, *q;
while ( p ) /* Non-compliant */
{
}
while ( q != NULL ) /* Compliant */
{
}
while ( true ) /* Compliant */
{
}
extern bool_t flag;
while ( flag ) /* Compliant */
{
}
int32_t i;
if ( i ) /* Non-compliant */
{
}
if ( i != 0 ) /* Compliant */
{
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
#include <stdint.h>
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
void f ( void )
{
int32_t j = 0;
L1:
++j;
if ( 10 == j )
{
goto L2; /* Compliant */
}
goto L1; /* Non-compliant */
L2:
++j;
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
#include <stdint.h>
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
void f1 ( int32_t a )
{
if ( a <= 0 )
{
goto L2; /* Non-compliant */
}
goto L1; /* Compliant */
if ( a == 0 )
{
goto L1; /* Compliant */
}
goto L2; /* Non-compliant */
L1:
if ( a > 0 )
{
L2:
;
}
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
void example ( void )
{
int32_t x, y;
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
switch ( x )
{
case 0:
if ( x == y )
{
goto L1; /* Non-compliant */
}
break;
case 1:
y = x;
L1:
++x;
break;
default:
break;
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
#include <stdint.h>
#define LIMIT 5u
void example ( void )
{
uint16_t x, y, z;
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
/* Compliant */
for ( x = 0; x < LIMIT; ++x )
{
if ( ExitNow ( x ) )
{
break;
}
for ( y = 0; y < x; ++y )
{
if ( ExitNow ( LIMIT - y ) )
{
break;
}
}
}
/* Non-compliant */
for ( x = 0; x < LIMIT; ++x )
{
if ( BreakNow ( x ) )
{
break;
}
else if ( GotoNow ( x ) )
{
goto EXIT;
}
else
{
KeepGoing ( x );
}
}
EXIT:
;
/* Non-compliant */
while ( x != 0u )
{
x = calc_new_x ( );
if ( x == 1u )
{
break;
}
/* Compliant */
while ( y != 0u )
{
y = calc_new_y ( );
if ( y == 1u )
{
goto L1;
}
}
}
L1:
z = x + y;
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
#include <stddef.h>
#include <stdint.h>
#define false 0
#define true 1
typedef _Bool bool_t;
#define MAX 5u
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
bool_t f ( uint16_t n, char *p ) /* Non-compliant */
{
if ( n > MAX )
{
return false;
}
if ( p == NULL )
{
return false;
}
return true;
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
typedef _Bool bool_t;
void example ( void )
{
bool_t data_available, flag_1, flag_2, flag;
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
while ( data_available )
process_data ( ); /* Non-compliant */
while ( data_available )
process_data ( ); /* Non-compliant */
servive_watchdog ( );
if ( flag_1 )
if ( flag_2 ) /* Non-compliant */
action_1 ( ); /* Non-compliant */
else
action_2 ( ); /* Non-compliant */
if ( flag_1 )
{
if ( flag_2 )
{
action_1 ( );
}
else
{
action_2 ( );
}
}
if ( flag_1 )
{
action_1 ( );
}
else if ( flag_2 ) /* Compliant */
{
action_2 ( );
}
else
{
;
}
while ( flag ); /* Non-compliant */
{
flag = fn ( );
}
while ( !data_available ) /* Compliant */
{
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
typedef _Bool bool_t;
void example ( void )
{
bool_t flag_1, flag_2;
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
if ( flag_1 ) /* Non-compliant */
{
action_1 ( );
}
else if ( flag_2 )
{
action_2 ( );
}
if ( flag_1 ) /* Compliant */
{
action_1 ( );
}
else if ( flag_2 )
{
action_2 ( );
}
else
{
;
}
/* ------------------ 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