29.01.2014 Views

A CIL Tutorial - Department of Computer Science - ETH Zürich

A CIL Tutorial - Department of Computer Science - ETH Zürich

A CIL Tutorial - Department of Computer Science - ETH Zürich

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 1. THE AST 13<br />

1.2 Printing the AST<br />

The function tut1FixInstr also uses <strong>CIL</strong>'s logging and pretty-printing features. The call to E.log<br />

causes messages to be printed to stdout. It is similar to Printf.printf from the OCaml standard<br />

library with a few dierences that are documented in cil/ocamlutil/errormsg.mli. Most<br />

importantly, you can print elements <strong>of</strong> the <strong>CIL</strong> AST by using the %a format specier. Then, corresponding<br />

to that format you must supply two arguments: a function (unit → α → doc), and such<br />

an α that you want to print. Functions like this for the AST elements are dened in cil.ml (e.g.<br />

Cil.d exp for Cil.exp's). Functions for building objects <strong>of</strong> type doc are available in the Pretty<br />

module documented in cil/ocamlutil/pretty.mli.<br />

1.3 test/tut1.c<br />

The module tut1.ml deletes writes inside functions named target to global variables named<br />

deleted. Thus, the function target below will return 37.<br />

# include <br />

int deleted = 37;<br />

../test/tut1.c<br />

int target()<br />

{<br />

int l;<br />

deleted = 0;<br />

l = deleted;<br />

return l;<br />

}<br />

int main()<br />

{<br />

int r;<br />

r = target();<br />

printf("r = %d\n", r);<br />

return 0;<br />

}<br />

We can build this le with the following command:<br />

$ ciltutcc --enable-tut1 -o tut1 test/tut1.c<br />

test/tut1.c:14: Deleted assignment: #line 14 "test/tut1.c"<br />

deleted = 0;<br />

Using the log message emitted by tut1FixInstr, ciltutcc informs us that it has removed an<br />

assignment to deleted. Now, we can run the resulting program:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!