UnitTestFrameworkPkg/Include: Update GoogleTestLib for CLANG

Update GoogleTestLib.h to support Windows/CLANGPDB build.
When using Windows/CLANGPDB, _MSC_VER is not defined. If
it is not defined, then set to 1900 for Visual Studio 2015
or newer compatibility when using windows include files for
host based unit test application builds. If _MSC_VER is not
set, then those application fail to build.

Also address a Windows/CLANG issue when ASAN is enabled
by providing an alternate implementation of the
EXPECT_THROW_MESSAGE() and ASSERT_THROW_MESSAGE() macros.
Without this alternate implementation, an exception is
generated due to the description of the ASSERT() condition
being NULL.

Update all implementations of EXPECT_THROW_MESSAGE() and
ASSERT_THROW_MESSAGE() to support parameters to the
statement executed to return values.

Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
This commit is contained in:
Michael D Kinney
2025-09-19 14:03:23 -07:00
committed by mergify[bot]
parent 9731114a00
commit 81a7efddd5

View File

@ -9,9 +9,20 @@
#ifndef GOOGLE_TEST_LIB_H_
#define GOOGLE_TEST_LIB_H_
//
// For Windows/clang builds if _MSC_VER is not defined, then set to
// Visual Studio 2015 value of 1900
//
#if defined (__clang__) && defined (_WIN32)
#ifndef _MSC_VER
#define _MSC_VER 1900
#endif
#endif
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <cstring>
#include <iomanip>
//
// For all use of GoogleTestLib, make sure NULL is defined to nullptr to
@ -31,16 +42,53 @@ using ::testing::HasSubstr;
// in the exception message. Typically used to check that the expression
// that generates an ASSERT() matches the expected expression.
//
// NOTE: Windows/CLANG builds with ASAN enabled generate an exception
// due to the generated exception string being NULL. An alternate implementation
// of these macros is provided for Windows/CLANG builds that disables ASAN
// checks in a function that uses try/catch for the expected exception type
// and provides the correct exception string for comparison.
//
#if defined (__clang__) && defined (_WIN32)
#define EXPECT_THROW_MESSAGE(statement, description) \
[&]() __attribute__((no_sanitize_address)){ \
try { \
(statement); \
FAIL() << "Exception not thrown"; \
} catch (const std::runtime_error& ex) { \
EXPECT_THAT ( \
std::string(ex.what()), \
HasSubstr(description) \
); \
} catch(...) { \
FAIL() << "Unexpected exception"; \
} \
}()
#define ASSERT_THROW_MESSAGE(statement, description) \
[&]() __attribute__((no_sanitize_address)){ \
try { \
(statement); \
FAIL() << "Exception not thrown"; \
} catch (const std::runtime_error& ex) { \
ASSERT_THAT ( \
std::string(ex.what()), \
HasSubstr(description) \
); \
} catch(...) { \
FAIL() << "Unexpected exception"; \
} \
}()
#else
#define EXPECT_THROW_MESSAGE(statement, description) \
EXPECT_THAT ( \
[]() { statement; }, \
[&]() { statement; }, \
ThrowsMessage<std::runtime_error>(HasSubstr (description)) \
)
#define ASSERT_THROW_MESSAGE(statement, description) \
ASSERT_THAT ( \
[]() { statement; }, \
[&]() { statement; }, \
ThrowsMessage<std::runtime_error>(HasSubstr (description)) \
)
#endif
extern "C" {
#include <Uefi.h>