0

I want to write a refactor tool, where I want to move the function at the cursor(cursor is in the function name) to the source file.

I found the FunctionMover.cc in https://github.com/lanl/CoARCT which is a good starting point to move the function. However, I cannot find anything how to get the symbol (i.e. in my case function) from a file:line:column (or file:offset) combination. I imagine this should be quite easy with the AST, SourceManager and libtooling of clang, but I cannot find anything about it.

Thanks for any help in advance!

veio
  • 507
  • 4
  • 16
  • not sure if i understood the question. Are you trying to get the function (functionDecl maybe?) from a clang::SourceLocation ? – Booo Dec 18 '20 at 08:33
  • I dont know if clang::SourceLocation the correct term. I have a file:line:column tuple from a different tool and want to get the symbol at that location (could be a functionDecl). – veio Dec 18 '20 at 14:16

1 Answers1

1

I would try LibTooling, clang::SourceManager has a member function translateFileLineCol():

SourceManager& SM = ctx.getSourceManager();
const FileEntry* FE = SM.getFileManager().getFile(filename);
SourceLocation loc = SM.translateFileLineCol(FE, line, column);

after obtaining the loc, you can maybe use SM.getCharacterData() or do other maneuvers.

This post has a very similar purpose, I believe. Seems like there is also a solution with libClang.

Booo
  • 493
  • 3
  • 13
  • Thanks, translateFileLineCol() is a nice convenience function, but I guess the tricky part is too get the symbol at that source location. printToString() just prints the file:line:column again. In your linked post, there is also no solution for this part. – veio Dec 18 '20 at 17:26
  • @veio, sorry, i re-edited. does getCharacterData() from sourceManager work? – Booo Dec 18 '20 at 17:52
  • having said those, i wonder is doing so really more convenience than creating a for loop to iterate all chars ;) – Booo Dec 18 '20 at 17:55
  • 1
    I think we have a misunderstanding. I know what code is written at the source location (this is easy to get from just reading the file). I want to know which semantic meaning does the text at the cursor have. Is it a function, a variable, a body etc. – veio Dec 18 '20 at 21:55