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!

2
  • not sure if i understood the question. Are you trying to get the function (functionDecl maybe?) from a clang::SourceLocation ? Commented Dec 18, 2020 at 8: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). Commented Dec 18, 2020 at 14:16

1 Answer 1

1
+50

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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, sorry, i re-edited. does getCharacterData() from sourceManager work?
having said those, i wonder is doing so really more convenience than creating a for loop to iterate all chars ;)
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.