如何使用clang工具检测SEXP类型



我想检测"SEXP";使用clang工具返回type函数,并将其打印出来。

我的叮当声工具:

//define vistor
class myVisitor : public RecursiveASTVisitor<myVisitor>{
private:
ASTContext *ast_c ;
public:
explicit myVisitor(CompilerInstance *CI): ast_c(&(CI->getASTContext())){
myrewrite.setSourceMgr(ast_c->getSourceManager(), ast_c->getLangOpts());
}
virtual bool VisitFunctionDecl(FunctionDecl *func){
string funcType = func->getDeclaredReturnTypeSourceRange().getAsString();
std::cerr<<"type is "<<funcType<<endl;
return true;
}
};
//define consumer
class myASTConsumer : public ASTConsumer {
private:
myVisitor *visitor; 
public:
explicit myASTConsumer(CompilerInstance *CI): visitor(new myVisitor(CI)) {}// initialize the visitor
virtual void HandleTranslationUnit(ASTContext &Context) {
visitor->TraverseDecl(Context.getTranslationUnitDecl());
}
};
//define frontend action
class FA : public ASTFrontendAction {
public:
FA() {}
virtual std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, StringRef file) {
return  make_unique<myASTConsumer>(&CI);
}
};
int main(int argc , const char **argv){
auto ExpectedParser = CommonOptionsParser::create(argc, argv, MyToolCategory);
if (!ExpectedParser) {
// Fail gracefully for unsupported options.
llvm::errs() << ExpectedParser.takeError();
return 1;
}
CommonOptionsParser& op = ExpectedParser.get();
ClangTool Tool(op.getCompilations(),op.getSourcePathList());
int result = Tool.run(newFrontendActionFactory<FA>().get());
return result;
}

我的测试.cpp:

SEXP test() { 
int a=0;
SEXP result;
for(int i=0;i<2;i++){
a++;
}
return result; 
}

预期输出为type is SEXP

但结果是type is int,错误消息未知类型名称"SEXP">

如何编辑我的工具以获得SEXP类型的函数?

我发现了问题所在。

  1. 需要在代码的顶部添加#include<R.h> #include<Rinternal.h>

  2. R的标头在/usr/share/R/include中但是clang的路径会在正常的include路径中找到头,所以我们需要将它们复制到/usr/local/include/

最新更新