[Haskell-cafe] Library for package preprocessing

Matthew Pickering matthewtpickering at gmail.com
Thu Aug 6 21:50:14 UTC 2015

Hi Carlo,

If a library uses CPP then there two things you have to be aware of.

1. The library could include macros (such as MIN_VERSION_...) which
are generated by cabal. The macro file is generated by "cabal
configure", placed in "dist/" and called cabal_macros.h.
2. The file can also have #includes, where to find these files is also
specified by in the cabal file.

Both of these are annoying to deal with because they require
additional contextual knowledge not present in the source file. As a
result most tooling asks the user to provide information about the
location of the cabal_macros.h file and includes directory.

Hlint is a good example to look at if you want to see how a library
which uses haskell-src-exts deals with CPP. I think it also provides
an endpoint parseModuleEx[1] which does the preprocessing for you. It
has two strategies, the first is to simply strip all lines with start
with # and attempt to parse the file. This is surprisingly effective
as most uses of CPP are quite simple. It also has built in support for
running cpphs as a library but the user must say where the specific
cpp files (cabal_macros.h etc) are.

Directly using GHC can in fact be a bit easier in this regard as it
has support for all the preprocessor steps. ghc-exactprint exposes a
parser which you might find useful[1].

As for other preprocessing, I'm not sure what else you specifically
want to know about but I might be able to help if you're more


[1]: https://hackage.haskell.org/package/hlint-1.9.21/docs/Language-Haskell-HLint3.html#v:parseModuleEx
[2]: https://hackage.haskell.org/package/ghc-exactprint-0.3.1/docs/Language-Haskell-GHC-ExactPrint-Parsers.html#v:parseModuleWithCpp

On Thu, Aug 6, 2015 at 10:33 PM, Carlo Nucera <meditans at gmail.com> wrote:
> Hi all, I'm writing a tool to analyze source code of a package, which uses
> haskell-src-extra. As copied from hackage, libraries may contain some extra
> stuff (for example, cpp directives). What is the modern way to preprocess the
> package (configuring for the environment, running the cpp ecc) to obtain code
> which is guaranteed to be parsed by haskell-src-extra?
> Ideally I'd like to leave most of the logic for this part outside of the
> package. Could you point me towards some libraries doing a similar thing?
> Best regards
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

More information about the Haskell-Cafe mailing list