AbstractCallSiteTest.cpp
1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//===----- AbstractCallSiteTest.cpp - AbstractCallSite Unittests ----------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/AsmParser/Parser.h"
#include "llvm/IR/AbstractCallSite.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/SourceMgr.h"
#include "gtest/gtest.h"
using namespace llvm;
static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) {
SMDiagnostic Err;
std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C);
if (!Mod)
Err.print("AbstractCallSiteTests", errs());
return Mod;
}
TEST(AbstractCallSite, CallbackCall) {
LLVMContext C;
const char *IR =
"define void @callback(i8* %X, i32* %A) {\n"
" ret void\n"
"}\n"
"define void @foo(i32* %A) {\n"
" call void (i32, void (i8*, ...)*, ...) @broker(i32 1, void (i8*, ...)* bitcast (void (i8*, i32*)* @callback to void (i8*, ...)*), i32* %A)\n"
" ret void\n"
"}\n"
"declare !callback !0 void @broker(i32, void (i8*, ...)*, ...)\n"
"!0 = !{!1}\n"
"!1 = !{i64 1, i64 -1, i1 true}";
std::unique_ptr<Module> M = parseIR(C, IR);
ASSERT_TRUE(M);
Function *Callback = M->getFunction("callback");
ASSERT_NE(Callback, nullptr);
const Use *CallbackUse = Callback->getSingleUndroppableUse();
ASSERT_NE(CallbackUse, nullptr);
AbstractCallSite ACS(CallbackUse);
EXPECT_TRUE(ACS);
EXPECT_TRUE(ACS.isCallbackCall());
EXPECT_TRUE(ACS.isCallee(CallbackUse));
EXPECT_EQ(ACS.getCalledFunction(), Callback);
}