EmulatorPkg: 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:22:31 -05:00 committed by mergify[bot]
parent de527ff606
commit e679741305
30 changed files with 30 additions and 120 deletions

View file

@ -7,8 +7,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#ifndef _CPU_ARCHITECTURAL_PROTOCOL_DRIVER_H_
#define _CPU_ARCHITECTURAL_PROTOCOL_DRIVER_H_
#pragma once
#include <PiDxe.h>
#include <IndustryStandard/SmBios.h>
@ -219,5 +218,3 @@ CpuMpServicesWhoAmI (
);
extern EFI_MP_SERVICES_PROTOCOL mMpServicesTemplate;
#endif

View file

@ -18,8 +18,7 @@ Abstract:
**/
#ifndef _EMU_BLOCK_IO_H_
#define _EMU_BLOCK_IO_H_
#pragma once
#include <PiDxe.h>
#include <Protocol/EmuIoThunk.h>
@ -71,5 +70,3 @@ extern EFI_COMPONENT_NAME2_PROTOCOL gEmuBlockIoComponentName2;
extern EFI_DRIVER_CONFIGURATION_PROTOCOL gEmuBlockIoDriverConfiguration;
extern EFI_DRIVER_DIAGNOSTICS_PROTOCOL gEmuBlockIoDriverDiagnostics;
extern EFI_DRIVER_DIAGNOSTICS2_PROTOCOL gEmuBlockIoDriverDiagnostics2;
#endif

View file

@ -6,8 +6,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#ifndef __EMU_BUS_DRIVER_H__
#define __EMU_BUS_DRIVER_H__
#pragma once
#include <PiDxe.h>
@ -99,5 +98,3 @@ EmuBusCreateDevicePath (
IN EFI_GUID *Guid,
IN UINT16 InstanceNumber
);
#endif

View file

@ -6,8 +6,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#ifndef __GOP_H_
#define __GOP_H_
#pragma once
#include <PiDxe.h>
@ -176,5 +175,3 @@ GopPrivateBreakCallbackFunction (
IN VOID *Context,
IN EFI_KEY_DATA *KeyData
);
#endif

View file

@ -10,8 +10,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#ifndef _EMU_SIMPLE_FILE_SYSTEM_H_
#define _EMU_SIMPLE_FILE_SYSTEM_H_
#pragma once
#include "PiDxe.h"
@ -67,5 +66,3 @@ typedef struct {
EfiFile, \
EMU_EFI_FILE_PRIVATE_SIGNATURE \
)
#endif

View file

@ -12,8 +12,7 @@ Abstract:
-**/
#ifndef _EMU_SNP_H_
#define _EMU_SNP_H_
#pragma once
#include <Uefi.h>
@ -476,5 +475,3 @@ EmuSnpWaitForPacketNotify (
IN EFI_EVENT Event,
IN VOID *Private
);
#endif // _EMU_SNP_H_

View file

@ -7,8 +7,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#ifndef _FW_BLOCK_SERVICE_H
#define _FW_BLOCK_SERVICE_H
#pragma once
//
// BugBug: Add documentation here for data structure!!!!
@ -208,5 +207,3 @@ FvbProtocolEraseBlocks (
...
)
;
#endif

View file

@ -7,12 +7,9 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#ifndef __EMU_PHYSICAL_DISK_H__
#define __EMU_PHYSICAL_DISK_H__
#pragma once
#define EFI_EMU_PHYSICAL_DISK_GUID \
{ 0xf2ba331a, 0x8985, 0x11db, { 0xa4, 0x06, 0x00, 0x40, 0xd0, 0x2b, 0x18, 0x35 } }
extern EFI_GUID gEmuPhysicalDisksGuid;
#endif

View file

@ -7,8 +7,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#ifndef __EMU_SYSTEM_CONFIG_H__
#define __EMU_SYSTEM_CONFIG_H__
#pragma once
#define EFI_EMU_SYSTEM_CONFIG_GUID \
{ 0x9C4FB516, 0x3A1E, 0xD847, { 0xA1, 0xA1, 0x70, 0x58, 0xB6, 0x98, 0x67, 0x32 } }
@ -24,5 +23,3 @@ typedef struct {
#pragma pack()
extern EFI_GUID gEmuSystemConfigGuid;
#endif

View file

@ -7,12 +7,9 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#ifndef __EMU_VIRTUAL_DISK_H__
#define __EMU_VIRTUAL_DISK_H__
#pragma once
#define EFI_EMU_VIRTUAL_DISK_GUID \
{ 0xf2ba331a, 0x8985, 0x11db, { 0xa4, 0x06, 0x00, 0x40, 0xd0, 0x2b, 0x18, 0x35 } }
extern EFI_GUID gEmuVirtualDisksGuid;
#endif

View file

@ -8,8 +8,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#ifndef __EMU_MAGIC_PAGE_LIB_H__
#define __EMU_MAGIC_PAGE_LIB_H__
#pragma once
#include <PiPei.h>
#include <Library/PcdLib.h>
@ -28,5 +27,3 @@ typedef struct {
} EMU_MAGIC_PAGE_LAYOUT;
#define EMU_MAGIC_PAGE() ((EMU_MAGIC_PAGE_LAYOUT *)((UINTN)PcdGet64 (PcdPeiServicesTablePage)))
#endif

View file

@ -5,8 +5,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#ifndef __EMU_THUNK_LIB_H__
#define __EMU_THUNK_LIB_H__
#pragma once
#include <Protocol/EmuThunk.h>
@ -29,5 +28,3 @@ GetIoThunkInstance (
IN EFI_GUID *Protocol,
IN UINTN Instance
);
#endif

View file

@ -5,9 +5,6 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#ifndef __PPI_LIST_LIB_H__
#define __PPI_LIST_LIB_H__
#pragma once
extern CONST EFI_PEI_PPI_DESCRIPTOR *gPpiList;
#endif

View file

@ -9,8 +9,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#ifndef _SMBIOS_LIB_H__
#define _SMBIOS_LIB_H__
#pragma once
#include <IndustryStandard/SmBios.h>
#include <Protocol/Smbios.h>
@ -182,5 +181,3 @@ EFIAPI
SmbiosLibRemove (
OUT EFI_SMBIOS_HANDLE SmbiosHandle
);
#endif

View file

@ -7,8 +7,7 @@
**/
#ifndef __EMU_THUNK_PPI_H__
#define __EMU_THUNK_PPI_H__
#pragma once
#define EMU_THUNK_PPI_GUID \
{ 0xB958B78C, 0x1D3E, 0xEE40, { 0x8B, 0xF4, 0xF0, 0x63, 0x2D, 0x06, 0x39, 0x16 } }
@ -115,5 +114,3 @@ typedef struct {
} EMU_THUNK_PPI;
extern EFI_GUID gEmuThunkPpiGuid;
#endif

View file

@ -10,8 +10,7 @@
**/
#ifndef __EMU_BLOCK_IO_H__
#define __EMU_BLOCK_IO_H__
#pragma once
#include <Protocol/BlockIo.h>
#include <Protocol/BlockIo2.h>
@ -177,5 +176,3 @@ struct _EMU_BLOCK_IO_PROTOCOL {
};
extern EFI_GUID gEmuBlockIoProtocolGuid;
#endif

View file

@ -13,8 +13,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#ifndef _EMU_GRAPHICS_WINDOW_H_
#define _EMU_GRAPHICS_WINDOW_H_
#pragma once
#include <Protocol/SimplePointer.h>
#include <Protocol/SimpleTextIn.h>
@ -127,5 +126,3 @@ struct _EMU_GRAPHICS_WINDOW_PROTOCOL {
};
extern EFI_GUID gEmuGraphicsWindowProtocolGuid;
#endif

View file

@ -6,8 +6,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#ifndef _EMU_GRAPHICS_WINDOW_H_
#define _EMU_GRAPHICS_WINDOW_H_
#pragma once
#include <Protocol/SimplePointer.h>
#include <Protocol/SimpleTextIn.h>
@ -121,5 +120,3 @@ struct _EMU_GRAPHICS_WINDOW_PROTOCOL {
};
extern EFI_GUID gEmuGraphicsWindowProtocolGuid;
#endif

View file

@ -7,8 +7,7 @@
**/
#ifndef __EMU_IO_THUNK__
#define __EMU_IO_THUNK__
#pragma once
#define EMU_IO_THUNK_PROTOCO_GUID \
{ 0x453368F6, 0x7C85, 0x434A, { 0xA9, 0x8A, 0x72, 0xD1, 0xB7, 0xFF, 0xA9, 0x26 } }
@ -38,5 +37,3 @@ struct _EMU_IO_THUNK_PROTOCOL {
};
extern EFI_GUID gEmuIoThunkProtocolGuid;
#endif

View file

@ -9,8 +9,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#ifndef __EMU_SNP_H__
#define __EMU_SNP_H__
#pragma once
#include <Protocol/SimpleNetwork.h>
@ -447,5 +446,3 @@ struct _EMU_SNP_PROTOCOL {
};
extern EFI_GUID gEmuSnpProtocolGuid;
#endif

View file

@ -8,8 +8,7 @@
**/
#ifndef __EMU_THREAD_THUNK__
#define __EMU_THREAD_THUNK__
#pragma once
typedef struct _EMU_THREAD_THUNK_PROTOCOL EMU_THREAD_THUNK_PROTOCOL;
@ -82,5 +81,3 @@ struct _EMU_THREAD_THUNK_PROTOCOL {
};
extern EFI_GUID gEmuThreadThunkProtocolGuid;
#endif

View file

@ -8,8 +8,7 @@
**/
#ifndef __EMU_THUNK_PROTOCOL_H__
#define __EMU_THUNK_PROTOCOL_H__
#pragma once
#define EMU_THUNK_PROTOCOL_GUID \
{ 0x5CF32E0B, 0x8EDF, 0x2E44, { 0x9C, 0xDA, 0x93, 0x20, 0x5E, 0x99, 0xEC, 0x1C } }
@ -243,5 +242,3 @@ struct _EMU_THUNK_PROTOCOL {
};
extern EFI_GUID gEmuThunkProtocolGuid;
#endif

View file

@ -6,8 +6,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#ifndef _PLATFORM_BM_H
#define _PLATFORM_BM_H
#pragma once
#include <PiDxe.h>
@ -85,5 +84,3 @@ VOID
PlatformBdsConnectSequence (
VOID
);
#endif // _PLATFORM_BM_H

View file

@ -8,8 +8,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#ifndef __SEC_H___
#define __SEC_H___
#pragma once
#include <PiPei.h>
#include <Library/EmuMagicPageLib.h>
@ -28,5 +27,3 @@ SecTemporaryRamSupport (
IN EFI_PHYSICAL_ADDRESS PermanentMemoryBase,
IN UINTN CopySize
);
#endif

View file

@ -10,8 +10,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#ifndef _TIMER_H_
#define _TIMER_H_
#pragma once
//
// Legal timer value range in 100 ns units
@ -60,5 +59,3 @@ EFIAPI
EmuTimerDriverGenerateSoftInterrupt (
IN EFI_TIMER_ARCH_PROTOCOL *This
);
#endif

View file

@ -7,8 +7,7 @@
**/
#ifndef _GASKET_H_
#define _GASKET_H_
#pragma once
//
// EMU_THUNK_PROTOCOL gaskets (EFIAPI to UNIX ABI)
@ -621,5 +620,3 @@ GasketSnpReceive (
OUT EFI_MAC_ADDRESS *DestAddr OPTIONAL,
OUT UINT16 *Protocol OPTIONAL
);
#endif

View file

@ -7,8 +7,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#ifndef _SEC_MAIN_H__
#define _SEC_MAIN_H__
#pragma once
//
// Name mangle to prevent build errors. I.e conflicts between EFI and OS
@ -354,5 +353,3 @@ extern EMU_IO_THUNK_PROTOCOL gPosixFileSystemThunkIo;
extern EMU_IO_THUNK_PROTOCOL gPthreadThunkIo;
extern EMU_IO_THUNK_PROTOCOL gBlockIoThunkIo;
extern EMU_IO_THUNK_PROTOCOL gSnpThunkIo;
#endif

View file

@ -14,8 +14,7 @@ Abstract:
**/
#ifndef _WIN_GOP_H_
#define _WIN_GOP_H_
#pragma once
#include "WinHost.h"
@ -197,5 +196,3 @@ GopPrivateDestroyQ (
IN GRAPHICS_PRIVATE_DATA *Private,
IN GOP_QUEUE_FIXED *Queue
);
#endif

View file

@ -13,8 +13,7 @@ Abstract:
**/
#ifndef _HOST_H_
#define _HOST_H_
#pragma once
#include <stdio.h>
#include <time.h>
@ -211,5 +210,3 @@ extern EMU_IO_THUNK_PROTOCOL mWinNtWndThunkIo;
extern EMU_IO_THUNK_PROTOCOL mWinNtFileSystemThunkIo;
extern EMU_IO_THUNK_PROTOCOL mWinNtBlockIoThunkIo;
extern EMU_IO_THUNK_PROTOCOL mWinNtSnpThunkIo;
#endif

View file

@ -5,8 +5,7 @@ Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#ifndef __WIN_NT_INCLUDE_H__
#define __WIN_NT_INCLUDE_H__
#pragma once
#undef VOID
@ -46,5 +45,3 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
#define IMAGE_FILE_MACHINE_ARM64 0xAA64
#endif
#endif
#endif