0

EDITED:

Me and my colleague are very new to clang and llvm .

I have three functions..

function 1{}
function 2{}
function 3{}

Is there anyway to swap the functions to

function 3{}
function 2{}
function 1{}

using clang libtooling / rewriter and print out the function name and also the parameter inside the function ?

HiWorld
  • 31
  • 7
  • Do you want to swap their locations in the source code? I believe since clang only gives you the option to look at one translational unit (function) at a time, it will hard to do this. – Nishant Sharma Jul 14 '16 at 14:52
  • You want to swap the order of the function *declarations*? [just confirming]. If you only want to do this once, why don't you just edit the file? Or is there something bigger behind this? – Ira Baxter Jul 14 '16 at 16:13
  • @iraBaxter appraently, we are making and executable file where we run this executable file and then produce a file with c++ source code, it will then produce us with results where the functions switch locations (jumble up) . So I think by editing the file is not the way to do it. – HiWorld Jul 15 '16 at 01:24

1 Answers1

0

You can parse the AST first using an ASTConsumer. You get one function's AST at a time, you might want to store it globally somewhere and then you can add them to the clang's REWRITTER API and finally dump the buffer back to a file.

This is an example of editing some AST nodes and writing back to file. In your case, you won't edit the AST's but just rearrange the buffer push calls to rearrange the functions. In VisitFunctionDecl:

bool VisitFunctionDecl(FunctionDecl *f) {
    // Only function definitions (with bodies), not declarations.
    if (f->hasBody()) {
         //store in a global array/vector
    }

    return true;
}

In main after ParseAST and before writing to file, you will do a rearrangeFunctionDecls.

Nishant Sharma
  • 683
  • 9
  • 16
  • is rearrangeFunctionDecls a clang methods/functions? – HiWorld Jul 15 '16 at 01:27
  • No, it will be a user defined function. What I meant was that since you already have the functionDecls stored somewhere. Now you can rearrange them (like if it was a vector, randomly rearrange it's elements) and then write the buffer. – Nishant Sharma Jul 15 '16 at 01:30
  • My colleague is now able to swap **ONLY** the method name. How to instead get the whole function body rather than just the function name? – HiWorld Jul 15 '16 at 01:47
  • Using the [Rewriter API](http://clang.llvm.org/doxygen/classclang_1_1Rewriter.html) you can remove text first for each function as you are storing it by calling the Remove text function and passing the source locations using getSourcerange [here](http://clang.llvm.org/doxygen/classclang_1_1FunctionDecl.html). Before you might want to save the text in a buffer, you can retrive the text for the same range above using the lexer [getSourceText](http://clang.llvm.org/doxygen/classclang_1_1Lexer.html#a013b6a61a2f81044169446ca911a87db). Now finally, use lexer's insert text with rearranged text. – Nishant Sharma Jul 15 '16 at 02:39
  • *last line should be "use Rewritter's insert text" instead of lexer's. – Nishant Sharma Jul 15 '16 at 02:49