我已成功构建示例代码
现在我有一个要求,如果我有如下示例代码:
int inc(int& p)
{
p++;
printf("In inc [%d]\n", p);
return p;
}
int main()
{
int i = 0;
int y,z;
if(y == 0)
print(inc(i) , inc(i));
else
{
print(inc(i) , inc(i));
}
printf("y = [%d] z = [%d]\n", y , z);
return 0;
}
代码应转换为
int inc(int& p)
{
p++;
printf("%s %d", __FILE__, __LINE__);
printf("In inc [%d]\n", p);
printf("%s %d", __FILE__, __LINE__);
return p;
}
int main()
{
int i = 0;
printf("%s %d", __FILE__, __LINE__);
int y,z;
printf("%s %d", __FILE__, __LINE__);
if(y == 0)
print(inc(i) , inc(i));
else
{
print(inc(i) , inc(i));
printf("%s %d", __FILE__, __LINE__);
}
printf("y = [%d] z = [%d]\n", y , z);
printf("%s %d", __FILE__, __LINE__);
return 0;
}
我尝试了以下代码更改:
bool VisitStmt(Stmt *s) {
// Only care about If statements.
if (isa<CompoundStmt>(s)) {
CompoundStmt *Statement = cast<CompoundStmt>(s);
TheRewriter.InsertText(Statement->getLocStart(),
"printf(\"%s %d\", __FILE__, __LINE__);\n",
true, true);
}
但输出如下:
// Begin function inc returning int
int inc(int& p)
printf("%s %d", __FILE__, __LINE__);
{
p++;
printf("In inc [%d]\n", p);
return p;
}
// End function inc
// Begin function main returning int
int main()
printf("%s %d", __FILE__, __LINE__);
{
int i = 0;
int y,z;
if(y == 0)
print(inc(i) , inc(i));
else
{
print(inc(i) , inc(i));
}
printf("y = [%d] z = [%d]\n", y , z);
return 0;
}
// End function main
请让我知道如何实现目标?
我也得到如下输出:
test.cpp:4:26: error: use of undeclared identifier 'p'
printf("In inc [%d]\n", p);
^
test.cpp:5:9: error: use of undeclared identifier 'p'
return p;
如何停止代码呈现相同的内容?只是复合块中的语句应该添加额外的语句。