我能弄明白這一點。這是您需要的代碼。
在標題:
class MyAnalysis {
public:
static char ID;
MyAnalysis();
~MyAnalysis();
}
在來源:
struct MyPass : public llvm::ImmutablePass, public MyAnalysis {
static char ID;
MyPass() : llvm::ImmutablePass(ID) {
}
/// getAdjustedAnalysisPointer - This method is used when a pass implements
/// an analysis interface through multiple inheritance.
virtual void *getAdjustedAnalysisPointer(AnalysisID PI) {
if (PI == &MyAnalysis::ID)
return (MyAnalysis*)this;
return this;
}
};
struct UsingPass : public FunctionPass {
static char ID;
UsingPass() : FunctionPass(ID) {}
void getAnalysisUsage(AnalysisUsage &AU) const {
AU.addRequired<MyAnalysis>();
}
virtual bool runOnFunction(Function &F) {
MyAnalysis& info =getAnalysis<MyAnalysis>();
...
//use the analysis here
...
}
}
char MyAnalysis::ID = 0;
static llvm::RegisterAnalysisGroup<MyAnalysis> P("My Super Awesome Analysis");
char MyPass::ID = 0;
static llvm::RegisterPass<MyPass> X("my-pass", "My pass which provides MyAnalysis", true, true);
//This is the line that is necessary, but for some reason is never mentioned in the
//llvm source or documentation. This adds MyPass to the analysis group MyAnalysis.
//Note that the "true" arguemnt to the macro makes MyPass the default implementation.
//Omit it to register other passes.
static llvm::RegisterAnalysisGroup<MyAnalysis,true> Y(X);
char UsingPass::ID = 0;
static RegisterPass<UsingPass> X("using-pass", "Some other pass that uses MyAnalysis", true, true);
爲了使它更清楚,當我試圖按照寫作的LLVM通文檔中給出的說明,我得到一個錯誤「兩次有相同論點的通行證......試圖登記」 –