clang - 获取功能的主体

时间:2013-05-07 06:00:05

标签: c++ clang

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

我有以下代码

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

我将如何获得整个功能?我现在已经得到了第一部分。但其余的呢?

1 个答案:

答案 0 :(得分: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);
}