www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - [SAOC 2025] Improve importC Weekly Update #6

Hi everyone,

This week, I looked into `Macros inside a typedef enum not seen` 
issue.

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

After a thorough look into it, I realized the issue had nothing 
to do with typedef enums.
The real issue was that macros defined in a C file that refer to 
identifiers could not be resolved when imported from D. when you 
compile that C file directly, it worked but not when
you import it into a D file to use.

if you have
```
int a = 6;
  #define num a
```
num doesn't get to D symbol table because no case was handling 
identifiers. I attempted that using D aliases to represent C 
macros that refer to identifiers. my choice of aliases was 
because it could refer to a variable or a function which is 
typical in legacy C code.

But the most interesting part is that, my fix ended up in 
thousands of undefined identifiers when compiling druntime. That 
was largely due to builtin macros that D had no idea about in the 
C headers.

I tracked that and realized that was contributing to about 80% to 
a lot of the macros issues reported. so I had to disable alias 
creation for `__` beginning macros and also macros that had the 
same fields and also specifiers etc.
like `#define if if` which semantically had no sense in D but 
common in C code. but after that I still had few symbols left. I 
resolved the rest in a small table to check against. now, we can 
use defines in C that refer to (identifiers) variables and 
functions.

but I am now researching a lot of the builtins and see a few that 
could be reasonably defined in `importc.h`. for this work, I am 
adding all macro issues to well organize a fix that can help us 
fully win against macros.


can follow PR here: https://github.com/dlang/dmd/pull/22022
Oct 27