得到函数体

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);
}