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 12<br />

Comments<br />

<strong>CIL</strong> has a very basic mechanism for tracking comments. In this chapter we'll see how to use<br />

it. <strong>CIL</strong> only sees comments when they are maintained in the output <strong>of</strong> the preprocessor. The<br />

preprocessor may be instructed to maintain comments in its output by using the -C switch to<br />

ciltutcc. Then, the comments in the source are collected by <strong>CIL</strong>'s parser and placed in the<br />

array Cabshelper.commentsGA. commentsGA is a GrowArray.t <strong>of</strong> triples <strong>of</strong> type (Cabs.cabsloc ×<br />

string × bool). The cabsloc is the source location <strong>of</strong> the comment. The string is the comment<br />

itself, and the bool is set aside for application bookkeeping.<br />

12.1 tut12.ml<br />

In this example, we'll visit the AST and print out comments nearby instructions and statements,<br />

taking care to only print each comment once. This will be accomplished by extracting the source<br />

location from instructions and statements and then doing a binary search on the array <strong>of</strong> comments.<br />

The array <strong>of</strong> comments lives in the Cabshelper module. The locations <strong>of</strong> the comments are dened<br />

in terms <strong>of</strong> the Cabs.cabsloc record type.<br />

module GA = GrowArray<br />

module A = Cabs<br />

module CH = Cabshelper<br />

First, we'll need a few utility functions, some <strong>of</strong> which are hidden away in Tututil (e.g. functions<br />

for ordering source locations and comments). prepareCommentArray lters out comments not from<br />

source le fname, and sorts the results according to source location.<br />

let prepareCommentArray (cca : comment array) (fname : string) : comment array =<br />

cca | > array filter (fun (cl, , ) → fname = cl.A.filename)<br />

|> array sort result comment compare<br />

The funcion commentsAdjacent returns the indexes <strong>of</strong> at most two comments that are immediately<br />

105

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

Saved successfully!

Ooh no, something went wrong!