




已阅读5页,还剩10页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
MISRAC2004 一、开发环境(Environment) 二、语言外延(LanguageExtensions) 三、注释(Documentation) 四、字符集(CharacterSets) 五、标识符(Identifiers) 六、类型(Types) 七、常量(Constants) 八、声明和定义(DeclarationsandDefinitions) 九、初始化(Initialisation) 十、算术类型转换(ArithmeticTypeConversion) 十一、指针类型转换(PointTypeConversion) 十二、表达式(Expressions) 十三、控制表达式(ControlStatementExpressions) 十四、控制流(ControlFlow) 十五、Switch语句(SwitchStatements) 十六、函数(Functions) 十七、指针和数组(PointersandArrays) 十八、结构体和联合体(StructuresandUnions) 十九、预处理命令(PreprocessingDirectives) 二十、标准库(StandardLibraries) 二十一、运行失败(Run-TimeFailures) 一、开发环境(Environment) 1.1AllcodeshallconformtoISO9899:1990“ProgramminglanguagesC”,amendedandcorrectedbyISO/IEC9899/COR1:1995,ISO/IEC9899/AMD1:1995,andISO/IEC9899/COR2:1996. 翻译: 1.2Norelianceshallbeplacedonundefinedorunspecifiedbehaviour. 翻译: 1.3Multiplecompilersand/orlanguagesshallonlybeusedifthereisacommondefinedinterfacestandardforobjectcodetowhichthelanguages/compilers/assemblersconform. 翻译: 1.4Thecompiler/linkershallbecheckedtoensurethat31charactersignificanceandcasesensitivityaresupportedforexternalidentifiers. 翻译: 1.5Floating-pointimplementationsshouldcomplywithadefinedfloating-pointstandard. 翻译: 二、语言外延(LanguageExtensions) 2.1Assemblylanguageshallbeencapsulatedandisolated. 翻译: 2.2Sourcecodeshallonlyuse/*/stylecomments. 翻译: 2.3Thecharactersequence/*shallnotbeusedwithinacomment. 翻译: 2.4Sectionsofcodeshouldnotbe“commentedout”. 翻译: 三、注释(Documentation) 3.1Allusageofimplementation-definedbehaviourshallbedocumented. 翻译: 3.2Thecharactersetandthecorrespondingencodingshallbedocumented. 翻译: 3.3Theimplementationofintegerdivisioninthechosencompilershouldbedetermined,documentedandtakenintoaccount. 翻译: 3.4Allusesofthe#pragmadirectiveshallbedocumentedandexplained. 翻译: 3.5Ifitisbeingreliedupon,theimplementationdefinedbehaviourandpackingofbitfieldsshallbedocumented. 翻译: 3.6Alllibrariesusedinproductioncodeshallbewrittentocomplywiththeprovisionsofthisdocument,andshallhavebeensubjecttoappropriatevalidation. 翻译: 四、字符集(CharacterSets) 4.1OnlythoseescapesequencesthataredefinedintheISOCstandardshallbeused. 翻译: 4.2Trigraphsshallnotbeused. 翻译: 五、标识符(Identifiers) 5.1Identifiers(internalandexternal)shallnotrelyonthesignificanceofmorethan31characters. 翻译: 5.2Identifiersinaninnerscopeshallnotusethesamenameasanidentifierinanouterscope,andthereforehidethatidentifier. 翻译: 5.3Atypedefnameshallbeauniqueidentifier. 翻译: 5.4Atagnameshallbeauniqueidentifier. 翻译: 5.5Noobjectorfunctionidentifierwithstaticstoragedurationshouldbereused. 翻译: 5.6Noidentifierinonenamespaceshouldhavethesamespellingasanidentifierinanothernamespace,withtheexceptionofstructureandunionmembernames. 翻译: 5.7Noidentifiernameshouldbereused. 翻译: 六、类型(Types) 6.1Theplainchartypeshallbeusedonlyforthestorageanduseofcharactervalues. 翻译: 6.2Signedandunsignedchartypeshallbeusedonlyforthestorageanduseofnumericvalues. 翻译: 6.3Typedefsthatindicatesizeandsignednessshouldbeusedinplaceofthebasictypes. 翻译: 6.4Bitfieldsshallonlybedefinedtobeoftypeunsignedintorsignedint. 翻译: 6.5Bitfieldsoftypesignedintshallbeatleast2bitslong. 翻译: 七、常量(Constants) 7.1Octalconstants(otherthanzero)andoctalescapesequencesshallnotbeused. 八、声明和定义(DeclarationsandDefinitions) 8.1Functionsshallhaveprototypedeclarationsandtheprototypeshallbevisibleatboththefunctiondefinitionandcall. 翻译: 8.2Wheneveranobjectorfunctionisdeclaredordefined,itstypeshallbeexplicitlystated. 翻译: 8.3Foreachfunctionparameterthetypegiveninthedeclarationanddefinitionshallbeidentical,andthereturntypesshallalsobeidentical. 翻译: 8.4Ifobjectsorfunctionsaredeclaredmorethanoncetheirtypesshallbecompatible. 翻译: 8.5Thereshallbenodefinitionsofobjectsorfunctionsinaheaderfile. 翻译: 8.6Functionsshallbedeclaredatfilescope. 翻译: 8.7Objectsshallbedefinedatblockscopeiftheyareonlyaccessedfromwithinasinglefunction. 翻译: 8.8Anexternalobjectorfunctionshallbedeclaredinoneandonlyonefile. 翻译: 8.9Anidentifierwithexternallinkageshallhaveexactlyoneexternaldefinition. 翻译: 8.10Alldeclarationsanddefinitionsofobjectsorfunctionsatfilescopeshallhaveinternallinkageunlessexternallinkageisrequired. 翻译: 8.11Thestaticstorageclassspecifiershallbeusedindefinitionsanddeclarationsofobjectsandfunctionsthathaveinternallinkage. 翻译: 8.12Whenanarrayisdeclaredwithexternallinkage,itssizeshallbestatedexplicitlyordefinedimplicitlybyinitialisation. 翻译: 九、初始化(Initialisation) 9.1Allautomaticvariablesshallhavebeenassignedavaluebeforebeingused. 翻译: 9.2Bracesshallbeusedtoindicateandmatchthestructureinthenon-zeroinitialisationofarraysandstructures. 翻译: 9.3Inanenumeratorlist,the“=”constructshallnotbeusedtoexplicitlyinitialisemembersotherthanthefirst,unlessallitemsareexplicitlyInitialised. 翻译: 十、算术类型转换(ArithmeticTypeConversion) 10.1Thevalueofanexpressionofintegertypeshallnotbeimplicitlyconvertedtoadifferentunderlyingtypeif: a)itisnotaconversiontoawiderintegertypeofthesamesignedness,or b)theexpressioniscomplex,or c)theexpressionisnotconstantandisafunctionargument,or d)theexpressionisnotconstantandisareturnexpression 翻译: 10.2Thevalueofanexpressionoffloatingtypeshallnotbeimplicitlyconvertedtoadifferenttypeif: a)itisnotaconversiontoawiderfloatingtype,or b)theexpressioniscomplex,or c)theexpressionisafunctionargument,or d)theexpressionisareturnexpression 翻译: 10.3Thevalueofacomplexexpressionofintegertypemayonlybecasttoatypethatisnarrowerandofthesamesignednessastheunderlyingtypeoftheexpression. 翻译: 10.4Thevalueofacomplexexpressionoffloatingtypemayonlybecasttoanarrowerfloatingtype. 翻译: 10.5Ifthebitwiseoperatorsand,=,=shallnotbeappliedtopointertypesexceptwheretheypointtothesamearray. 翻译: 17.4Arrayindexingshallbetheonlyallowedformofpointerarithmetic. 翻译: 17.5Thedeclarationofobjectsshouldcontainnomorethan2levelsofpointerindirection. 翻译: 17.6Theaddressofanobjectwithautomaticstorageshallnotbeassignedtoanotherobjectthatmaypersistafterthefirstobjecthasceasedtoexist. 翻译: 十八、结构体和联合体(StructuresandUnions) 18.1Allstructureanduniontypesshallbecompleteattheendofatranslationunit. 翻译: 18.2Anobjectshallnotbeassignedtoanoverlappingobject. 翻译: 18.3Anareaofmemoryshallnotbereusedforunrelatedpurposes. 翻译: 18.4Unionsshallnotbeused. 翻译: 十九、预处理命令(PreprocessingDirectives) 19.1#includestatementsinafileshouldonlybeprecededbyotherpreprocessordirectivesorcomments. 翻译: 19.2Non-standardcharactersshouldnotoccurinheaderfilenamesin#includedirectives. 翻译: 19.3The#includedirectiveshallbefollowedbyeitheraorfilenamesequence. 翻译: 19.4Cmacrosshallonlyexpandtoabracedinitialiser,aconstant,aparenthesizedexpression,atypequalifier,astorageclassspecifier,orado-while-zeroconstruct. 翻译: 19.5Macrosshallnotbe#definedor#undefdwithinablock. 翻译: 19.6#undefshallnotbeused. 翻译: 19.7Afunctionshouldbeusedinpreferencetoafunction-likemacro. 翻译: 19.8Afunction-likemacroshallnotbeinvokedwithoutallofitsarguments. 翻译: 19.9Argumentstoafunction-likemacroshallnotcontaintokensthatlooklikepreprocessingdirectives. 翻译: 19.10Inthedefinitionofafunction-likemacroeachinstanceofaparametershallbeenclosedinparenthesesunlessitisusedastheoperandof#or#. 翻译: 19.11Allmacroidentifiersinpreprocessordirectivesshallbedefinedbeforeuse,exceptin#ifdefand#ifndefpreprocessordirectivesandthedefined()operator. 翻译: 19.12Thereshallbeatmostoneoccurrenceofthe#or#operatorsinasinglemacrodefinition. 翻译: 19.13The#and#operatorsshouldnotbeused. 翻译: 19.14Thedefinedpreprocessoroperatorshallonlybeusedinoneofthetwostandardforms. 翻译; 19.15Precautionsshallbetakeninordertopreventthecontentsofaheaderfilebeingincludedtwice. 翻译: 19.16Preprocessingdirectivesshallbesyntacticallymeaningfulevenwhenexcludedbythepreprocessor. 翻译: 19.17All#else,#elifand#endifpreprocessordirectivesshallresideinthesamefileasthe#ifor#ifdefdirectivetowhichtheyarerelated. 翻译: 二十、标准库(StandardLibraries) 20.1Reservedidentifiers,macrosandfunctionsinthestandardlibrary,shallnotbedefined,redefinedorundefined. 翻译: 20.2Thenamesofstandardlibrarymacros,objectsandfunctionsshallnotbereused. 翻译: 20.3Thevalidityofvaluespassedtolibraryfunctionsshallbechecked. 翻译: 20.4Dynamicheapmemoryallocationshallnotbeused. 翻译: 20.5Theerrorindicatorerrnoshallnotbeused. 翻译: 20.6Themacrooffsetof,inlibrary,shallnotbeused. 翻译: 20.7Thesetjmpmacroandthelongjmpfunctionshallnotbeused. 翻译: 20.8Thesignalhandlingfacilitiesofshallnotbeused. 翻译: 20.9Theinput/outputlibraryshallnotbeusedinproductioncode. 翻译: 20.10Thelibraryfunctionsatof,atoiandatolfromlibraryshallnotbeused. 翻译: 20.11Thelibraryfunctionsabort,exit,getenvandsystemfromlibraryshallnotbeused. 翻译; 20.12Thetimehandlingfunctionsoflibraryshallnotbeused. 翻译: 二十一、运行失败(Run-TimeFailures) 21.1Minimisationofrun-timefailuresshallbeensuredbytheuseofatleastoneof a)staticanalysistools/techniques; b)dynamicanalysistools/techniques; c)explicitcodingofcheckstohandlerun-timefaults. MISRA2004概览MISRA是汽车工业C语言编程指导,是嵌入式C语言的编程指导,目前在各个行业,包括:航空,航天,船舶,电信,医疗等行业广泛采纳,是最优秀的嵌入式C语言标准,在MISRA1998版的基础上,最新发布的MISRA2004有一下改进: 121条强制遵守,20条建议遵守l 复杂规则细化l 模糊规则明确l 引入一些数学操作的规则l 规则分类包括: 环境l 语言扩展l 文档化l 字符集l 标识符l 类型l 约束l 声明和定义l 初始化l 数学类型转换l 指针类型转换l 表达式l 控制语句表达式l 控制流l Swith语句l 函数l 指针和数组l 结构和联合l 预处理指令l 标准库l 运行时失效l 下面概要的介绍MISRA2004中的一些规则: Rule1.1(强制):所有的代码应该遵守ISO9899:1990“ProgrammingLanguageC” Rule1.2(强制):只有当具备统一接口的目标代码的时候才可以采用多种编译器和语言 Rule1.4(强制)检查编译器/连接器以确保支持31一个有效字符,支持大小写敏感 Rule2.1(强制):汇编语言应该封装起来并且隔离: 例如:#defineNOPasm(“NOP”) Rule2.2(强制):源代码只能采用/*/风格的注释 Rule2.3(强制):字符序列/*不能在注释中使用 注:C语言不支持注释的嵌套即使一些编译器支持这个语言扩展 Rule2.4(建议):代码段不能注释掉 注:应采用#IF或者#ifdef来构成一个注释,否则代码里如果有注释会改变代码的作用 Rule3.3(建议):编译器对于整数除法运算的实施应该写入文档 例编译器:-5/3=-1余-2有些编译器结果是-2于1 Rule4.1(强制):只能使用ISO标准定义的字符集 Rule6.5(强制):在内部范围的标识符不能和外部的标识符用同样的名字,因为会隐藏那个标识符 例: Int16_ti: Voidf() int16_tI; i=3; Rule5.2(强制):typedef名称只能唯一,不能重复定义 Rule5.4(强制):标记名应该是唯一的标识符 Rule5.7(建议):标识符不能重复使用 Rule6.1(强制):Char类型只能用来存储使用字符 Rule6.2(强制):signed和unsignedchar只能用来存储和使用数据值 Rule6.3(建议)对于基本的类型使用Typedef来表示大小和有无符号 例: Typedefcharchar_t Typedefsignedintint32_t Rule7.1(强制):不要用八进制数 注:整型常数以”0“开始会被认为是8进制 例:code1=109 code2=100 code3=052 code4=071 如果是对总线消息初始化,会有危险 Rule8.1(强制):函数都应该有原型声明,且相对函数定义和调用可见 Rule8.2(强制):无论何时一个对象和函数声明或者定义,它的类型应该明确声明 Rule8.5(强制):头文件中不要定义对象或者函数 Rule8.3(强制):每个函数声明中的参数的类型应该和定义中的类型一致 Rule8.8(强制):外部变量或者函数只能声明在一个文件中 注:一般来讲,声明在头文件中,然后包含在定义和使用的文件中 Rule8.12(强制):数组声明为外部,应该明确声明大小或者直接初始化确定 例:externintarray2/*违反Rule8.8*/ Rule9.1(强制):所有变量在使用之前都应该赋值 Rule10.1(强制):整型表达式
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论