SourceLevelDebugPkg: 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 14:29:01 -05:00 committed by mergify[bot]
parent 8b7617151e
commit 5f2142819f
17 changed files with 17 additions and 68 deletions

View file

@ -6,8 +6,7 @@
**/
#ifndef __EFI_DEBUG_AGENT_GUID_H__
#define __EFI_DEBUG_AGENT_GUID_H__
#pragma once
///
/// This guid is used as a variable GUID for the capsule variable
@ -22,5 +21,3 @@
}
extern EFI_GUID gEfiDebugAgentGuid;
#endif

View file

@ -7,8 +7,7 @@
**/
#ifndef __PROCESSOR_CONTEXT_H__
#define __PROCESSOR_CONTEXT_H__
#pragma once
//
// IA-32/x64 processor register index table
@ -299,5 +298,3 @@ typedef struct {
} DEBUG_DATA_X64_SYSTEM_CONTEXT;
#pragma pack()
#endif

View file

@ -7,8 +7,7 @@
**/
#ifndef __IMAGE_DEBUG_SUPPORT_H__
#define __IMAGE_DEBUG_SUPPORT_H__
#pragma once
#define IO_PORT_BREAKPOINT_ADDRESS 0x84
#define IMAGE_LOAD_SIGNATURE SIGNATURE_32('L','O','A','D')
@ -17,5 +16,3 @@
#define DEBUG_AGENT_IMAGE_WAIT 0x00
#define DEBUG_AGENT_IMAGE_CONTINUE 0x01
#endif

View file

@ -6,8 +6,7 @@
**/
#ifndef __DEBUG_COMMUNICATION_LIB_H__
#define __DEBUG_COMMUNICATION_LIB_H__
#pragma once
typedef VOID *DEBUG_PORT_HANDLE;
@ -139,5 +138,3 @@ EFIAPI
DebugPortPollBuffer (
IN DEBUG_PORT_HANDLE Handle
);
#endif

View file

@ -7,8 +7,7 @@
**/
#ifndef __TRANSFER_PROTOCOL_H__
#define __TRANSFER_PROTOCOL_H__
#pragma once
#include "ProcessorContext.h"
@ -365,5 +364,3 @@ typedef struct {
} DEBUG_DATA_RESPONSE_SEARCH_SIGNATURE;
#pragma pack()
#endif

View file

@ -6,8 +6,7 @@
**/
#ifndef _DEBUG_AGENT_H_
#define _DEBUG_AGENT_H_
#pragma once
#include <Register/LocalApic.h>
#include <Guid/DebugAgentGuid.h>
@ -492,5 +491,3 @@ DebugAgentReadBuffer (
IN UINTN NumberOfBytes,
IN UINTN Timeout
);
#endif

View file

@ -6,8 +6,7 @@
**/
#ifndef _DEBUG_MP_H_
#define _DEBUG_MP_H_
#pragma once
#define DEBUG_CPU_MAX_COUNT 256
@ -211,5 +210,3 @@ BOOLEAN
IsFirstBreakProcessor (
IN UINT32 ProcessorIndex
);
#endif

View file

@ -6,8 +6,7 @@
**/
#ifndef _DEBUG_TIMER_H_
#define _DEBUG_TIMER_H_
#pragma once
/**
Initialize CPU local APIC timer.
@ -40,5 +39,3 @@ IsDebugTimerTimeout (
IN UINT32 Timer,
IN UINT32 TimeoutTicker
);
#endif

View file

@ -6,8 +6,7 @@
**/
#ifndef _ARCH_DEBUG_SUPPORT_H_
#define _ARCH_DEBUG_SUPPORT_H_
#pragma once
#include "ProcessorContext.h"
#include "TransferProtocol.h"
@ -17,5 +16,3 @@
typedef DEBUG_DATA_IA32_FX_SAVE_STATE DEBUG_DATA_FX_SAVE_STATE;
typedef DEBUG_DATA_IA32_SYSTEM_CONTEXT DEBUG_CPU_CONTEXT;
#endif

View file

@ -6,8 +6,7 @@
**/
#ifndef _DEBUG_EXCEPTION_H_
#define _DEBUG_EXCEPTION_H_
#pragma once
#define DEBUG_EXCEPT_DIVIDE_ERROR 0
#define DEBUG_EXCEPT_DEBUG 1
@ -26,5 +25,3 @@
#define DEBUG_EXCEPT_ALIGNMENT_CHECK 17
#define DEBUG_EXCEPT_MACHINE_CHECK 18
#define DEBUG_EXCEPT_SIMD 19
#endif

View file

@ -6,8 +6,7 @@
**/
#ifndef _ARCH_DEBUG_SUPPORT_H_
#define _ARCH_DEBUG_SUPPORT_H_
#pragma once
#include "ProcessorContext.h"
#include "TransferProtocol.h"
@ -17,5 +16,3 @@
typedef DEBUG_DATA_X64_FX_SAVE_STATE DEBUG_DATA_FX_SAVE_STATE;
typedef DEBUG_DATA_X64_SYSTEM_CONTEXT DEBUG_CPU_CONTEXT;
#endif

View file

@ -6,8 +6,7 @@
**/
#ifndef _DEBUG_EXCEPTION_H_
#define _DEBUG_EXCEPTION_H_
#pragma once
#define DEBUG_EXCEPT_DIVIDE_ERROR 0
#define DEBUG_EXCEPT_DEBUG 1
@ -26,5 +25,3 @@
#define DEBUG_EXCEPT_ALIGNMENT_CHECK 17
#define DEBUG_EXCEPT_MACHINE_CHECK 18
#define DEBUG_EXCEPT_SIMD 19
#endif

View file

@ -6,8 +6,7 @@
**/
#ifndef _DXE_CORE_DEBUG_AGENT_LIB_H_
#define _DXE_CORE_DEBUG_AGENT_LIB_H_
#pragma once
#include <PiDxe.h>
@ -30,5 +29,3 @@ VOID
InstallSerialIo (
VOID
);
#endif

View file

@ -6,8 +6,7 @@
**/
#ifndef _SEC_CORE_DEBUG_AGENT_LIB_H_
#define _SEC_CORE_DEBUG_AGENT_LIB_H_
#pragma once
#include <PiPei.h>
#include <Ppi/MemoryDiscovered.h>
@ -54,5 +53,3 @@ DebugAgentCallbackMemoryDiscoveredPpi (
IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
IN VOID *Ppi
);
#endif

View file

@ -6,8 +6,7 @@
**/
#ifndef _SMM_DEBUG_AGENT_LIB_H_
#define _SMM_DEBUG_AGENT_LIB_H_
#pragma once
#include <PiDxe.h>
@ -15,5 +14,3 @@
#include <Library/SmmServicesTableLib.h>
#include "DebugAgent.h"
#endif

View file

@ -6,8 +6,7 @@
**/
#ifndef __USB3_DEBUG_PORT_LIB_INTERNAL__
#define __USB3_DEBUG_PORT_LIB_INTERNAL__
#pragma once
#include <Uefi.h>
#include <Base.h>
@ -733,5 +732,3 @@ USB3_DEBUG_PORT_HANDLE *
GetUsb3DebugPortInstance (
VOID
);
#endif //__SERIAL_PORT_LIB_USB__

View file

@ -6,8 +6,7 @@
**/
#ifndef _PE_COFF_EXTRA_ACTION_LIB_H_
#define _PE_COFF_EXTRA_ACTION_LIB_H_
#pragma once
#include <Base.h>
#include <Library/PeCoffExtraActionLib.h>
@ -68,5 +67,3 @@ RestoreIdtEntry1 (
IN IA32_DESCRIPTOR *IdtDescriptor,
IN IA32_IDT_GATE_DESCRIPTOR *RestoredIdtEntry
);
#endif