2017-02-27 1 views
2

J'ai un MatchFinder défini comme:Remplacements retour carte vide clang RefactoringTool

MatchFinder Finder; 
Finder.addMatcher(FunctionCallMatcher, &printer); 

Et le DeclarationMatcher et le MatchCallback sont comme ci-dessous:

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; 
}; 

Dans mon code principal, j'exécute la action frontal pour le viseur, et obtenir les remplacements:

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(); 

Bien que je peux observer que le remplacer va Riable dans le FuncPrinter être rempli lorsque le FrontendAction est exécuté, Tool.getReplacements() retourner un vide std::map<std::string, Replacements>. Je serais reconnaissant si quelqu'un peut venir avec où je me suis trompé.

Répondre

0

Géré pour le résoudre, affichant ainsi ma propre solution. Le problème était dans l'utilisation du constructeur par défaut, alors que le constructeur avec le 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; 
};