The line number range of a function in source code level

Hi all,

I need to know the line number range of a function. The start of the function line number can be found by the definition point, which is stored at the subprogram metadata: DISubprogram::getLineNumber().

However, there is no API (or the metadata in the first place) to know the end of the function.

I have to visit all the instructions of the function and maintain the max line number of the instructions. I use the inst_begin(func): inst_end(func) as the iterator. But I don’t think it’s effective to iterate all the instructions from the beginning. It’s supposed that we can iterate the instructions reversely and the first one who has attached metadata should be treated as the end of the function. Unfortunately, there is no reverse iterator of a function.

Any idea?

Thank you very much!

“liuml07 [via LLVM]” <[hidden email]> writes:

There is a `std::reverse_iterator’ that might be useful. See this page
below for more information.

https://github.com/jirislaby/LLVMSlicer/blob/master/src/Slicing/FunctionStaticSlicer.cpp#L331

Keep in mind that some transforms may move instructions, so the last instruction in the function with meta-data does not necessarily come from the last instruction in the function. For example, there is a transform that modifies a function so that it only has one return instruction. Which meta-data does the merged return instruction get? I think iterating over all the instructions is more robust. If you need more accuracy than that, it might be a good idea to modify the front-end to generate meta-data that marks the line number of the end of the function. – John T.

Thank you very much.