brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.4 KiB · 0e16d01 Raw
81 lines · cpp
1//===- GlobalObjectTest.cpp - Global object unit tests --------------------===//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-exception6//7//===----------------------------------------------------------------------===//8 9#include "llvm/IR/GlobalObject.h"10#include "llvm/AsmParser/Parser.h"11#include "llvm/IR/Module.h"12#include "llvm/Support/SourceMgr.h"13#include "gmock/gmock.h"14#include "gtest/gtest.h"15using namespace llvm;16namespace {17using testing::Eq;18using testing::Optional;19using testing::StrEq;20 21static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) {22  SMDiagnostic Err;23  std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C);24  if (!Mod)25    Err.print("GlobalObjectTests", errs());26  return Mod;27}28 29static LLVMContext C;30static std::unique_ptr<Module> M;31 32class GlobalObjectTest : public testing::Test {33public:34  static void SetUpTestSuite() {35    M = parseIR(C, R"(36@foo = global i32 3, !section_prefix !037@bar = global i32 038 39!0 = !{!"section_prefix", !"hot"}40)");41  }42};43 44TEST_F(GlobalObjectTest, SectionPrefix) {45  GlobalVariable *Foo = M->getGlobalVariable("foo");46 47  // Initial section prefix is hot.48  ASSERT_NE(Foo, nullptr);49  ASSERT_THAT(Foo->getSectionPrefix(), Optional(StrEq("hot")));50 51  // Test that set method returns false since existing section prefix is hot.52  EXPECT_FALSE(Foo->setSectionPrefix("hot"));53 54  // Set prefix from hot to unlikely.55  Foo->setSectionPrefix("unlikely");56  EXPECT_THAT(Foo->getSectionPrefix(), Optional(StrEq("unlikely")));57 58  // Set prefix to empty is the same as clear.59  Foo->setSectionPrefix("");60  // Test that section prefix is cleared.61  EXPECT_THAT(Foo->getSectionPrefix(), Eq(std::nullopt));62 63  GlobalVariable *Bar = M->getGlobalVariable("bar");64 65  // Initial section prefix is empty.66  ASSERT_NE(Bar, nullptr);67  ASSERT_THAT(Bar->getSectionPrefix(), Eq(std::nullopt));68 69  // Test that set method returns false since Bar doesn't have prefix metadata.70  EXPECT_FALSE(Bar->setSectionPrefix(""));71 72  // Set from empty to hot.73  EXPECT_TRUE(Bar->setSectionPrefix("hot"));74  EXPECT_THAT(Bar->getSectionPrefix(), Optional(StrEq("hot")));75 76  // Test that set method returns true and section prefix is cleared.77  EXPECT_TRUE(Bar->setSectionPrefix(""));78  EXPECT_THAT(Bar->getSectionPrefix(), Eq(std::nullopt));79}80} // namespace81