clang.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * llvm C frontend for perf. Support dynamically compile C file
  3. *
  4. * Inspired by clang example code:
  5. * http://llvm.org/svn/llvm-project/cfe/trunk/examples/clang-interpreter/main.cpp
  6. *
  7. * Copyright (C) 2016 Wang Nan <wangnan0@huawei.com>
  8. * Copyright (C) 2016 Huawei Inc.
  9. */
  10. #include "clang/CodeGen/CodeGenAction.h"
  11. #include "clang/Frontend/CompilerInvocation.h"
  12. #include "clang/Frontend/CompilerInstance.h"
  13. #include "clang/Frontend/TextDiagnosticPrinter.h"
  14. #include "clang/Tooling/Tooling.h"
  15. #include "llvm/IR/Module.h"
  16. #include "llvm/Option/Option.h"
  17. #include "llvm/Support/FileSystem.h"
  18. #include "llvm/Support/ManagedStatic.h"
  19. #include <memory>
  20. #include "clang.h"
  21. #include "clang-c.h"
  22. namespace perf {
  23. static std::unique_ptr<llvm::LLVMContext> LLVMCtx;
  24. using namespace clang;
  25. static CompilerInvocation *
  26. createCompilerInvocation(StringRef& Path, DiagnosticsEngine& Diags)
  27. {
  28. llvm::opt::ArgStringList CCArgs {
  29. "-cc1",
  30. "-triple", "bpf-pc-linux",
  31. "-fsyntax-only",
  32. "-ferror-limit", "19",
  33. "-fmessage-length", "127",
  34. "-O2",
  35. "-nostdsysteminc",
  36. "-nobuiltininc",
  37. "-vectorize-loops",
  38. "-vectorize-slp",
  39. "-Wno-unused-value",
  40. "-Wno-pointer-sign",
  41. "-x", "c"};
  42. CompilerInvocation *CI = tooling::newInvocation(&Diags, CCArgs);
  43. FrontendOptions& Opts = CI->getFrontendOpts();
  44. Opts.Inputs.clear();
  45. Opts.Inputs.emplace_back(Path, IK_C);
  46. return CI;
  47. }
  48. static std::unique_ptr<llvm::Module>
  49. getModuleFromSource(StringRef Path,
  50. IntrusiveRefCntPtr<vfs::FileSystem> VFS)
  51. {
  52. CompilerInstance Clang;
  53. Clang.createDiagnostics();
  54. Clang.setVirtualFileSystem(&*VFS);
  55. IntrusiveRefCntPtr<CompilerInvocation> CI =
  56. createCompilerInvocation(Path, Clang.getDiagnostics());
  57. Clang.setInvocation(&*CI);
  58. std::unique_ptr<CodeGenAction> Act(new EmitLLVMOnlyAction(&*LLVMCtx));
  59. if (!Clang.ExecuteAction(*Act))
  60. return std::unique_ptr<llvm::Module>(nullptr);
  61. return Act->takeModule();
  62. }
  63. std::unique_ptr<llvm::Module>
  64. getModuleFromSource(StringRef Name, StringRef Content)
  65. {
  66. using namespace vfs;
  67. llvm::IntrusiveRefCntPtr<OverlayFileSystem> OverlayFS(
  68. new OverlayFileSystem(getRealFileSystem()));
  69. llvm::IntrusiveRefCntPtr<InMemoryFileSystem> MemFS(
  70. new InMemoryFileSystem(true));
  71. /*
  72. * pushOverlay helps setting working dir for MemFS. Must call
  73. * before addFile.
  74. */
  75. OverlayFS->pushOverlay(MemFS);
  76. MemFS->addFile(Twine(Name), 0, llvm::MemoryBuffer::getMemBuffer(Content));
  77. return getModuleFromSource(Name, OverlayFS);
  78. }
  79. std::unique_ptr<llvm::Module>
  80. getModuleFromSource(StringRef Path)
  81. {
  82. IntrusiveRefCntPtr<vfs::FileSystem> VFS(vfs::getRealFileSystem());
  83. return getModuleFromSource(Path, VFS);
  84. }
  85. }
  86. extern "C" {
  87. void perf_clang__init(void)
  88. {
  89. perf::LLVMCtx.reset(new llvm::LLVMContext());
  90. }
  91. void perf_clang__cleanup(void)
  92. {
  93. perf::LLVMCtx.reset(nullptr);
  94. llvm::llvm_shutdown();
  95. }
  96. }