Issue 23864 - importC fails on assert( pointer != NULL )
Summary: importC fails on assert( pointer != NULL )
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Linux
: P1 enhancement
Assignee: No Owner
URL:
Keywords: ImportC
Depends on:
Blocks:
 
Reported: 2023-04-28 15:05 UTC by emteria_wegwerf
Modified: 2023-05-02 03:13 UTC (History)
2 users (show)

See Also:


Attachments
files needed for reproduction (7.85 KB, application/zip)
2023-04-28 15:05 UTC, emteria_wegwerf
Details

Note You need to log in before you can comment on or make changes to this issue.
Description emteria_wegwerf 2023-04-28 15:05:11 UTC
Created attachment 1872 [details]
files needed for reproduction

DMD64 D Compiler v2.103.0

dmd test.c 

fails for attached test.c / my_assert.h with 

test.c(8): Error: expression expected, not `{`


When I run the preprocessor command dmd claims to use, I get the attached test.i which does not compile with dmd either.

my_assert.h is a copy of the system's assert.h

System is x64 linux with gcc 12.2.1.
Comment 1 Walter Bright 2023-04-28 19:09:24 UTC
The error message I get is:

test.c(6): Error: undefined identifier `NULL`, did you mean `null`?

Is that what you got? NULL is not defined in test.i. NULL is supposed to be a macro defined in <stddef.h>.

gcc also complains that NULL is undefined.
Comment 2 emteria_wegwerf 2023-04-29 06:52:34 UTC
I get

$ dmd test.i
test.c(8): Error: expression expected, not `{`
test.c(8): Error: found `if` when expecting `)`
test.c(8): Error: found `;` when expecting `)`
test.c(8): Error: found `else` when expecting `;` following statement
test.c(8): Error: no type for declarator before `)`
test.c(10): Error: no type for declarator before `return`
test.c(11): Error: no type for declarator before `}`


This also happens if I replace NULL with 0 in test.c. After that, gcc compiles the (regenerated) test.i
Comment 3 Steven Schveighoffer 2023-04-30 02:25:10 UTC
Walter, this is the same issue I got when building stb_image.h (one of them)

The file I used is here: https://github.com/raysan5/raylib/blob/4.0.0/src/external/stb_image.h

The C file I tried to compile is:

```c
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
```

The errors I get are:

```
/usr/lib/gcc/x86_64-linux-gnu/11/include/xmmintrin.h(1016): Error: expression expected, not `__attribute__`
/usr/lib/gcc/x86_64-linux-gnu/11/include/xmmintrin.h(1016): Error: found `int` when expecting `)`
/usr/lib/gcc/x86_64-linux-gnu/11/include/xmmintrin.h(1017): Error: found `{` when expecting `;` following `return` statement
/usr/lib/gcc/x86_64-linux-gnu/11/include/xmmintrin.h(1017): Error: found `}` when expecting `;` following statement
/usr/lib/gcc/x86_64-linux-gnu/11/include/xmmintrin.h(1017): Error: found `)` instead of statement
stb_image.h(1249): Error: expression expected, not `{`
stb_image.h(1249): Error: found `if` when expecting `)`
stb_image.h(1249): Error: found `;` when expecting `)`
stb_image.h(1249): Error: found `else` when expecting `;` following statement
stb_image.h(1249): Error: no type for declarator before `)`
stb_image.h(1251): Error: no type for declarator before `if`
stb_image.h(1253): Error: no type-specifier for declarator
stb_image.h(1254): Error: no type for declarator before `}`
stb_image.h(1258): Error: no type for declarator before `if`
stb_image.h(1260): Error: found `,` when expecting `)`
stb_image.h(1260): Error: `=`, `;` or `,` expected to end declaration instead of `*`
stb_image.h(1261): Error: no type for declarator before `}`
stb_image.h(1263): Error: no type for declarator before `return`
stb_image.h(1264): Error: no type for declarator before `}`
stb_image.h(1275): Error: expression expected, not `{`
```
The errors in stb_image.h are assert calls. The ones deep in xmmintrin.h are some sort of vector call, probably an intrinsic. The code is:

```c
extern __inline __m128 __attribute__((__gnu_inline__, __always_inline__, __artificial__))
_mm_move_ss (__m128 __A, __m128 __B)
{
  return (__m128) __builtin_shuffle ((__v4sf)__A, (__v4sf)__B,
                                     __extension__
                                     (__attribute__((__vector_size__ (16))) int)
                                     {4,1,2,3});
}
```
Comment 4 Walter Bright 2023-05-02 02:16:11 UTC
(In reply to emteria_wegwerf from comment #2)
> I get
> 
> $ dmd test.i
> test.c(8): Error: expression expected, not `{`

This is because of the expression statement. This problem was fixed with:

https://github.com/dlang/dmd/pull/15093
Comment 5 Walter Bright 2023-05-02 02:19:02 UTC
(In reply to Steven Schveighoffer from comment #3)
> Walter, this is the same issue I got when building stb_image.h (one of them)

Your's appears to be a completely different problem, which is:

__attribute__((__vector_size__ (16))) int
Comment 6 Walter Bright 2023-05-02 03:13:40 UTC
Therefore, since the original bug for this issue was fixed, I created a new issue from Steven's report.

https://issues.dlang.org/show_bug.cgi?id=23875

and closed this one as already fixed.