Using libclang to build an LLVM Module from a Transform Pass?

Frontend noob here. I was attempting to play around with libclang and build another IR Module at transform stage.
Currently the solution I could think of is like this:

  • Use attribute((constructor)) to obtain raw arguments passed to CFE
  • Somehow use the arguments to build a CompilerInstance object
  • Run the frontend action EmitLLVMOnly and extract the build module

I’m not sure if this is the correct/easiest way to go or I screwed up something fundamentally. And if is the way to go, I failed to find any method to clean up the all the frontend input files and leave everything else intact. Any feedback is welcome.