Documentation, tests for hsc2hs's new #alignment macro
authorRyanGlScott <ryan.gl.scott@gmail.com>
Sat, 19 Dec 2015 10:09:52 +0000 (11:09 +0100)
committerThomas Miedema <thomasmiedema@gmail.com>
Sat, 19 Dec 2015 10:09:59 +0000 (11:09 +0100)
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

.gitignore
docs/users_guide/7.12.1-notes.rst
docs/users_guide/utils.rst
testsuite/tests/hsc2hs/Makefile
testsuite/tests/hsc2hs/T10272.h [new file with mode: 0644]
testsuite/tests/hsc2hs/T10272.hsc [new file with mode: 0644]
testsuite/tests/hsc2hs/T10272.stdout [new file with mode: 0644]
testsuite/tests/hsc2hs/T4340.hsc [new file with mode: 0644]
testsuite/tests/hsc2hs/T4340.stdout [new file with mode: 0644]
testsuite/tests/hsc2hs/all.T

index ae23fbb..bb69e91 100644 (file)
@@ -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
index 749f7af..d443a0a 100644 (file)
@@ -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
 ---------
 
index 9c1237d..f490bfa 100644 (file)
@@ -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.
index ec16b16..54fa5ad 100644 (file)
@@ -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 $@
+       ./$@
diff --git a/testsuite/tests/hsc2hs/T10272.h b/testsuite/tests/hsc2hs/T10272.h
new file mode 100644 (file)
index 0000000..6d8142d
--- /dev/null
@@ -0,0 +1,12 @@
+#ifndef _T10272_H_
+#define _T10272_H_
+
+#include <stdint.h>
+
+typedef struct {
+  uint8_t  a;
+  uint64_t b;
+  uint16_t c;
+} eight;
+
+#endif
diff --git a/testsuite/tests/hsc2hs/T10272.hsc b/testsuite/tests/hsc2hs/T10272.hsc
new file mode 100644 (file)
index 0000000..c4ff6d1
--- /dev/null
@@ -0,0 +1,6 @@
+module Main where
+
+#include "T10272.h"
+
+main :: IO ()
+main = print #{alignment eight}
diff --git a/testsuite/tests/hsc2hs/T10272.stdout b/testsuite/tests/hsc2hs/T10272.stdout
new file mode 100644 (file)
index 0000000..45a4fb7
--- /dev/null
@@ -0,0 +1 @@
+8
diff --git a/testsuite/tests/hsc2hs/T4340.hsc b/testsuite/tests/hsc2hs/T4340.hsc
new file mode 100644 (file)
index 0000000..5ae9e2a
--- /dev/null
@@ -0,0 +1,8 @@
+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}
diff --git a/testsuite/tests/hsc2hs/T4340.stdout b/testsuite/tests/hsc2hs/T4340.stdout
new file mode 100644 (file)
index 0000000..45a4fb7
--- /dev/null
@@ -0,0 +1 @@
+8
index b095791..d4fc69d 100644 (file)
@@ -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'])