Skip to content

Commit c72d652

Browse files
authored
Merge pull request #3030 from WalterBright/ImportC4
ImportC: add Wrapping C Code section
2 parents 3e40a94 + 9ef58f3 commit c72d652

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
@@ -170,6 +170,31 @@ $(H2 $(LNAME2 d-side, ImportC from D's Point of View))
170170
static assert(E.min == 0 && E.max == 2);
171171
---
172172

173+
$(H2 $(LNAME2 wrapping, Wrapping C Code))
174+
175+
$(P Many difficulties with adapting C code to ImportC can be done without
176+
editing the C code itself. Wrap the C code in another C file and then
177+
$(CCODE #include) it. Consider the following problematic C file $(TT file.c):)
178+
179+
$(CCODE
180+
void func(int *__restrict p);
181+
int S;
182+
struct S { int a, b; };
183+
)
184+
185+
$(P The problems are that $(CCODE __restrict) is not a type qualifier recognized by ImportC
186+
(or C11),
187+
and the struct `S` is hidden from D by the declaration $(CCODE int S;).
188+
To wrap $(TT file.c) with a fix, create the file $(TT file_ic.c) with the contents:)
189+
190+
$(CCODE
191+
#define __restrict restrict
192+
#include "file.c"
193+
typedef struct S S_t;
194+
)
195+
196+
$(P Then, `import file_ic;` instead of `import file;`, and use `S_t` when $(CCODE struct S) is desired.)
197+
173198
$(H2 $(LNAME2 warnings, Warnings))
174199

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

0 commit comments

Comments
 (0)