Search code examples
xcodeclangllvmllvm-clang

Use LLVM/Clang to find fclose() calls in an Xcode project


I would like to learn how I might programmatically integrate with LLVM/Clang to find all of the fclose() calls in my Xcode project. I realize I can accomplish this via normal text searching but this is just the first step in a more detailed problem.


Solution

  • You can write function pass and find the name of the function as below:

    #include "llvm/Pass.h"
    #include "llvm/IR/Function.h"
    #include "llvm/Support/raw_ostream.h"
    
    using namespace llvm;
    
    namespace {
    struct Hello : public FunctionPass {
    static char ID;
    Hello() : FunctionPass(ID) {}
    
        virtual bool runOnFunction(Function &F) {
          errs() << "Hello: ";
          errs().write_escaped(F.getName()) << '\n';
          return false;
        }
      };
    }
    
    char Hello::ID = 0;
    static RegisterPass<Hello> X("hello", "Hello World Pass", false, false);
    

    Call this pass from opt using opt -hello input.ll and you will get the names of all functions printed. Change the logic in the above code to find your required function. See the following link for more details on writing passes:

    http://llvm.org/docs/WritingAnLLVMPass.html