lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Mon, Dec 27, 2010 at 11:38 AM, steve donovan
<steve.j.donovan@gmail.com> wrote:
> Which is a long way of saying: there must be a more
> rigorous way of analyzing C code than ctags!

A few years ago, I asked myself the same question for various kinds of
static analysis on C code.  I briefly looked at both Splint[1] and
Sparse[2], figuring I could adapt either to output some kind of
structured representation of the C code that I could then feed into
various programs for analysis or formatting.  Unfortunately a bright
shiny object caught my attention and my attention span was focused
elsewhere.  Probably on food.

Then I hit on the idea of using a real C compiler, figuring that a C
compiler might have a clue about the syntax and semantics of C.
Somewhere in the conversion of source to tokens to AST's and DAG's and
other whatnot, I figured I could put in code to spit out interesting
metadata about the code.  So I first tried to take everyone's favorite
C compiler, gcc, and see about adding a back-end to emit that
information.  How hard could it be?  Then I looked at the gcc sources
and realized that I would keep the neighbors awake with my gnashing of
teeth and constant crying.

So I went to lcc[3] and found a reasonably clean and understandable
system that I could build a back-end for.  And I did.  It was actually
pretty easy.  Well, I had the book that the authors wrote; I'm not
sure how comprehensible it is without the book.  Still, the mechanics
aren't difficult-- you describe your fake architecture, register a
bunch of callbacks on significant events (like parsing a symbol or a
function) and you get passed a data structure with goodness in it.  At
the time I wrote the back-end, I was singing the praises of Lua at the
company I worked for, but for some idiotic reason, it didn't occur to
me to make a Lua-based back-end.  Duh.  Now several years later, both
lcc and Lua are better and it's something I'd recommend someone with
more free time than me to explore.  Making a Lua back-end would allow
all sorts of fun arbitrary back-ends without the usual pain and
boredom of writing that in C.

The ultimate goal of all this, of course, is to have Lua code that can
understand it's own C source.  Once you have that, sentience isn't far
away and I for one welcome our future robotic Lua-based overlords.
Maybe the next release of Lua should incorporate Asimov's laws of
robotics now, because it's only a matter of time.

[1] http://www.splint.org/
[2] https://sparse.wiki.kernel.org/index.php
[3] http://sites.google.com/site/lccretargetablecompiler/