如何使用 clang-tool 检测 SEXP 类型
How to detect SEXP type by using clang-tool
我想使用 clang-tool 检测“SEXP”return 类型的函数,并打印它。
我的 clang 工具:
//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;
}
我的 test.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 类型?
我找到问题所在了。
需要在代码开头加上#include<R.h> #include<Rinternal.h>
R的header在/usr/share/R/include
但是clang的路径会在正常的include路径中找到header,所以我们需要将它们复制到/usr/local/include/
我想使用 clang-tool 检测“SEXP”return 类型的函数,并打印它。
我的 clang 工具:
//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;
}
我的 test.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 类型?
我找到问题所在了。
需要在代码开头加上
#include<R.h> #include<Rinternal.h>
R的header在
/usr/share/R/include
但是clang的路径会在正常的include路径中找到header,所以我们需要将它们复制到/usr/local/include/