Index: llvm/include/llvm/TargetParser/Architecture/ArchitectureHelpers.h =================================================================== --- /dev/null +++ llvm/include/llvm/TargetParser/Architecture/ArchitectureHelpers.h @@ -0,0 +1,43 @@ +//===--- ArchitectureHelpers.h --------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +#pragma once + +#include "IArchitecture.h" + +namespace llvm { +class ArchitectureHelpers final { +public: + ArchitectureHelpers() = delete; + + static bool is16BitPointerArchitecture(const IArchitecture &Architecture); + + static bool is32BitPointerArchitecture(const IArchitecture &Architecture); + + static bool is64BitPointerArchitecture(const IArchitecture &Architecture); + + static std::unique_ptr + get32BitArchitectureVariant(const IArchitecture &Architecture); + + static std::unique_ptr + get64BitArchitectureVariant(const IArchitecture &Architecture); + + static std::unique_ptr + getBigEndianArchVariant(const IArchitecture &Architecture); + + static std::unique_ptr + getLittleEndianArchVariant(const IArchitecture &Architecture); + + static bool isLittleEndian(const IArchitecture &Architecture); + + static bool isBigEndian(const IArchitecture &Architecture); + + static Triple::ObjectFormatType + getDefaultFormat(const IArchitecture &Architecture, + const Triple::OSType &OsType); +}; +} // namespace llvm Index: llvm/include/llvm/TargetParser/Architecture/Common/ELFDefaultFormatProvider.h =================================================================== --- /dev/null +++ llvm/include/llvm/TargetParser/Architecture/Common/ELFDefaultFormatProvider.h @@ -0,0 +1,20 @@ +//===-- ELFDefaultFormatProvider.h ----------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#pragma once + +#include "llvm/TargetParser/Architecture/SimpleDefaultFormatProvider.h" + +namespace llvm { +class ELFDefaultFormatProvider : public SimpleDefaultFormatProvider { +public: + Triple::ObjectFormatType getDefaultFormat() const override { + return Triple::ELF; + } +}; +} // namespace llvm Index: llvm/include/llvm/TargetParser/Architecture/Common/NullDefaultedArchitectureVariantProvider.h =================================================================== --- /dev/null +++ llvm/include/llvm/TargetParser/Architecture/Common/NullDefaultedArchitectureVariantProvider.h @@ -0,0 +1,29 @@ +//===-- NullDefaultedArchitectureVariantProvider.h ------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#pragma once +#include "llvm/TargetParser/Architecture/IArchitectureVariantProvider.h" + +namespace llvm { +class NullDefaultedArchitectureVariantProvider + : public IArchitectureVariantProvider { +public: + std::unique_ptr get32BitArchitectureVariant() override { + return nullptr; + } + std::unique_ptr get64BitArchitectureVariant() override { + return nullptr; + } + std::unique_ptr getBigEndianArchVariant() override { + return nullptr; + } + std::unique_ptr getLittleEndianArchVariant() override { + return nullptr; + } +}; +} // namespace llvm \ No newline at end of file Index: llvm/include/llvm/TargetParser/Architecture/Common/OsAwareDefaultFormatVisitor.h =================================================================== --- /dev/null +++ llvm/include/llvm/TargetParser/Architecture/Common/OsAwareDefaultFormatVisitor.h @@ -0,0 +1,28 @@ +//===-- OsAwareDefaultFormatVisitor.h ------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#pragma once + +#include "llvm/TargetParser/Architecture/IDefaultFormatVisitor.h" + +namespace llvm { +class OsAwareDefaultFormatVisitor : public IDefaultFormatVisitor { +private: + Triple::OSType OsType; + +public: + OsAwareDefaultFormatVisitor(const Triple::OSType &OsType) + : IDefaultFormatVisitor(), OsType(OsType) {} + + Triple::ObjectFormatType + visit(const SimpleDefaultFormatProvider &Provider) const override; + + Triple::ObjectFormatType + visit(const OsAwareDefaultFormatProvider &Provider) const override; +}; +} // namespace llvm Index: llvm/include/llvm/TargetParser/Architecture/IArchitecture.h =================================================================== --- /dev/null +++ llvm/include/llvm/TargetParser/Architecture/IArchitecture.h @@ -0,0 +1,52 @@ +//===-- IArchitecture.h ---------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#pragma once + +#include "llvm/Support/ExtensibleRTTI.h" +#include "llvm/TargetParser/Triple.h" +#include + +namespace llvm { +enum class ArchitectureEndianness { LITTLE, BIG }; + +class IArchitectureVariantProvider; +class IDefaultFormatProvider; + +class IArchitecture { +public: + virtual ArchitectureEndianness getEndianness() const = 0; + virtual size_t getPointerBitWidth() const = 0; + + virtual std::string_view getArchitectureTypeName() const = 0; + virtual std::string_view getArchitectureTypePrefix() const = 0; + virtual Triple::ArchType getLegacyArchType() const = 0; + + virtual std::unique_ptr + getArchitectureVariantProvider() const = 0; + + virtual std::unique_ptr + getDefaultFormatProvider() const = 0; + + virtual bool isCompatibleWith(const IArchitecture &Other) const = 0; + + virtual bool operator==(const IArchitecture &Other) const { + if (this == std::addressof(Other)) + return true; + if (&typeid(this) == &typeid(Other)) + return true; + return false; + } + + bool operator!=(const IArchitecture &Other) const { + return !(*this == Other); + } + + virtual ~IArchitecture() = default; +}; +} // namespace llvm Index: llvm/include/llvm/TargetParser/Architecture/IArchitectureVariantProvider.h =================================================================== --- /dev/null +++ llvm/include/llvm/TargetParser/Architecture/IArchitectureVariantProvider.h @@ -0,0 +1,25 @@ +//===-- IArchitectureVariantProvider.h ------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#pragma once + +#include + +namespace llvm { +class IArchitecture; + +class IArchitectureVariantProvider { +public: + virtual std::unique_ptr get32BitArchitectureVariant() = 0; + virtual std::unique_ptr get64BitArchitectureVariant() = 0; + virtual std::unique_ptr getBigEndianArchVariant() = 0; + virtual std::unique_ptr getLittleEndianArchVariant() = 0; + + virtual ~IArchitectureVariantProvider() = default; +}; +} // namespace llvm Index: llvm/include/llvm/TargetParser/Architecture/IDefaultFormatProvider.h =================================================================== --- /dev/null +++ llvm/include/llvm/TargetParser/Architecture/IDefaultFormatProvider.h @@ -0,0 +1,22 @@ +//===-- IDefaultFormatProvider.h -----------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#pragma once + +#include "IDefaultFormatVisitor.h" +#include "llvm/TargetParser/Triple.h" + +namespace llvm { +class IDefaultFormatProvider { +public: + virtual Triple::ObjectFormatType + accept(const IDefaultFormatVisitor &Visitor) const = 0; + + virtual ~IDefaultFormatProvider() = default; +}; +} // namespace llvm Index: llvm/include/llvm/TargetParser/Architecture/IDefaultFormatVisitor.h =================================================================== --- /dev/null +++ llvm/include/llvm/TargetParser/Architecture/IDefaultFormatVisitor.h @@ -0,0 +1,26 @@ +//===-- IDefaultFormatVisitor.h ------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#pragma once + +#include "llvm/TargetParser/Triple.h" + +namespace llvm { +class SimpleDefaultFormatProvider; +class OsAwareDefaultFormatProvider; + +class IDefaultFormatVisitor { +public: + virtual Triple::ObjectFormatType + visit(const SimpleDefaultFormatProvider &Provider) const = 0; + virtual Triple::ObjectFormatType + visit(const OsAwareDefaultFormatProvider &Provider) const = 0; + + virtual ~IDefaultFormatVisitor() = default; +}; +} // namespace llvm Index: llvm/include/llvm/TargetParser/Architecture/OsAwareDefaultFormatProvider.h =================================================================== --- /dev/null +++ llvm/include/llvm/TargetParser/Architecture/OsAwareDefaultFormatProvider.h @@ -0,0 +1,24 @@ +//===-- OsAwareDefaultFormatProvider.h ------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#pragma once + +#include "IDefaultFormatProvider.h" + +namespace llvm { +class OsAwareDefaultFormatProvider : public IDefaultFormatProvider { +public: + Triple::ObjectFormatType + accept(const IDefaultFormatVisitor &Visitor) const override final { + return Visitor.visit(*this); + } + + virtual Triple::ObjectFormatType + getDefaultFormat(const Triple::OSType &OsType) const = 0; +}; +} // namespace llvm Index: llvm/include/llvm/TargetParser/Architecture/SimpleDefaultFormatProvider.h =================================================================== --- /dev/null +++ llvm/include/llvm/TargetParser/Architecture/SimpleDefaultFormatProvider.h @@ -0,0 +1,23 @@ +//===-- SimpleDefaultFormatProvider.h -------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#pragma once + +#include "IDefaultFormatProvider.h" + +namespace llvm { +class SimpleDefaultFormatProvider : public IDefaultFormatProvider { +public: + Triple::ObjectFormatType + accept(const IDefaultFormatVisitor &Visitor) const override { + return Visitor.visit(*this); + } + + virtual Triple::ObjectFormatType getDefaultFormat() const = 0; +}; +} // namespace llvm Index: llvm/lib/TargetParser/Architecture/ArchitectureHelpers.cpp =================================================================== --- /dev/null +++ llvm/lib/TargetParser/Architecture/ArchitectureHelpers.cpp @@ -0,0 +1,69 @@ +//===-- ArchitectureHelpers.cpp ------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "llvm/TargetParser/Architecture/ArchitectureHelpers.h" +#include "llvm/TargetParser/Architecture/Common/OsAwareDefaultFormatVisitor.h" +#include "llvm/TargetParser/Architecture/IArchitectureVariantProvider.h" +#include "llvm/TargetParser/Architecture/IDefaultFormatProvider.h" + +using namespace llvm; + +bool llvm::ArchitectureHelpers::is16BitPointerArchitecture( + const IArchitecture &Architecture) { + return Architecture.getPointerBitWidth() == 16u; +} + +bool ArchitectureHelpers::is32BitPointerArchitecture( + const IArchitecture &Architecture) { + return Architecture.getPointerBitWidth() == 32u; +} + +bool ArchitectureHelpers::is64BitPointerArchitecture( + const IArchitecture &Architecture) { + return Architecture.getPointerBitWidth() == 64u; +} + +std::unique_ptr ArchitectureHelpers::get32BitArchitectureVariant( + const IArchitecture &Architecture) { + return Architecture.getArchitectureVariantProvider() + ->get32BitArchitectureVariant(); +} + +std::unique_ptr ArchitectureHelpers::get64BitArchitectureVariant( + const IArchitecture &Architecture) { + return Architecture.getArchitectureVariantProvider() + ->get64BitArchitectureVariant(); +} + +std::unique_ptr ArchitectureHelpers::getBigEndianArchVariant( + const IArchitecture &Architecture) { + return Architecture.getArchitectureVariantProvider() + ->getBigEndianArchVariant(); +} + +std::unique_ptr ArchitectureHelpers::getLittleEndianArchVariant( + const IArchitecture &Architecture) { + return Architecture.getArchitectureVariantProvider() + ->getLittleEndianArchVariant(); +} + +bool ArchitectureHelpers::isLittleEndian(const IArchitecture &Architecture) { + return Architecture.getEndianness() == ArchitectureEndianness::LITTLE; +} + +bool ArchitectureHelpers::isBigEndian(const IArchitecture &Architecture) { + return Architecture.getEndianness() == ArchitectureEndianness::BIG; +} + +Triple::ObjectFormatType +ArchitectureHelpers::getDefaultFormat(const IArchitecture &Architecture, + const Triple::OSType &OsType) { + auto Visitor = OsAwareDefaultFormatVisitor(OsType); + auto Provider = Architecture.getDefaultFormatProvider(); + return Provider->accept(Visitor); +} Index: llvm/lib/TargetParser/Architecture/CMakeLists.txt =================================================================== --- /dev/null +++ llvm/lib/TargetParser/Architecture/CMakeLists.txt @@ -0,0 +1,5 @@ +set(LLVM_REQUIRES_RTTI ON) +add_llvm_component_library(LLVMArchitecture + ArchitectureHelpers.cpp + OsAwareDefaultFormatVisitor.cpp +) \ No newline at end of file Index: llvm/lib/TargetParser/Architecture/OsAwareDefaultFormatVisitor.cpp =================================================================== --- /dev/null +++ llvm/lib/TargetParser/Architecture/OsAwareDefaultFormatVisitor.cpp @@ -0,0 +1,23 @@ +//===-- OsAwareDefaultFormatVisitor.cpp ----------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "llvm/TargetParser/Architecture/Common/OsAwareDefaultFormatVisitor.h" +#include "llvm/TargetParser/Architecture/OsAwareDefaultFormatProvider.h" +#include "llvm/TargetParser/Architecture/SimpleDefaultFormatProvider.h" + +using namespace llvm; + +Triple::ObjectFormatType OsAwareDefaultFormatVisitor::visit( + const SimpleDefaultFormatProvider &Provider) const { + return Provider.getDefaultFormat(); +} + +Triple::ObjectFormatType OsAwareDefaultFormatVisitor::visit( + const OsAwareDefaultFormatProvider &Provider) const { + return Provider.getDefaultFormat(this->OsType); +} Index: llvm/lib/TargetParser/CMakeLists.txt =================================================================== --- llvm/lib/TargetParser/CMakeLists.txt +++ llvm/lib/TargetParser/CMakeLists.txt @@ -26,7 +26,10 @@ LINK_COMPONENTS Support + Architecture DEPENDS RISCVTargetParserTableGen ) + +add_subdirectory(Architecture) \ No newline at end of file Index: llvm/unittests/TargetParser/Architecture/ArchitectureHelpersTest.cpp =================================================================== --- /dev/null +++ llvm/unittests/TargetParser/Architecture/ArchitectureHelpersTest.cpp @@ -0,0 +1,221 @@ +//===-- ArchitectureHelpersTest.cpp ---------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "llvm/TargetParser/Architecture/ArchitectureHelpers.h" +#include "llvm/TargetParser/Architecture/Common/NullDefaultedArchitectureVariantProvider.h" +#include "llvm/TargetParser/Architecture/IArchitectureVariantProvider.h" +#include "llvm/TargetParser/Architecture/SimpleDefaultFormatProvider.h" + +#include "gtest/gtest.h" + +using namespace llvm; + +namespace { +class StubArchitecture : public IArchitecture { +public: + ArchitectureEndianness getEndianness() const override { + return ArchitectureEndianness::LITTLE; + } + size_t getPointerBitWidth() const override { return 0; } + std::string_view getArchitectureTypeName() const override { + return std::string_view(); + } + std::string_view getArchitectureTypePrefix() const override { + return std::string_view(); + } + Triple::ArchType getLegacyArchType() const override { + return Triple::UnknownArch; + } + std::unique_ptr + getArchitectureVariantProvider() const override { + return nullptr; + } + std::unique_ptr + getDefaultFormatProvider() const override { + return nullptr; + } + bool isCompatibleWith(const IArchitecture &Other) const override { + return false; + } +}; + +class The16BitArchitecture : public StubArchitecture { +public: + size_t getPointerBitWidth() const override { return 16u; } +}; + +class The32BitArchitecture : public StubArchitecture { +public: + size_t getPointerBitWidth() const override { return 32u; } +}; + +class The64BitArchitecture : public StubArchitecture { +public: + size_t getPointerBitWidth() const override { return 64u; } +}; + +/// Weird pointer size to check corner cases +class The81BitArchitecture : public StubArchitecture { +public: + size_t getPointerBitWidth() const override { return 81u; } +}; + +TEST(ArchitectureHelpersTest, is16BitPointerArchitecture) { + EXPECT_TRUE( + ArchitectureHelpers::is16BitPointerArchitecture(The16BitArchitecture())); + EXPECT_FALSE( + ArchitectureHelpers::is16BitPointerArchitecture(The32BitArchitecture())); + EXPECT_FALSE( + ArchitectureHelpers::is16BitPointerArchitecture(The64BitArchitecture())); + EXPECT_FALSE( + ArchitectureHelpers::is16BitPointerArchitecture(The81BitArchitecture())); +} + +TEST(ArchitectureHelpersTest, is32BitPointerArchitecture) { + EXPECT_FALSE( + ArchitectureHelpers::is32BitPointerArchitecture(The16BitArchitecture())); + EXPECT_TRUE( + ArchitectureHelpers::is32BitPointerArchitecture(The32BitArchitecture())); + EXPECT_FALSE( + ArchitectureHelpers::is32BitPointerArchitecture(The64BitArchitecture())); + EXPECT_FALSE( + ArchitectureHelpers::is32BitPointerArchitecture(The81BitArchitecture())); +} + +TEST(ArchitectureHelpersTest, is64BitPointerArchitecture) { + EXPECT_FALSE( + ArchitectureHelpers::is64BitPointerArchitecture(The16BitArchitecture())); + EXPECT_FALSE( + ArchitectureHelpers::is64BitPointerArchitecture(The32BitArchitecture())); + EXPECT_TRUE( + ArchitectureHelpers::is64BitPointerArchitecture(The64BitArchitecture())); + EXPECT_FALSE( + ArchitectureHelpers::is64BitPointerArchitecture(The81BitArchitecture())); +} + +class TheBigEndianArchitecture : public StubArchitecture { +public: + ArchitectureEndianness getEndianness() const override { + return ArchitectureEndianness::BIG; + } +}; + +class TheLittleEndianArchitecture : public StubArchitecture { +public: + ArchitectureEndianness getEndianness() const override { + return ArchitectureEndianness::LITTLE; + } +}; + +class StubArchitectureVariantProvider : public IArchitectureVariantProvider { +public: + std::unique_ptr get32BitArchitectureVariant() override { + return std::make_unique(); + } + std::unique_ptr get64BitArchitectureVariant() override { + return std::make_unique(); + } + std::unique_ptr getBigEndianArchVariant() override { + return std::make_unique(); + } + std::unique_ptr getLittleEndianArchVariant() override { + return std::make_unique(); + } +}; + +class TheVariantArchitecture : public StubArchitecture { +public: + std::unique_ptr + getArchitectureVariantProvider() const override { + return std::make_unique(); + } +}; + +class TheNonVariantArchitecture : public StubArchitecture { +public: + std::unique_ptr + getArchitectureVariantProvider() const override { + return std::make_unique(); + } +}; + +TEST(ArchitectureHelpersTest, get32BitArchitectureVariant) { + EXPECT_EQ( + ArchitectureHelpers::get32BitArchitectureVariant(TheVariantArchitecture()) + ->getPointerBitWidth(), + 32u); + EXPECT_EQ(ArchitectureHelpers::get32BitArchitectureVariant( + TheNonVariantArchitecture()), + nullptr); +} + +TEST(ArchitectureHelpersTest, get64BitArchitectureVariant) { + EXPECT_EQ( + ArchitectureHelpers::get64BitArchitectureVariant(TheVariantArchitecture()) + ->getPointerBitWidth(), + 64u); + EXPECT_EQ(ArchitectureHelpers::get64BitArchitectureVariant( + TheNonVariantArchitecture()), + nullptr); +} + +TEST(ArchitectureHelpersTest, getBigEndianArchVariant) { + EXPECT_EQ( + ArchitectureHelpers::getBigEndianArchVariant(TheVariantArchitecture()) + ->getEndianness(), + ArchitectureEndianness::BIG); + EXPECT_EQ( + ArchitectureHelpers::getBigEndianArchVariant(TheNonVariantArchitecture()), + nullptr); +} + +TEST(ArchitectureHelpersTest, getLittleEndianArchVariant) { + EXPECT_EQ( + ArchitectureHelpers::getLittleEndianArchVariant(TheVariantArchitecture()) + ->getEndianness(), + ArchitectureEndianness::LITTLE); + EXPECT_EQ(ArchitectureHelpers::getLittleEndianArchVariant( + TheNonVariantArchitecture()), + nullptr); +} + +TEST(ArchitectureHelpersTest, isLittleEndian) { + EXPECT_EQ(ArchitectureHelpers::isLittleEndian(TheLittleEndianArchitecture()), + true); + EXPECT_EQ(ArchitectureHelpers::isLittleEndian(TheBigEndianArchitecture()), + false); +} + +TEST(ArchitectureHelpersTest, isBigEndian) { + EXPECT_EQ(ArchitectureHelpers::isBigEndian(TheLittleEndianArchitecture()), + false); + EXPECT_EQ(ArchitectureHelpers::isBigEndian(TheBigEndianArchitecture()), true); +} + +class StubOsAwareDefaultFormatProvider : public SimpleDefaultFormatProvider { +public: + Triple::ObjectFormatType getDefaultFormat() const override { + return Triple::ELF; + } +}; + +class TheDefaultFormatArchitecture : public StubArchitecture { +public: + std::unique_ptr + getDefaultFormatProvider() const override { + return std::make_unique(); + } +}; + +TEST(ArchitectureHelpersTest, getDefaultFormat) { + EXPECT_EQ(ArchitectureHelpers::getDefaultFormat( + TheDefaultFormatArchitecture(), Triple::FreeBSD), + Triple::ELF); +} + +} // namespace \ No newline at end of file Index: llvm/unittests/TargetParser/Architecture/CMakeLists.txt =================================================================== --- /dev/null +++ llvm/unittests/TargetParser/Architecture/CMakeLists.txt @@ -0,0 +1,12 @@ +set(LLVM_REQUIRES_RTTI ON) +set(LLVM_LINK_COMPONENTS + Support + Architecture + ) + +add_llvm_unittest(ArchitectureTests + ArchitectureHelpersTest.cpp + OsAwareDefaultFormatVisitorTest.cpp + ) + +target_link_libraries(ArchitectureTests PRIVATE LLVMTestingSupport) \ No newline at end of file Index: llvm/unittests/TargetParser/Architecture/OsAwareDefaultFormatVisitorTest.cpp =================================================================== --- /dev/null +++ llvm/unittests/TargetParser/Architecture/OsAwareDefaultFormatVisitorTest.cpp @@ -0,0 +1,44 @@ +//===-- OsAwareDefaultFormatVisitorTest.cpp -------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "llvm/TargetParser/Architecture/Common/OsAwareDefaultFormatVisitor.h" +#include "llvm/TargetParser/Architecture/OsAwareDefaultFormatProvider.h" +#include "llvm/TargetParser/Architecture/SimpleDefaultFormatProvider.h" + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +using namespace llvm; + +namespace { +class MockSimpleDefaultFormatProvider : public SimpleDefaultFormatProvider { +public: + MOCK_METHOD(Triple::ObjectFormatType, getDefaultFormat, (), + (const, override)); +}; + +class MockOsAwareDefaultFormatProvider : public OsAwareDefaultFormatProvider { +public: + MOCK_METHOD(Triple::ObjectFormatType, getDefaultFormat, + (const Triple::OSType &), (const, override)); +}; + +TEST(OsAwareDefaultFormatVisitor, visitSimpleDefaultFormatProvider) { + auto Provider = MockSimpleDefaultFormatProvider(); + EXPECT_CALL(Provider, getDefaultFormat()); + Provider.accept(OsAwareDefaultFormatVisitor(Triple::UnknownOS)); +} + +TEST(OsAwareDefaultFormatVisitor, visitOsAwareDefaultFormatProvider) { + auto Provider = MockOsAwareDefaultFormatProvider(); + EXPECT_CALL(Provider, getDefaultFormat(Triple::FreeBSD)); + EXPECT_CALL(Provider, getDefaultFormat(Triple::Linux)); + Provider.accept(OsAwareDefaultFormatVisitor(Triple::FreeBSD)); + Provider.accept(OsAwareDefaultFormatVisitor(Triple::Linux)); +} +} // namespace \ No newline at end of file Index: llvm/unittests/TargetParser/CMakeLists.txt =================================================================== --- llvm/unittests/TargetParser/CMakeLists.txt +++ llvm/unittests/TargetParser/CMakeLists.txt @@ -11,3 +11,5 @@ ) target_link_libraries(TargetParserTests PRIVATE LLVMTestingSupport) + +add_subdirectory(Architecture) \ No newline at end of file