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

main

parents
#include <stdint.h>
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
int x = 0; /* Non-compliant */
typedef int SINT_16; /* Compliant */
typedef int speed_t; /* Non-compliant */
typedef int16_t torque_t; /* Compliant */
/* ------------------ Example code from MISRA C:2012 end ------------------ */
#include <stdint.h>
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
#define EVAL_BINOP( OP, L, R ) ( ( L ) OP ( R ) )
uint32_t x = EVAL_BINOP ( +, 1, 2 ); /* Compliant */
#define DIV2( X ) ( ( X ) / 2 )
void f ( void )
{
static uint16_t x = DIV2 ( 10 ); /* Compliant */
uint16_t y = DIV2 ( 10 ); /* Non-compliant */
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
#include <stdint.h>
typedef float float32_t;
typedef _Bool bool_t;
void example ( void )
{
float32_t f32a, f32b;
int16_t s16b;
uint16_t u16b;
bool_t bla, blb;
int8_t s8a, s8b;
uint8_t u8a, u8b;
char cha, chb;
bool_t bla, blb;
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
enum enuma { a1, a2, a3 } ena, enb;
enum { K1 = 1, K2 = 2 };
f32a & 2U; /* Non-compliant */
f32a << 2; /* Non-compliant */
cha && bla; /* Non-compliant */
ena ? a1 : a2; /* Non-compliant */
s8a && bla; /* Non-compliant */
u8a ? a1 : a2; /* Non-compliant */
f32a && bla; /* Non-compliant */
bla * blb; /* Non-compliant */
bla > blb; /* Non-compliant */
cha & chb; /* Non-compliant */
cha << 1; /* Non-compliant */
ena--; /* Non-compliant */
ena * a1; /* Non-compliant */
s8a & 2; /* Non-compliant */
50 << 3U; /* Non-compliant */
u8a << s8a; /* Non-compliant */
u8a << -1; /* Non-compliant */
-u8a; /* Non-compliant */
ena += a1; /* Non-compliant */
bla && blb; /* Compliant */
bla ? u8a : u8b; /* Compliant */
cha - chb; /* Compliant */
cha > chb; /* Compliant */
ena > a1; /* Compliant */
K1 * s8a; /* Compliant */
s8a + s16b; /* Compliant */
-( s8a ) * s8b; /* Compliant */
s8a > 0; /* Compliant */
--s16b; /* Compliant */
u8a + u16b; /* Compliant */
u8a & 2U; /* Compliant */
u8a > 0U; /* Compliant */
u8a << 2U; /* Compliant */
u8a << 1; /* Compliant */
f32a + f32b; /* Compliant */
f32a > 0.0; /* Compliant */
cha + chb; /* Compliant */
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
#include <stdint.h>
typedef float float32_t;
void example ( void )
{
enum enuma { a1, a2, a3 } ena;
float32_t f32a;
int16_t s16a;
int8_t s8a;
uint8_t u8a;
char cha;
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
'0' + u8a; /* Compliant */
s8a + '0'; /* Compliant */
cha - '0'; /* Compliant */
'0' - s8a; /* Compliant */
s16a - 'a'; /* Non-compliant */
'0' + f32a; /* Non-compliant */
cha + ':'; /* Non-compliant */
cha - ena; /* Non-compliant */
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
#include <stdint.h>
#define false 0
#define true 1
typedef _Bool bool_t;
void use_uint32 ( uint32_t u32a );
void use_uint16 ( uint16_t u16a );
void example ( void )
{
int32_t s32a;
uint32_t u32a;
uint16_t u16a, u16b;
int8_t s8a;
uint8_t u8b, u8c, u8d, *pu8a, *pu8b;
char cha;
bool_t bla;
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
enum enuma { A1, A2, A3 } ena;
enum enumb { B1, B2, B3 } enb;
enum { K1=1, K2=128 };
uint8_t u8a = 0; /* Compliant */
bool_t flag = ( bool_t ) 0; /* Compliant */
bool_t set = true; /* Compliant */
bool_t get = ( u8b > u8c ); /* Compliant */
ena = A1; /* Compliant */
s8a = K1; /* Compliant */
u8a = 2; /* Compliant */
u8a = 2 * 24; /* Compliant */
cha += 1; /* Compliant */
pu8a = pu8b; /* Compliant */
u8a = u8b + u8c + u8d; /* Compliant */
u8a = ( uint8_t )s8a; /* Compliant */
u32a = u16a; /* Compliant */
u32a = 2U + 125U; /* Compliant */
use_uint16 ( u8a ); /* Compliant */
use_uint16 ( u8a + u16b ); /* Compliant */
u8a = 1.0f; /* Non-compliant */
bla = 0; /* Non-compliant */
cha = 7; /* Non-compliant */
u8a = 'a'; /* Non-compliant */
u8b = 1 - 2; /* Non-compliant */
u8c += 'a'; /* Non-compliant */
use_uint32 ( s32a ); /* Non-compliant */
s8a = K2; /* Non-compliant */
u16a = u32a; /* Non-compliant */
use_uint16 ( u32a ); /* Non-compliant */
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
uint8_t foo1 ( uint16_t x )
{
return x; /* Non-compliant */
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
#include <stdint.h>
void example ( void )
{
uint16_t u16b;
int8_t s8a;
uint8_t u8a, u8b;
char cha;
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
enum enuma { A1, A2, A3 } ena;
enum enumb { B1, B2, B3 } enb;
ena > A1; /* Compliant */
u8a + u16b; /* Compliant */
cha += u8a; /* Compliant */
s8a += u8a; /* Non-compliant */
u8b + 2; /* Non-compliant */
enb > A1; /* Non-compliant */
ena == enb; /* Non-compliant */
u8a += cha; /* Non-compliant */
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
#include <stdint.h>
#define false 0
#define true 1
typedef _Bool bool_t;
void example ( void )
{
enum enuma { A1, A2, A3 } ena;
enum enumc { C1, C2, C3 } enc;
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
( bool_t ) false; /* Compliant */
( int32_t ) 3U; /* Compliant */
( bool_t ) 0; /* Compliant */
( bool_t ) 3U; /* Non-compliant */
( int32_t ) ena; /* Compliant */
( enum enuma ) 3; /* Non-compliant */
( char ) enc; /* Compliant */
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
#include <stdint.h>
void use_uint32 ( uint32_t u32a );
void example ( void )
{
uint32_t u32a;
uint16_t u16a, u16b, u16c;
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
u16c = u16a + u16b; /* Compliant */
u32a = ( uint32_t ) u16a + u16b; /* Compliant */
u32a = u16a + u16b; /* Non-compliant */
use_uint32 ( u16a + u16b ); /* Non-compliant */
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
#include <stdint.h>
void example ( void )
{
uint32_t u32a, u32b;
uint16_t u16a, u16b;
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
u32a * u16a + u16b; /* Compliant */
( u32a * u16a ) + u16b; /* Compliant */
u32a * ( ( uint32_t ) u16a + u16b ); /* Compliant */
u32a += ( u32b + u16b ); /* Compliant */
u32a * ( u16a + u16b ); /* Non-compliant */
u32a += ( u16a + u16b ); /* Non-compliant */
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
#include <stdint.h>
void example ( void )
{
int32_t s32a, s32b;
uint32_t u32a, u32b;
uint16_t u16a, u16b;
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
( uint16_t ) ( u32a + u32b ); /* Compliant */
( uint16_t ) ( s32a + s32b ); /* Non-compliant */
( uint16_t ) s32a; /* Compliant */
( uint32_t ) ( u16a + u16b ); /* Non-compliant */
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
#include <stdint.h>
void example ( void )
{
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
typedef void ( *fp16 ) ( int16_t n );
typedef void ( *fp32 ) ( int32_t n );
#include <stdlib.h>
fp16 fp1 = NULL; /* Compliant */
fp32 fp2 = ( fp32 ) fp1; /* Non-compliant */
if ( fp2 != NULL ) /* Compliant */
{
}
fp16 fp3 = ( fp16 ) 0x8000; /* Non-compliant */
fp16 fp4 = ( fp16 ) 1.0e6F; /* Non-compliant */
typedef fp16 ( *pfp16 ) ( void );
pfp16 pfp1;
( void ) ( *pfp1 ( ) ); /* Compliant */
extern void f ( int16_t n );
f ( 1 ); /* Compliant */
fp16 fp5 = f; /* Compliant */
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
#include <stdint.h>
void example ( void )
{
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
struct s;
struct t;
struct s *sp;
struct t *tp;
int16_t *ip;
#include <stdlib.h>
ip = ( int16_t * ) sp; /* Non-compliant */
sp = ( struct s * ) 1234; /* Non-compliant */
tp = ( struct t * ) sp; /* Non-compliant */
sp = NULL; /* Compliant */
struct s *f ( void );
( void ) f ( ); /* Compliant */
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
#include <stdint.h>
void possibleIncompatibleAlignment ( void )
{
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
uint8_t *p1;
uint32_t *p2;
p2 = ( uint32_t * ) p1; /* Non-compliant */
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
extern uint32_t read_value ( void );
extern void print ( uint32_t n );
void f ( void )
{
uint32_t u = read_value ( );
uint16_t *hi_p = ( uint16_t * ) &u; /* Non-compliant */
*hi_p = 0;
print ( u );
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
void typeQualifiers ( void )
{
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
const short *p;
const volatile short *q;
q = ( const volatile short * ) p; /* Compliant */
int * const * pcpi;
const int * const * pcpci;
pcpci = ( const int * const * ) pcpi; /* Non-compliant */
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
#include <stdint.h>
typedef _Bool bool_t;
void example ( void )
{
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
uint8_t *PORTA = ( uint8_t * ) 0x0002; /* Non-compliant */
uint16_t *p;
int32_t addr = ( int32_t ) &p; /* Non-compliant */
uint8_t *q = ( uint8_t * ) addr; /* Non-compliant */
bool_t b = ( bool_t ) p; /* Non-compliant */
enum etag { A, B } e = ( enum etag ) p; /* Non-compliant */
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
#include <stdint.h>
void example ( void )
{
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
uint32_t *p32;
void *p;
uint16_t *p16;
p = p32; /* Compliant */
p16 = p; /* Non-compliant */
p = ( void * ) p16; /* Compliant */
p32 = ( uint32_t * ) p; /* Non-compliant */
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
#include <stdint.h>
void example ( void )
{
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
void *p;
uint32_t u;
p = ( void * ) 0x1234u; /* Non-compliant */
p = ( void * ) 1024.0f; /* Non-compliant */
u = ( uint32_t ) p; /* 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 ----------------- */
int16_t *p;
float32_t f;
f = ( float32_t ) p; /* Non-compliant */
p = ( int16_t * ) f; /* Non-compliant */
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
#include <stdint.h>
void example ( void )
{
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
uint16_t x;
uint16_t * const cpi = &x;
uint16_t * const *pcpi;
uint16_t * *ppi;
const uint16_t *pci;
volatile uint16_t *pvi;
uint16_t *pi;
pi = cpi; /* Compliant */
pi = ( uint16_t * ) pci; /* Non-compliant */
pi = ( uint16_t * ) pvi; /* Non-compliant */
ppi = ( uint16_t * * ) pcpi; /* Non-compliant */
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
#include <stdint.h>
void example ( void )
{
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
int32_t *p1 = 0; /* Non-compliant */
int32_t *p2 = ( void * ) 0; /* Compliant */
#define MY_NULL_1 0
#define MY_NULL_2 ( void * ) 0
if ( p1 == MY_NULL_1 ) /* Non-compliant */
{
}
if ( p2 == MY_NULL_2 ) /* Compliant */
{
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
void goodExample ( void )
{
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
#include <stddef.h>
extern void f ( uint8_t *p );
f ( NULL ); /* Compliant */
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
#include <stdint.h>
struct str
{
int8_t n;
};
void unaryOrPostfixOperator ( void )
{
struct str *a[ 3 ];
int8_t i, *p, x, y;
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
a[ i ]->n; /* Compliant */
*p++; /* Compliant */
sizeof x + y; /* Non-compliant */
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
void samePrecedenceLevel ( void )
{
int8_t a, b, c, d;
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
a + b; /* Compliant */
a + b + c; /* Compliant */
( a + b ) + c; /* Compliant */
a + ( b + c ); /* Compliant */
a + b - c + d; /* Compliant */
( a + b ) - ( c + d ); /* Compliant */
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
extern int8_t f ( int8_t a, int8_t b );
void mixedOperator ( void )
{
int8_t x, a, b, c;
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
x = f ( a + b, c ); /* Compliant */
x = a == b ? a : a - b; /* Non-compliant */
x = ( a == b ) ? a : ( a - b ); /* Compliant */
x = a << ( b + c ); /* Compliant */
if ( a && b && c ) /* Compliant */
{
}
#if defined ( X ) && ( ( X + Y ) > Z ) /* Compliant */
#endif
#if !defined ( X ) && defined ( Y ) /* Compliant */
#endif
x = a, b; /* Compliant */
/* ------------------ 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