<div dir="ltr"><div>I've isolated the issue to the handling of if defined on multi-argument macros.</div><div><br></div><div>I took a crack at interpreting the cpphs source for this and I think it may be a bug in the conversion of defined expressions here in Language.Preprocessor.CppIfdef here:</div><div><br></div><div>    convert "defined" [arg] =</div><div>      case lookupST arg st of</div><div>        Nothing | all isDigit arg    -> return arg </div><div>        Nothing                      -> return "0"</div><div>        Just (a@AntiDefined{})       -> return "0"</div><div>        Just (a@SymbolReplacement{}) -> return "1"</div><div>        Just (a@MacroExpansion{})    -> return "1"</div><div><br></div><div>It looks like it will macro expand the contents of a defined expression which isn't what GCC does.  I don't know if GCC is wrong or if using parameterized macros within </div><div><br></div><div>if defined works on single-argument macros.</div><div><br></div><div>working1.hs:</div><div><br></div><div>    {-# LANGUAGE CPP #-}</div><div>    </div><div>    #define EXAMPLE_MACRO(arg) (\</div><div>                                   arg)</div><div>    </div><div>    #if defined(EXAMPLE_MACRO)</div><div>    #endif</div><div><br></div><div>preprocess it (it works!):</div><div><br></div><div>    $ cpphs --cpp working1.hs -o $tempfile</div><div>    $</div><div><br></div><div>ifdef works on multiple-argument macros.</div><div><br></div><div>working2.hs:</div><div><br></div><div>    {-# LANGUAGE CPP #-}</div><div>    </div><div>    #define EXAMPLE_MACRO(arg1,arg2) (\</div><div>                                   arg1 > arg2)</div><div>    </div><div>    #ifdef EXAMPLE_MACRO</div><div>    #endif</div><div><br></div><div>preprocess it (it works!):</div><div><br></div><div>    $ cpphs --cpp working2.hs -o $tempfile</div><div>    $</div><div><br></div><div>if defined fails on multi-argument macros.</div><div><br></div><div>broken2.hs:</div><div><br></div><div>    {-# LANGUAGE CPP #-}</div><div>    </div><div>    #define EXAMPLE_MACRO(arg1,arg2) (\</div><div>                                   arg1 > arg2)</div><div>    </div><div>    #if defined(EXAMPLE_MACRO)</div><div>    #endif</div><div><br></div><div>preprocess it (it fails!):</div><div><br></div><div>    $ cpphs --cpp broken2.hs -o $tempfile</div><div>    cpphs: macro EXAMPLE_MACRO expected 2 arguments, but was given 0</div><div>    $</div><div><br></div><div>I've posted a StackOverflow question to see if any of them know if this is undefined behavior:</div><div><a href="http://stackoverflow.com/questions/34709769/is-cpphs-wrong-or-is-the-behavior-of-macros-with-arguments-in-if-defined-express">http://stackoverflow.com/questions/34709769/is-cpphs-wrong-or-is-the-behavior-of-macros-with-arguments-in-if-defined-express</a><br></div><div><br></div><div>If it is undefined behavior we should stop relying on it in GHC sources.  Either way the behavior is inconsistent with GCC which complicates things.</div><div><br></div><div>Best,</div><div>Alain</div></div><br><div class="gmail_quote"><div dir="ltr">On Sun, Jan 10, 2016 at 2:04 PM Alain O'Dea <<a href="mailto:alain.odea@gmail.com">alain.odea@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi Malcolm:<div><br></div><div>cpphs is under consideration as a replacement for GCC's C preprocessor in the GHC toolchain:</div><div><a href="https://ghc.haskell.org/trac/ghc/wiki/Proposal/NativeCpp" target="_blank">https://ghc.haskell.org/trac/ghc/wiki/Proposal/NativeCpp</a><br></div><div><br></div><div>GHC 7.10.3's build fails when cpphs is used as the C preprocessor (--with-hs-cpp=cpphs --with-hs-cpp-flags="--cpp").</div><div><br></div><div><div>It runs into this error when preprocessing libraries/base/GHC/Natural.hs:<br></div><div><br></div><div>cpphs: macro MIN_VERSION_integer_gmp expected 3 arguments, but was given 0</div></div><div><br></div><div>I've reproduced this issue on Ubuntu 14.04 x86-64 and SmartOS 15.3.0 x86-64.</div><div><br></div><div>Interestingly the error seems to arise only when preprocessing Natural.hs while the autogenerated cabal-macros.h is present.  Removing that include from the cpphs flags leads to a clean preprocessing run.</div><div><br></div><div>I have more details of this investigation here:</div><div><a href="https://gist.github.com/AlainODea/bd5b3e0e6f7c4227f009" target="_blank">https://gist.github.com/AlainODea/bd5b3e0e6f7c4227f009</a></div><div><br></div><div>Is this a bug?</div><div><br></div><div>Best,</div><div>Alain</div></div></blockquote></div>