1 //===----- AbstractCallSiteTest.cpp - AbstractCallSite Unittests ----------===//
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 
9 #include "llvm/AsmParser/Parser.h"
10 #include "llvm/IR/AbstractCallSite.h"
11 #include "llvm/IR/Function.h"
12 #include "llvm/IR/Module.h"
13 #include "llvm/Support/SourceMgr.h"
14 #include "gtest/gtest.h"
15 
16 using namespace llvm;
17 
parseIR(LLVMContext & C,const char * IR)18 static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) {
19   SMDiagnostic Err;
20   std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C);
21   if (!Mod)
22     Err.print("AbstractCallSiteTests", errs());
23   return Mod;
24 }
25 
TEST(AbstractCallSite,CallbackCall)26 TEST(AbstractCallSite, CallbackCall) {
27   LLVMContext C;
28 
29   const char *IR =
30       "define void @callback(i8* %X, i32* %A) {\n"
31       "  ret void\n"
32       "}\n"
33       "define void @foo(i32* %A) {\n"
34       "  call void (i32, void (i8*, ...)*, ...) @broker(i32 1, void (i8*, ...)* bitcast (void (i8*, i32*)* @callback to void (i8*, ...)*), i32* %A)\n"
35       "  ret void\n"
36       "}\n"
37       "declare !callback !0 void @broker(i32, void (i8*, ...)*, ...)\n"
38       "!0 = !{!1}\n"
39       "!1 = !{i64 1, i64 -1, i1 true}";
40 
41   std::unique_ptr<Module> M = parseIR(C, IR);
42   ASSERT_TRUE(M);
43 
44   Function *Callback = M->getFunction("callback");
45   ASSERT_NE(Callback, nullptr);
46 
47   const Use *CallbackUse = Callback->getSingleUndroppableUse();
48   ASSERT_NE(CallbackUse, nullptr);
49 
50   AbstractCallSite ACS(CallbackUse);
51   EXPECT_TRUE(ACS);
52   EXPECT_TRUE(ACS.isCallbackCall());
53   EXPECT_TRUE(ACS.isCallee(CallbackUse));
54   EXPECT_EQ(ACS.getCalledFunction(), Callback);
55 }
56