Code Style C - Wayne777Chiu/Chinese_Practice_about_TianoCore GitHub Wiki

(2019/03/08)

這只是一個 用來我們專案的 C 編碼風格 概論。官方的 C 編碼標準規範可以於此找到:

Table of Contents

概論

  • 駝峰式大小寫用於變數 (Variables), 函式(funcitons) 和 檔案名稱
  • 全大寫 (UPPERCASE) 用於 型態(types) 和 巨集 (macros)
  • 優先使用 UEFI 的型態 (types) 勝於 C 的型態
    • int=>INTN; unsigned int=>UINTN; void=>VOID; 等等...
  • 行長度限定在 80 字元 (characters) 以內
  • 兩個空格 (spaces) 的縮排
  • 不使用 tab 字元。
    • 設定編輯器優先插入空格 (spaeces) 而不是 tab 字元
  • if, for, while, 等總是使用 { },即使只有一個敘述
    • 左大括弧 ({) 應該總是出現在前一行的尾端。
  • 對函式 (function) 來說,左大括弧 ({) 應該總是各自出現在新的一行。

適當格式化的原始碼的例子

C 檔例子:

 /**
   Brief and Detailed Descriptions.
 
   @param[in]      Arg1 Description of Arg1.
   @param[in]      Arg2 Description of Arg2, which is optional.
   @param[out]     Arg3 Description of Arg3.
   @param[in, out] Arg4 Description of Arg4.
 
   @retval EFI_SUCCESS   Description of what EFI_SUCCESS means.
   @retval !EFI_SUCCESS  Failure.
 
 **/
 EFI_STATUS
 EFIAPI
 FooName (
   IN     UINTN  Arg1,
   IN     UINTN  Arg2 OPTIONAL,
      OUT UINTN  *Arg3,
   IN OUT UINTN  *Arg4
   )
 {
   UINTN Local;
   UINTN AnotherLocal;
 
   ...
 
   for (Local = 0; Local < 5; Local++) {
     if (Local == 2) {
       Print (L"Local: %d (yes! 2)\n", Local);
     } else {
       Print (L"Local: %d\n", Local);
     }
   }
 
   ...
 }

H 檔例子:

 #define FOO_MACRO(a, b) ((a) * (b))
 
 #define FOO_CONSTANT  0xcafe
 
 /**
   Brief and Detailed Descriptions.
 
   @param[in]      Arg1 Description of Arg1.
   @param[in]      Arg2 Description of Arg2, which is optional.
   @param[out]     Arg3 Description of Arg3.
   @param[in, out] Arg4 Description of Arg4.
 
   @retval EFI_SUCCESS   Description of what EFI_SUCCESS means.
   @retval !EFI_SUCCESS  Failure.
 
 **/
 EFI_STATUS
 EFIAPI
 FooName (
   IN     UINTN  Arg1,
   IN     UINTN  Arg2 OPTIONAL,
      OUT UINTN  *Arg3,
   IN OUT UINTN  *Arg4
   );
 
 ...
 
 #endif

例子

駝峰式大小寫(CamelCase)

用於變數 (Variables), 函式(funcitons) 和 檔案名稱

Correct:

 #include "FooFileName.h"
 ...
 VOID SuperFunction (
 ...
   UINTN ALocalVariable;
   UINTN UefiVersion;

Incorrect:

 #include "foo-file_name.h"
 ...
 VOID superFunction (
 ...
   UINTN a_local_variable;
   UINTN UEFIVersion;

全大寫(UPPERCASE)

用於 型態(types) 和 巨集 (macros)

Correct:

 #define FOO_MACRO(a, b) ((a) * (b))
 typedef struct _STRUCT_NAME STRUCT_NAME;
 struct _STRUCT_NAME {
   ...
 };

Incorrect:

 #define FooMacro(a, b) ((a) * (b))
 #define Foo_Macro(a, b) ((a) * (b))
 typedef struct _Struct_Name StructName;

使用 UEFI 型態 (types)

不直接使用 C 型態 (C types)

Correct:

   INTN   ALocalVariable;
   UINTN  UefiVersion;
   VOID   *Ptr;

Incorrect:

   int           ALocalVariable;
   unsigned int  UefiVersion;
   void          *Ptr;

兩個空格 (spaces) 的縮排

Correct:

   if (TRUE) {
     Print (L"Hello, world!\n");
   }

Incorrect:

   if (TRUE) {
       Print (L"Hello, world!\n");
   }

總是使用 { }

 if, for, while,  等總是使用 { },即使只有一個敘述

Correct:

   if (TRUE) {
     Print (L"Hello, world!\n");
   }

Incorrect:

   if (TRUE)
     Print (L"Hello, world!\n");

原文

This is just a brief overview of the C Code Style used for our projects. The official C Coding Standards specification can be found at:

Table of Contents

Overview

  • CamelCase used for variables, functions and file names
  • UPPERCASE used for types and macros
  • Use UEFI types rather than C types
    • int=>INTN; unsigned int=>UINTN; void=>VOID; etc...
  • Limit line length to 80 characters
  • 2 spaces of indentation
  • Never use tab characters.
    • Set editor to insert spaces rather than a tab character.
  • if, for, while, etc. always use { }, even when there is only one statement
    • The opening brace ({) should always appear at the end of the line previous line.
  • The opening brace ({) for a function should always appear separately on the a new line.

Example of Properly Formatted Code

C File Example:

 /**
   Brief and Detailed Descriptions.
 
   @param[in]      Arg1 Description of Arg1.
   @param[in]      Arg2 Description of Arg2, which is optional.
   @param[out]     Arg3 Description of Arg3.
   @param[in, out] Arg4 Description of Arg4.
 
   @retval EFI_SUCCESS   Description of what EFI_SUCCESS means.
   @retval !EFI_SUCCESS  Failure.
 
 **/
 EFI_STATUS
 EFIAPI
 FooName (
   IN     UINTN  Arg1,
   IN     UINTN  Arg2 OPTIONAL,
      OUT UINTN  *Arg3,
   IN OUT UINTN  *Arg4
   )
 {
   UINTN Local;
   UINTN AnotherLocal;
 
   ...
 
   for (Local = 0; Local < 5; Local++) {
     if (Local == 2) {
       Print (L"Local: %d (yes! 2)\n", Local);
     } else {
       Print (L"Local: %d\n", Local);
     }
   }
 
   ...
 }

H File Example:

 #define FOO_MACRO(a, b) ((a) * (b))
 
 #define FOO_CONSTANT  0xcafe
 
 /**
   Brief and Detailed Descriptions.
 
   @param[in]      Arg1 Description of Arg1.
   @param[in]      Arg2 Description of Arg2, which is optional.
   @param[out]     Arg3 Description of Arg3.
   @param[in, out] Arg4 Description of Arg4.
 
   @retval EFI_SUCCESS   Description of what EFI_SUCCESS means.
   @retval !EFI_SUCCESS  Failure.
 
 **/
 EFI_STATUS
 EFIAPI
 FooName (
   IN     UINTN  Arg1,
   IN     UINTN  Arg2 OPTIONAL,
      OUT UINTN  *Arg3,
   IN OUT UINTN  *Arg4
   );
 
 ...
 
 #endif

Examples

CamelCase

Used for variables, functions and file names

Correct:

 #include "FooFileName.h"
 ...
 VOID SuperFunction (
 ...
   UINTN ALocalVariable;
   UINTN UefiVersion;

Incorrect:

 #include "foo-file_name.h"
 ...
 VOID superFunction (
 ...
   UINTN a_local_variable;
   UINTN UEFIVersion;

UPPERCASE

Used for types and macros

Correct:

 #define FOO_MACRO(a, b) ((a) * (b))
 typedef struct _STRUCT_NAME STRUCT_NAME;
 struct _STRUCT_NAME {
   ...
 };

Incorrect:

 #define FooMacro(a, b) ((a) * (b))
 #define Foo_Macro(a, b) ((a) * (b))
 typedef struct _Struct_Name StructName;

Use UEFI types

Don't use C types directly

Correct:

   INTN   ALocalVariable;
   UINTN  UefiVersion;
   VOID   *Ptr;

Incorrect:

   int           ALocalVariable;
   unsigned int  UefiVersion;
   void          *Ptr;

2 spaces of indentation

Correct:

   if (TRUE) {
     Print (L"Hello, world!\n");
   }

Incorrect:

   if (TRUE) {
       Print (L"Hello, world!\n");
   }

Always use { }

If, for, while, etc. always use { }, even when there is only one statement

Correct:

   if (TRUE) {
     Print (L"Hello, world!\n");
   }

Incorrect:

   if (TRUE)
     Print (L"Hello, world!\n");
⚠️ **GitHub.com Fallback** ⚠️