在 clang RefactoringTool 中替换返回空地图
Replacements returning empty map in clang RefactoringTool
我有一个 MatchFinder 定义为:
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();
尽管我可以观察到 FuncPrinter 中的 Replace 变量在执行 FrontendAction 时被填充,Tool.getReplacements()
return 一个空的 std::map<std::string, Replacements>
。如果有人能指出我哪里出错了,我将不胜感激。
设法解决了它,所以发布了我自己的解决方案。问题在于使用默认构造函数,而带有 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;
};
我有一个 MatchFinder 定义为:
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();
尽管我可以观察到 FuncPrinter 中的 Replace 变量在执行 FrontendAction 时被填充,Tool.getReplacements()
return 一个空的 std::map<std::string, Replacements>
。如果有人能指出我哪里出错了,我将不胜感激。
设法解决了它,所以发布了我自己的解决方案。问题在于使用默认构造函数,而带有 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;
};