2017-02-27 67 views
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>。如果有人能想出我出錯的地方,我將不勝感激。

回答

0

管理解決它,所以張貼我自己的解決方案。問題在於使用默認構造函數,而構造函數的FuncPrinter(reps_t *reps) does the trick.

class FuncPrinter : public MatchFinder::MatchCallback { 
    using reps_t = std::map<std::string, Replacements>;  
public : 
    FuncPrinter(reps_t *reps) : Replace(reps) {} 
    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); 
     Replace->insert(std::pair<std::string,Replacements>(path, Replacements(Rep))); 
    } 
    } 
private: 
    reps_t *Replace; 
};