1 //===- llvm/unittest/IR/ManglerTest.cpp - Mangler unit tests --------------===//
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 //===----------------------------------------------------------------------===//
9 #include "llvm/IR/Mangler.h"
10 #include "llvm/IR/CallingConv.h"
11 #include "llvm/IR/DataLayout.h"
12 #include "llvm/IR/GlobalValue.h"
13 #include "llvm/IR/Module.h"
14 #include "gtest/gtest.h"
18 static std::string
mangleStr(StringRef IRName
, Mangler
&Mang
,
19 const DataLayout
&DL
) {
21 raw_string_ostream
SS(Mangled
);
22 Mang
.getNameWithPrefix(SS
, IRName
, DL
);
27 static std::string
mangleFunc(StringRef IRName
,
28 GlobalValue::LinkageTypes Linkage
,
29 llvm::CallingConv::ID CC
, Module
&Mod
,
31 Type
*VoidTy
= Type::getVoidTy(Mod
.getContext());
32 Type
*I32Ty
= Type::getInt32Ty(Mod
.getContext());
34 FunctionType::get(VoidTy
, {I32Ty
, I32Ty
, I32Ty
}, /*isVarArg=*/false);
35 Function
*F
= Function::Create(FTy
, Linkage
, IRName
, &Mod
);
36 F
->setCallingConv(CC
);
38 raw_string_ostream
SS(Mangled
);
39 Mang
.getNameWithPrefix(SS
, F
, false);
47 TEST(ManglerTest
, MachO
) {
49 DataLayout
DL("m:o"); // macho
50 Module
Mod("test", Ctx
);
51 Mod
.setDataLayout(DL
);
53 EXPECT_EQ(mangleStr("foo", Mang
, DL
), "_foo");
54 EXPECT_EQ(mangleStr("\01foo", Mang
, DL
), "foo");
55 EXPECT_EQ(mangleStr("?foo", Mang
, DL
), "_?foo");
56 EXPECT_EQ(mangleFunc("foo", llvm::GlobalValue::ExternalLinkage
,
57 llvm::CallingConv::C
, Mod
, Mang
),
59 EXPECT_EQ(mangleFunc("?foo", llvm::GlobalValue::ExternalLinkage
,
60 llvm::CallingConv::C
, Mod
, Mang
),
62 EXPECT_EQ(mangleFunc("foo", llvm::GlobalValue::PrivateLinkage
,
63 llvm::CallingConv::C
, Mod
, Mang
),
67 TEST(ManglerTest
, WindowsX86
) {
69 DataLayout
DL("m:x-p:32:32"); // 32-bit windows
70 Module
Mod("test", Ctx
);
71 Mod
.setDataLayout(DL
);
73 EXPECT_EQ(mangleStr("foo", Mang
, DL
), "_foo");
74 EXPECT_EQ(mangleStr("\01foo", Mang
, DL
), "foo");
75 EXPECT_EQ(mangleStr("?foo", Mang
, DL
), "?foo");
76 EXPECT_EQ(mangleFunc("foo", llvm::GlobalValue::ExternalLinkage
,
77 llvm::CallingConv::C
, Mod
, Mang
),
79 EXPECT_EQ(mangleFunc("?foo", llvm::GlobalValue::ExternalLinkage
,
80 llvm::CallingConv::C
, Mod
, Mang
),
82 EXPECT_EQ(mangleFunc("foo", llvm::GlobalValue::PrivateLinkage
,
83 llvm::CallingConv::C
, Mod
, Mang
),
86 // Test calling conv mangling.
87 EXPECT_EQ(mangleFunc("stdcall", llvm::GlobalValue::ExternalLinkage
,
88 llvm::CallingConv::X86_StdCall
, Mod
, Mang
),
90 EXPECT_EQ(mangleFunc("fastcall", llvm::GlobalValue::ExternalLinkage
,
91 llvm::CallingConv::X86_FastCall
, Mod
, Mang
),
93 EXPECT_EQ(mangleFunc("vectorcall", llvm::GlobalValue::ExternalLinkage
,
94 llvm::CallingConv::X86_VectorCall
, Mod
, Mang
),
97 // Adding a '?' prefix blocks calling convention mangling.
98 EXPECT_EQ(mangleFunc("?fastcall", llvm::GlobalValue::ExternalLinkage
,
99 llvm::CallingConv::X86_FastCall
, Mod
, Mang
),
103 TEST(ManglerTest
, WindowsX64
) {
105 DataLayout
DL("m:w-p:64:64"); // windows
106 Module
Mod("test", Ctx
);
107 Mod
.setDataLayout(DL
);
109 EXPECT_EQ(mangleStr("foo", Mang
, DL
), "foo");
110 EXPECT_EQ(mangleStr("\01foo", Mang
, DL
), "foo");
111 EXPECT_EQ(mangleStr("?foo", Mang
, DL
), "?foo");
112 EXPECT_EQ(mangleFunc("foo", llvm::GlobalValue::ExternalLinkage
,
113 llvm::CallingConv::C
, Mod
, Mang
),
115 EXPECT_EQ(mangleFunc("?foo", llvm::GlobalValue::ExternalLinkage
,
116 llvm::CallingConv::C
, Mod
, Mang
),
118 EXPECT_EQ(mangleFunc("foo", llvm::GlobalValue::PrivateLinkage
,
119 llvm::CallingConv::C
, Mod
, Mang
),
122 // Test calling conv mangling.
123 EXPECT_EQ(mangleFunc("stdcall", llvm::GlobalValue::ExternalLinkage
,
124 llvm::CallingConv::X86_StdCall
, Mod
, Mang
),
126 EXPECT_EQ(mangleFunc("fastcall", llvm::GlobalValue::ExternalLinkage
,
127 llvm::CallingConv::X86_FastCall
, Mod
, Mang
),
129 EXPECT_EQ(mangleFunc("vectorcall", llvm::GlobalValue::ExternalLinkage
,
130 llvm::CallingConv::X86_VectorCall
, Mod
, Mang
),
133 // Adding a '?' prefix blocks calling convention mangling.
134 EXPECT_EQ(mangleFunc("?vectorcall", llvm::GlobalValue::ExternalLinkage
,
135 llvm::CallingConv::X86_VectorCall
, Mod
, Mang
),
139 TEST(ManglerTest
, XCOFF
) {
141 DataLayout
DL("m:a"); // XCOFF/AIX
142 Module
Mod("test", Ctx
);
143 Mod
.setDataLayout(DL
);
145 EXPECT_EQ(mangleStr("foo", Mang
, DL
), "foo");
146 EXPECT_EQ(mangleStr("\01foo", Mang
, DL
), "foo");
147 EXPECT_EQ(mangleStr("?foo", Mang
, DL
), "?foo");
148 EXPECT_EQ(mangleFunc("foo", llvm::GlobalValue::ExternalLinkage
,
149 llvm::CallingConv::C
, Mod
, Mang
),
151 EXPECT_EQ(mangleFunc("?foo", llvm::GlobalValue::ExternalLinkage
,
152 llvm::CallingConv::C
, Mod
, Mang
),
154 EXPECT_EQ(mangleFunc("foo", llvm::GlobalValue::PrivateLinkage
,
155 llvm::CallingConv::C
, Mod
, Mang
),
159 TEST(ManglerTest
, GOFF
) {
161 DataLayout
DL("m:l"); // GOFF
162 Module
Mod("test", Ctx
);
163 Mod
.setDataLayout(DL
);
166 EXPECT_EQ(mangleStr("foo", Mang
, DL
), "foo");
167 EXPECT_EQ(mangleStr("\01foo", Mang
, DL
), "foo");
168 EXPECT_EQ(mangleStr("?foo", Mang
, DL
), "?foo");
169 EXPECT_EQ(mangleFunc("foo", llvm::GlobalValue::ExternalLinkage
,
170 llvm::CallingConv::C
, Mod
, Mang
),
172 EXPECT_EQ(mangleFunc("foo", llvm::GlobalValue::PrivateLinkage
,
173 llvm::CallingConv::C
, Mod
, Mang
),
177 } // end anonymous namespace