Another CPP gotcha for the manual

Gabriel Dos Reis gdr at integrable-solutions.net
Thu Nov 7 02:15:08 UTC 2013


On Sun, Nov 3, 2013 at 11:57 AM, Mark Lentczner
<mark.lentczner at gmail.com> wrote:
> Turns out that c pre-processing removes C style (/*…*/) comments. Of course,
> it will do this anywhere in the source. The following program can tell if it
> is compiled -cpp or not:
>
> module Main where
>
> (*/**) :: Double -> Double -> Double
> (*/**) a b = a / (2 ** b)
>
> howCompiled = if use > 1/16 then "normal" else "-cpp"
>   where
>     use = 1 */** 2 + 3 */** 4
>
> main = putStrLn $ "compiled " ++ howCompiled
>
>
> When run:
>
> & runhaskell CppTest.hs
> compiled normal
>
> & runhaskell -cpp CppTest.hs
> compiled -cpp
>
>
> An example in the wild is in the package wai-extra, in the file
> Network/Wai/Middleware/RequestLogger.hs where the */* construct appears
> twice in the comments.
>
> Short of defining and implementing our own CPP-like preprocessing (something
> we might actually consider), I don't think there really is any fix for this,
> so the bug is that it should appear in the GHC documentation on CPP mode
> (§4.12.3), along with similar warnings about trailing back-slashes.
>
> Note that the way in which a multi-line comment is removed differs between
> gcc and clang. In gcc, the comment is removed and content of the line before
> the comment, and contents of the line after the comment are joined into a
> single line. In clang, the two line fragments are kept on separate lines. In
> both cases extra empty lines are added to keep the line count the same.
>
> The consequence of the gcc / clang difference is that neither the above
> code, nor wai-extra will compile with clang.
>
> Note: As far as I can tell this is not a clang bug, but a failure of specs:
> The C definition of comments and their removal is vague, and my guess is gcc
> choose its method based on historical use. The C++ definition makes it clear
> that comments are whitespace, even once removed, and so the clang method is
> correct.

The effect of feeding CPP with a non-C or non-C++ program isn't defined :-)

-- Gaby


More information about the ghc-devs mailing list