Skip to content

Commit 9ef58f3

Browse files
committed
add Wrapping C Code section
1 parent 931583c commit 9ef58f3

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

spec/importc.dd

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,31 @@ $(H2 $(LNAME2 d-side, ImportC from D's Point of View))
143143
static assert(E.min == 0 && E.max == 2);
144144
---
145145

146+
$(H2 $(LNAME2 wrapping, Wrapping C Code))
147+
148+
$(P Many difficulties with adapting C code to ImportC can be done without
149+
editing the C code itself. Wrap the C code in another C file and then
150+
$(CCODE #include) it. Consider the following problematic C file $(TT file.c):)
151+
152+
$(CCODE
153+
void func(int *__restrict p);
154+
int S;
155+
struct S { int a, b; };
156+
)
157+
158+
$(P The problems are that $(CCODE __restrict) is not a type qualifier recognized by ImportC
159+
(or C11),
160+
and the struct `S` is hidden from D by the declaration $(CCODE int S;).
161+
To wrap $(TT file.c) with a fix, create the file $(TT file_ic.c) with the contents:)
162+
163+
$(CCODE
164+
#define __restrict restrict
165+
#include "file.c"
166+
typedef struct S S_t;
167+
)
168+
169+
$(P Then, `import file_ic;` instead of `import file;`, and use `S_t` when $(CCODE struct S) is desired.)
170+
146171
$(H2 $(LNAME2 warnings, Warnings))
147172

148173
$(P Many suspicious C constructs normally cause warnings to be emitted by default by

0 commit comments

Comments
 (0)