Merge tag 'block-5.9-2020-08-14' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / tools / perf / util / c++ / clang-test.cpp
blob21b23605f78b74349994c5782884d4d23ca4f5f9
1 // SPDX-License-Identifier: GPL-2.0
2 #include "clang.h"
3 #include "clang-c.h"
4 extern "C" {
5 #include "../util.h"
7 #include "llvm/IR/Function.h"
8 #include "llvm/IR/LLVMContext.h"
10 #include <tests/llvm.h>
11 #include <string>
13 class perf_clang_scope {
14 public:
15 explicit perf_clang_scope() {perf_clang__init();}
16 ~perf_clang_scope() {perf_clang__cleanup();}
19 static std::unique_ptr<llvm::Module>
20 __test__clang_to_IR(void)
22 unsigned int kernel_version;
24 if (fetch_kernel_version(&kernel_version, NULL, 0))
25 return std::unique_ptr<llvm::Module>(nullptr);
27 std::string cflag_kver("-DLINUX_VERSION_CODE=" +
28 std::to_string(kernel_version));
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;
37 extern "C" {
38 int test__clang_to_IR(void)
40 perf_clang_scope _scope;
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;
51 int test__clang_to_obj(void)
53 perf_clang_scope _scope;
55 auto M = __test__clang_to_IR();
56 if (!M)
57 return -1;
59 auto Buffer = perf::getBPFObjectFromModule(&*M);
60 if (!Buffer)
61 return -1;
62 return 0;