Issue 24200 - ImportC: .di file collected macro conflicts with Special Token
Summary: ImportC: .di file collected macro conflicts with Special Token
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords: ImportC, pull
Depends on:
Blocks:
 
Reported: 2023-10-25 05:51 UTC by dave287091
Modified: 2023-11-22 09:42 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description dave287091 2023-10-25 05:51:30 UTC
My generated .di file includes the following:

	enum __VERSION__ = "Apple LLVM 15.0.0 (clang-1500.0.40.1)";

This leads to an error, as __VERSION__ is a special token in D. __VERSION__ is also pre-defined by the preprocessor.

If the macros include any of the special tokens (https://dlang.org/spec/lex.html#specialtokens), you will get errors.

For example, if your c file looks like:

#define __DATE__ 1
#define __TIME__  1
#define __TIMESTAMP__  1
#define __VENDOR__  1
#define __EOF__  1

Then, after converting to a .di file, you will get some enums that look like:

	enum int __DATE__ = 1;
	enum int __TIME__ = 1;
	enum int __TIMESTAMP__ = 1;
	enum int __VENDOR__ = 1;
	enum int __EOF__ = 1;

which will all error out
Comment 1 Walter Bright 2023-10-27 20:40:25 UTC
I don't really know what to do about that. D has a different set of keywords than C does, and if D keywords are used in C code, how can it be translated into D code?

I expect that such generated .di files will need to be tweaked by hand.
Comment 2 dave287091 2023-10-27 22:38:02 UTC
(In reply to Walter Bright from comment #1)
> I don't really know what to do about that. D has a different set of keywords
> than C does, and if D keywords are used in C code, how can it be translated
> into D code?
> 
> I expect that such generated .di files will need to be tweaked by hand.

I think it’d be acceptable to just not emit them into the .di file. The __VERSION__ one especially as clang pre-defines that one and no one will want to use them. No one will want the conflicting macros anyway, but they currently prevent the .di file from being parsed.
Comment 3 Walter Bright 2023-11-06 18:32:42 UTC
(In reply to dave287091 from comment #2)
> I think it’d be acceptable to just not emit them into the .di file. The
> __VERSION__ one especially as clang pre-defines that one and no one will
> want to use them. No one will want the conflicting macros anyway, but they
> currently prevent the .di file from being parsed.

I'm a bit reluctant to do that - silently removing things that don't work. The user should be aware of what is not working so they can make appropriate changes.

I'll think about it some more.
Comment 4 Dlang Bot 2023-11-11 05:36:39 UTC
@WalterBright created dlang/dmd pull request #15797 "fix Issue 24200 - ImportC: .di file collected macro conflicts with Sp…" fixing this issue:

- fix Issue 24200 - ImportC: .di file collected macro conflicts with Special Token

https://github.com/dlang/dmd/pull/15797
Comment 5 Dlang Bot 2023-11-22 09:42:04 UTC
dlang/dmd pull request #15797 "fix Issue 24200 - ImportC: .di file collected macro conflicts with Sp…" was merged into master:

- db28d2195c13cb661b266d93d70a1337d562cb17 by Walter Bright:
  fix Issue 24200 - ImportC: .di file collected macro conflicts with Special Token

https://github.com/dlang/dmd/pull/15797