<div dir="ltr">Hi everyone,<div><br></div><div>I've been working on a pure binding to libclang that lets you easily analyze any code that the Clang compiler understands.</div><div><br></div><div>Github: <a href="https://github.com/chpatrick/clang-pure">https://github.com/chpatrick/clang-pure</a></div><div>Hackage: <a href="http://hackage.haskell.org/package/clang-pure">http://hackage.haskell.org/package/clang-pure</a></div><div>Haddock: <a href="http://chpatrick.github.io/clang-pure/">http://chpatrick.github.io/clang-pure/</a></div><div>libclang documentation: <a href="http://clang.llvm.org/doxygen/group__CINDEX.html">http://clang.llvm.org/doxygen/group__CINDEX.html</a></div><div><br></div><div>A short example that prints out the type of every function in a header file (very useful for binding generation):</div><div><br></div><div><pre style="color:rgb(0,0,0)">main = do
  idx <- createIndex
  tu <- parseTranslationUnit idx "foo.h" []
  let
    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</pre><pre style="color:rgb(0,0,0)"><font face="arial, helvetica, sans-serif">Or to find the number of </font><font face="monospace, monospace">goto</font><font face="arial, helvetica, sans-serif">s:</font></pre><font face="monospace, monospace">gotoCount = lengthOf (cosmosOf cursorChildrenF . filtered (\c -> cursorKind c == GotoStmt)) root</font><pre style="color:rgb(0,0,0)"><span style="font-family:arial,helvetica,sans-serif">(This uses lens, but the library itself only has one optional lens-style function).</span></pre></div><div>Please refer to libclang's good documentation for an explanation of the functions. The only thing to keep in mind is that functions <font face="monospace, monospace">clang_foo</font> are renamed to just <font face="monospace, monospace">foo</font>, functions <font face="monospace, monospace">clang_getBar</font> are renamed to just <font face="monospace, monospace">bar</font>, and types <font face="monospace, monospace">CXBaz</font> are renamed to just <font face="monospace, monospace">Baz</font> in the Haskell bindings.<br></div><div><br></div><div>Under the hood, a reference counting system ensures that the C objects are freed at </div><div>the right time and in the right order, which allows the public API to be simple and pure.</div><div><br></div><div>The bindings are not yet complete, so if you find something missing please make an issue or better yet send a pull request. :)</div><div><br></div><div>Patrick</div><div><br></div></div>