Make hsc_alignment work in clang
authorBartosz Nitka <bnitka@fb.com>
Wed, 15 Mar 2017 21:52:36 +0000 (17:52 -0400)
committerBen Gamari <ben@smart-cactus.org>
Wed, 15 Mar 2017 21:52:36 +0000 (17:52 -0400)
Summary:
With the old definition clang gives this error:
```
error: '(anonymous struct at example.cpp:15:3)'
  cannot be defined in a type specifier
```
This makes it work under clang.

Test Plan:
Test with https://godbolt.org/g/kIBwyP and look at the
assembly.

Reviewers: O25 HSC2HS, hvr, austin, bgamari, mpickering

Reviewed By: bgamari

Subscribers: simonmar

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

template-hsc.h

index 71832f5..dec0e43 100644 (file)
@@ -88,8 +88,14 @@ void *hsc_stdout(void);
 #define hsc_size(t...) \
     hsc_printf("(%ld)", (long) sizeof(t));
 
-#define hsc_alignment(t...) \
-    hsc_printf("(%ld)", (long) offsetof(struct {char x__; t (y__); }, y__));
+#define hsc_alignment(x...)                                           \
+  do {                                                                \
+    struct __anon_x__ {                                               \
+      char a;                                                         \
+      x b;                                                            \
+    };                                                                \
+    hsc_printf("%lu", (unsigned long)offsetof(struct __anon_x__, b)); \
+  } while (0)
 
 #define hsc_enum(t, f, print_name, x)                   \
     print_name;                                         \