UnitTestFrameworkPkg: 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:35:23 -05:00 committed by mergify[bot]
parent f3c45d2f0c
commit 59f58197dd
11 changed files with 11 additions and 44 deletions

View file

@ -5,8 +5,7 @@
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#ifndef FUNCTION_MOCK_LIB_H_
#define FUNCTION_MOCK_LIB_H_
#pragma once
#include <Library/GoogleTestLib.h>
#include <Library/SubhookLib.h>
@ -127,5 +126,3 @@
MOCK :: ~ MOCK () { \
MOCK :: Instance = NULL; \
}
#endif // FUNCTION_MOCK_LIB_H_

View file

@ -6,8 +6,7 @@
**/
#ifndef GOOGLE_TEST_LIB_H_
#define GOOGLE_TEST_LIB_H_
#pragma once
#include <gtest/gtest.h>
#include <gmock/gmock.h>
@ -173,5 +172,3 @@ MATCHER_P (
*result_listener << "strings match";
return true;
}
#endif

View file

@ -6,10 +6,7 @@
**/
#ifndef SUBHOOK_LIB_H_
#define SUBHOOK_LIB_H_
#pragma once
#define SUBHOOK_STATIC
#include <subhook.h>
#endif

View file

@ -9,8 +9,7 @@
**/
#ifndef _UNIT_TEST_PERSISTENCE_LIB_H_
#define _UNIT_TEST_PERSISTENCE_LIB_H_
#pragma once
#include <Library/UnitTestLib.h>
@ -76,5 +75,3 @@ LoadUnitTestCache (
OUT VOID **SaveData,
OUT UINTN *SaveStateSize
);
#endif

View file

@ -5,8 +5,7 @@
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#ifndef WIN_INCLUDE_H_
#define WIN_INCLUDE_H_
#pragma once
#define GUID _WINNT_DUP_GUID_____
#define _LIST_ENTRY _WINNT_DUP_LIST_ENTRY_FORWARD
@ -24,5 +23,3 @@
#undef _LIST_ENTRY
#undef LIST_ENTRY
#define VOID void
#endif

View file

@ -6,8 +6,7 @@
**/
#ifndef PEI_SERVICES_TABLE_POINTER_LIB_UNIT_TEST_H_
#define PEI_SERVICES_TABLE_POINTER_LIB_UNIT_TEST_H_
#pragma once
#include <Base.h>
#include <PiPei.h>
@ -649,5 +648,3 @@ UnitTestPeiFreePages (
IN EFI_PHYSICAL_ADDRESS Memory,
IN UINTN Pages
);
#endif

View file

@ -9,8 +9,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#ifndef UEFI_BOOT_SERVICES_TABLE_LIB_UNIT_TEST_H_
#define UEFI_BOOT_SERVICES_TABLE_LIB_UNIT_TEST_H_
#pragma once
#include <Uefi.h>
@ -1038,5 +1037,3 @@ UnitTestCreateEventEx (
IN CONST EFI_GUID *EventGroup, OPTIONAL
OUT EFI_EVENT *Event
);
#endif

View file

@ -9,8 +9,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#ifndef UEFI_BOOT_SERVICES_TABLE_LIB_UNIT_TEST_PROTOCOL_H_
#define UEFI_BOOT_SERVICES_TABLE_LIB_UNIT_TEST_PROTOCOL_H_
#pragma once
#include "UnitTestUefiBootServicesTableLib.h"
@ -116,5 +115,3 @@ IHANDLE *
IN OUT LOCATE_POSITION *Position,
OUT VOID **Interface
);
#endif

View file

@ -10,8 +10,7 @@
**/
#ifndef __UNIT_TEST_BOOT_LIB_H__
#define __UNIT_TEST_BOOT_LIB_H__
#pragma once
/**
Set the boot manager to boot from a specific device on the next boot. This
@ -27,5 +26,3 @@ EFIAPI
SetBootNextDevice (
VOID
);
#endif

View file

@ -8,8 +8,7 @@
**/
#ifndef __UNIT_TEST_RESULT_REPORT_LIB_H__
#define __UNIT_TEST_RESULT_REPORT_LIB_H__
#pragma once
#include <UnitTestFrameworkTypes.h>
@ -23,5 +22,3 @@ EFIAPI
OutputUnitTestFrameworkReport (
IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle
);
#endif

View file

@ -7,8 +7,7 @@
**/
#ifndef __UNIT_TEST_TYPES_H__
#define __UNIT_TEST_TYPES_H__
#pragma once
#include <Library/UnitTestLib.h>
@ -174,5 +173,3 @@ UNIT_TEST_FRAMEWORK_HANDLE
GetActiveFrameworkHandle (
VOID
);
#endif