1//===----- unittests/RISCVAttributeParserTest.cpp -------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8#include "llvm/Support/RISCVAttributeParser.h"
9#include "llvm/Support/ARMBuildAttributes.h"
10#include "llvm/Support/ELFAttributes.h"
11#include "gtest/gtest.h"
12#include <string>
13
14using namespace llvm;
15
16struct RISCVAttributeSection {
17 unsigned Tag;
18 unsigned Value;
19
20 RISCVAttributeSection(unsigned tag, unsigned value)
21 : Tag(tag), Value(value) {}
22
23 void write(raw_ostream &OS) {
24 OS.flush();
25 // length = length + "riscv\0" + TagFile + ByteSize + Tag + Value;
26 // length = 17 bytes
27
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;
32 }
33};
34
35static bool testAttribute(unsigned Tag, unsigned Value, unsigned ExpectedTag,
36 unsigned ExpectedValue) {
37 std::string buffer;
38 raw_string_ostream OS(buffer);
39 RISCVAttributeSection Section(Tag, Value);
40 Section.write(OS);
41 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(OS.str().c_str()),
42 OS.str().size());
43
44 RISCVAttributeParser Parser;
45 cantFail(Err: Parser.parse(section: Bytes, endian: llvm::endianness::little));
46
47 std::optional<unsigned> Attr = Parser.getAttributeValue(tag: ExpectedTag);
48 return Attr && *Attr == ExpectedValue;
49}
50
51static bool testTagString(unsigned Tag, const char *name) {
52 return ELFAttrs::attrTypeAsString(attr: Tag, tagNameMap: RISCVAttrs::getRISCVAttributeTags())
53 .str() == name;
54}
55
56TEST(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));
60}
61
62TEST(UnalignedAccess, testAttribute) {
63 EXPECT_TRUE(testTagString(6, "Tag_unaligned_access"));
64 EXPECT_TRUE(testAttribute(6, 0, RISCVAttrs::UNALIGNED_ACCESS,
65 RISCVAttrs::NOT_ALLOWED));
66 EXPECT_TRUE(
67 testAttribute(6, 1, RISCVAttrs::UNALIGNED_ACCESS, RISCVAttrs::ALLOWED));
68}
69

source code of llvm/unittests/Support/RISCVAttributeParserTest.cpp