clang-test.cpp 1.2 KB

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