Coder Social home page Coder Social logo

clang-pure's Introduction

Pure Haskell bindings to libclang

Hackage Build Status

A Haskell library for pure C++ code analysis

API examples

Enumerate all function declarations in main.cpp

module Main (main) where

import Control.Monad
import Language.C.Clang

main :: IO ()
main = do
    idx <- createIndex
    tu <- parseTranslationUnit idx "main.cpp" ["-I/usr/local/include"]
    let root = translationUnitCursor tu
        children = cursorChildren root
        functionDecls = filter (\c -> cursorKind c == FunctionDecl) children
    forM_ functionDecls (print . cursorSpelling)

List all function declarations and their types, lens-style

idx <- createIndex
tu <- parseTranslationUnit idx path clangArgs
let funDecs =
      -- fold over cursors recursively
        cursorDescendantsF
      -- finding FunctionDecls...
      . folding (matchKind @'FunctionDecl)
      -- ...that are actually in the given file
      . filtered (isFromMainFile . rangeStart . cursorExtent)
      -- and get their names and types
      . to (\funDec -> cursorSpelling funDec <> " :: " <> typeSpelling (cursorType funDec))
BS.putStrLn $ BS.unlines (translationUnitCursor tu ^.. funDecs)

Development

View development guide

clang-pure's People

Contributors

bendmorris avatar chpatrick avatar jchia avatar kodemeister avatar krantz-xrf avatar rcook avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

clang-pure's Issues

<command line>: does not exist: src/Language/C/Clang/Internal/FFI.c

Cannot build clang-pure-0.2.0.2 with ghc 8.2.2.

Building C Sources...
creating dist/build
.../ghc ... src/Language/C/Clang/Internal/FFI.c
<command line>: does not exist: src/Language/C/Clang/Internal/FFI.c

The following patch does allow it to build. I'm not sure what the stanza was ever trying to do, but current hsc2hs does not generate a FFI.c from FFI.hsc.

diff -Nur old/clang-pure-0.2.0.2/Setup.hs new/clang-pure-0.2.0.2/Setup.hs
--- old/clang-pure-0.2.0.2/Setup.hs     2016-09-22 09:20:36.000000000 +0000
+++ new/clang-pure-0.2.0.2/Setup.hs     2018-07-02 09:25:13.651052572 +0000
@@ -85,12 +85,6 @@
         libBuildInfo = lbi
         { includeDirs = llvmIncludeDir : includeDirs lbi
         , extraLibDirs = llvmLibraryDir : extraLibDirs lbi
-        , cSources = -- define the generated c-sources here so that they don't get picked up by sdist
-#ifdef mingw32_HOST_OS
-            ["srcLanguageCClangInternalFFI.c"] -- work around a bug in inline-c (?)
-#else
-            ["src/Language/C/Clang/Internal/FFI.c"]
-#endif
         }
       }
     }

Future of clang-pure

Hello @chpatrick, do you have any plans for maintaining clang-pure in the future? Like fixing bugs, incorporating new features from libclang, accepting PRs, or anything?

I might have use for this package, and might consider forking it, if it turns out to that it is able to do what I want. I probably would need to extend clang-pure somewhat. I also have no idea about how many new features have been added to libclang, if any.

I might also consider upstreaming my changes. However, there are things that I am not that much interested in – like maintaining backwards-compatibility with older ecosystem packages or support for the Stack tool.

Examples in the wild

Do you know if there are any publicly availible examples of people using clang-pure in the wild?
It would be nice if there was a list of projects.

Build failure with Cabal 2.0/GHC 8.2

Configuring now fails when trying to find the clang library:

> /tmp/stackage-build14$ stack unpack clang-pure-0.2.0.2@gitsha1:9e1e30660eae21019295683e11f326529aec17c1
Unpacked clang-pure-0.2.0.2 to /tmp/stackage-build14/clang-pure-0.2.0.2/
> /tmp/stackage-build14/clang-pure-0.2.0.2$ ghc -clear-package-db -global-package-db -package-db=/var/stackage/work/builds/nightly/pkgdb Setup
[1 of 1] Compiling Main             ( Setup.hs, Setup.o )
Linking Setup ...
> /tmp/stackage-build14/clang-pure-0.2.0.2$ ./Setup configure --package-db=clear --package-db=global --package-db=/var/stackage/work/builds/nightly/pkgdb --libdir=/var/stackage/work/builds/nightly/lib --bindir=/var/stackage/work/builds/nightly/bin --datadir=/var/stackage/work/builds/nightly/share --libexecdir=/var/stackage/work/builds/nightly/libexec --sysconfdir=/var/stackage/work/builds/nightly/etc --docdir=/var/stackage/work/builds/nightly/doc/clang-pure-0.2.0.2 --htmldir=/var/stackage/work/builds/nightly/doc/clang-pure-0.2.0.2 --haddockdir=/var/stackage/work/builds/nightly/doc/clang-pure-0.2.0.2 --flags= --extra-lib-dirs /usr/lib/llvm-3.7/lib --extra-include-dirs /usr/lib/llvm-3.7/include
Configuring clang-pure-0.2.0.2...
Setup: Missing dependency on a foreign library:
* Missing C library: clang
This problem can usually be solved by installing the system package that
provides this library (you may need the "-dev" version). If the library is
already installed but in a non-standard location then you can use the flags
--extra-include-dirs= and --extra-lib-dirs= to specify where it is.

hsc2hs generates output with invalid path on Windows

I'm working on getting this package to build on Windows (see rcook@42ca11c). Currently, there is a problem related to hsc2hs: running stack build generates an output file srcLanguageCClangInternalFFI.c in the root directory corresponding to the source file src/Language/C/Clang/Internal/FFI.hsc.

When I get some more time I'll investigate and figure out if there is a straightforward fix for this.

Build failure on macOS 10.15

There are an infinite number of errors like this

Preprocessing library for clang-pure-0.2.0.5..
In file included from dist/build/Language/C/Clang/Internal/FFI_hsc_utils.c:3:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/stdio.h:64:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/_stdio.h:93:16: error: 
      pointer is missing a nullability type specifier (_Nonnull, _Nullable, or _Null_unspecified)
      [-Werror,-Wnullability-completeness]
        unsigned char   *_base;
                        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/_stdio.h:93:16: note: 
      insert '_Nullable' if the pointer may be null
        unsigned char   *_base;
                        ^
                          _Nullable 
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/_stdio.h:93:16: note: 
      insert '_Nonnull' if the pointer should never be null
        unsigned char   *_base;
                        ^
                          _Nonnull 
[omitted]

Maybe -Werror should not be enabled by default?

PS: I was trying to install it with cabal new-install + llvm 9 installed from homebrew.

Clarify licence

Patrick,

I'm thinking of contributing to this project at some point in the future, but I'm concerned about the remnants of the Google Individual Contributor License Agreement. I'm guessing that this is a holdover from the clang-lens project.

Given that this project has the straightforward Apache licence, I'm guessing that this should be a simple as updating CONTRIBUTING to remove references to the Google Individual Contributor License Agreement.

Thanks, Richard.

Build error

Hi, I'm trying to build clang-pure into my project and I get this:

clang-pure  > /private/var/folders/gt/vldsywv513jgcg3vf4ynfyw40000gn/T/stack-3c9bb5133b4445a3/clang-pure-0.2.0.6/src/Language/C/Clang/Internal/Types.hs:25:1: error:
clang-pure  >     Could not find module ‘Data.Singletons.TH’
clang-pure  >     Perhaps you meant
clang-pure  >       Data.Singletons (from singletons-3.0.1)
clang-pure  >       Data.Singletons.Sigma (from singletons-3.0.1)
clang-pure  >     Use -v (or `:set -v` in ghci) to see a list of the files searched for.
clang-pure  >    |
clang-pure  > 25 | import Data.Singletons.TH
clang-pure  >    | ^^^^^^^^^^^^^^^^^^^^^^^^^

Outdated description?

It looks to me like commits show updates so that the library works with newer llvm/clangs?
The DEV.md should be updated to reflect that it works with newer verisons than llvm 3.8.1?

AST matcher combinators?

Have you considered implementing https://clang.llvm.org/docs/LibASTMatchersReference.html ?
They are very good at the least for prototyping with clang-check, and it would be nice if one could just transfer that over directly.

Are there any worries of scope creep? I don't know how much of clang is implemented here, but I imagine only a subset is? (I'm still learning my way around the clang project, and I'm not sure how various pieces fit together.)

Problems linking against clang-pure with hybrid c/c++ code

I have problems linking other haskell libraries against clang-pure if I add c++ code using inline-c-cpp, I don't have everything on hand so I need to expand on the issue later.

I haven't been able to diagnose the issue particularly well, but I wanted to at least file it. I'll also put up a minimal repro later. - which is to say I'm not actually sure this is a c/c++ hybridization issue.

I'm using c++ because I'm writing some patches where I want to leverage features more internal to clang than what's exposed by the c interface, and this can by done by linking against clang-cpp.so .

Clang-pure itself compiles fine, the issue happens when I import clang-pure from another downstream library of mine, and then it complains:

> cabal repl --repl-options="-fobject-code" --repl-options="-O0"
[snip]
<command line>: /tmp/tmp.AGPRrbE1Fi/src2/dist-newstyle/build/x86_64-linux/ghc-8.8.3/clang-pure-0.2.0.6/build/libHSclang-pure-0.2.0.6-inplace-ghc8.8.3.so: undefined symbol: _ZTIN5clang12ast_matchers11MatchFinder13MatchCallbackE

_ZTIN5clang12ast_matchers11MatchFinder13MatchCallbackE is a virtual thing at https://clang.llvm.org/doxygen/classclang_1_1ast__matchers_1_1MatchFinder_1_1MatchCallback.html
nm shows the symbol as as U which means undefined, but it's not immediately clear from my logs which .so this was on, presumably libHSclang-pure-0.2.0.6-inplace-ghc8.8.3.so.

Here's a partial lddtree:

$ lddtree dist-newstyle/build/x86_64-linux/ghc-8.6.5/clang-pure-0.2.0.6/build/libHSclang-pure-0.2.0.6-inplace-ghc8.6.5.so
dist-newstyle/build/x86_64-linux/ghc-8.6.5/clang-pure-0.2.0.6/build/libHSclang-pure-0.2.0.6-inplace-ghc8.6.5.so (interpreter => None)
    libm.so.6 => /lib64/libm.so.6
        ld-linux-x86-64.so.2 => /lib64/ld-linux-x86-64.so.2
    libclang-cpp.so.10 => /usr/lib/llvm/10/lib64/libclang-cpp.so.10
        libpthread.so.0 => /lib64/libpthread.so.0
        libLLVM-10.so => /usr/lib/llvm/10/lib64/libLLVM-10.so
            libffi.so.7 => /usr/lib64/libffi.so.7
            libz.so.1 => /lib64/libz.so.1
            libdl.so.2 => /lib64/libdl.so.2
            libtinfo.so.6 => /lib64/libtinfo.so.6

Building clang-pure-0.2.0.2 on Windows

Hi there,

I managed to build clang-pure on Windows, but for that I had to change the following:

Remove the "work around a bug in inline-c (?)" in Setup.hs:
#ifdef mingw32_HOST_OS
["srcLanguageCClangInternalFFI.c"] -- work around a bug in inline-c (?)
and make it always ["src/Language/C/Clang/Internal/FFI.c"]

In clang-pure.cabal, rename
extra-libraries: clang
to
extra-libraries: libclang

Then, having --extra-lib-dirs pointing to LLVM\bin, LLVM\lib and
LLVM\lib\clang\3.8.1\lib\windows,
I managed to build clang-pure on windows
(with LLVM 3.8.1 64bit Windows binary installed)

Maybe its worth to include this in the project?

Cheers!
Robert

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.