MISRA C まとめ #include
どうやって MISRA Example Suiteをコンパイルするか
C codingのための背景知識
C言語(C++)の理解、溶解、爽快。仮説・検証(173)
MISRA C断片コンパイル用ヘッダ
misra_c.h
/* Author, (c) Dr. Ogawa Kiyoshi*/
/* ver 1.0 January 1, 1999 */
/* ver 2.0 Feburary 2, 2005 */
/* ver 3.0 March 3, 2013 */
/* @kaizen_nagoya, researchmap.jp/kaizen/MISRA-C/ */
/* Purpose: macro, definition and/or information to MISRA-C Examples.*/
/* https://gcc.gnu.org/onlinedocs/gcc/Standards.html */
/* https://gcc.gnu.org/c99status.html */
/* http://www.polyomino.org.uk/computer/c/ */
#ifndef __MISRA_C__
#define __MISRA_C__
#include <float.h>
#include <iso646.h>
#include <limits.h>
#include <stdarg.h>
#include <stdbool.h> /* define true */
#include <stddef.h> /* define NULL */
#include <stdint.h> /* C99: define int16_t */
#ifdef __STDC_VERSION__
#include <stdalign.h>
#include <stdnoreturn.h>
#endif
#include <stdio.h>
// #include <sys/types.h>
typedef bool bool_t;
typedef float float32_t; /* dir4.6 */
typedef long double float64_t; /* dir4.6 */
#ifdef nofreestanding
#include <string.h>
#include <stdlib.h>
#endif
#ifndef DIR4_6
#ifndef __STDC_VERSION__
typedef char char_t;
typedef unsigned char uint8_t; /* dir4.6 */
typedef unsigned short uint16_t; /* dir4.6 */
/*typedef unsigned long uint32_t;*/ /* dir4.6 */
typedef unsigned long long uint64_t; /* dir4.6 */
typedef unsigned long long uint128_t;
typedef signed char int8_t; /* dir4.6 */
typedef signed short int16_t; /* dir4.6 */
/* typedef signed long int32_t; *//* dir4.6 */
typedef signed long long int64_t; /* dir4.6 */
typedef signed long long int128_t;
typedef float float32_t; /* dir4.6 */
typedef long double float64_t; /* dir4.6 */
typedef long double float128_t; /*dir4.6 */
#endif
#endif
#ifndef NC30 /* without renesas NC30WA, manual C.3.3 predefined macro */
#define __ISO_IEC_9899_1999__
#define __int32bit__
#define PR1x(a,b) (void)printf(" "#a " = %" #b "=%x\n", a,a)
#define PR2x(a,b,c) (void)printf(" "#a " = %" #c "=%x \n " #b " = %" #c "=%x\n", a,a,b,b)
#define PR3x(a,b,c,d) PR1x(a,d),PR2x(b,c,d);
#define PR1(a,b) (void)printf(" "#a " = %" #b "\n", a)
#define PR2(a,b,c) (void)printf(" "#a " = %" #c "\n " #b " = %" #c "\n", a, b)
#define PR3(a,b,c,d) PR1(a,d),PR2(b,c,d);
#else /* for renesas NC30W only */
#define __ISO_IEC_9899_1990__
#define __int16bit__
#define PR1(a,b)
#define PR2(a,b,c)
#define PR3(a,b,c,d)
#endif /*__STANDARD_IO__ */
#ifdef __ISO_IEC_9899_2011__
#include <stdarg.h>
#include <stdint.h>
#define PRSV() printf("ISO/IEC 9899:2011\n")
#elseif __ISO_IEC_9899_1999__
#include <stdarg.h>
#include <stdint.h>
#define PRSV() printf("ISO/IEC 9899:1999"\n)
#elseif __ISO_IEC_9899_1990__
/* #include <stdint.h> */ /*nc30wa is not supported */
typedef _bool bool_t;
typedef signed char int8_t;
typedef signed short int16_t;
typedef signed int int32_t;
typedef signed long int64_t;
typedef signed long long int128_t;
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long uint64_t;
typedef unsigned long long uint128_t;
typedef float float32_t;
typedef double float64_t;
typedef long double float128_t;
/* macro using printf debug or not.*/
#define PRSV() printf("ISO/IEC 9899:1990\n")
#elseif __int16bit__
typedef unsigned short uint16_t;
typedef unsigned long uint32_t;
typedef unsigned long uint64_t;
typedef unsigned long uint128_t;
#define PRSV() printf("no ISO/IEC 9899,16bit\n")
#else
/* typedef unsigned long long uint128_t;*/
#define PRSV() printf("no ISO/IEC 9899,32bit\n")
#endif
/* for each example*/
#ifdef DIR4_8
typedef struct OpaqueType *pOpaqueType;
pOpaqueType GetObject(void);
void UseObject(pOpaqueType);
#endif
#ifdef __MISRAC_6_6__
void f (uint16_t *p){
printf("uint16_t *p = %d, p = %d\n",*p,(int)p);
}
#endif /*__MISRAC_6_6__ */
#ifdef __MISRAC_6_9__
//typedef signed char int8_t;
//typedef signed short int16_t;
//typedef signed int int32_t;
#ifndef _INT64_T
#define _INT64_T
typedef signed long int64_t;
#endif
#ifndef _bool
typedef unsigned int _bool;
#endif
#endif
#ifdef __MISRAC_DIR_4_1__
#include <string.h>
#include <stdbool.h>
typedef float float32_t;
#endif
#ifdef DIR4_13
typedef struct mutex mutex_t;
struct mutex
{
unsigned int number;
unsigned char* name;
}mutex;
mutex_t n;
mutex_t mutex_lock (void)
{
return n;
}
void mutex_unlock (mutex_t m)
{
m.number = 0;
m.name = (unsigned char *)"";
return;
}
int16_t x;
#endif
#ifdef __MISRAC_RULE_1_1__
#define __zpage // zero page, 0x0000
#define __near // short address
#define __far // full address
#define zpage __zpage
int i = 1;
#endif
#ifdef R2_1
int error_handler (void){
return true;
}
#endif
#ifdef __MISRAC_RULE_2_2__
volatile uint16_t v=1;
char a[]={"123456789ABCDEF"};
char *p=a;
#endif
#ifdef __MISRAC_RULE_3_2__
bool_t b=0;
#endif
#ifdef __MISRAC_RULE_5_1__
int32_t ABC = 1;
#endif
#ifdef __MISRAC_RULE_5_2__
int32_t engine_exhaust_gas_temperature_raw=0;
#endif
#ifdef __MISRAC_RULE_5_3__
struct astruct{
int16_t m;
};
void g (struct astruct *p){printf("struct astruct *p->m = %d\n",p->m);}
#endif
#ifdef __MISRAC_RULE_5_8__
extern void foo (void);
#endif
#ifdef __MISRAC_RULE_5_9__
extern void bar1(void) ;
extern void bar2(void);
#endif
#ifdef __MISRAC_RULE_7_1__
uint16_t code[ 10 ];
#endif
#ifdef __MISRAC_RULE_7_4__
void f1 (char * s1){
PR1(s1,s);
}
void f2(const char *s2){
PR1(s2,s);
}
#endif
#ifdef __MISRAC_RULE_8_1__
xn = 0;
int16_t xc = 1;
fn(void){
return printf("fn(void);\n");
}
int16_t fc(void){
return printf("int16_t fc(void);\n");
}
void gn(char c, const k){
PR2(c,k,d);
}
void gc(char c, const int16_t k){
PR2(c,k,d);
}
#endif
#ifdef __MISRAC_RULE_8_2__
#endif
#ifdef R8_4
extern uint16_t var1; // 宣言
extern void func1( uint16_t var1 );
extern void func2( uint16_t var1 );
#endif
#ifdef __MISRAC_RULE_8_4__
extern int16_t count;
extern void func1 (void);
extern void func2 (int16_t x, int16_t y);
extern void func3 (int16_t x, int16_t y);
#endif
#ifdef __MISRAC_RULE_8_5__
extern int16_t a;
#endif
#ifdef __MISRAC_RULE_8_6__
void f1(void);
void f2(void);
#endif
#ifdef __MISRAC_RULE_8_11__
int32_t array1[10]={0,1,2,3,4,5,6,7,8,9};
int32_t array2[]={9,8,7,6,5,4,3,2,1,0};
#endif
#ifdef __MISRAC_RULE_8_13__
char last_char (const char * const s);
// uint16_t first( const uint16_t a[5]);
#endif
#ifdef __MISRAC_8_14__
volatile bool_t b=true;
struct s{
uint16_t count;
uint16_t a[ 10 ];
};
struct s sps = {
0,
{0,1,2,3,4,5,6,7,8,9}
};
struct s * sp;
char *p="Control Statement Expressions\n";
#endif
#ifdef R11_1
void f(int16_t n){
// printf("%d\n",n);
}
#endif
#ifdef __MISRAC_RULE_11_1__
void f(int16_t n){
PR1(n,d);
}
#endif
#ifdef R11_2
void f(int16_t n){
// printf("%d\n",n);
}
#endif
#ifdef R11_3
uint32_t read_value ( void ){
return true;
}
void print ( uint32_t n ){
PR1(n,d);
}
#endif
#ifdef __MISRAC_RULE_11_3__
uint32_t read_value ( void ){
return true;
}
void print ( uint32_t n ){
PR1(n,d);
}
#endif
#ifdef R11_4
void print ( uint16_t n ){
PR1(n,d);
}
#endif
#ifdef __MISRAC_RULE_11_9__
void f ( uint8_t *p ){
if (NULL == p){
PR1(NULL,d);
} else {
PR2(*p,NULL,d);
}
}
#endif
#ifdef __MISRAC_RULE_12_1__
int8_t f(int8_t a, int8_t b){
return a+b;
}
#endif
#ifdef __MISRAC_RULE_12_3__
void f(int8_t a, int8_t b){
PR2(a,b,d);
}
#endif
#ifdef __MISRAC_RULE_13_1__
void p ( uint16_t a[ 2 ] ){
PR2(a[0],a[1],d);
}
#endif
#ifdef __MISRAC_RULE_13_2__
#endif
#ifdef R14_1
uint32_t read_u32(void){
static uint32_t i = 14;
return --i;
}
float32_t read_float32(void){
static float32_t f = 0;
return ++f;
}
#endif
#ifdef __MISRAC_RULE_14_4__
bool_t flag= true;
#endif
#ifdef __MISRAC_RULE_17_3__
double power (double d, int n){
return (double)d*n;
}
#endif
#ifdef R17_6
#define TEN 10U
uint16_t v1[TEN]={0,1,2,3,4,5,6,7,8,9};
uint16_t v2[TEN+TEN]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
uint16_t v3[TEN+TEN+TEN]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30};
uint16_t t2(uint16_t n, uint16_t a[20]) {
uint16_t i;
uint16_t sum=0U;
for(i = 0U; i < n; ++i){
sum = sum + a[i];
}
return sum;
}
uint16_t t(uint16_t n, uint16_t a[]) {
uint16_t i;
uint16_t sum=0U;
for(i = 0U; i < n; ++i){
sum = sum + a[i];
}
return sum;
}
#endif
#ifdef __MISRAC_RULE_17_6__
uint16_t v1[10]={0,1,2,3,4,5,6,7,8,9};
uint16_t v2[20]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
#endif
#ifdef __MISRAC_C_2_1_
#ifndef __STANDARD_IO__ /* without renesas NC30W */
#define PR(a,b,c,d,e) printf("%" #a " + %" #a "= %" #a ", %" #b "\n", c, d, c + d, e)
#define PRC(a,b,c,d,e,f) printf("%" #a" + %" #a " = %" #a ", %" #b "\n", c, d, (f) c + d, e)
#define PRT(a,b,c,d,e,f) PR(a,b, c, d, e); e = c + d;PR(a,b, c, d, e); e = (f) c + d;/*printf(#e"= %llu ",e)*/;PRC(a,a, c, d, e, f);PRC(a,b, c, d, e, f)
#else /* for renesas NC30W only */
#define PR(a,b,c,d,e)
#define PRC(a,b,c,d,e,f)
#define PRT(a,b,c,d,e,f)
#endif /*__STANDARD_IO__ */
#endif /* __MISRAC_C_2_1_ */
#endif /* __MISRAC__ */