This can be done using the module pass. Below is my code, and if you need help, you can look here .
bar.c
int your_fun(int arg2) { int x = arg2; return x+2; }
Skeleton.cpp
#include "llvm/Pass.h" #include "llvm/IR/Module.h" #include "llvm/Support/raw_ostream.h" #include "llvm/IR/LegacyPassManager.h" #include "llvm/Transforms/IPO/PassManagerBuilder.h" using namespace llvm; namespace { struct SkeletonPass : public ModulePass { static char ID; SkeletonPass() : ModulePass(ID) {} virtual bool runOnModule(Module &M) { for (auto& F : M) { errs() << "\tFunction: " << F.getName() << "\n"; for (auto& BB : F) { errs() << "\t\tBasic Block: " << BB.getName() << "\n"; for (auto& I : BB) { errs() << "\t\t\tInstruction: " << I.getOpcodeName() << "\n"; } } } return false; } }; } char SkeletonPass::ID = 0;
Conclusion:
| => clang -Xclang -load -Xclang build/skeleton/libSkeletonPass.so foo.c bar.c Module: foo.c! Function: my_fun! Basicblock: entry! Instruction: alloca Instruction: alloca Instruction: store Instruction: load Instruction: store Instruction: load Instruction: add Instruction: ret Function: main! Basicblock: entry! Instruction: alloca Instruction: alloca Instruction: alloca Instruction: alloca Instruction: alloca Instruction: store Instruction: store Instruction: store Instruction: store Instruction: store Instruction: load Instruction: icmp Instruction: br Basicblock: if.then! Instruction: load Instruction: store Instruction: br Basicblock: if.else! Instruction: load Instruction: call Instruction: store Instruction: br Basicblock: if.end! Instruction: load Instruction: ret Module: bar.c! Function: your_fun! Basicblock: entry! Instruction: alloca Instruction: alloca Instruction: store Instruction: load Instruction: store Instruction: load Instruction: add Instruction: ret
Exit: if you include a link to the header file in bar.c
Module: foo.c! Function: your_fun! Basicblock: entry! Instruction: alloca Instruction: alloca Instruction: store Instruction: load Instruction: store Instruction: load Instruction: add Instruction: ret Function: my_fun! Basicblock: entry! Instruction: alloca Instruction: alloca Instruction: store Instruction: load Instruction: store Instruction: load Instruction: add Instruction: ret Function: main! Basicblock: entry! Instruction: alloca Instruction: alloca Instruction: alloca Instruction: alloca Instruction: alloca Instruction: store Instruction: store Instruction: store Instruction: store Instruction: store Instruction: load Instruction: icmp Instruction: br Basicblock: if.then! Instruction: load Instruction: store Instruction: br Basicblock: if.else! Instruction: load Instruction: call Instruction: store Instruction: load Instruction: call Instruction: store Instruction: br Basicblock: if.end! Instruction: load Instruction: ret
user5390668
source share