我正在尝试获取函数主体的完整代码。

我有以下代码

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

08-15 22:02