Dump Translation Unit

I was inspired by:

   Reflectance from C/C++ code (pdf)
   Duncan Temple Lang
   Statistics and Data Mining Research,
   Bell Labs, Lucent Technologies January 15, 2003

Fun things based on leveraging the GCC compiler’s willingness to dump what it’s compiling after it’s done the parsing etc. So I wasted a few hours writing this hack.

Common Lisp is fun; for example this code that takes things like this xpath mimic “//function-decl/name/strn” and converts it into this SEXP ((:class tu-classes::tu-function-decl) (:attribute tu-classes::tu-name) (:attribute tu-classes::tu-strn)).

(defun parse-path (path-string)
  (let ((path-from-string
	  (with-perl-style (path-string)
            (sg "//" ")(:class tu-")
	    (sg "/" ") (:attribute tu-")
	    (s "$" ")")
	    (s "^)" "")
	    (s "(.*)" "(1)"))))
    (let ((*package* (find-package "TU-CLASSES")))
      (read-from-string path-from-string))))

Then later i can write things like (do-path (x "//function-decl/name/strn") (print x)) to print the name of the functions declared in what ever GCC was asked to compile.

Some things about Lisp have gotten better since I was last playing with it. For example with-perl-style is a little macro I wrote that depends on CL-PPRE. So now we have a solid regular expression package that’s Lisp native. Installing things extentions is easier too. Installing CL-PPRE? Type: (asdf-install:install ‘CL-PPRE).

Leave a Reply

Your email address will not be published. Required fields are marked *