Using stringize and string concatenation in ghc preprocessing

Hi, To reduce boilerplate code in an FFI implementation file I am trying to use the stringizing and string concatenation features of the C preprocessor. Since ghc passes '-traditional' to the preprocessor which disables these features I thought I can pass my own flags to the preprocessor like this: {-# OPTIONS_GHC -optP -E -optP -undef #-} But "-optP" seems to only append to the flags that GHC already passes and gcc has no "-no-traditional" option to undo the effect of the "-traditional" that GHC has already passed. I think "-optP" should override the flags passed by ghc rather than appending to them. Is there a reason not to do that? Is there any other better way to achieve this? What is the standard way of doing this if any? -harendra

On Sat, Aug 20, 2016 at 2:27 PM, Harendra Kumar
But "-optP" seems to only append to the flags that GHC already passes and gcc has no "-no-traditional" option to undo the effect of the "-traditional" that GHC has already passed. I think "-optP" should override the flags passed by ghc rather than appending to them. Is there a reason not to do that?
Is there any other better way to achieve this? What is the standard way of doing this if any?
Removing -traditional will break much Haskell source. Go look at the history of clang with ghc (clang doesn't do -traditional) to see what happens. (tl;dr: without -traditional, cpp knows too much about what constitutes valid C, and mangles and/or throws errors on valid Haskell that doesn't lex the way C does.) You might want to look at cpphs as an alternative preprocessor. There are some ancient K&R-era hacks that could be used if absolutely necessary, but cpphs should be a much simpler and cleaner solution. -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

Hi Harendra,
I ran into this very problem recently. Turns out -traditional knows string
concatenation too. I seem to remember learning this by browsing the GHC
source code, but now I can't find any occurrence of this pattern. But
here's an example of how to do string concatenation with CPP in
-traditional mode:
https://github.com/tweag/sparkle/blob/a4e481aa5180b6ec93c219f827aefe932b66a9...
.
HTH,
--
Mathieu Boespflug
Founder at http://tweag.io.
On 20 August 2016 at 20:33, Brandon Allbery
On Sat, Aug 20, 2016 at 2:27 PM, Harendra Kumar
wrote: But "-optP" seems to only append to the flags that GHC already passes and gcc has no "-no-traditional" option to undo the effect of the "-traditional" that GHC has already passed. I think "-optP" should override the flags passed by ghc rather than appending to them. Is there a reason not to do that?
Is there any other better way to achieve this? What is the standard way of doing this if any?
Removing -traditional will break much Haskell source. Go look at the history of clang with ghc (clang doesn't do -traditional) to see what happens. (tl;dr: without -traditional, cpp knows too much about what constitutes valid C, and mangles and/or throws errors on valid Haskell that doesn't lex the way C does.)
You might want to look at cpphs as an alternative preprocessor. There are some ancient K&R-era hacks that could be used if absolutely necessary, but cpphs should be a much simpler and cleaner solution.
-- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
_______________________________________________ ghc-devs mailing list ghc-devs@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs

On Sun, Aug 21, 2016 at 4:31 PM, Boespflug, Mathieu
I ran into this very problem recently. Turns out -traditional knows string concatenation too. I seem to remember learning this by browsing the GHC source code, but now I can't find any occurrence of this pattern. But here's an example of how to do string concatenation with CPP in -traditional mode: https://github.com/tweag/sparkle/blob/ a4e481aa5180b6ec93c219f827aefe932b66a953/inline-java/src/ Foreign/JNI.hs#L274
.
That's the hacky K&R way I mentioned earlier. -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

Thanks Mathieu. This works pretty well for gcc (
https://gcc.gnu.org/onlinedocs/cpp/Traditional-macros.html) but sadly it
does not work for clang cpp as Brandon too pointed out earlier that clang
does not have a traditional mode.
-harendra
On 22 August 2016 at 02:01, Boespflug, Mathieu
Hi Harendra,
I ran into this very problem recently. Turns out -traditional knows string concatenation too. I seem to remember learning this by browsing the GHC source code, but now I can't find any occurrence of this pattern. But here's an example of how to do string concatenation with CPP in -traditional mode: https://github.com/tweag/sparkle/blob/ a4e481aa5180b6ec93c219f827aefe932b66a953/inline-java/src/ Foreign/JNI.hs#L274.
HTH,
-- Mathieu Boespflug Founder at http://tweag.io.
On 20 August 2016 at 20:33, Brandon Allbery
wrote: On Sat, Aug 20, 2016 at 2:27 PM, Harendra Kumar
wrote:
But "-optP" seems to only append to the flags that GHC already passes and gcc has no "-no-traditional" option to undo the effect of the "-traditional" that GHC has already passed. I think "-optP" should override the flags passed by ghc rather than appending to them. Is there a reason not to do that?
Is there any other better way to achieve this? What is the standard way of doing this if any?
Removing -traditional will break much Haskell source. Go look at the history of clang with ghc (clang doesn't do -traditional) to see what happens. (tl;dr: without -traditional, cpp knows too much about what constitutes valid C, and mangles and/or throws errors on valid Haskell that doesn't lex the way C does.)
You might want to look at cpphs as an alternative preprocessor. There are some ancient K&R-era hacks that could be used if absolutely necessary, but cpphs should be a much simpler and cleaner solution.
-- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
_______________________________________________ ghc-devs mailing list ghc-devs@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
participants (3)
-
Boespflug, Mathieu
-
Brandon Allbery
-
Harendra Kumar