EmbeddedPkg: 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 <michael.kubacki@microsoft.com>
This commit is contained in:
Michael Kubacki 2026-02-03 13:18:20 -05:00 committed by mergify[bot]
parent 01f75b3927
commit de527ff606
44 changed files with 44 additions and 176 deletions

View file

@ -6,8 +6,7 @@
**/
#ifndef __ANDROID_FASTBOOT_APP_H__
#define __ANDROID_FASTBOOT_APP_H__
#pragma once
#include <Library/AndroidBootImgLib.h>
#include <Library/BaseLib.h>
@ -33,5 +32,3 @@ ParseAndroidBootImg (
OUT UINTN *RamdiskSize,
OUT CHAR8 *KernelArgs
);
#endif //ifdef __ANDROID_FASTBOOT_APP_H__

View file

@ -6,8 +6,7 @@
*
**/
#ifndef __CONSOLE_PREF_DXE_H__
#define __CONSOLE_PREF_DXE_H__
#pragma once
#include <Guid/HiiPlatformSetupFormset.h>
#include <Guid/ConsolePrefFormSet.h>
@ -21,5 +20,3 @@ typedef struct {
UINT8 Console;
UINT8 Reserved[3];
} CONSOLE_PREF_VARSTORE_DATA;
#endif

View file

@ -6,8 +6,7 @@
*
**/
#ifndef __DT_PLATFORM_DXE_H__
#define __DT_PLATFORM_DXE_H__
#pragma once
#include <Guid/HiiPlatformSetupFormset.h>
#include <Guid/DtPlatformFormSet.h>
@ -21,5 +20,3 @@ typedef struct {
UINT8 Pref;
UINT8 Reserved[3];
} DT_ACPI_VARSTORE_DATA;
#endif

View file

@ -6,8 +6,7 @@
**/
#ifndef MEMORY_ATTRIBUTE_MANAGER_DXE_H_
#define MEMORY_ATTRIBUTE_MANAGER_DXE_H_
#pragma once
#include <Guid/MemoryAttributeManagerFormSet.h>
@ -18,5 +17,3 @@
typedef struct {
BOOLEAN Enabled;
} MEMORY_ATTRIBUTE_MANAGER_VARSTORE_DATA;
#endif // __MEMORY_ATTRIBUTE_MANAGER_DXE_H__

View file

@ -7,8 +7,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#ifndef _VIRTUAL_KEYBOARD_COMPONENT_NAME_H_
#define _VIRTUAL_KEYBOARD_COMPONENT_NAME_H_
#pragma once
extern EFI_COMPONENT_NAME_PROTOCOL gVirtualKeyboardComponentName;
extern EFI_COMPONENT_NAME2_PROTOCOL gVirtualKeyboardComponentName2;
@ -141,5 +140,3 @@ VirtualKeyboardComponentNameGetControllerName (
IN CHAR8 *Language,
OUT CHAR16 **ControllerName
);
#endif

View file

@ -7,8 +7,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#ifndef _VIRTUAL_KEYBOARD_H_
#define _VIRTUAL_KEYBOARD_H_
#pragma once
#include <Guid/StatusCodeDataTypeId.h>
#include <Protocol/DevicePath.h>
@ -533,5 +532,3 @@ VirtualKeyboardReadKeyStrokeEx (
IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,
OUT EFI_KEY_DATA *KeyData
);
#endif /* _VIRTUAL_KEYBOARD_H_ */

View file

@ -7,8 +7,7 @@
**/
#ifndef __GDB_STUB_INTERNAL__
#define __GDB_STUB_INTERNAL__
#pragma once
#include <Uefi.h>
#include <Library/BaseLib.h>
@ -711,5 +710,3 @@ ValidateException (
IN EFI_EXCEPTION_TYPE ExceptionType,
IN OUT EFI_SYSTEM_CONTEXT SystemContext
);
#endif

View file

@ -6,12 +6,9 @@
*
**/
#ifndef __CONSOLE_PREF_FORMSET_H__
#define __CONSOLE_PREF_FORMSET_H__
#pragma once
#define CONSOLE_PREF_FORMSET_GUID \
{ 0x2d2358b4, 0xe96c, 0x484d, { 0xb2, 0xdd, 0x7c, 0x2e, 0xdf, 0xc7, 0xd5, 0x6f } }
extern EFI_GUID gConsolePrefFormSetGuid;
#endif

View file

@ -6,12 +6,9 @@
*
**/
#ifndef __DT_PLATFORM_DEFAULT_DTB_FILE_H__
#define __DT_PLATFORM_DEFAULT_DTB_FILE_H__
#pragma once
#define DT_PLATFORM_DEFAULT_DTB_FILE_GUID \
{ 0x25462cda, 0x221f, 0x47df, { 0xac, 0x1d, 0x25, 0x9c, 0xfa, 0xa4, 0xe3, 0x26 } }
extern EFI_GUID gDtPlatformDefaultDtbFileGuid;
#endif

View file

@ -6,12 +6,9 @@
*
**/
#ifndef __DT_PLATFORM_FORMSET_H__
#define __DT_PLATFORM_FORMSET_H__
#pragma once
#define DT_PLATFORM_FORMSET_GUID \
{ 0x2b7a240d, 0xd5ad, 0x4fd6, { 0xbe, 0x1c, 0xdf, 0xa4, 0x41, 0x5f, 0x55, 0x26 } }
extern EFI_GUID gDtPlatformFormSetGuid;
#endif

View file

@ -6,8 +6,7 @@
**/
#ifndef __EXTRACT_SECTION_GUID_H__
#define __EXTRACT_SECTION_GUID_H__
#pragma once
#include <Library/ExtractGuidedSectionLib.h>
@ -24,5 +23,3 @@ typedef struct {
EFI_HOB_GUID_TYPE Hob;
EXTRACT_SECTION_DATA Data;
} EXTRACT_SECTION_HOB;
#endif

View file

@ -6,12 +6,9 @@
**/
#ifndef MEMORY_ATTRIBUTE_MANAGER_FORMSET_H_
#define MEMORY_ATTRIBUTE_MANAGER_FORMSET_H_
#pragma once
#define MEMORY_ATTRIBUTE_MANAGER_FORMSET_GUID \
{ 0xefab3427, 0x4793, 0x4e9e, { 0xaa, 0x29, 0x88, 0x0c, 0x9a, 0x77, 0x5b, 0x5f } }
extern EFI_GUID gMemoryAttributeManagerFormSetGuid;
#endif // __MEMORY_ATTRIBUTE_MANAGER_FORMSET_H__

View file

@ -18,8 +18,7 @@
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#ifndef __EDKII_NV_VAR_STORE_FORMATTED_H__
#define __EDKII_NV_VAR_STORE_FORMATTED_H__
#pragma once
#define EDKII_NV_VAR_STORE_FORMATTED_GUID \
{ \
@ -28,5 +27,3 @@
}
extern EFI_GUID gEdkiiNvVarStoreFormattedGuid;
#endif

View file

@ -14,8 +14,7 @@
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#ifndef __EDKII_PLATFORM_HAS_DEVICE_TREE_H__
#define __EDKII_PLATFORM_HAS_DEVICE_TREE_H__
#pragma once
#define EDKII_PLATFORM_HAS_DEVICE_TREE_GUID \
{ \
@ -24,5 +23,3 @@
}
extern EFI_GUID gEdkiiPlatformHasDeviceTreeGuid;
#endif

View file

@ -8,8 +8,7 @@
**/
#ifndef __ACPI_LIB_H__
#define __ACPI_LIB_H__
#pragma once
#include <Uefi.h>
@ -198,5 +197,3 @@ AcpiAmlObjectUpdateInteger (
IN CHAR8 *AsciiObjectPath,
IN UINTN Value
);
#endif // __ACPI_LIB_H__

View file

@ -7,8 +7,7 @@
**/
#ifndef __ABOOTIMG_H__
#define __ABOOTIMG_H__
#pragma once
#include <Library/BaseLib.h>
#include <Library/DebugLib.h>
@ -62,5 +61,3 @@ AndroidBootImgBoot (
IN VOID *Buffer,
IN UINTN BufferSize
);
#endif /* __ABOOTIMG_H__ */

View file

@ -9,8 +9,7 @@
**/
#ifndef __GDB_TIMER_LIB__
#define __GDB_TIMER_LIB__
#pragma once
/**
Setup all the hardware needed for the debug agents timer.
@ -46,5 +45,3 @@ EFIAPI
DebugAgentTimerEndOfInterrupt (
VOID
);
#endif

View file

@ -31,8 +31,7 @@
**/
#ifndef __DMA_LIB_H__
#define __DMA_LIB_H__
#pragma once
typedef enum {
///
@ -167,5 +166,3 @@ DmaAllocateAlignedBuffer (
IN UINTN Alignment,
OUT VOID **HostAddress
);
#endif

View file

@ -6,8 +6,7 @@
*
**/
#ifndef __DT_PLATFORM_DTB_LOADER_LIB_H__
#define __DT_PLATFORM_DTB_LOADER_LIB_H__
#pragma once
#include <Uefi/UefiBaseType.h>
@ -29,5 +28,3 @@ DtPlatformLoadDtb (
OUT VOID **Dtb,
OUT UINTN *DtbSize
);
#endif

View file

@ -29,8 +29,7 @@
**/
#ifndef __EFI_FILE_LIB_H__
#define __EFI_FILE_LIB_H__
#pragma once
#include <PiDxe.h>
#include <Protocol/FirmwareVolume2.h>
@ -328,5 +327,3 @@ CHAR8 *
EfiGetCwd (
VOID
);
#endif

View file

@ -6,8 +6,7 @@
*
**/
#ifndef _FDT_LOAD_LIB_H_
#define _FDT_LOAD_LIB_H_
#pragma once
/**
Load and Install FDT from Semihosting
@ -38,5 +37,3 @@ EFI_STATUS
InstallFdtFromFv (
IN CONST EFI_GUID *FileName
);
#endif

View file

@ -7,8 +7,7 @@
**/
#ifndef __GDB_SERIAL_LIB_H__
#define __GDB_SERIAL_LIB_H__
#pragma once
/**
Sets the baud rate, receive FIFO depth, transmit/receive time out, parity,
@ -90,5 +89,3 @@ VOID
GdbPutString (
IN CHAR8 *String
);
#endif

View file

@ -6,8 +6,7 @@
**/
#ifndef __RUNTIME_SERVICES_LIB_H__
#define __RUNTIME_SERVICES_LIB_H__
#pragma once
VOID
LibMtcInitialize (
@ -168,5 +167,3 @@ LibReportStatusCode (
IN EFI_GUID *CallerId,
IN EFI_STATUS_CODE_DATA *Data OPTIONAL
);
#endif

View file

@ -6,8 +6,7 @@
*
**/
#ifndef __NOR_FLASH_ID_LIB_H__
#define __NOR_FLASH_ID_LIB_H__
#pragma once
#include <Uefi/UefiBaseType.h>
@ -87,5 +86,3 @@ EFIAPI
NorFlashPrintInfo (
IN NOR_FLASH_INFO *Info
);
#endif

View file

@ -6,8 +6,7 @@
*
**/
#ifndef __PREPI_HOBLIST_POINTER_LIB_H__
#define __PREPI_HOBLIST_POINTER_LIB_H__
#pragma once
/**
Returns the pointer to the HOB list.
@ -34,5 +33,3 @@ EFIAPI
PrePeiSetHobList (
IN VOID *HobList
);
#endif

View file

@ -7,8 +7,7 @@
**/
#ifndef __PRE_PI_LIB_H__
#define __PRE_PI_LIB_H__
#pragma once
#include <Guid/ExtractSection.h>
#include <Pi/PiPeiCis.h>
@ -787,5 +786,3 @@ EFIAPI
DecompressFirstFv (
VOID
);
#endif

View file

@ -8,8 +8,7 @@
*
**/
#ifndef _TIME_BASE_LIB_H_
#define _TIME_BASE_LIB_H_
#pragma once
#include <Uefi/UefiBaseType.h>
@ -174,5 +173,3 @@ UINTN
EfiTimeToWday (
IN EFI_TIME *Time
);
#endif

View file

@ -7,8 +7,7 @@
**/
#ifndef __EMBEDDED_GPIO_PPI_H__
#define __EMBEDDED_GPIO_PPI_H__
#pragma once
//
// Protocol interface structure
@ -138,5 +137,3 @@ struct _EMBEDDED_GPIO_PPI {
};
extern EFI_GUID gEmbeddedGpioPpiGuid;
#endif

View file

@ -6,8 +6,7 @@
**/
#ifndef __ANDROID_BOOTIMG_PROTOCOL_H__
#define __ANDROID_BOOTIMG_PROTOCOL_H__
#pragma once
//
// Protocol interface structure
@ -37,5 +36,3 @@ struct _ANDROID_BOOTIMG_PROTOCOL {
};
extern EFI_GUID gAndroidBootImgProtocolGuid;
#endif /* __ANDROID_BOOTIMG_PROTOCOL_H__ */

View file

@ -6,8 +6,7 @@
**/
#ifndef __ANDROID_FASTBOOT_PLATFORM_H__
#define __ANDROID_FASTBOOT_PLATFORM_H__
#pragma once
extern EFI_GUID gAndroidFastbootPlatformProtocolGuid;
@ -135,5 +134,3 @@ typedef struct _FASTBOOT_PLATFORM_PROTOCOL {
FASTBOOT_PLATFORM_GETVAR GetVar;
FASTBOOT_PLATFORM_OEM_COMMAND DoOemCommand;
} FASTBOOT_PLATFORM_PROTOCOL;
#endif

View file

@ -20,8 +20,7 @@
**/
#ifndef __EMBEDDED_DEVICE_PROTOCOL_H__
#define __EMBEDDED_DEVICE_PROTOCOL_H__
#pragma once
//
// Protocol GUID
@ -43,5 +42,3 @@ typedef struct {
} EMBEDDED_DEVICE_PROTOCOL;
extern EFI_GUID gEmbeddedDeviceGuid;
#endif

View file

@ -6,8 +6,7 @@
**/
#ifndef __EMBEDDED_EXTERNAL_DEVICE_H__
#define __EMBEDDED_EXTERNAL_DEVICE_H__
#pragma once
//
// Protocol GUID
@ -86,5 +85,3 @@ struct _EMBEDDED_EXTERNAL_DEVICE {
};
extern EFI_GUID gEmbeddedExternalDeviceProtocolGuid;
#endif // __EMBEDDED_EXTERNAL_DEVICE_H__

View file

@ -6,8 +6,7 @@
**/
#ifndef __EMBEDDED_GPIO_H__
#define __EMBEDDED_GPIO_H__
#pragma once
//
// Protocol interface structure
@ -173,5 +172,3 @@ struct _PLATFORM_GPIO_CONTROLLER {
};
extern EFI_GUID gPlatformGpioProtocolGuid;
#endif

View file

@ -9,8 +9,7 @@
**/
#ifndef __FDT_CLIENT_H__
#define __FDT_CLIENT_H__
#pragma once
#define FDT_CLIENT_PROTOCOL_GUID {\
0xE11FACA0, 0x4710, 0x4C8E, {0xA7, 0xA2, 0x01, 0xBA, 0xA2, 0x59, 0x1B, 0x4C} \
@ -125,5 +124,3 @@ struct _FDT_CLIENT_PROTOCOL {
};
extern EFI_GUID gFdtClientProtocolGuid;
#endif

View file

@ -15,8 +15,7 @@
**/
#ifndef __HARDWARE_INTERRUPT_H__
#define __HARDWARE_INTERRUPT_H__
#pragma once
#include <Protocol/DebugSupport.h>
@ -148,5 +147,3 @@ struct _EFI_HARDWARE_INTERRUPT_PROTOCOL {
};
extern EFI_GUID gHardwareInterruptProtocolGuid;
#endif

View file

@ -6,8 +6,7 @@
**/
#ifndef __HARDWARE_INTERRUPT2_H__
#define __HARDWARE_INTERRUPT2_H__
#pragma once
#include <Protocol/HardwareInterrupt.h>
@ -168,5 +167,3 @@ struct _EFI_HARDWARE_INTERRUPT2_PROTOCOL {
};
extern EFI_GUID gHardwareInterrupt2ProtocolGuid;
#endif

View file

@ -7,8 +7,7 @@
**/
#ifndef __MMC_HOST_H__
#define __MMC_HOST_H__
#pragma once
///
/// Global ID for the MMC Host Protocol
@ -177,5 +176,3 @@ struct _EFI_MMC_HOST_PROTOCOL {
Host->IsMultiBlock != NULL)
extern EFI_GUID gEmbeddedMmcHostProtocolGuid;
#endif

View file

@ -6,8 +6,7 @@
**/
#ifndef __PE_COFF_LOADER_H__
#define __PE_COFF_LOADER_H__
#pragma once
// Needed for PE_COFF_LOADER_IMAGE_CONTEXT
#include <Library/PeCoffLib.h>
@ -216,5 +215,3 @@ struct _PE_COFF_LOADER_PROTOCOL {
};
extern EFI_GUID gPeCoffLoaderProtocolGuid;
#endif

View file

@ -6,8 +6,7 @@
**/
#ifndef __PLATFORM_BOOT_MANAGER_PROTOCOL_H__
#define __PLATFORM_BOOT_MANAGER_PROTOCOL_H__
#pragma once
//
// Protocol interface structure
@ -76,5 +75,3 @@ struct _PLATFORM_BOOT_MANAGER_PROTOCOL {
};
extern EFI_GUID gPlatformBootManagerProtocolGuid;
#endif /* __PLATFORM_BOOT_MANAGER_PROTOCOL_H__ */

View file

@ -6,8 +6,7 @@
**/
#ifndef __PLATFORM_VIRTUAL_KEYBOARD_H__
#define __PLATFORM_VIRTUAL_KEYBOARD_H__
#pragma once
//
// Protocol interface structure
@ -55,5 +54,3 @@ struct _PLATFORM_VIRTUAL_KBD_PROTOCOL {
};
extern EFI_GUID gPlatformVirtualKeyboardProtocolGuid;
#endif /* __PLATFORM_VIRTUAL_KEYBOARD_H__ */

View file

@ -6,8 +6,7 @@
**/
#ifndef __USB_DEVICE_PROTOCOL_H__
#define __USB_DEVICE_PROTOCOL_H__
#pragma once
#include <IndustryStandard/Usb.h>
@ -108,5 +107,3 @@ struct _USB_DEVICE_PROTOCOL {
};
typedef struct _USB_DEVICE_PROTOCOL USB_DEVICE_PROTOCOL;
#endif //ifndef __USB_DEVICE_PROTOCOL_H__

View file

@ -7,8 +7,7 @@
**/
#ifndef _PI_PEI_H_
#define _PI_PEI_H_
#pragma once
#include <PiPei.h>
@ -36,5 +35,3 @@
//
#define GET_GUID_HOB_DATA(GuidHob) ((VOID *) (((UINT8 *) &((GuidHob)->Name)) + sizeof (EFI_GUID)))
#define GET_GUID_HOB_DATA_SIZE(GuidHob) (((GuidHob)->Header).HobLength - sizeof (EFI_HOB_GUID_TYPE))
#endif

View file

@ -6,8 +6,7 @@
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#ifndef MOCK_DT_PLATFORM_DTB_LOADER_LIB_H_
#define MOCK_DT_PLATFORM_DTB_LOADER_LIB_H_
#pragma once
#include <Library/GoogleTestLib.h>
#include <Library/FunctionMockLib.h>
@ -27,5 +26,3 @@ struct MockDtPlatformDtbLoaderLib {
OUT UINTN *DtbSize)
);
};
#endif

View file

@ -7,8 +7,7 @@
**/
#ifndef __MMC_H
#define __MMC_H
#pragma once
#include <Uefi.h>
@ -518,5 +517,3 @@ VOID
PrintCID (
IN UINT32 *Cid
);
#endif