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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 12. COMMENTS 106<br />

adjacent to the given source location. The indexes are into the the array returned as the second<br />

element <strong>of</strong> the return value.<br />

let commentsAdjacent (cca : comment array) (l : location)<br />

: int list × comment array =<br />

if l = locUnknown then [ ], cca else<br />

let cca = prepareCommentArray cca l.file in<br />

(cca | > array bin search comment compare (comment <strong>of</strong> cilloc l)), cca<br />

The function commentsBetween returns a list <strong>of</strong> indexes into the array returned as the second<br />

element <strong>of</strong> the return value. The indexes indicate the comments lying between source locations l1<br />

and l2. If the exact location is not in the comments array, the binary search function returns the<br />

two closest elements. Therefore commentsBetween returns the highest <strong>of</strong> the lower bounds, and the<br />

smallest <strong>of</strong> the upper bounds, so that only the indexes for the comments between the two locations<br />

are returned.<br />

let commentsBetween (cca : comment array) (l1 : location) (l2 : location)<br />

: int list × comment array<br />

=<br />

if l1 = locUnknown then commentsAdjacent cca l2 else<br />

if l1.file ≠ l2.file then commentsAdjacent cca l2 else begin<br />

let cca = prepareCommentArray cca l1.file in<br />

let ll = array bin search comment compare (comment <strong>of</strong> cilloc l1) cca in<br />

let hl = array bin search comment compare (comment <strong>of</strong> cilloc l2) cca in<br />

let l, h =<br />

match ll, hl with<br />

| ([l] | [ ; l]), h :: → l, h<br />

| → E.s(E.bug "bad result from array bin search")<br />

in<br />

(Array.init (h − l + 1) (fun i → i + l) | > Array.to list), cca<br />

end<br />

The function markComment searches a comment array for an exact source location, and marks the<br />

third element <strong>of</strong> the tuple for that location as true, indicating in this example that the comment<br />

has been printed by printComments.<br />

let markComment (l : A.cabsloc) (cca : comment array) : unit =<br />

Array.iteri (fun i (l', s, b) →<br />

if compare l l' = 0 then cca.(i) ← (l', s,true)<br />

) cca<br />

The function printComments prints the comments from the array cca' indicated by the indexes in<br />

il and marks the comments as having been printed in cca. The location l is used to indicate the<br />

source location being inspected by an instance <strong>of</strong> the commentVisitorClass that triggered the call<br />

to printComments.

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

Saved successfully!

Ooh no, something went wrong!