Issue 23622 - ImportC #defines conflict with declarations
Summary: ImportC #defines conflict with declarations
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Linux
: P3 normal
Assignee: No Owner
URL:
Keywords: ImportC, pull
Depends on:
Blocks:
 
Reported: 2023-01-12 09:54 UTC by Krzysztof Jajeśnica
Modified: 2023-01-14 12:38 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Krzysztof Jajeśnica 2023-01-12 09:54:09 UTC
The following C code fails to compile with DMD 2.101.1:


#include <math.h>

float norm2D(float x, float y) {
    return sqrt(x*x + y*y);
}


DMD error message:

#defines(1074): Error: variable `vec_math.FP_NAN` conflicts with enum member `__anonymous.FP_NAN` at /usr/include/math.h(936)
#defines(1075): Error: variable `vec_math.FP_INFINITE` conflicts with enum member `__anonymous.FP_INFINITE` at /usr/include/math.h(939)
#defines(1076): Error: variable `vec_math.FP_ZERO` conflicts with enum member `__anonymous.FP_ZERO` at /usr/include/math.h(942)
#defines(1077): Error: variable `vec_math.FP_SUBNORMAL` conflicts with enum member `__anonymous.FP_SUBNORMAL` at /usr/include/math.h(945)
#defines(1078): Error: variable `vec_math.FP_NORMAL` conflicts with enum member `__anonymous.FP_NORMAL` at /usr/include/math.h(948)


The problem seems to be caused by this strange enum declaration in math.h:

enum
  {
    FP_NAN =
# define FP_NAN 0
      FP_NAN,
    FP_INFINITE =
# define FP_INFINITE 1
      FP_INFINITE,
    FP_ZERO =
# define FP_ZERO 2
      FP_ZERO,
    FP_SUBNORMAL =
# define FP_SUBNORMAL 3
      FP_SUBNORMAL,
    FP_NORMAL =
# define FP_NORMAL 4
      FP_NORMAL
  };
Comment 1 Walter Bright 2023-01-13 00:37:27 UTC
What's happening is ImportC sees those macro definitions and transforms them into manifest constant declarations. Those declarations then conflict with the enum declarations.
Comment 2 Walter Bright 2023-01-13 08:28:17 UTC
A reduced test case:

  enum {  FP_NAN = 0 };
  #define FP_NAN 0
Comment 3 Dlang Bot 2023-01-13 08:32:52 UTC
@WalterBright created dlang/dmd pull request #14811 "fix Issue 23622 - ImportC #defines conflict with declarations" fixing this issue:

- fix Issue 23622 - ImportC #defines conflict with declarations

https://github.com/dlang/dmd/pull/14811
Comment 4 Dlang Bot 2023-01-14 00:41:43 UTC
dlang/dmd pull request #14811 "fix Issue 23622 - ImportC #defines conflict with declarations" was merged into master:

- 6ad91b95ce902a86199b234117b58bc48d14710a by Walter Bright:
  fix Issue 23622 - ImportC #defines conflict with declarations

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