This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Access to source code from an analyser


On Thu, 2012-01-19 at 14:06 +0100, Alberto Lozano Alelu wrote:
> Hello.
> 
> Thanks for your fast response.
> 
> With expand_location I get struct expanded_location which has these fields:
> type = struct {
>     const char *file;
>     int line;
>     int column;
>     unsigned char sysp;
> }
> 
> But it hasn't source line text.
> 
> I know how I have to use expand_location function but I would like to
> get source text from a source location. I would like a funcion such
> as:
> 
> char *v_line_text = <a_gcc_function>(file,line);
> 
> or
> 
> expanded_location v_location=expand_location(SOURCE_LOCATION(my_element));
> char * v_line_text = a_function(v_location);
> 
> v_line_text is a source code line.
> 
> I need to have source text

I'm interested in hearing if there's a more "official" way of doing this
from the GCC experts, but can't you just read the file from disk and
split it on newline characters? (probably with some caching)

FWIW, in my Python plugin [1] for GCC I created a helper function:

def get_src_for_loc(loc):
    # Given a gcc.Location, get the source line as a string
    import linecache
    return linecache.getline(loc.file, loc.line).rstrip()

which uses Python's linecache module to do the heavy lifting (in
particular, it adds the caching).

[snip]

Dave

[1] https://fedorahosted.org/gcc-python-plugin/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]