|
| 1 | +//===- GlobalObjectTest.cpp - Global Object unit tests |
| 2 | +//-----------------------------===// |
| 3 | +// |
| 4 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | +// See https://llvm.org/LICENSE.txt for license information. |
| 6 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | +// |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | + |
| 10 | +#include "llvm-c/Core.h" |
| 11 | +#include "llvm/AsmParser/Parser.h" |
| 12 | +#include "llvm/IR/Function.h" |
| 13 | +#include "llvm/IR/Module.h" |
| 14 | +#include "llvm/Support/SourceMgr.h" |
| 15 | +#include "gtest/gtest.h" |
| 16 | +using namespace llvm; |
| 17 | + |
| 18 | +namespace { |
| 19 | + |
| 20 | +static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) { |
| 21 | + SMDiagnostic Err; |
| 22 | + std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C); |
| 23 | + if (!Mod) |
| 24 | + Err.print("GlobalObjectTest", errs()); |
| 25 | + return Mod; |
| 26 | +} |
| 27 | + |
| 28 | +} // namespace |
| 29 | + |
| 30 | +TEST(GlobalObjectTest, updateSectionPrefix) { |
| 31 | + LLVMContext C; |
| 32 | + |
| 33 | + std::unique_ptr<Module> M = parseIR(C, R"( |
| 34 | +@foo = global i32 1, !section_prefix !0 |
| 35 | +@bar = global i32 2 |
| 36 | +@baz = global i32 0, !section_prefix !1 |
| 37 | +
|
| 38 | +!0 = !{!"section_prefix", !"hot"} |
| 39 | +!1 = !{!"section_prefix", !"unlikely"} |
| 40 | +)"); |
| 41 | + |
| 42 | + GlobalVariable *Foo = M->getGlobalVariable("foo"); |
| 43 | + EXPECT_EQ(Foo->getSectionPrefix(), "hot"); |
| 44 | + Foo->updateSectionPrefix("unlikely", std::make_optional(StringRef("hot"))); |
| 45 | + EXPECT_EQ(Foo->getSectionPrefix(), "hot"); |
| 46 | + |
| 47 | + GlobalVariable *Bar = M->getGlobalVariable("bar"); |
| 48 | + EXPECT_EQ(Bar->getSectionPrefix(), std::nullopt); |
| 49 | + Bar->setSectionPrefix("unlikely"); |
| 50 | + EXPECT_EQ(Bar->getSectionPrefix(), "unlikely"); |
| 51 | + |
| 52 | + GlobalVariable *Baz = M->getGlobalVariable("baz"); |
| 53 | + EXPECT_EQ(Baz->getSectionPrefix(), "unlikely"); |
| 54 | + Baz->updateSectionPrefix("hot"); |
| 55 | + EXPECT_EQ(Baz->getSectionPrefix(), "hot"); |
| 56 | +} |
0 commit comments