Opened 6 years ago

Closed 16 months ago

#4931 closed bug (fixed)

hsc2hs emits invalid OPTIONS_GHC pragmas

Reported by: awson Owned by: thomie
Priority: low Milestone: 8.0.1
Component: hsc2hs Version: 7.0.1
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case: driver/T4931
Blocked By: Blocking:
Related Tickets: Differential Rev(s): Phab:D1452
Wiki Page:


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 (20)

comment:1 Changed 6 years ago by igloo

Milestone: 7.2.1

Thanks for the report.

comment:2 Changed 5 years ago by igloo

Priority: normallow

comment:3 Changed 4 years ago by igloo


comment:4 Changed 3 years ago by thoughtpolice


Moving to 7.10.1.

comment:5 Changed 2 years ago by thomie

difficulty: 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 2 years ago by thoughtpolice


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 23 months ago by thomie

Keywords: newcomer added

See comment:5.

comment:8 Changed 23 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 23 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 23 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 23 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 23 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 21 months ago by thomie

peddle: did you make any progress with this ticket?

comment:14 Changed 21 months ago by terrelln

Owner: set to terrelln

I'll checkout out toArgs.

comment:15 Changed 18 months ago by thoughtpolice


Milestone renamed

comment:16 Changed 17 months ago by thomie

terrelin: did you make any progress with this ticket?

comment:17 Changed 16 months ago by thomie

Differential Rev(s): Phab:D1452
Keywords: newcomer removed
Owner: changed from terrelln to thomie
Test Case: driver/T4931

comment:18 Changed 16 months ago by thomie

Status: newpatch

The patch in Phab:D1452 doesn't handle #define FOO "bar \"baz\"" (comment:8), but it does handle the example from the description: #define FOO "bar baz".

I still don't know if emitting {-# OPTIONS_GHC -optc-DFOO=... #-} makes sense at all, but oh well.

comment:19 Changed 16 months ago by Ben Gamari <ben@…>

In fbc2537/ghc:

OPTIONS_GHC compiler flags may contain spaces (#4931)

When a .hsc contains `#define FOO "bar baz"`, hsc2hs emits:

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

Make sure GHC can compile this, by tweaking `HeaderInfo.getOptions` a

Test Plan: driver/T4931

Reviewers: austin, bgamari

Reviewed By: bgamari

Differential Revision:

GHC Trac Issues: #4931

comment:20 Changed 16 months ago by thomie

Resolution: fixed
Status: patchclosed
Note: See TracTickets for help on using tickets.