得到函数体
getting body of the function
本文关键字:函数体 更新时间:2023-10-16
我试图获得函数体的整个代码。
我有以下代码 bool VisitFunctionDecl(FunctionDecl *f) {
Stmt *FuncBody = f->getBody();
stringstream SSAfter;
SSAfter << f->getBody();
SourceLocation ST = f->getSourceRange().getBegin();
ST = FuncBody->getLocEnd().getLocWithOffset(1);
TheRewriter.InsertText(ST, SSAfter.str(), true, true);
}
我有以下代码示例
int multiplyOrSum (int a, int b, bool t)
{
int c=0;
if (a<=0){
return 0;
}
if (t){
c= a*b;
}
else {
c = a+b;
}
__asm
{
jz _P01
jnz _P01
// _emit 0e9h
_P01:
}
return (c + multiplyOrSum(a-1, b, t)+ loopOne (-1));
}
用我提供的代码解析后。结果是
int multiplyOrSum (int a, int b, bool t)
{
int c=0;
if (a<=0){
return 0;
}
if (t){
c= a*b;
}
else {
c = a+b;
}
__asm
{
jz _P01
jnz _P01
// _emit 0e9h
_P01:
}
return (c + multiplyOrSum(a-1, b, t)+ loopOne (-1));
}
02053B28
我想达到的是
int multiplyOrSum (int a, int b, bool t)
{
int c=0;
if (a<=0){
return 0;
}
if (t){
c= a*b;
}
else {
c = a+b;
}
__asm
{
jz _P01
jnz _P01
// _emit 0e9h
_P01:
}
return (c + multiplyOrSum(a-1, b, t)+ loopOne (-1));
}
{
int c=0;
if (a<=0){
return 0;
}
if (t){
c= a*b;
}
else {
c = a+b;
}
__asm
{
jz _P01
jnz _P01
// _emit 0e9h
_P01:
}
return (c + multiplyOrSum(a-1, b, t)+ loopOne (-1));
}
请建议。谢谢你。
编辑:基本上我想做的是复制函数体。
编辑2:
我尝试了以下代码,并对我的原始
进行了一些编辑bool VisitFunctionDecl(FunctionDecl *f) {
Stmt *FuncBody = f->getBody();
stringstream SSAfter;
SSAfter << f->getBody();
LangOptions LangOpts;
LangOpts.CPlusPlus = true;
PrintingPolicy Policy(LangOpts);
std::string s;
llvm::raw_string_ostream as(s);
FuncBody->printPretty(as, 0, Policy);
SSAfter << as.str() << "n";
SourceLocation ST = f->getSourceRange().getBegin();
ST = FuncBody->getLocEnd().getLocWithOffset(1);
TheRewriter.InsertText(ST, SSAfter.str(), true, true);
}
我现在的输出是
int multiplyOrSum (int a, int b, bool t)
{
int c=0;
if (a<=0){
return 0;
}
if (t){
c= a*b;
}
else {
c = a+b;
}
__asm
{
jz _P01
jnz _P01
// _emit 0e9h
_P01:
}
return (c + multiplyOrSum(a-1, b, t)+ loopOne (-1));
}
{
int c = 0;
if (a <= 0) {
return 0;
}
}
我如何得到函数的整个体?我现在已经得到了第一部分。但剩下的呢?
希望这对你有帮助。
string getExprAsString(Expr *expression)
{
// Might return until the beginning of the last token though
SourceRange exprRange = expression->getSourceRange();
string exprString;
int rangeSize = TheRewriter.getRangeSize(exprRange);
if (rangeSize == -1) {
return "";
}
SourceLocation startLoc = exprRange.getBegin();
const char *strStart = TheSourceMgr.getCharacterData(startLoc);
exprString.assign(strStart, rangeSize);
return exprString;
}
bool VisitFunctionDecl(FunctionDecl *f) {
string funcBody = getExprAsString(f->getBody());
SourceLocation ST = f->getBody()->getSourceRange().getBegin();
ST = f->getBody()->getLocEnd().getLocWithOffset(1);
// not sure if it contains the { } so added extra, just in case
TheRewriter.InsertText(ST, "n{n" + funcBody + "n}n" , true, true);
}
相关文章:
- 用C++在单独的头文件中完成函数体
- 我可以在 C++ 中的函数体之外进行操作吗?
- 有没有办法在不使用 #ifdef 的情况下不编译发布版本中的单元测试函数体?
- binary_search() 函数在我的函数体中不起作用
- 如何显示函数开始、结束行和函数体?
- 主函数体未检测到对重载可变变量模板化函数C++的调用
- 错误:函数声明符之后的预期函数体
- C++编译器如何检测非常量函数体
- 使用委托调用构造函数和在另一个构造函数体中调用的区别
- Flex和Bison忽略了函数体
- 我看到将我的类成员函数指定为内联实际上会增加执行时间,即使函数体非常小
- 为什么将函数体放置在单独的源代码文件而不是标题中的C/C 中
- 类(模板)成员函数体内部的ADL查找
- 如果未设置编译符号,如何排除方法?如何传递可变数量的参数并在函数体中格式化它们
- constexpr函数体c++14中可以有非文字类型的定义变量吗
- 内联失败:可以在链接时覆盖函数体
- C++函数体中的Const变量
- 在类定义之外的模板类成员函数体中,何时需要模板参数
- c++--直接--访问非成员函数体中的类成员
- 是否可以在初始化列表以外的构造函数体中进行一些计算