I want to detect "SEXP" return type function by using clang-tool , and print it .
My clang tool :
//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;
}
My test.cpp :
SEXP test() {
int a=0;
SEXP result;
for(int i=0;i<2;i++){
a++;
}
return result;
}
The expected output is type is SEXP
But the result is type is int
, with error message unknown type name 'SEXP'
How do I edit my tool to get the SEXP type of a function ?
I found where the problem is .
Need to add #include<R.h> #include<Rinternal.h>
at the top of code
The headers of R are in /usr/share/R/include
But the path of clang will find header in normal include path , so we need to copy them into /usr/local/include/