Code Style C - Wayne777Chiu/Chinese_Practice_about_TianoCore GitHub Wiki
(2019/03/08)
這只是一個 用來我們專案的 C 編碼風格 概論。官方的 C 編碼標準規範可以於此找到:
- 駝峰式大小寫用於變數 (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
用於變數 (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;
用於 型態(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;
不直接使用 C 型態 (C types)
Correct:
INTN ALocalVariable; UINTN UefiVersion; VOID *Ptr;
Incorrect:
int ALocalVariable; unsigned int UefiVersion; void *Ptr;
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:
- 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.
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
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;
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;
Don't use C types directly
Correct:
INTN ALocalVariable; UINTN UefiVersion; VOID *Ptr;
Incorrect:
int ALocalVariable; unsigned int UefiVersion; void *Ptr;
Correct:
if (TRUE) { Print (L"Hello, world!\n"); }
Incorrect:
if (TRUE) { Print (L"Hello, world!\n"); }
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");