JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.68k stars 5.48k forks source link

cpp namespace pollution in julia.h #10682

Open stevengj opened 9 years ago

stevengj commented 9 years ago

The julia.h file #defines a bunch of symbols that could easily conflict with other header files if someone is embedding Julia: NWORDS, NORETURN, DLLEXPORT, MAX_ALIGN, STORE_ARRAY_LEN, ARRAY_INLINE_NBYTES, ENABLE_INFERENCE, COPY_STACKS, STDCALL, NBITS ... (tons of stuff via libsupport.h)

It seems like it would be better to

JeffBezanson commented 9 years ago

+1

ViralBShah commented 9 years ago

Perhaps should be backported too.

yuyichao commented 8 years ago

The most used DLLEXPORT and NORETURN as well as some non-exported function without the jl_ prefix are fixed in #14219 .

fingolfin commented 4 years ago

While julia.h itself now is pretty "clean" in this regard, it pulls in a bunch of other headers which are not. I used the C preprocessor to extract a list of all Julia headers which julia.h eventually includes, and got this:

Then I used the following crude command to get a list of #define symbols that don't start with jl_, JL_, etc.

egrep "#\s*define" atomics.h julia_assert.h julia_threads.h julia_version.h locks.h support/analyzer_annotations.h support/arraylist.h support/bitvector.h support/dirpath.h support/dtypes.h support/hashing.h support/htable.h support/ios.h support/libsupport.h support/platform.h support/ptrhash.h support/strtod.h support/timefuncs.h support/utf8.h support/utils.h tls.h julia.h  | egrep -vi '#\s*define _?(jl|julia)_'

which produced the following result:

julia_assert.h:#    define NDEBUG
julia_threads.h:#define UNW_LOCAL_ONLY
support/analyzer_annotations.h:#define __has_feature(x) 0
support/analyzer_annotations.h:#define _Nonnull
support/arraylist.h:#define AL_N_INLINE 29
support/arraylist.h:#define SMALL_AL_N_INLINE 6
support/dirpath.h:#define PATHSEPSTRING "\\"
support/dirpath.h:#define PATHLISTSEPSTRING ";"
support/dirpath.h:#define PATH_MAX MAX_PATH
support/dirpath.h:#define PATHSEPSTRING "/"
support/dirpath.h:#define PATHLISTSEPSTRING ":"
support/dirpath.h:#define PATH_MAX 1024
support/dtypes.h:#define strtoull                                            _strtoui64
support/dtypes.h:#define strtoll                                             _strtoi64
support/dtypes.h:#define strcasecmp                                          _stricmp
support/dtypes.h:#define strncasecmp                                         _strnicmp
support/dtypes.h:#define snprintf                                            _snprintf
support/dtypes.h:#define stat                                                _stat
support/dtypes.h:#define STDIN_FILENO                                        0
support/dtypes.h:#define STDOUT_FILENO                                       1
support/dtypes.h:#define STDERR_FILENO                                       2
support/dtypes.h:#define STDCALL __stdcall
support/dtypes.h:#define STDCALL
support/dtypes.h:#define LITTLE_ENDIAN  __LITTLE_ENDIAN
support/dtypes.h:#define BIG_ENDIAN     __BIG_ENDIAN
support/dtypes.h:#define PDP_ENDIAN     __PDP_ENDIAN
support/dtypes.h:#define BYTE_ORDER     __BYTE_ORDER
support/dtypes.h:#define __LITTLE_ENDIAN  LITTLE_ENDIAN
support/dtypes.h:#define __BIG_ENDIAN     BIG_ENDIAN
support/dtypes.h:#define __PDP_ENDIAN     PDP_ENDIAN
support/dtypes.h:#define __BYTE_ORDER     BYTE_ORDER
support/dtypes.h:#define __LITTLE_ENDIAN    1234
support/dtypes.h:#define __BIG_ENDIAN       4321
support/dtypes.h:#define __PDP_ENDIAN       3412
support/dtypes.h:#define __BYTE_ORDER       __LITTLE_ENDIAN
support/dtypes.h:#define __FLOAT_WORD_ORDER __LITTLE_ENDIAN
support/dtypes.h:#define LITTLE_ENDIAN      __LITTLE_ENDIAN
support/dtypes.h:#define BIG_ENDIAN         __BIG_ENDIAN
support/dtypes.h:#define PDP_ENDIAN         __PDP_ENDIAN
support/dtypes.h:#define BYTE_ORDER         __BYTE_ORDER
support/dtypes.h:#define LLT_ALLOC(n) malloc(n)
support/dtypes.h:#define LLT_REALLOC(p,n) realloc((p),(n))
support/dtypes.h:#define LLT_FREE(x) free(x)
support/dtypes.h:#  define STATIC_INLINE static
support/dtypes.h:#  define STATIC_INLINE static __inline
support/dtypes.h:#  define STATIC_INLINE static inline
support/dtypes.h:#  define NOINLINE __declspec(noinline)
support/dtypes.h:#  define NOINLINE_DECL(f) __declspec(noinline) f
support/dtypes.h:#  define NOINLINE __attribute__((noinline))
support/dtypes.h:#  define NOINLINE_DECL(f) f __attribute__((noinline))
support/dtypes.h:#define TOP_BIT 0x8000000000000000
support/dtypes.h:#define NBITS 64
support/dtypes.h:#define TOP_BIT 0x80000000
support/dtypes.h:#define NBITS 32
support/dtypes.h:#define LLT_ALIGN(x, sz) (((x) + (sz)-1) & ~((sz)-1))
support/dtypes.h:#define __unlikely(x) __builtin_expect(!!(x), 0)
support/dtypes.h:#define __likely(x)   __builtin_expect(!!(x), 1)
support/dtypes.h:#define __unlikely(x) (x)
support/dtypes.h:#define __likely(x)   (x)
support/dtypes.h:#define DBL_MAXINT 9007199254740992LL
support/dtypes.h:#define FLT_MAXINT 16777216
support/dtypes.h:#define U64_MAX    18446744073709551615ULL
support/dtypes.h:#define S64_MAX    9223372036854775807LL
support/dtypes.h:#define S64_MIN    (-S64_MAX - 1LL)
support/dtypes.h:#define BIT63      0x8000000000000000LL
support/dtypes.h:#define U32_MAX    4294967295L
support/dtypes.h:#define S32_MAX    2147483647L
support/dtypes.h:#define S32_MIN    (-S32_MAX - 1L)
support/dtypes.h:#define BIT31      0x80000000
support/dtypes.h:#define D_PNAN ((double)+NAN)
support/dtypes.h:#define D_NNAN ((double)-NAN)
support/dtypes.h:#define D_PINF ((double)+INFINITY)
support/dtypes.h:#define D_NINF ((double)-INFINITY)
support/dtypes.h:#define F_PNAN ((float)+NAN)
support/dtypes.h:#define F_NNAN ((float)-NAN)
support/dtypes.h:#define F_PINF ((float)+INFINITY)
support/dtypes.h:#define F_NINF ((float)-INFINITY)
support/dtypes.h:#define N_NUMTYPES ((int)T_DOUBLE+1)
support/dtypes.h:# define T_PTRDIFF T_INT64
support/dtypes.h:# define T_SIZE T_UINT64
support/dtypes.h:# define T_PTRDIFF T_INT32
support/dtypes.h:# define T_SIZE T_UINT32
support/hashing.h:#define inthash int64hash
support/hashing.h:#define inthash int32hash
support/hashing.h:#define bitmix(a, b) (bitmix)((uintptr_t)(a), (uintptr_t)(b))
support/htable.h:#define HT_N_INLINE 32
support/htable.h:#define HT_NOTFOUND ((void*)1)
support/htable.h:#define HTPROT(HTNAME)                                                  \
support/htable.h:#define HTPROT_R(HTNAME)                                                \
support/ios.h:#define IOS_INLSIZE 54
support/ios.h:#define IOS_BUFSIZE 32768
support/ios.h:#define ON_P64(x) x
support/ios.h:#define ON_P64(x)
support/ios.h:#define IOS_EOF (-1)
support/ios.h:#define ios_puts(str, s) ios_write(s, str, strlen(str))
support/platform.h:#define _COMPILER_CLANG_
support/platform.h:#define _COMPILER_INTEL_
support/platform.h:#define _COMPILER_MICROSOFT_
support/platform.h:#define _COMPILER_GCC_
support/platform.h:#define _OS_FREEBSD_
support/platform.h:#define _OS_LINUX_
support/platform.h:#define _OS_WINDOWS_
support/platform.h:#define _OS_DARWIN_
support/platform.h:#define _OS_EMSCRIPTEN_
support/platform.h:#define _CPU_X86_64_
support/platform.h:#define _CPU_X86_
support/platform.h:#define _CPU_AARCH64_
support/platform.h:#define _CPU_ARM_
support/platform.h:#define _CPU_PPC64_
support/platform.h:#define _CPU_PPC_
support/platform.h:#define _CPU_WASM_
support/platform.h:#  define _P64
support/platform.h:#  define _P32
support/platform.h:#    define _P64
support/platform.h:#    define _P32
support/platform.h:#    define _P64
support/platform.h:#    define _P32
support/utf8.h:#define isutf(c) (((c)&0xC0)!=0x80)
support/utf8.h:#define UEOF ((uint32_t)-1)
support/utils.h:#define conv_to_ptrdiff conv_to_int64
support/utils.h:#define conv_to_size conv_to_uint64
support/utils.h:#define conv_to_ptrdiff conv_to_int32
support/utils.h:#define conv_to_size conv_to_uint32
support/utils.h:#define bswap_16(x) __builtin_bswap16(x)
support/utils.h:#define bswap_32(x) __builtin_bswap32(x)
support/utils.h:#define bswap_64(x) __builtin_bswap64(x)
support/utils.h:#define bswap_16(x) _byteswap_ushort(x)
support/utils.h:#define bswap_32(x) _byteswap_ulong(x)
support/utils.h:#define bswap_64(x) _byteswap_uint64(x)
support/utils.h:#define bswap_16(x) _bswap16(x)
support/utils.h:#define bswap_32(x) _bswap(x)
support/utils.h:#define bswap_64(x) _bswap64(x)
support/utils.h:#define bswap_16(x) (((x) & 0x00ff) << 8 | ((x) & 0xff00) >> 8)
support/utils.h:#define bswap_32(x) \
support/utils.h:#define bswap_64(x) ByteSwap64(x)
julia.h:#define STORE_ARRAY_LEN
julia.h:#    define MAX_ALIGN 8
julia.h:#    define MAX_ALIGN 16
julia.h:#    define MAX_ALIGN 8
julia.h:#    define MAX_ALIGN 4
julia.h:#  define MAX_ALIGN 8
julia.h:#define NWORDS(sz) (((sz)+7)>>3)
julia.h:#define NWORDS(sz) (((sz)+3)>>2)
julia.h:#define container_of(ptr, type, member) \
julia.h:#define DEFINE_FIELD_ACCESSORS(f)                                             \

However, DEFINE_FIELD_ACCESSORS is later #undefined.

stevengj commented 3 years ago

Would be good for someone to take this on with a PR.

Are we worried about backward compatibility for these preprocessor defines in Julia 1.x? (One option would be to also define the legacy symbols like MAX_ALIGN, but have a preprocessor option to omit them.)

vtjnash commented 3 years ago

Many of these we should probably attempt to stop using from here (remove or move to julia_internal.h, as applicable).