malcolmwallace / cpphs

The C pre-processor, implemented in Haskell.
2 stars 0 forks source link

Stringizing deviates from the spec #14

Open psibi opened 7 years ago

psibi commented 7 years ago

According to the specification, In stringization the parameters should not be replaced inside string constants i.e the argument should not be macro expanded.

A sample code demonstrating the problem:

{-#LANGUAGE CPP#-}

#define CHECK_FOR_EQUALITY(exp) if exp then "Equal" else #exp
#define x 3

check :: String
check = CHECK_FOR_EQUALITY(x == 0)

main :: IO ()
main = print check

Running it via cpphs gives this:

#line 1 "cppCode.hs"
{-#LANGUAGE CPP#-}

check :: String
check = if 3 == 0 then "Equal" else "3 == 0"

main :: IO ()
main = print check

But the check definition should be like this:

check = if 3 == 0 then "Equal" else "x == 0"

In fact, I can confirm that gnu's cpp behaves as expected

~/g/scripts $ cpp < cppCode.hs
# 1 "<stdin>"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "<stdin>"
{-#LANGUAGE CPP#-}

check :: String
check = if 3 == 0 then "Equal" else "x == 0"

main :: IO ()
main = print check