Commit 2cc5b607 authored by Ryan Scott's avatar Ryan Scott Committed by thomie

Documentation, tests for hsc2hs's new #alignment macro

Adds two tests (one for Trac #4340 and one for Trac #10272), as well as
advice on how to fix your code if `hsc2hs` emits warnings with GHC 8.0
due to a redefinition of `#alignment`. (I also put the advice in the
[GHC 8.0 Migration
Guide](https://ghc.haskell.org/trac/ghc/wiki/Migration/8.0).)

Reviewed By: thomie

Differential Revision: https://phabricator.haskell.org/D1663

GHC Trac Issues: #4340, #10272
parent b2670fc4
......@@ -126,6 +126,7 @@ _darcs/
/libraries/frames.html
/libraries/ghc-boot/GNUmakefile
/libraries/ghc-boot/ghc.mk
/libraries/ghci/ghc.mk
/libraries/haddock-util.js
/libraries/hslogo-16.png
/libraries/index-frames.html
......
......@@ -321,6 +321,32 @@ Package system
- TODO FIXME.
hsc2hs
~~~~~~
- ``hsc2hs`` now supports the ``#alignment`` macro, which can be used to
calculate the alignment of a struct in bytes. Previously, ``#alignment``
had to be implemented manually via a ``#let`` directive, e.g., ::
#let alignment t = "%lu", (unsigned long)offsetof(struct {char x__; t (y__); }, y__)
As a result, if you have the above directive in your code, it will now emit
a warning when compiled with GHC 8.0. ::
Module.hsc:24:0: warning: "hsc_alignment" redefined [enabled by default]
In file included from dist/build/Module_hsc_make.c:1:0:
/path/to/ghc/lib/template-hsc.h:88:0: note: this is the location of the previous definition
#define hsc_alignment(t...) \
^
To make your code free of warnings on GHC 8.0 and still support earlier
versions, surround the directive with a pragma checking for the right GHC
version. ::
#if __GLASGOW_HASKELL__ < 800
#let alignment t = "%lu", (unsigned long)offsetof(struct {char x__; t (y__); }, y__)
#endif
Libraries
---------
......
......@@ -224,6 +224,10 @@ Meanings of specific keywords:
Computes the size, in bytes, of ``struct_type``. It will have type
``Int``.
``#alignment ⟨struct_type⟩``
Computes the alignment, in bytes, of ``struct_type``. It will have type
``Int``.
``#enum ⟨type⟩, ⟨constructor⟩, ⟨value⟩, ⟨value⟩, ...``
A shortcut for multiple definitions which use ``#const``. Each
``value`` is a name of a C integer constant, e.g. enumeration value.
......
......@@ -28,3 +28,15 @@ hsc2hs004:
T3837:
LANG=C '$(HSC2HS)' $@.hsc
'$(TEST_HC)' $(TEST_HC_OPTS) -c $@.hs
.PHONY: T4340
T4340:
'$(HSC2HS)' $@.hsc
'$(TEST_HC)' $(TEST_HC_OPTS) -v0 --make $@
./$@
.PHONY: T10272
T10272:
'$(HSC2HS)' --cross-compile $@.hsc
'$(TEST_HC)' $(TEST_HC_OPTS) -v0 --make $@
./$@
#ifndef _T10272_H_
#define _T10272_H_
#include <stdint.h>
typedef struct {
uint8_t a;
uint64_t b;
uint16_t c;
} eight;
#endif
module Main where
#include "T10272.h"
main :: IO ()
main = print #{alignment eight}
module Main where
#include <stdint.h>
#def typedef struct { uint8_t a; uint64_t b; uint16_t c; } eight;
main :: IO ()
main = print #{alignment eight}
......@@ -25,3 +25,13 @@ test('T3837',
run_command,
['$MAKE -s --no-print-directory T3837'])
test('T4340',
[extra_clean(['T4340.hs', 'T4340_hsc_make.c',
'T4340_hsc.c', 'T4340_hsc.h'])],
run_command,
['$MAKE -s --no-print-directory T4340'])
test('T10272',
[extra_clean(['T10272.hs', 'T10272_hsc_make.c'])],
run_command,
['$MAKE -s --no-print-directory T10272'])
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment