C++ 如何使用clang将Return语句放入块中
我试图解析C文件,并使用clang在函数的出口点添加调试 我可以在返回之前添加调试,或者使用以下代码退出函数C++ 如何使用clang将Return语句放入块中,c++,clang,C++,Clang,我试图解析C文件,并使用clang在函数的出口点添加调试 我可以在返回之前添加调试,或者使用以下代码退出函数 if (isa<ReturnStmt>(s)) { ReturnStmt *ReturnStatement = cast<ReturnStmt>(s); TheRewriter.InsertText(ReturnStatement->getLocStart(), "{printf(\"[%s:%d] OUT \\n\", __FUNCTION__,
if (isa<ReturnStmt>(s)) {
ReturnStmt *ReturnStatement = cast<ReturnStmt>(s);
TheRewriter.InsertText(ReturnStatement->getLocStart(), "{printf(\"[%s:%d] OUT \\n\", __FUNCTION__, __LINE__);\n", true, true);
}
不幸的是,简单的getLocEnd不起作用
TheRewriter.InsertText(ReturnStatement->getLocEnd(), "}", true, true);
它在返回后放置}
if(a)
{printf("[%s:%d] OUT \n", __FUNCTION__, __LINE__);
return }1;
请帮我检测Return语句结尾的位置,把closing}放进去,或者把ReturnStatement放进复合语句中比较容易
我还尝试查找返回语句的结尾,如下所示:
//before preprocessing
int foo(int a)
{
if(a)
return 1;
else
return a;
}
//after
int foo(int a)
{
if(a)
{printf("[%s:%d] OUT \n", __FUNCTION__, __LINE__);
return 1;
else
{printf("[%s:%d] OUT \n", __FUNCTION__, __LINE__);
return a;
}
ReturnStatement->getLocStart().getLocWithOffset(strlen(retvalue) + 1);
但我只能在字符串视图中为ImplicitCastExpr获取返回值
谢谢。这是一个没有简单解决方案的问题。这里我们有一个答案。我在这里找到的: 您所需要的只是:
// Note Stmt::getLocEnd() returns the source location prior to the
// token at the end of the line. For instance, for:
// var = 123;
// ^---- getLocEnd() points here.
SourceLocation END = stmt->getLocEnd();
// MeasureTokenLength gets us past the last token, and adding 1 gets
// us past the ';'.
int offset = Lexer::MeasureTokenLength(END,
TheRewriter.getSourceMgr(),
TheRewriter.getLangOpts()) + 1;
SourceLocation END1 = END.getLocWithOffset(offset);
TheRewriter.InsertText(END1, "\n}", true, true);
ReturnStatement->getRetValue->getLocEnd可能不会有帮助;因为这将产生返回1};。谢谢,不幸的是,它仍然返回}0;