clang-test.cpp 1.2 KB

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