2
我有一個MatchFinder定義爲:替換返回在鐺RefactoringTool空地圖
MatchFinder Finder;
Finder.addMatcher(FunctionCallMatcher, &printer);
而且DeclarationMatcher和MatchCallback情況如下:
DeclarationMatcher FunctionCallMatcher = functionDecl(isDefinition()).bind("func");
class FuncPrinter : public MatchFinder::MatchCallback {
public :
FuncPrinter(){Replace = new Replacements;}
FuncPrinter(Replacements *Replace) : Replace(Replace) {}
virtual void run(const MatchFinder::MatchResult &Result) {
clang::ASTContext *Context = Result.Context;
if (const FunctionDecl *FS = Result.Nodes.getNodeAs<clang::FunctionDecl>("func")) {
FS->dump();
SourceRange sr = FS->getSourceRange();
std::string s = std::string("test");
Replacement Rep(*(Result.SourceManager), sr.getEnd(), s.length(), s);
if(llvm::Error err = Replace->add(Rep)) {
}
}
}
private:
Replacements *Replace;
};
我主要的代碼,我執行前端操作的取景器,並獲得替代:
RefactoringTool Tool(OptionsParser.getCompilations(),
OptionsParser.getSourcePathList());
FuncPrinter printer;
MatchFinder Finder;
Finder.addMatcher(FunctionCallMatcher, &printer);
if (int Result = Tool.run(newFrontendActionFactory(&Finder).get())) {
return Result;
}
std::map<std::string, Replacements>& reps = Tool.getReplacements();
雖然我可以觀察到,更換VA當FrontendAction被執行時,FuncPrinter中的riable會被填充,Tool.getReplacements()
返回一個空的std::map<std::string, Replacements>
。如果有人能想出我出錯的地方,我將不勝感激。