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

main

parents
#include <stdint.h>
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
void f ( int16_t n )
{
uint16_t val[ n ]; /* Non-compliant */
}
void g ( void )
{
f ( 0 );
f ( -1 );
f ( 10 );
}
void h ( uint16_t n,
uint16_t a[ 10 ][ n ] ) /* Non-compliant */
{
uint16_t ( *p )[ 20 ];
p = a;
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
#include <stdint.h>
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
void fn ( void )
{
union
{
int16_t i;
int32_t j;
} a = { 0 }, b = { 1 };
a.j = a.i; /* Non-compliant */
a = b; /* Compliant */
}
#include <string.h>
int16_t a[ 20 ];
void f ( void )
{
memcpy ( &a[ 5 ], &a[ 4 ], 2u * sizeof ( a[ 0 ] ) ); /* Non-compliant */
}
void g ( void )
{
int16_t *p = &a[ 0 ];
int16_t *q = &a[ 0 ];
*p = *q; /* Compliant */
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
#include <stdint.h>
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
uint32_t zext ( uint16_t s )
{
union /* Non-compliant */
{
uint32_t ul;
uint16_t us;
} tmp;
tmp.us = s;
return tmp.ul;
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
// Example code from MISRA C:2012 begin
enum light { red, amber, red_amber, green };
enum light next_light ( enum light c )
{
enum light res;
switch ( c )
{
case red:
res = red_amber;
break;
case red_amber:
res = green;
break;
case green:
res = amber;
break;
case amber:
res = red;
break;
default:
{
/*
* This default will only be reachable if the parameter c
* holds a value that is not a member of enum light.
*/
error_handler ( );
break;
}
}
return res;
res = c; /* Non-compliant - this statement is
* certainly unreachable */
}
// Example code from MISRA C:2012 end
#include "../include/typedefs.h"
// Example code from MISRA C:2012 begin
extern volatile uint16_t v;
extern char *p;
void f ( void )
{
uint16_t x;
( void ) v; /* Compliant - v is accessed for its side effect
* and the cast to void is permitted
* by exception */
( int32_t ) v; /* Non-compliant - the cast operator is dead */
v >> 3; /* Non-compliant - the >> operator is dead */
x = 3; /* Non-compliant - the = operator is dead
* - x is not subsequently read */
*p++; /* Non-compliant - result of * operator is not used */
( *p )++; /* Compliant - *p is incremented */
}
/* Compliant */
__asm ( "NOP" );
void g ( void )
{
/* Compliant - there are no operations in this function */
}
void h ( void )
{
g ( ); /* Non-compliant - the call could be removed */
}
// Example code from MISRA C:2012 end
#include "../include/typedefs.h"
// Example code from MISRA C:2012 begin
int16_t unusedtype ( void )
{
typedef int16_t local_Type; /* Non-compliant */
return 67;
}
// Example code from MISRA C:2012 end
#include "../include/typedefs.h"
// Example code from MISRA C:2012 begin
void unusedtag ( void )
{
enum sate { s_init, s_run, s_sleep }; /* Non-compliant */
}
typedef struct record_t /* Non-compliant */
{
uint16_t key;
uint16_t val;
} record1_t;
typedef struct /* Compliant */
{
uint16_t key;
uint16_t val;
} record2_t;
// Example code from MISRA C:2012 end
// Example code from MISRA C:2012 begin
void use_macro ( void )
{
#define SIZE 4
/* Non-compliant - DATA not used */
#define DATA 3
use_int16 ( SIZE );
}
// Example code from MISRA C:2012 end
#include "../include/typedefs.h"
// Example code from MISRA C:2012 begin
void unused_label ( void )
{
int16_t x = 6;
label: /* Non-compliant */
use_int16 ( x );
}
// Example code from MISRA C:2012 end
#include "../include/typedefs.h"
// Example code from MISRA C:2012 begin
void withunusedpara ( uint16_t *para1,
int16_t unusedpara ) /* Non-compliant - unused */
{
*para1 = 42U;
}
// Example code from MISRA C:2012 end
#include <stdint.h>
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
int16_t
#include "f.h" /* Non-compliant */
/* ------------------ Example code from MISRA C:2012 end ------------------ */
#include <stdint.h>
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
#define F1_MACRO
#include "f1.h" /* compliant */
#include "f2.h" /* compliant */
int32_t i = 0;
#include "f3.h" /* Non-compliant */
/* ------------------ Example code from MISRA C:2012 end ------------------ */
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
xyz = 0;
/* ------------------ Example code from MISRA C:2012 end ------------------ */
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
#define A( x ) #x /* Compliant */
#define B( x, y ) x ## y /* Compliant */
#define C( x, y ) #x ## y /* Non-compliant */
/* ------------------ Example code from MISRA C:2012 end ------------------ */
#include <stdint.h>
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
#define AA 0xffff
#define BB( x ) ( x ) + wow ## x /* Non-compliant */
void f ( void )
{
int32_t wowAA = 0;
wowAA = BB ( AA );
}
int32_t speed;
int32_t speed_scale;
int32_t scaled_speed;
#define SCALE( X ) ( ( X ) * X ## _scale ) /* Compliant */
scaled_speed = SCALE ( speed );
/* ------------------ Example code from MISRA C:2012 end ------------------ */
#include <stdint.h>
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
#define AAA 2
int32_t foo ( void )
{
int32_t x = 0;
#ifndef AAA
x = 1;
#else1 /* Non-compliant */
x = AAA;
#endif
return x;
}
/* Compliant */
/*
#start is not a token in a comment
*/
/* ------------------ Example code from MISRA C:2012 end ------------------ */
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
#ifdef A /* Compliant */
#include "file1.h"
#endif
/* ------------------ 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