Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

GHC and CPP

52 views
Skip to first unread message

chb hemma

unread,
Dec 27, 2011, 4:06:29 PM12/27/11
to
Hello.

I'm using:
> ghc -V
The Glorious Glasgow Haskell Compilation System, version 6.12.3

I'm trying to do an ASSERT makro for my Haskell test programs but I have
no luck with the stringizing operator # of cpp. This code:

#define STRINGIZE(x) #x
#define ASSERT_EQUALS(e,r) (if e==r then return () else putStrLn $
"Error: " ++ STRINGIZE(e))

main = do ASSERT_EQUALS(1,2)

compiling with:

> ghc -XCPP testPP.hs

fails with:

testPP.hs:4:63: parse error on input `#'

Using the cpp on the file produces expected results (no # char but value
of arg e put in quotes):
> cpp testPP.hs
# 1 "testPP.hs"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "testPP.hs"

main = do (if 1==2 then return () else putStrLn $ "Error: " ++ "1")

Compiling with ghc and the -E flag gives on the other hand:

> ghc -XCPP -E testPP.hs
> cat testPP.hspp

{-# LINE 1 "testPP.hs" #-}
# 1 "testPP.hs"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "testPP.hs"

main = do (if 1==2 then return () else putStrLn $ "Error: " ++ #1)

Why is it expanded differently?

chb hemma

unread,
Dec 27, 2011, 4:54:36 PM12/27/11
to
Solved it by removing my STRINGIZE macro and put the argument in the
string. It seems cpp is not the preprocessor used when specifying -XCPP
to ghc.

new code:
#define ASSERT_EQUALS(e,r) (if e==r then return () else putStrLn $
"Error: e")

replaces:
0 new messages