From f2b15ce3f5e266c033c2c8a93444c558d1bb3bb1 Mon Sep 17 00:00:00 2001 From: Michael Kubacki Date: Tue, 3 Feb 2026 13:26:43 -0500 Subject: [PATCH] FatPkg: Replace include guards with #pragma once Replace traditional `#ifndef`/`#define`/`#endif` include guards with `#pragma` once. `#pragma once` is a widely supported preprocessor directive that prevents header files from being included multiple times. It is supported by all toolchains used to build edk2: GCC, Clang/LLVM, and MSVC. Compared to macro-based include guards, `#pragma once`: - Eliminates the risk of macro name collisions or copy/paste errors where two headers inadvertently use the same guard macro. - Eliminate inconsistency in the way include guard macros are named (e.g., some files use `__FILE_H__`, others use `FILE_H_`, etc.). - Reduces boilerplate (three lines replaced by one). - Avoids polluting the macro namespace with guard symbols. - Can improve build times as the preprocessor can skip re-opening the file entirely, rather than re-reading it to find the matching `#endif` ("multiple-include optimization"). - Note that some compilers may already optimize traditional include guards, by recognzining the idiomatic pattern. This change is made acknowledging that overall portability of the code will technically be reduced, as `#pragma once` is not part of the C/C++ standards. However, this is considered acceptable given: 1. edk2 already defines a subset of supported compilers in BaseTools/Conf/tools_def.template, all of which have supported `#pragma once` for over two decades. 2. There have been concerns raised to the project about inconsistent include guard naming and potential macro collisions. Approximate compiler support dates: - MSVC: Supported since Visual C++ 4.2 (1996) - GCC: Supported since 3.4 (2004) (http://gnu.ist.utl.pt/software/gcc/gcc-3.4/changes.html) - Clang (LLVM based): Since initial release in 2007 Signed-off-by: Michael Kubacki --- FatPkg/EnhancedFatDxe/Fat.h | 5 +---- FatPkg/EnhancedFatDxe/FatFileSystem.h | 5 +---- FatPkg/FatPei/FatLiteApi.h | 5 +---- FatPkg/FatPei/FatLiteFmt.h | 5 +---- FatPkg/FatPei/FatLitePeim.h | 5 +---- 5 files changed, 5 insertions(+), 20 deletions(-) diff --git a/FatPkg/EnhancedFatDxe/Fat.h b/FatPkg/EnhancedFatDxe/Fat.h index fb6699061e..1e739af82e 100644 --- a/FatPkg/EnhancedFatDxe/Fat.h +++ b/FatPkg/EnhancedFatDxe/Fat.h @@ -6,8 +6,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent **/ -#ifndef _FAT_H_ -#define _FAT_H_ +#pragma once #include @@ -2030,5 +2029,3 @@ extern EFI_COMPONENT_NAME2_PROTOCOL gFatComponentName2; extern EFI_LOCK FatFsLock; extern EFI_LOCK FatTaskLock; extern EFI_FILE_PROTOCOL FatFileInterface; - -#endif diff --git a/FatPkg/EnhancedFatDxe/FatFileSystem.h b/FatPkg/EnhancedFatDxe/FatFileSystem.h index 60b9c56b71..e490e510c8 100644 --- a/FatPkg/EnhancedFatDxe/FatFileSystem.h +++ b/FatPkg/EnhancedFatDxe/FatFileSystem.h @@ -7,8 +7,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent **/ -#ifndef _FATFILESYSTEM_H_ -#define _FATFILESYSTEM_H_ +#pragma once #pragma pack(1) // @@ -203,5 +202,3 @@ typedef struct { } FAT_DIRECTORY_LFN; #pragma pack() - -#endif diff --git a/FatPkg/FatPei/FatLiteApi.h b/FatPkg/FatPei/FatLiteApi.h index a9025633a0..904a2fd68b 100644 --- a/FatPkg/FatPei/FatLiteApi.h +++ b/FatPkg/FatPei/FatLiteApi.h @@ -7,8 +7,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent **/ -#ifndef _FAT_API_H_ -#define _FAT_API_H_ +#pragma once // // API data structures @@ -21,5 +20,3 @@ typedef enum { Fat32, FatUnknown } PEI_FAT_TYPE; - -#endif diff --git a/FatPkg/FatPei/FatLiteFmt.h b/FatPkg/FatPei/FatLiteFmt.h index fef70fe1de..7a99323fa5 100644 --- a/FatPkg/FatPei/FatLiteFmt.h +++ b/FatPkg/FatPei/FatLiteFmt.h @@ -7,8 +7,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent **/ -#ifndef _FAT_FMT_H_ -#define _FAT_FMT_H_ +#pragma once // // Definitions @@ -130,5 +129,3 @@ typedef struct { } PEI_FAT_BOOT_SECTOR_EX; #pragma pack() - -#endif diff --git a/FatPkg/FatPei/FatLitePeim.h b/FatPkg/FatPei/FatLitePeim.h index 1f90ef885d..79e6d1682e 100644 --- a/FatPkg/FatPei/FatLitePeim.h +++ b/FatPkg/FatPei/FatLitePeim.h @@ -7,8 +7,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent **/ -#ifndef _FAT_PEIM_H_ -#define _FAT_PEIM_H_ +#pragma once #include @@ -495,5 +494,3 @@ VOID FatFindPartitions ( IN PEI_FAT_PRIVATE_DATA *PrivateData ); - -#endif // _FAT_PEIM_H_