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.
Asked
Active
Viewed 109 times
0

RobertJoseph
- 7,968
- 12
- 68
- 113
1 Answers
1
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:

shrm
- 1,112
- 2
- 8
- 20
-
Thank you! Do you happen to know if I can hook in early enough to detect macro definitions? I looked at the Pass sublclasses but don't see anything that fits. – RobertJoseph Jun 29 '14 at 14:22
-
1@RobertJoseph I am afraid you cannot do that in a pass like this, because these kind of passes take the IR as an input, which in turn means that the frontend (Clang for example) has already parsed the whole code and spit out the IR. Remember that macros are replaced as is by the preprocessor. So in order to get a handle on macros, you will need to play around with Clang's parser modules. I am not an expert of Clang though. – shrm Jun 29 '14 at 14:30