C coding style

The coding style of mcdecoder in C is based on Google C++ Style Guide. The mcdecoder adds some modifications to it because it is for C++ and some codes in mcdecoder are generated by a tool.

Exceptions to the rule

Naming

For type names, constant names, function names and enumerator names:

This project

You can use underscores to split the namespace part, the base part and the generated part: <namespace>_<base>_<generated>. The namespace part and the generated part are optional.

Examples:

  • Type: DecodeRequest, arm_DecodeResult, DecodeRequest_add, riscv_DecodeResult_push, etc.

  • Constant: kInstructionIdMax, arm_kDecodeErrorsMax, kInstructionIdMax_add, riscv_kDecodeErrorsMax_push, etc.

  • Function: DecodeInstruction, arm_PrintInstruction, DecodeInstruction_add, riscv_PrintInstruction_push, etc.

  • Enumeration type: InstructionId, arm_DecodeErrors, InstructionId_add, riscv_DecodeErrors_push, etc.

  • Enumerator: InstructionId_kUnknown, arm_DecodeErrors_kOk, InstructionId_k_add, riscv_DecodeErrors_k_push, etc.

Original

With no underscores.

Rationale

There are the following reasons to support this.

  • C language doesn’t support namespaces, so we use the namespace prefix to separate the namespaces among symbols.

  • To clarify the boundary between the fixed part and the generated part of a type name, we put underscore symbol between them.

You can see also the original ‘Naming’.

Naming / Enumerator Names

This project

Use an enumeration type name prefix in an enumerator name: <enumeration_type>_<enumerator>.

Examples: InstructionId_kUnknown, DecodeErrors_kOk, etc.

Original

Enumerators are named with a leading “k”.

Rationale

C language doesn’t support enumeration type qualifier to specify an enumerator, so we use an enumeration type prefix to correct the ambiguity among enumeration types.

You can see also the original ‘Naming / Enumerator Names’.

Formatting / Line length

This project

127 characters is the maximum.

Original

80 characters is the maximum.

Rationale

80 characters are too short and make it code vertically longer and less readable in my opinion.

You can see also the original ‘Formatting / Line length’.

Addition to the rule

C Version

Rule

Code should target C11, i.e., should not use C18 features.

Rationale

To be portable to environments supporting C11 and above.

Comments / Comment Style

Rule

Use /** */ in API comments and /* */ for others.

Rationale

To identify API comments from Doxygen.