GNU Linux-libre 5.10.217-gnu1
[releases.git] / tools / perf / util / c++ / clang-test.cpp
1 // SPDX-License-Identifier: GPL-2.0
2 #include "clang.h"
3 #include "clang-c.h"
4 extern "C" {
5 #include "../util.h"
6 }
7 #include "llvm/IR/Function.h"
8 #include "llvm/IR/LLVMContext.h"
9
10 #include <tests/llvm.h>
11 #include <string>
12
13 class perf_clang_scope {
14 public:
15         explicit perf_clang_scope() {perf_clang__init();}
16         ~perf_clang_scope() {perf_clang__cleanup();}
17 };
18
19 static std::unique_ptr<llvm::Module>
20 __test__clang_to_IR(void)
21 {
22         unsigned int kernel_version;
23
24         if (fetch_kernel_version(&kernel_version, NULL, 0))
25                 return std::unique_ptr<llvm::Module>(nullptr);
26
27         std::string cflag_kver("-DLINUX_VERSION_CODE=" +
28                                 std::to_string(kernel_version));
29
30         std::unique_ptr<llvm::Module> M =
31                 perf::getModuleFromSource({cflag_kver.c_str()},
32                                           "perf-test.c",
33                                           test_llvm__bpf_base_prog);
34         return M;
35 }
36
37 extern "C" {
38 int test__clang_to_IR(void)
39 {
40         perf_clang_scope _scope;
41
42         auto M = __test__clang_to_IR();
43         if (!M)
44                 return -1;
45         for (llvm::Function& F : *M)
46                 if (F.getName() == "bpf_func__SyS_epoll_pwait")
47                         return 0;
48         return -1;
49 }
50
51 int test__clang_to_obj(void)
52 {
53         perf_clang_scope _scope;
54
55         auto M = __test__clang_to_IR();
56         if (!M)
57                 return -1;
58
59         auto Buffer = perf::getBPFObjectFromModule(&*M);
60         if (!Buffer)
61                 return -1;
62         return 0;
63 }
64
65 }