I have a quick and dirty solution to your problem. What you need to do is get the closest line from the AST. I don't like modifying libraries unless I have to. I assume you are familiar with parsing and data manipulation. If not, I can add more details.The parser.parse method generates a AST class object. gcc_or_cpp_output is some intermediate code generated by gcc or cpp.
ast = parser.parse(gcc_or_cpp_output,filename)
AST's function has a show method and default arguments. You will need to set showcoord True for your problem.
ast.show(buf=fb,attrnames=True, nodenames=True, showcoord=True)
buf:
Open IO buffer into which the Node is printed.
offset:
Initial offset (amount of leading spaces)
attrnames:
True if you want to see the attribute names in
name=value pairs. False to only see the values.
nodenames:
True if you want to see the actual node names
within their parents.
showcoord:
Do you want the coordinates of each Node to be
displayed
You will then need to change the buf default from sys.stdout to your own buffer class so you can capture the ast graph. You could also traverse the tree but I'll save a tree traverse solution for another day. I wrote a simple fake_buffer below.
class fake_buffer():
def __init__(self):
self.buffer =[]
def write(self,string):
self.buffer.append(string)
def get_buffer(self):
return self.buffer
So all you need to do now is save is pass your fake buffer to the ast.show() method to get the AST.
fb = fake_buffer()
ast.show(buf=fb,attrnames=True, nodenames=True, showcoord=True)
You will have your AST as a list at this point. The function Declarations will be near the bottom. Now you just need to parse out all the extra stuff and get the max coordinate in that function delectation.
FuncCall <block_items[12]>: (at ...blah_path_stuff.../year.c:48)
ABC
Always Be Coding