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:
perf_clang_scope()15 	explicit perf_clang_scope() {perf_clang__init();}
~perf_clang_scope()16 	~perf_clang_scope() {perf_clang__cleanup();}
17 };
18 
19 static std::unique_ptr<llvm::Module>
__test__clang_to_IR(void)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" {
test__clang_to_IR(struct test_suite * test __maybe_unused,int subtest __maybe_unused)38 int test__clang_to_IR(struct test_suite *test __maybe_unused,
39                       int subtest __maybe_unused)
40 {
41 	perf_clang_scope _scope;
42 
43 	auto M = __test__clang_to_IR();
44 	if (!M)
45 		return -1;
46 	for (llvm::Function& F : *M)
47 		if (F.getName() == "bpf_func__SyS_epoll_pwait")
48 			return 0;
49 	return -1;
50 }
51 
test__clang_to_obj(struct test_suite * test __maybe_unused,int subtest __maybe_unused)52 int test__clang_to_obj(struct test_suite *test __maybe_unused,
53                        int subtest __maybe_unused)
54 {
55 	perf_clang_scope _scope;
56 
57 	auto M = __test__clang_to_IR();
58 	if (!M)
59 		return -1;
60 
61 	auto Buffer = perf::getBPFObjectFromModule(&*M);
62 	if (!Buffer)
63 		return -1;
64 	return 0;
65 }
66 
67 }
68