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