This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
GCC plugins: mapping AST back to source files, missing locations.
- From: mefyl <mefyl at gruntech dot org>
- To: gcc-help at gcc dot gnu dot org
- Date: Thu, 18 Oct 2012 18:34:07 +0200
- Subject: GCC plugins: mapping AST back to source files, missing locations.
Hi,
I've been playing with GCC plugins for a few weeks for static code analysis
and source-to-source refactoring, and I figured a good first exercise would be
to extract variable bindings, i.e. output a map that binds
types/functions/variables usages to their declaration, with source code
locations. A semantic etags, one may say - I know such tools already exist.
I had a hard time figuring everything out due to the lack of documentation,
but thanks to headers and helpful blog posts I am now able to walk the AST and
registers entity declarations and their usages. However I have a few problem I
can't seem to solve on my own despite intense googling, here are the two main
ones:
* I can retrieve source file location of declarations with
DECL_SOURCE_LOCATION without any problem, but I can't get locations for
expressions since their EXPR_LOCATION is always null. I used the
PLUGIN_PRE_GENERICIZE hook, which AFAIK is the earliest, so I expected
locations to still be there. Did I miss something, or is there no way to
retrieve such locations from plugins, in which case my example seems
impossible ?
* Even if we suppose I fixed the previous issue, from what I inferred from the
AST, a reference to a variable is directly represented by the corresponding
VAR_DECL. That is, in "int x; int y; x + y;", "x + y" is a PLUS_EXPR whose
operands are the VAR_DECL corresponding to "int x" and "int y". This would
make it impossible for me to get the location of "x" and "y", since they are
not represented as such by nodes in the AST.
I fear that due to the compiler nature of GCC, the AST I'm working on is
altered, with some processing and simplification already applied, preventing
source-to-source code transformation since one cannot map ast chunks back to
source files. Could anyone confirm or point me in some direction if I'm wrong
? In the case it's indeed impossible, is there any intent to improve this
aspect, like giving access to earlier stages of the AST (freshly parsed, with
only variable bindings for instance) ? This would enable to write extremely
powerfull analysis & refactoring tools.
Thanks in advance for your help,
--
mefyl