SW – C Coding Standard P730006L56 1 Maturity

Released

SV I IS SW Process

DO/signature
Process Owner

Check/signature(s)
Arno Semmelroth SV I IS RD SW Date: 14-Mar-05 e-Sign ListID: 28392 Process Manager

Release/signature
Process Executive

Transmittal, reproduction, dissemination and/or editing of this document as well as utilization of its contents and communication there of to others without express authorization are prohibited. Offenders will be held liable for payment of damages. All rights created by patent grant or registration of a utility model or design patent are reserved.

CJ.Bonetto SV I IS RD PTQ Date: 10-Mar-05 e-Sign ListID: 28392

Frans Bosch SV I IS RD PTQ Date: 10-Mar-05 e-Sign ListID: 28392

Bernd Ruthemeyer SV I IS O PMC Date: 23-Mar-05 e-Sign ListID: 28392

2 Title: 3 Purpose: This document defines C Coding Standard for the development of SW embedded in Automotive Entertainment Systems and delivered to the customer. 4 Scope (validity area & date, replaced documents): This Standard is valid for all C code, whether or not produced by subcontractors or development partners under contract of Siemens VDO. Nevertheless they are not covering all aspects of necessary or good coding. They need local and/or project specific supplements. These rules add to existing ones and take precedence in case of differences or contradictions. 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

SW – C Coding Standard

Table of contents
1 Process ................................................................................................................................................... 2 1.1 Introduction ......................................................................................................................................... 2 1.2 C Coding Guidelines ........................................................................................................................... 2 2 Metrics ..................................................................................................................................................... 4 3 Further explanations .............................................................................................................................. 4 3.1.1 International Character Sets ............................................................................................................ 4 3.1.2 POSIX .............................................................................................................................................. 4 3.1.3 Microprocessor interfaces ................................................................................................................ 4 4 Terminology, Definitions and Abbreviations ...................................................................................... 5 5 Related documents ................................................................................................................................ 5 5.1 Related upstream documents ............................................................................................................. 5 5.2 Further related documents .................................................................................................................. 5 5.3 Source of figures at this procedure ..................................................................................................... 5 6 Teams ...................................................................................................................................................... 6 6.1 Procedure Definition Team ................................................................................................................. 6 6.2 Review Team ...................................................................................................................................... 6 6.3 CCB Team .......................................................................................................................................... 6 6.4 Distribution List.................................................................................................................................... 7 6.5 History of Changes.............................................................................................................................. 7

Copyright © by Siemens AG 2004 All rights reserved

1/7

For internal use only Printout from G+O portal

SW – C Coding Standard P730006L56

SV I IS SW Process

1 2 3
Transmittal, reproduction, dissemination and/or editing of this document as well as utilization of its contents and communication there of to others without express authorization are prohibited. Offenders will be held liable for payment of damages. All rights created by patent grant or registration of a utility model or design patent are reserved.

1 Process
SV I IS SW Coding Rules & language independent Code Metrics are described in [U-1]. C Specific Coding Guidelines are described here.

4 5 6 7 8 9 10 11 12 13

1.1 Introduction
MISRA has defined "Guidelines for the use of the C language in vehicle based software". HIS has defined a subset of these coding guidelines. This subset can be seen as industry standard for C coding guidelines in automotive industry. For more information see [R-1] The following subset of MISRA Coding Guidelines for C is the SV I IS standard for C Coding Guidelines.

1.2 C Coding Guidelines
Both "shall" and "should" describe mandatory requirements. The rule numbers refer to the MISRA standard's rule numbering. Mandatory Subset of MISRA Coding Guidelines as defined by HIS Description Only those escape sequences that are defined in the ISO C standard shall be used Trigraphs shall not be used Comments shall not be nested The basic type of char, int, short, long, float or double should not be used, but specific-length should be typedef'd for the specific compiler, and these type names used in the code. The type char shall always be declared as unsigned char or signed char The underlying bit representation of floating-point numbers shall not be used in any way by the programmer Typedef names shall not be reused. Octal constants (other than zero) shall not be used. Identifiers in an inner scope shall not use the same name as an identifier in the outer scope and therefore hide that identifier. Declaration of objects should be at function scope unless a wider scope is necessary. All declarations at file scope should be static where possible. An identifier with external linkage shall have exactly one external definition. If objects or functions are declared more than once they shall have compatible declarations. External objects should not be declared in more than one file. The use of a tag shall agree with its declaration. All automatic variables shall be assigned a value before being used. Braces shall be used to indicate and match the structure in non-zero initializations of arrays and structures. In an enumerator list, the "=" construct shall not be used to explicitly initialize members other than the first, unless all items are explicitly initialized. The right hand operator of a logical && or || operator shall not contain side effects. The operands of a logical && or || shall be primary expressions. Assignment operators shall not be used in expressions which return Boolean values. Bitwise operations shall not be performed on signed integer types.

Rule # 5 7 9 13 14 16 17 19 21 22 23 25 26 27 29 30 31 32 33 34 35 37

Copyright © by Siemens AG 2004 All rights reserved

2/7

For internal use only Printout from G+O portal

SW – C Coding Standard P730006L56 38 39 40 43 45 46 48 50 52 56 59 61 62 64 65 68 69 70 71 75 76 78 81 83

SV I IS SW Process

85 87 88 89 91 94 95 96 98 99 100

The right-hand operator of a shift operator shall lie between zero and one less than the width in bits of the left-hand operator (inclusive). The unary minus operator shall not be applied to an unsigned expression. The sizeof-operator should not be used on expressions that contain side effects. Implicit conversions which may result in a loss of information shall not be used. Type casting from any type to and from pointers shall not be used. The value of an expression shall be the same under any order of evaluation the standard permits. Mixed precision arithmetic should use explicit casting to generate the desired result. Floating point variables shall not be tested for explicit equality and inequality. There shall be no unreachable code. The goto statement shall not be used. The statement forming the body of an if, else if, else, while, do… while or for statement shall always be enclosed in braces. Every non-empty clause in a switch-statement shall be terminated with a break-statement. All switch-statements should contain a final default-clause. Every switch-statement shall have at least one case. Floating point variables shall not be used as loop counters. Functions shall always be declared on file scope. Functions with variable number of arguments shall not be used. Functions shall not call themselves, either directly or indirectly. Functions shall always have prototype declarations and the prototype shall be visible at both the function definition and call. Every function shall have an explicit return type. Functions with no parameters shall be declared with parameter type void. The number of parameters passed to a function shall match the function prototype. Const qualification should be used on function parameters which are passed by reference, where it is intended that the function will not modify that parameter. For functions with a non-void return type i) there shall be one return statement for every exit branch (including the end of program) ii) each return shall have an expression iii) the return expression shall match the declared return type. Functions called with no parameters should have empty parenthesis. #include-statements in a file shall only be preceded by other preprocessor directives or by comments. Non-standard characters shall not occur in header file names in #include-directives. The #include-directives shall be followed either by a <filename> or by a "filename" sequence. Macros shall not be #define'd and #undef'd within a block. A function-like macro shall not be "called" without all of its arguments. Arguments to a function-like macro shall not contain tokens that look like preprocessordirectives. In the definition of a function macro, the whole definition and each instance of a parameter shall be enclosed in brackets. There shall be at most one definition of the #- and ##-preprocessor operators in a single macro definition. All uses of the #pragma-directive shall be documented and explained. The "defined" preprocessor-operators shall only be used in one of the two standard forms.

Transmittal, reproduction, dissemination and/or editing of this document as well as utilization of its contents and communication there of to others without express authorization are prohibited. Offenders will be held liable for payment of damages. All rights created by patent grant or registration of a utility model or design patent are reserved.

Copyright © by Siemens AG 2004 All rights reserved

3/7

For internal use only Printout from G+O portal

SW – C Coding Standard P730006L56 102 103 106 108
Transmittal, reproduction, dissemination and/or editing of this document as well as utilization of its contents and communication there of to others without express authorization are prohibited. Offenders will be held liable for payment of damages. All rights created by patent grant or registration of a utility model or design patent are reserved.

SV I IS SW Process

109 112 114 115 118 119 120 122 123 124 125 126 127 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

No more than 2 levels of pointer indirection should be used. Relational operators shall not be applied to pointer types except where both operands are of the same type and point to the same array, structure or union. The address of an object with automatic storage shall not be assigned to an object which may persist after the object has ceased to exist. In the specification of a structure or union-type, all members of the structure or union shall be fully specified. Overlapping variable storage shall not be used. Bit fields of type signed int shall be at least 2 bits long. Reserved words or standard library function names shall not be redefined or undefined. Standard library function names shall not be reused. Dynamic heap memory allocation shall not be used. The error indicator errno shall not be used. The macro offsetof in library <stddef.h> shall not be used. The setjmp macro and the longjmp function shall not be used. The signal handling facilities of <signal.h> shall not be used. The input/output library <stdio.h> shall not be used in production code. The library function atof, atoi and toll from library <stdlib.h> shall not be used. The library function abort, exit, getenv and system from library <stdlib.h> shall not be used. The time handling functions from library <time.h> shall not be used.

2 Metrics
Use metrics as defined in the document [R-2].

3 Further explanations
According to VW, every single violation of a rule has to be documented. The following exceptions apply for SV I IS Code:

3.1.1 International Character Sets
For international (e.g. far-east) versions it may be necessary to deal with Unicode character sets.

3.1.2 POSIX
Third-party defined operating system interfaces and relating standards (e.g. POSIX) may violate several rules. They may also lead to necessary violations within newly created source code e.g. necessary casts between types declared in the software and types used in the interface.

3.1.3 Microprocessor interfaces
(From the MISRA Rules) Source code written to interface with the microprocessor hardware will inevitably require the use of proprietary extensions to the language.

Copyright © by Siemens AG 2004 All rights reserved

4/7

For internal use only Printout from G+O portal

SW – C Coding Standard P730006L56

SV I IS SW Process

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

4 Terminology, Definitions and Abbreviations
Use SV I IS common Glossary of Abbreviations, Terms & Definitions – P300002L02. For additional definitions or abbreviations use CR for Glossary - P300002F02. Acronyms and Abbreviations Specific acronyms / abbreviations, used only in this document: HIS Herstellerinitiative Software (Manufacturer Initiative Software) http://www.automotivehis.de/ MISRA Motor Industry Software Reliability Association Definitions Specific Definitions, used only in this document: Trigraph (from C language) – any combination of two question marks with another character, such as??! Which is interpreted as a single | (bitwise or)

Transmittal, reproduction, dissemination and/or editing of this document as well as utilization of its contents and communication there of to others without express authorization are prohibited. Offenders will be held liable for payment of damages. All rights created by patent grant or registration of a utility model or design patent are reserved.

5 Related documents
5.1 Related upstream documents
[U-1] F730006LAF SW Coding Procedure, SV I IS

5.2 Further related documents
[R-1] [R-2] MISRA C Guideline, can be found on Intranet G+O portal P730006LAY Measurement Procedure, SV I IS

5.3 Source of figures at this procedure
[F-1] …

Copyright © by Siemens AG 2004 All rights reserved

5/7

For internal use only Printout from G+O portal

SW – C Coding Standard P730006L56

SV I IS SW Process

1 2

6 Teams
6.1 Procedure Definition Team
Name Christian-Jacques Bonetto Department SV I IS RD PTQ Location SAP

Transmittal, reproduction, dissemination and/or editing of this document as well as utilization of its contents and communication there of to others without express authorization are prohibited. Offenders will be held liable for payment of damages. All rights created by patent grant or registration of a utility model or design patent are reserved.

3 4

6.2 Review Team
Name Arno Semmelroth Daniel Crane Norbert Burghardt Gerhard Gries King Young Lee Luc Mordacq Reiner Seitz Jean-Marc Soyez Jean-Pierre Arragon Wolfgang Bauer Bernhard Klein Philippe Besson Steffen Zehner Michael Fischer Evelyne Demarchez Hans Hermans Ralf Löffert Uwe Schilling Peter Schless Alwin Raaf Bernd Ruthemeyer Department SV I IS RD SW SV I IS RD SW HSV SV I IS RD SW WET SV I IS RD SW MCH SV I IS RD SW SGP SV I IS RD SW RBT SV I IS RD SW TSR SV I IS RD SW SAP SV I IS TLA SV I IS TLA SV I IS TLA SV I IS Radio SV I IS CO ARN SV I IS CO CMM SV I IS CO FRW SV I IS CO RRN SV I IS CO SSW SV I IS CP SV I IS RD PTQ SV I IS O PMC SV I IS O PMC Location WET HSV WET MCH SGP RBT TSR SAP RBT MCH RBG RBT RBG WET SAP EHV WET WET MCH WET RBG

5 6

6.3 CCB Team
Name Arno Semmelroth Peter Schless Frans Bosch Department SV I IS RD SW SV I IS RD PTQ SV I IS RD PTQ EHV Location WET MCH EHV

7 8

Copyright © by Siemens AG 2004 All rights reserved

6/7

For internal use only Printout from G+O portal

SW – C Coding Standard P730006L56 1

SV I IS SW Process

6.4 Distribution List
Intranet SV I IS G+O portal

2 3
Transmittal, reproduction, dissemination and/or editing of this document as well as utilization of its contents and communication there of to others without express authorization are prohibited. Offenders will be held liable for payment of damages. All rights created by patent grant or registration of a utility model or design patent are reserved.

6.5 History of Changes
At project start the latest version of this document has to be used. The latest version is available on Intranet. Version Status Date Name, Department Changes <dd-Mmm-YY> (e.g. CR-number)
a01 Draft 27-Jul-04 Rikowski, SV I IS RD SW Adapted VW Coding Guidelines to become general guidelines, adapted to ISQP Template, added scope chapter Updates after first review of some specialists. Update after general review. Update after re-review requested during general review Remove everything but C guidelines Rename requested during general review. Transition to MISRA-C 20004 started (1 rule reworked, some other removed). New template

a02 a03 a04 b01 b02

Draft Reviewed Reviewed Reviewed Released

29-July-04 19-Aug-04 15-Sep-04 31-Jan-05 04-Mar-05

Rikowski, SV I IS RD SW Rikowski, SV I IS RD SW Rikowski, SV I IS RD SW CJ.Bonetto – SV I IS RD PTQ CJ.Bonetto – SV I IS RD PTQ

4

Copyright © by Siemens AG 2004 All rights reserved

7/7

For internal use only Printout from G+O portal

Sign up to vote on this title
UsefulNot useful