MISRA C 工业标准的c编程规范.docx
《MISRA C 工业标准的c编程规范.docx》由会员分享,可在线阅读,更多相关《MISRA C 工业标准的c编程规范.docx(21页珍藏版)》请在冰豆网上搜索。
MISRAC工业标准的c编程规范
MISRA C 2004
一、开发环境(Environment)
二、语言外延(Language Extensions)
三、注释(Documentation)
四、字符集(Character Sets)
五、标识符(Identifiers)
六、类型(Types)
七、常量(Constants)
八、声明和定义(Declarations and Definitions)
九、初始化(Initialisation)
十、算术类型转换(Arithmetic Type Conversion)
十一、指针类型转换(Point Type Conversion)
十二、表达式(Expressions)
十三、控制表达式(Control Statement Expressions)
十四、控制流(Control Flow)
十五、Switch语句(Switch Statements)
十六、函数(Functions)
十七、指针和数组(Pointers and Arrays)
十八、结构体和联合体(Structures and Unions)
十九、预处理命令(Preprocessing Directives)
二十、标准库(Standard Libraries)
二十一、运行失败(Run-Time Failures)
一、开发环境(Environment)
1.1 All code shall conform to ISO 9899:
1990“Programming languages – C”, amended and corrected by ISO/IEC 9899/COR1:
1995, ISO/IEC 9899/AMD1:
1995, and ISO/IEC 9899/COR2:
1996.
翻译:
1.2 No reliance shall be placed on undefined or unspecified behaviour.
翻译:
1.3 Multiple compilers and/or languages shall only be used if there is a common defined interface standard for object code to which the languages/compilers/assemblers conform.
翻译:
1.4 The compiler/linker shall be checked to ensure that 31 character significance and case sensitivity are supported for external identifiers.
翻译:
1.5 Floating-point implementations should comply with a defined floating-point standard.
翻译:
二、语言外延(Language Extensions)
2.1 Assembly language shall be encapsulated and isolated.
翻译:
2.2 Source code shall only use /* … */ style comments.
翻译:
2.3 The character sequence /* shall not be used within a comment.
翻译:
2.4 Sections of code should not be “commented out”.
翻译:
三、注释(Documentation)
3.1 All usage of implementation-defined behaviour shall be documented.
翻译:
3.2 The character set and the corresponding encoding shall be documented.
翻译:
3.3 The implementation of integer division in the chosen compiler should be determined, documented and taken into account.
翻译:
3.4 All uses of the #pragma directive shall be documented and explained.
翻译:
3.5 If it is being relied upon, the implementation defined behaviour and packing of bitfields shall be documented.
翻译:
3.6 All libraries used in production code shall be written to comply with the provisions of this document, and shall have been subject to appropriate validation.
翻译:
四、字符集(Character Sets)
4.1 Only those escape sequences that are defined in the ISO C standard shall be used.
翻译:
4.2 Trigraphs shall not be used.
翻译:
五、标识符(Identifiers)
5.1 Identifiers (internal and external) shall not rely on the significance of more than 31 characters.
翻译:
5.2 Identifiers in an inner scope shall not use the same name as an identifier in an outer scope, and therefore hide that identifier.
翻译:
5.3 A typedef name shall be a unique identifier.
翻译:
5.4 A tag name shall be a unique identifier.
翻译:
5.5 No object or function identifier with static storage duration should be reused.
翻译:
5.6 No identifier in one name space should have the same spelling as an identifier in another name space, with the exception of structure and union member names.
翻译:
5.7 No identifier name should be reused.
翻译:
六、类型(Types)
6.1 The plain char type shall be used only for the storage and use of character values.
翻译:
6.2 Signed and unsigned char type shall be used only for the storage and use of numeric values.
翻译:
6.3 Typedefs that indicate size and signedness should be used in place of the basic types.
翻译:
6.4 Bit fields shall only be defined to be of type unsigned int or signed int.
翻译:
6.5 Bit fields of type signed int shall be at least 2 bits long.
翻译:
七、常量(Constants)
7.1 Octal constants (other than zero) and octal escape sequences shall not be used.
八、声明和定义(Declarations and Definitions)
8.1 Functions shall have prototype declarations and the prototype shall be visible at both the function definition and call.
翻译:
8.2 Whenever an object or function is declared or defined, its type shall be explicitly stated.
翻译:
8.3 For each function parameter the type given in the declaration and definition shall be identical, and the return types shall also be identical.
翻译:
8.4 If objects or functions are declared more than once their types shall be compatible.
翻译:
8.5 There shall be no definitions of objects or functions in a header file.
翻译:
8.6 Functions shall be declared at file scope.
翻译:
8.7 Objects shall be defined at block scope if they are only accessed from within a single function.
翻译:
8.8 An external object or function shall be declared in one and only one file.
翻译:
8.9 An identifier with external linkage shall have exactly one external definition.
翻译:
8.10 All declarations and definitions of objects or functions at file scope shall have internal linkage unless external linkage is required.
翻译:
8.11 The static storage class specifier shall be used in definitions and declarations of objects and functions that have internal linkage.
翻译:
8.12 When an array is declared with external linkage, its size shall be stated explicitly or defined implicitly by initialisation.
翻译:
九、初始化(Initialisation)
9.1 All automatic variables shall have been assigned a value before being used.
翻译:
9.2 Braces shall be used to indicate and match the structure in the non-zero initialisation of arrays and structures.
翻译:
9.3 In an enumerator list, the “=” construct shall not be used to explicitly initialise members other than the first, unless all items are explicitly Initialised.
翻译:
十、算术类型转换(Arithmetic Type Conversion)
10.1 The value of an expression of integer type shall not be implicitly converted to a different underlying type if:
a) it is not a conversion to a wider integer type of the same signedness, or
b) the expression is complex, or
c) the expression is not constant and is a function argument, or
d) the expression is not constant and is a return expression
翻译:
10.2 The value of an expression of floating type shall not be implicitly converted to a different type if:
a) it is not a conversion to a wider floating type, or
b) the expression is complex, or
c) the expression is a function argument, or
d) the expression is a return expression
翻译:
10.3 The value of a complex expression of integer type may only be cast to a type that is narrower and of the same signedness as the underlying type of the expression.
翻译:
10.4 The value of a complex expression of floating type may only be cast to a narrower floating type.
翻译:
10.5 If the bitwise operators ~ and << are applied to an operand of underlying type unsigned char or unsigned short, the result shall be immediately cast to the underlying type of the operand.
翻译:
10.6 A “U” suffix shall be applied to all constants of unsigned type.
翻译:
十一、指针类型转换(Point Type Conversion)
11.1 Conversions shall not be performed between a pointer to a function and any type other than an integral type.
翻译:
11.2 Conversions shall not be performed between a pointer to object and any type other than an integral type, another pointer to object type or a pointer to void.
翻译:
11.3 A cast should not be performed between a pointer type and an integral type.
翻译:
11.4 A cast should not be performed between a pointer to object type and a different pointer to object type.
翻译:
11.5 A cast shall not be performed that removes any const or volatile qualification from the type addressed by a pointer.
翻译:
十二、表达式(Expressions)
12.1 Limited dependence should be placed on C’s operator precedence rules in expressions.
翻译:
12.2 The value of an expression shall be the same under any order of evaluation that the standard permits.
翻译:
12.3 The sizeof operator shall not be used on expressions that contain side effects.
翻译:
12.4 The right hand operand of a logical && or || operator shall not contain side effects.
翻译:
12.5 The operands of a logical && or || shall be primary-expressions.
翻译:
12.6 The operands of logical operators (&&, || and !
) should be effectively Boolean. Expressions that are effectively Boolean should not be used as operands to operators other than (&&, || and !
).
翻译:
12.7 Bitwise operators shall not be applied to operands whose underlying type is signed.
翻译:
12.8 The right hand operand of a shift operator shall lie between zero and one less than the width in bits of the underlying type of the left hand operand.
翻译:
12.9 The unary minus operator shall not be applied to an expression whose underlying type is unsigned.
翻译:
12.10 The comma operator shall not be used.
翻译:
12.11 Evaluation of constant unsigned integer expressions should not lead to wrap-around.
翻译:
12.12 The underlying bit representations of floating-point values shall not be used.
翻译:
12.13 The increment (++) and decrement (--) operators should not be mixed with other operators in an expression.
翻译:
十三、控制表达式(Control Statement Expressions)
13.1 Assignment operators shall not be used in expressions that yield a Boolean value.
翻译:
13.2 Tests of a value against zero should be made explicit, unless the operand is effectively Boolean.
翻译:
13.3 Floating-point expressions shall not be tested for equality or inequality.
翻译:
13.4 The controlling expression of a for statement shall not contain any objects of floating type.
翻译:
13.5 The three expressions of a for statement shall be concerned only with loop control.
翻译:
13.6 Numeric variables being used within a for loop for iteration counting shall not be modified in the body of the loop.
翻译:
13.7 Boolean operations whose results are invariant shall not be permitted.
翻译:
十四、控制流(Control Flow)
14.1 There shall be no unreachable code.
翻译:
14.2 All non-null statements shall either:
a) have at least one side-effect however executed, or
b) cause control flow to change.
翻译:
14.3 Before preprocessing, a null statement shall only occur on a line by itself; it may be foll