1 //===----- unittests/RISCVAttributeParserTest.cpp -------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
8 #include "llvm/Support/RISCVAttributeParser.h"
9 #include "llvm/Support/ARMBuildAttributes.h"
10 #include "llvm/Support/ELFAttributes.h"
11 #include "gtest/gtest.h"
16 struct RISCVAttributeSection
{
20 RISCVAttributeSection(unsigned tag
, unsigned value
)
21 : Tag(tag
), Value(value
) {}
23 void write(raw_ostream
&OS
) {
25 // length = length + "riscv\0" + TagFile + ByteSize + Tag + Value;
28 OS
<< 'A' << (uint8_t)17 << (uint8_t)0 << (uint8_t)0 << (uint8_t)0;
29 OS
<< "riscv" << '\0';
30 OS
<< (uint8_t)1 << (uint8_t)7 << (uint8_t)0 << (uint8_t)0 << (uint8_t)0;
31 OS
<< (uint8_t)Tag
<< (uint8_t)Value
;
35 static bool testAttribute(unsigned Tag
, unsigned Value
, unsigned ExpectedTag
,
36 unsigned ExpectedValue
) {
38 raw_string_ostream
OS(buffer
);
39 RISCVAttributeSection
Section(Tag
, Value
);
41 ArrayRef
<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(OS
.str().c_str()),
44 RISCVAttributeParser Parser
;
45 cantFail(Parser
.parse(Bytes
, llvm::endianness::little
));
47 std::optional
<unsigned> Attr
= Parser
.getAttributeValue(ExpectedTag
);
48 return Attr
&& *Attr
== ExpectedValue
;
51 static bool testTagString(unsigned Tag
, const char *name
) {
52 return ELFAttrs::attrTypeAsString(Tag
, RISCVAttrs::getRISCVAttributeTags())
56 TEST(StackAlign
, testAttribute
) {
57 EXPECT_TRUE(testTagString(4, "Tag_stack_align"));
58 EXPECT_TRUE(testAttribute(4, 4, RISCVAttrs::STACK_ALIGN
, 4));
59 EXPECT_TRUE(testAttribute(4, 16, RISCVAttrs::STACK_ALIGN
, 16));
62 TEST(UnalignedAccess
, testAttribute
) {
63 EXPECT_TRUE(testTagString(6, "Tag_unaligned_access"));
64 EXPECT_TRUE(testAttribute(6, 0, RISCVAttrs::UNALIGNED_ACCESS
,
65 RISCVAttrs::NOT_ALLOWED
));
67 testAttribute(6, 1, RISCVAttrs::UNALIGNED_ACCESS
, RISCVAttrs::ALLOWED
));