0001
0002 #include "clang.h"
0003 #include "clang-c.h"
0004 extern "C" {
0005 #include "../util.h"
0006 }
0007 #include "llvm/IR/Function.h"
0008 #include "llvm/IR/LLVMContext.h"
0009
0010 #include <tests/llvm.h>
0011 #include <string>
0012
0013 class perf_clang_scope {
0014 public:
0015 explicit perf_clang_scope() {perf_clang__init();}
0016 ~perf_clang_scope() {perf_clang__cleanup();}
0017 };
0018
0019 static std::unique_ptr<llvm::Module>
0020 __test__clang_to_IR(void)
0021 {
0022 unsigned int kernel_version;
0023
0024 if (fetch_kernel_version(&kernel_version, NULL, 0))
0025 return std::unique_ptr<llvm::Module>(nullptr);
0026
0027 std::string cflag_kver("-DLINUX_VERSION_CODE=" +
0028 std::to_string(kernel_version));
0029
0030 std::unique_ptr<llvm::Module> M =
0031 perf::getModuleFromSource({cflag_kver.c_str()},
0032 "perf-test.c",
0033 test_llvm__bpf_base_prog);
0034 return M;
0035 }
0036
0037 extern "C" {
0038 int test__clang_to_IR(struct test_suite *test __maybe_unused,
0039 int subtest __maybe_unused)
0040 {
0041 perf_clang_scope _scope;
0042
0043 auto M = __test__clang_to_IR();
0044 if (!M)
0045 return -1;
0046 for (llvm::Function& F : *M)
0047 if (F.getName() == "bpf_func__SyS_epoll_pwait")
0048 return 0;
0049 return -1;
0050 }
0051
0052 int test__clang_to_obj(struct test_suite *test __maybe_unused,
0053 int subtest __maybe_unused)
0054 {
0055 perf_clang_scope _scope;
0056
0057 auto M = __test__clang_to_IR();
0058 if (!M)
0059 return -1;
0060
0061 auto Buffer = perf::getBPFObjectFromModule(&*M);
0062 if (!Buffer)
0063 return -1;
0064 return 0;
0065 }
0066
0067 }