diff --git a/llvm/unittests/Support/TargetParserTest.cpp b/llvm/unittests/Support/TargetParserTest.cpp --- a/llvm/unittests/Support/TargetParserTest.cpp +++ b/llvm/unittests/Support/TargetParserTest.cpp @@ -8,7 +8,9 @@ #include "llvm/Support/TargetParser.h" #include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/StringExtras.h" #include "llvm/Support/ARMBuildAttributes.h" +#include "llvm/Support/FormatVariadic.h" #include "gmock/gmock.h" #include "gtest/gtest.h" #include @@ -33,6 +35,47 @@ "iwmmxt2", "xscale", "armv8.1-m.main", }; +template +std::string FormatExtensionFlags(uint64_t Flags) { + std::vector Features; + + if (ISAKind == ARM::ISAKind::AARCH64) { + // AEK_NONE is not meant to be shown to the user so the target parser + // does not recognise it. It is relevant here though. + if (Flags & AArch64::AEK_NONE) + Features.push_back("none"); + AArch64::getExtensionFeatures(Flags, Features); + } else { + if (Flags & ARM::AEK_NONE) + Features.push_back("none"); + ARM::getExtensionFeatures(Flags, Features); + } + + // The target parser also includes every extension you don't have. + // E.g. if AEK_CRC is not set then it adds "-crc". Not useful here. + Features.erase(std::remove_if(Features.begin(), Features.end(), + [](StringRef extension) { + return extension.startswith("-"); + }), + Features.end()); + + return llvm::join(Features, ", "); +} + +template +testing::AssertionResult +AssertSameExtensionFlags(const char *m_expr, const char *n_expr, + uint64_t ExpectedFlags, uint64_t GotFlags) { + if (ExpectedFlags == GotFlags) + return testing::AssertionSuccess(); + + return testing::AssertionFailure() << llvm::formatv( + "Expected extension flags: {0} ({1:x})\n" + " Got extension flags: {2} ({3:x})\n", + FormatExtensionFlags(ExpectedFlags), ExpectedFlags, + FormatExtensionFlags(GotFlags), GotFlags); +} + struct ARMCPUTestParams { ARMCPUTestParams(StringRef CPUName, StringRef ExpectedArch, StringRef ExpectedFPU, uint64_t ExpectedFlags, @@ -67,7 +110,8 @@ EXPECT_EQ(params.ExpectedFPU, ARM::getFPUName(FPUKind)); uint64_t default_extensions = ARM::getDefaultExtensions(params.CPUName, AK); - EXPECT_EQ(params.ExpectedFlags, default_extensions); + EXPECT_PRED_FORMAT2(AssertSameExtensionFlags, + params.ExpectedFlags, default_extensions); EXPECT_EQ(params.CPUAttr, ARM::getCPUAttr(AK)); } @@ -813,7 +857,8 @@ uint64_t default_extensions = AArch64::getDefaultExtensions(params.CPUName, AK); - EXPECT_EQ(params.ExpectedFlags, default_extensions); + EXPECT_PRED_FORMAT2(AssertSameExtensionFlags, + params.ExpectedFlags, default_extensions); unsigned FPUKind = AArch64::getDefaultFPU(params.CPUName, AK); EXPECT_EQ(params.ExpectedFPU, ARM::getFPUName(FPUKind));