[commit: ghc] master: Documentation, tests for hsc2hs's new #alignment macro (2cc5b60)

git at git.haskell.org git at git.haskell.org
Sat Dec 19 10:13:11 UTC 2015


Repository : ssh://git@git.haskell.org/ghc

On branch  : master
Link       : http://ghc.haskell.org/trac/ghc/changeset/2cc5b607df27e702541985f7c4c987806c4ec2a1/ghc

>---------------------------------------------------------------

commit 2cc5b607df27e702541985f7c4c987806c4ec2a1
Author: RyanGlScott <ryan.gl.scott at gmail.com>
Date:   Sat Dec 19 11:09:52 2015 +0100

    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


>---------------------------------------------------------------

2cc5b607df27e702541985f7c4c987806c4ec2a1
 .gitignore                                         |  1 +
 docs/users_guide/7.12.1-notes.rst                  | 26 ++++++++++++++++++++++
 docs/users_guide/utils.rst                         |  4 ++++
 testsuite/tests/hsc2hs/Makefile                    | 12 ++++++++++
 testsuite/tests/hsc2hs/T10272.h                    | 12 ++++++++++
 testsuite/tests/hsc2hs/T10272.hsc                  |  6 +++++
 .../T4262.stdout => hsc2hs/T10272.stdout}          |  0
 testsuite/tests/hsc2hs/T4340.hsc                   |  8 +++++++
 .../T4262.stdout => hsc2hs/T4340.stdout}           |  0
 testsuite/tests/hsc2hs/all.T                       | 10 +++++++++
 10 files changed, 79 insertions(+)

diff --git a/.gitignore b/.gitignore
index ae23fbb..bb69e91 100644
--- a/.gitignore
+++ b/.gitignore
@@ -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
diff --git a/docs/users_guide/7.12.1-notes.rst b/docs/users_guide/7.12.1-notes.rst
index 749f7af..d443a0a 100644
--- a/docs/users_guide/7.12.1-notes.rst
+++ b/docs/users_guide/7.12.1-notes.rst
@@ -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
 ---------
 
diff --git a/docs/users_guide/utils.rst b/docs/users_guide/utils.rst
index 9c1237d..f490bfa 100644
--- a/docs/users_guide/utils.rst
+++ b/docs/users_guide/utils.rst
@@ -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.
diff --git a/testsuite/tests/hsc2hs/Makefile b/testsuite/tests/hsc2hs/Makefile
index ec16b16..54fa5ad 100644
--- a/testsuite/tests/hsc2hs/Makefile
+++ b/testsuite/tests/hsc2hs/Makefile
@@ -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
index 0000000..6d8142d
--- /dev/null
+++ b/testsuite/tests/hsc2hs/T10272.h
@@ -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
index 0000000..c4ff6d1
--- /dev/null
+++ b/testsuite/tests/hsc2hs/T10272.hsc
@@ -0,0 +1,6 @@
+module Main where
+
+#include "T10272.h"
+
+main :: IO ()
+main = print #{alignment eight}
diff --git a/testsuite/tests/concurrent/should_run/T4262.stdout b/testsuite/tests/hsc2hs/T10272.stdout
similarity index 100%
copy from testsuite/tests/concurrent/should_run/T4262.stdout
copy to testsuite/tests/hsc2hs/T10272.stdout
diff --git a/testsuite/tests/hsc2hs/T4340.hsc b/testsuite/tests/hsc2hs/T4340.hsc
new file mode 100644
index 0000000..5ae9e2a
--- /dev/null
+++ b/testsuite/tests/hsc2hs/T4340.hsc
@@ -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/concurrent/should_run/T4262.stdout b/testsuite/tests/hsc2hs/T4340.stdout
similarity index 100%
copy from testsuite/tests/concurrent/should_run/T4262.stdout
copy to testsuite/tests/hsc2hs/T4340.stdout
diff --git a/testsuite/tests/hsc2hs/all.T b/testsuite/tests/hsc2hs/all.T
index b095791..d4fc69d 100644
--- a/testsuite/tests/hsc2hs/all.T
+++ b/testsuite/tests/hsc2hs/all.T
@@ -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'])



More information about the ghc-commits mailing list