Another CPP gotcha for the manual
malcolm.wallace at me.com
Mon Nov 4 09:28:27 UTC 2013
Of course, cpphs solved this problem nearly a decade ago.
On 3 Nov 2013, at 17:57, Mark Lentczner 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"
> 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.
> - Mark
> ghc-devs mailing list
> ghc-devs at haskell.org
More information about the ghc-devs