[Haskell-cafe] [ANN] clang-pure- Pure C++ code analysis

Patrick Chilton chpatrick at gmail.com
Tue Dec 1 12:03:12 UTC 2015

Hi everyone,

I've been working on a pure binding to libclang that lets you easily
analyze any code that the Clang compiler understands.

Github: https://github.com/chpatrick/clang-pure
Hackage: http://hackage.haskell.org/package/clang-pure
Haddock: http://chpatrick.github.io/clang-pure/
libclang documentation: http://clang.llvm.org/doxygen/group__CINDEX.html

A short example that prints out the type of every function in a header file
(very useful for binding generation):

main = do
  idx <- createIndex
  tu <- parseTranslationUnit idx "foo.h" []
    root = translationUnitCursor tu
    funDecs =
      root ^..
        cosmosOf cursorChildrenF
        . filtered (\c -> cursorKind c == FunctionDecl)
        . folding (\c -> fmap (\t -> ( cursorSpelling c, typeSpelling
t )) (cursorType c))
  for_ funDecs $ \(f, t) -> putStrLn $ BS.unpack f ++ " :: " ++ BS.unpack t

Or to find the number of gotos:

gotoCount = lengthOf (cosmosOf cursorChildrenF . filtered (\c -> cursorKind
c == GotoStmt)) root

(This uses lens, but the library itself only has one optional
lens-style function).

Please refer to libclang's good documentation for an explanation of the
functions. The only thing to keep in mind is that functions clang_foo are
renamed to just foo, functions clang_getBar are renamed to just bar, and
types CXBaz are renamed to just Baz in the Haskell bindings.

Under the hood, a reference counting system ensures that the C objects are
freed at
the right time and in the right order, which allows the public API to be
simple and pure.

The bindings are not yet complete, so if you find something missing please
make an issue or better yet send a pull request. :)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20151201/b18c0566/attachment.html>

More information about the Haskell-Cafe mailing list