Opened 4 years ago

Last modified 3 weeks ago

#4931 new bug

hsc2hs emits invalid OPTIONS_GHC pragmas

Reported by: awson Owned by: terrelln
Priority: low Milestone: 7.12.1
Component: hsc2hs Version: 7.0.1
Keywords: newcomer Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Revisions:

Description

If I write

#define FOO "bar baz"

in my hsc code, hsc2hs emits

{-# OPTIONS_GHC -optc-DFOO="bar baz" #-}

which GHC refuses to compile.

For my local hsc2hs I didn't bother with goodForOptD AI and have removed outOption for "define" key in outSpecial altogether.

Change History (14)

comment:1 Changed 4 years ago by igloo

  • Milestone set to 7.2.1

Thanks for the report.

comment:2 Changed 3 years ago by igloo

  • Milestone changed from 7.4.1 to 7.6.1
  • Priority changed from normal to low

comment:3 Changed 3 years ago by igloo

  • Milestone changed from 7.6.1 to 7.6.2

comment:4 Changed 12 months ago by thoughtpolice

  • Milestone changed from 7.6.2 to 7.10.1

Moving to 7.10.1.

comment:5 Changed 8 months ago by thomie

  • difficulty set to Unknown

There are 2 possible solutions:

  • in hsc2hs, ignore #define NAME VALUE when VALUE contains a space. This could be done in the function outHeaderHs in utils/hsc2hs/C.hs as mentioned by @awson. I'm not sure this is good idea (wouldn't an error be better?), but note that #defines where NAME contains a '(' are currently also ignored.
  • when using the OPTIONS pragma -Dsymbol=value, accept spaces inside value when value is quoted. This would need to be done in the function getOptions' in compiler/main/HeaderInfo.hs, by changing (words str) to something more complicated. Note that GHC already accepts spaces in value when the option is passed as a command line argument: -D'"FOO BAR"'

comment:6 Changed 6 months ago by thoughtpolice

  • Milestone changed from 7.10.1 to 7.12.1

Moving to 7.12.1 milestone; if you feel this is an error and should be addressed sooner, please move it back to the 7.10.1 milestone.

comment:7 Changed 3 months ago by thomie

  • Keywords newcomer added

See comment:5.

comment:8 Changed 3 months ago by peddie

I started to poke at this, following the second suggestion in comment:6.

It's more complicated than I thought at first, because it ought to handle escaped strings (e.g. #define FOO "bar \"baz\"") and multiple command-line flags per line (though hsc2hs doesn't do this, the current code handles it). This is more than GHC has to do to parse -D'"FOO BAR"', I think, because GHC gets handed argv. I have a patch that addresses the above cases, but it's ~22 lines in place of words str, and it's still ad-hoc parsing, so I hesitate to submit it.

Can anyone suggest what I should shoot for here? Just address the immediate bug and punt on escaped strings etc.? More complicated ad-hoc parsing? Split as argv would be and pull in the command-line options parser?

comment:9 Changed 3 months ago by thomie

Thanks for working on this.

Check out compiler/utils/Util.hs, it has a function toArgs which seems to do what you need.

comment:10 Changed 3 months ago by peddie

Thanks for the tip! I came across toArgs earlier and tried to use it at first, but on my example,

-optc-DFOO="bar baz" -optc-DQUUX="qq rr"

in the OPTIONS_GHC pragma yields

Right ["-optc-DFOO=\"bar","baz\"","-optc-DQUUX=\"qq","rr\""]

when I run it through toArgs. So it seems it's the same as words for this purpose. Have I misunderstood something?

comment:11 Changed 3 months ago by thomie

Yes, that could be considered a bug. If you could try fixing it, then we don't need to add another function. Check call sites of toArgs and toCmdArgs to make sure nothing breaks.

*Main> toArgs "a \"b c\" d"
Right ["a","b c","d"]

*Main> toArgs "a\"b c\" d"
Right ["a\"b","c\"","d"]  -- * should be ["a\"b c\"", "d"] I suppose.

comment:12 Changed 3 months ago by peddie

Thank you for clarifying -- I couldn't tell from the comments that toArgs was expected to do something differently. I'll try that approach. I appreciate the guidance, as this is the first time I've dived into the GHC code.

comment:13 Changed 3 weeks ago by thomie

peddle: did you make any progress with this ticket?

comment:14 Changed 3 weeks ago by terrelln

  • Owner set to terrelln

I'll checkout out toArgs.

Note: See TracTickets for help on using tickets.