From 0f42cae11c06187ef72f34cfabdca200346ef660 Mon Sep 17 00:00:00 2001 From: Michael Kubacki Date: Tue, 3 Feb 2026 12:33:10 -0500 Subject: [PATCH] CryptoPkg: 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. Does not include updates to OpenSSL generated header files checked into the repo. Those in `CryptoPkg\Library\OpensslLib\OpensslGen\`. 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 --- CryptoPkg/Include/Library/BaseCryptLib.h | 5 +---- CryptoPkg/Include/Library/HashApiLib.h | 5 +---- CryptoPkg/Include/Library/TlsLib.h | 5 +---- CryptoPkg/Include/Pcd/PcdCryptoServiceFamilyEnable.h | 5 +---- CryptoPkg/Library/BaseCryptLib/Hash/CryptParallelHash.h | 5 +---- CryptoPkg/Library/BaseCryptLib/InternalCryptLib.h | 5 +---- CryptoPkg/Library/BaseCryptLibMbedTls/InternalCryptLib.h | 5 +---- .../Library/BaseCryptLibMbedTls/Pk/CryptPkcs7Internal.h | 5 +---- CryptoPkg/Library/BaseCryptLibNull/InternalCryptLib.h | 5 +---- CryptoPkg/Library/Include/CrtLibSupport.h | 4 +--- CryptoPkg/Library/Include/stdint.h | 6 ++---- CryptoPkg/Library/TlsLib/InternalTlsLib.h | 5 +---- CryptoPkg/Library/TlsLibNull/InternalTlsLib.h | 5 +---- CryptoPkg/Private/Library/IntrinsicLib.h | 5 +---- CryptoPkg/Private/Library/MbedTlsLib.h | 5 +---- CryptoPkg/Private/Library/OpensslLib.h | 5 +---- CryptoPkg/Private/Ppi/Crypto.h | 5 +---- CryptoPkg/Private/Protocol/Crypto.h | 5 +---- CryptoPkg/Private/Protocol/SmmCrypto.h | 5 +---- .../Test/UnitTest/Library/BaseCryptLib/TestBaseCryptLib.h | 5 +---- 20 files changed, 21 insertions(+), 79 deletions(-) diff --git a/CryptoPkg/Include/Library/BaseCryptLib.h b/CryptoPkg/Include/Library/BaseCryptLib.h index 919ee70924..a3759ecb0c 100644 --- a/CryptoPkg/Include/Library/BaseCryptLib.h +++ b/CryptoPkg/Include/Library/BaseCryptLib.h @@ -10,8 +10,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent **/ -#ifndef __BASE_CRYPT_LIB_H__ -#define __BASE_CRYPT_LIB_H__ +#pragma once #include @@ -4302,5 +4301,3 @@ EcDsaVerify ( IN CONST UINT8 *Signature, IN UINTN SigSize ); - -#endif // __BASE_CRYPT_LIB_H__ diff --git a/CryptoPkg/Include/Library/HashApiLib.h b/CryptoPkg/Include/Library/HashApiLib.h index c2b88bd785..a19db28cd5 100644 --- a/CryptoPkg/Include/Library/HashApiLib.h +++ b/CryptoPkg/Include/Library/HashApiLib.h @@ -9,8 +9,7 @@ **/ -#ifndef __HASH_API_LIB_H_ -#define __HASH_API_LIB_H_ +#pragma once typedef VOID *HASH_API_CONTEXT; @@ -106,5 +105,3 @@ HashApiHashAll ( IN UINTN DataToHashLen, OUT UINT8 *Digest ); - -#endif diff --git a/CryptoPkg/Include/Library/TlsLib.h b/CryptoPkg/Include/Library/TlsLib.h index abacbe7fc9..cc45fe3e50 100644 --- a/CryptoPkg/Include/Library/TlsLib.h +++ b/CryptoPkg/Include/Library/TlsLib.h @@ -6,8 +6,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent **/ -#ifndef __TLS_LIB_H__ -#define __TLS_LIB_H__ +#pragma once /** Initializes the OpenSSL library. @@ -968,5 +967,3 @@ TlsGetExportKey ( OUT VOID *KeyBuffer, IN UINTN KeyBufferLen ); - -#endif // __TLS_LIB_H__ diff --git a/CryptoPkg/Include/Pcd/PcdCryptoServiceFamilyEnable.h b/CryptoPkg/Include/Pcd/PcdCryptoServiceFamilyEnable.h index 5e9287188c..b5b52b408c 100644 --- a/CryptoPkg/Include/Pcd/PcdCryptoServiceFamilyEnable.h +++ b/CryptoPkg/Include/Pcd/PcdCryptoServiceFamilyEnable.h @@ -28,8 +28,7 @@ **/ -#ifndef __PCD_CRYPTO_SERVICE_FAMILY_ENABLE_H__ -#define __PCD_CRYPTO_SERVICE_FAMILY_ENABLE_H__ +#pragma once /// /// Define used to enable all the crypto services in a family @@ -431,5 +430,3 @@ typedef struct { UINT32 Family; } Ec; } PCD_CRYPTO_SERVICE_FAMILY_ENABLE; - -#endif diff --git a/CryptoPkg/Library/BaseCryptLib/Hash/CryptParallelHash.h b/CryptoPkg/Library/BaseCryptLib/Hash/CryptParallelHash.h index e712ef36f7..246e2e0146 100644 --- a/CryptoPkg/Library/BaseCryptLib/Hash/CryptParallelHash.h +++ b/CryptoPkg/Library/BaseCryptLib/Hash/CryptParallelHash.h @@ -21,8 +21,7 @@ and related or neighboring rights to the source code in this file. http://creativecommons.org/publicdomain/zero/1.0/ **/ -#ifndef CRYPT_PARALLEL_HASH_H_ -#define CRYPT_PARALLEL_HASH_H_ +#pragma once #include "InternalCryptLib.h" @@ -228,5 +227,3 @@ EFIAPI DispatchBlockToAp ( VOID ); - -#endif // CRYPT_PARALLEL_HASH_H_ diff --git a/CryptoPkg/Library/BaseCryptLib/InternalCryptLib.h b/CryptoPkg/Library/BaseCryptLib/InternalCryptLib.h index 477e3ae5f1..24e5b2d97e 100644 --- a/CryptoPkg/Library/BaseCryptLib/InternalCryptLib.h +++ b/CryptoPkg/Library/BaseCryptLib/InternalCryptLib.h @@ -6,8 +6,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent **/ -#ifndef __INTERNAL_CRYPT_LIB_H__ -#define __INTERNAL_CRYPT_LIB_H__ +#pragma once #undef _WIN32 #undef _WIN64 @@ -60,5 +59,3 @@ WrapPkcs7Data ( OUT UINT8 **WrapData, OUT UINTN *WrapDataSize ); - -#endif diff --git a/CryptoPkg/Library/BaseCryptLibMbedTls/InternalCryptLib.h b/CryptoPkg/Library/BaseCryptLibMbedTls/InternalCryptLib.h index d3fa5ffc89..dcfa93ae58 100644 --- a/CryptoPkg/Library/BaseCryptLibMbedTls/InternalCryptLib.h +++ b/CryptoPkg/Library/BaseCryptLibMbedTls/InternalCryptLib.h @@ -6,8 +6,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent **/ -#ifndef INTERNAL_CRYPT_LIB_H_ -#define INTERNAL_CRYPT_LIB_H_ +#pragma once #include #include @@ -70,5 +69,3 @@ WrapPkcs7Data ( OUT UINT8 **WrapData, OUT UINTN *WrapDataSize ); - -#endif diff --git a/CryptoPkg/Library/BaseCryptLibMbedTls/Pk/CryptPkcs7Internal.h b/CryptoPkg/Library/BaseCryptLibMbedTls/Pk/CryptPkcs7Internal.h index 3f2cab37d2..b6d4aa0cfa 100644 --- a/CryptoPkg/Library/BaseCryptLibMbedTls/Pk/CryptPkcs7Internal.h +++ b/CryptoPkg/Library/BaseCryptLibMbedTls/Pk/CryptPkcs7Internal.h @@ -9,8 +9,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent **/ -#ifndef CRYPT_PKCS7_INTERNAL_H_ -#define CRYPT_PKCS7_INTERNAL_H_ +#pragma once #include "InternalCryptLib.h" @@ -85,5 +84,3 @@ typedef struct MbedtlsPkcs7 { else \ (g) += Ret; \ } while( 0 ) - -#endif diff --git a/CryptoPkg/Library/BaseCryptLibNull/InternalCryptLib.h b/CryptoPkg/Library/BaseCryptLibNull/InternalCryptLib.h index 96958592e1..4b3779e45d 100644 --- a/CryptoPkg/Library/BaseCryptLibNull/InternalCryptLib.h +++ b/CryptoPkg/Library/BaseCryptLibNull/InternalCryptLib.h @@ -6,11 +6,8 @@ SPDX-License-Identifier: BSD-2-Clause-Patent **/ -#ifndef __INTERNAL_CRYPT_LIB_H__ -#define __INTERNAL_CRYPT_LIB_H__ +#pragma once #include #include #include - -#endif diff --git a/CryptoPkg/Library/Include/CrtLibSupport.h b/CryptoPkg/Library/Include/CrtLibSupport.h index 613d418493..d7b104eac6 100644 --- a/CryptoPkg/Library/Include/CrtLibSupport.h +++ b/CryptoPkg/Library/Include/CrtLibSupport.h @@ -9,8 +9,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent **/ -#ifndef __CRT_LIB_SUPPORT_H__ -#define __CRT_LIB_SUPPORT_H__ +#pragma once #include #include @@ -485,4 +484,3 @@ strpbrk ( #else #define UINTPTR_MAX 0xFFFFFFFFUL #endif -#endif diff --git a/CryptoPkg/Library/Include/stdint.h b/CryptoPkg/Library/Include/stdint.h index 004c31b70f..63922f3565 100644 --- a/CryptoPkg/Library/Include/stdint.h +++ b/CryptoPkg/Library/Include/stdint.h @@ -6,8 +6,8 @@ SPDX-License-Identifier: BSD-2-Clause-Patent **/ -#ifndef CRYPTO_CRT_STDIO_H_ -#define CRYPTO_CRT_STDIO_H_ +#pragma once + #include // @@ -24,5 +24,3 @@ typedef INT64 int64_t; typedef UINT64 uint64_t; typedef UINTN uintptr_t; #endif - -#endif diff --git a/CryptoPkg/Library/TlsLib/InternalTlsLib.h b/CryptoPkg/Library/TlsLib/InternalTlsLib.h index bdc41325d4..d43fbc3203 100644 --- a/CryptoPkg/Library/TlsLib/InternalTlsLib.h +++ b/CryptoPkg/Library/TlsLib/InternalTlsLib.h @@ -6,8 +6,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent **/ -#ifndef __INTERNAL_TLS_LIB_H__ -#define __INTERNAL_TLS_LIB_H__ +#pragma once #undef _WIN32 #undef _WIN64 @@ -56,5 +55,3 @@ typedef struct { BIO *BioDebug; INT32 Ack; } TLS_EXT_CTX; - -#endif diff --git a/CryptoPkg/Library/TlsLibNull/InternalTlsLib.h b/CryptoPkg/Library/TlsLibNull/InternalTlsLib.h index 888c9066bf..92605ba8e1 100644 --- a/CryptoPkg/Library/TlsLibNull/InternalTlsLib.h +++ b/CryptoPkg/Library/TlsLibNull/InternalTlsLib.h @@ -6,11 +6,8 @@ SPDX-License-Identifier: BSD-2-Clause-Patent **/ -#ifndef __INTERNAL_TLS_LIB_NULL_H__ -#define __INTERNAL_TLS_LIB_NULL_H__ +#pragma once #include #include #include - -#endif diff --git a/CryptoPkg/Private/Library/IntrinsicLib.h b/CryptoPkg/Private/Library/IntrinsicLib.h index 69172a0419..1c0cd4f29e 100644 --- a/CryptoPkg/Private/Library/IntrinsicLib.h +++ b/CryptoPkg/Private/Library/IntrinsicLib.h @@ -6,11 +6,8 @@ **/ -#ifndef INTRINSTIC_LIB_H_ -#define INTRINSTIC_LIB_H_ +#pragma once // // Compiler dependent intrinsic APIs. // - -#endif diff --git a/CryptoPkg/Private/Library/MbedTlsLib.h b/CryptoPkg/Private/Library/MbedTlsLib.h index 30517a76d3..d1d4c65fc1 100644 --- a/CryptoPkg/Private/Library/MbedTlsLib.h +++ b/CryptoPkg/Private/Library/MbedTlsLib.h @@ -6,7 +6,4 @@ **/ -#ifndef MBEDTLS_LIB_H_ -#define MBEDTLS_LIB_H_ - -#endif +#pragma once diff --git a/CryptoPkg/Private/Library/OpensslLib.h b/CryptoPkg/Private/Library/OpensslLib.h index 005eb84872..aa2000edcd 100644 --- a/CryptoPkg/Private/Library/OpensslLib.h +++ b/CryptoPkg/Private/Library/OpensslLib.h @@ -6,9 +6,6 @@ **/ -#ifndef OPENSSL_LIB_H_ -#define OPENSSL_LIB_H_ +#pragma once #include - -#endif diff --git a/CryptoPkg/Private/Ppi/Crypto.h b/CryptoPkg/Private/Ppi/Crypto.h index ad5a524644..e18da41c81 100644 --- a/CryptoPkg/Private/Ppi/Crypto.h +++ b/CryptoPkg/Private/Ppi/Crypto.h @@ -6,8 +6,7 @@ **/ -#ifndef __EDKII_CRYPTO_PPI_H__ -#define __EDKII_CRYPTO_PPI_H__ +#pragma once #include @@ -17,5 +16,3 @@ typedef EDKII_CRYPTO_PROTOCOL EDKII_CRYPTO_PPI; extern GUID gEdkiiCryptoPpiGuid; - -#endif diff --git a/CryptoPkg/Private/Protocol/Crypto.h b/CryptoPkg/Private/Protocol/Crypto.h index 7d3ff548d6..b9f603bbe7 100644 --- a/CryptoPkg/Private/Protocol/Crypto.h +++ b/CryptoPkg/Private/Protocol/Crypto.h @@ -7,8 +7,7 @@ **/ -#ifndef __EDKII_CRYPTO_PROTOCOL_H__ -#define __EDKII_CRYPTO_PROTOCOL_H__ +#pragma once #include #include @@ -5754,5 +5753,3 @@ struct _EDKII_CRYPTO_PROTOCOL { }; extern GUID gEdkiiCryptoProtocolGuid; - -#endif diff --git a/CryptoPkg/Private/Protocol/SmmCrypto.h b/CryptoPkg/Private/Protocol/SmmCrypto.h index fec5a45523..7e69646b01 100644 --- a/CryptoPkg/Private/Protocol/SmmCrypto.h +++ b/CryptoPkg/Private/Protocol/SmmCrypto.h @@ -6,8 +6,7 @@ **/ -#ifndef __EDKII_SMM_CRYPTO_PROTOCOL_H__ -#define __EDKII_SMM_CRYPTO_PROTOCOL_H__ +#pragma once #include @@ -17,5 +16,3 @@ typedef EDKII_CRYPTO_PROTOCOL EDKII_SMM_CRYPTO_PROTOCOL; extern GUID gEdkiiSmmCryptoProtocolGuid; - -#endif diff --git a/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/TestBaseCryptLib.h b/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/TestBaseCryptLib.h index 01e25e83a0..1b079c6546 100644 --- a/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/TestBaseCryptLib.h +++ b/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/TestBaseCryptLib.h @@ -6,8 +6,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent **/ -#ifndef __CRYPTEST_H__ -#define __CRYPTEST_H__ +#pragma once #include #include @@ -168,5 +167,3 @@ EFIAPI UefiTestMain ( VOID ); - -#endif