[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / llvm / unittests / Support / RISCVAttributeParserTest.cpp
bloba3aa88a91b2fdf87cb4b390a37cb94b276039cd4
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>
14 using namespace llvm;
16 struct RISCVAttributeSection {
17 unsigned Tag;
18 unsigned Value;
20 RISCVAttributeSection(unsigned tag, unsigned value)
21 : Tag(tag), Value(value) {}
23 void write(raw_ostream &OS) {
24 OS.flush();
25 // length = length + "riscv\0" + TagFile + ByteSize + Tag + Value;
26 // length = 17 bytes
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) {
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());
44 RISCVAttributeParser Parser;
45 cantFail(Parser.parse(Bytes, support::little));
47 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())
53 .str() == name;
56 TEST(StackAlign, testAttribute) {
57 EXPECT_TRUE(testTagString(4, "Tag_stack_align"));
58 EXPECT_TRUE(
59 testAttribute(4, 4, RISCVAttrs::STACK_ALIGN, RISCVAttrs::ALIGN_4));
60 EXPECT_TRUE(
61 testAttribute(4, 16, RISCVAttrs::STACK_ALIGN, RISCVAttrs::ALIGN_16));
64 TEST(UnalignedAccess, testAttribute) {
65 EXPECT_TRUE(testTagString(6, "Tag_unaligned_access"));
66 EXPECT_TRUE(testAttribute(6, 0, RISCVAttrs::UNALIGNED_ACCESS,
67 RISCVAttrs::NOT_ALLOWED));
68 EXPECT_TRUE(
69 testAttribute(6, 1, RISCVAttrs::UNALIGNED_ACCESS, RISCVAttrs::ALLOWED));