From 7f3dccd4d8fec72677d712a429ef5468824d4839 Mon Sep 17 00:00:00 2001 From: Jamie Schmeiser Date: Wed, 16 Jul 2025 15:40:09 -0400 Subject: [PATCH] Ensure proper NULL macro definition for system include files. The C standard allows for at least 2 valid definitions of a null pointer constant and mandates that several standard headers files define the macro NULL to be a null pointer constant. Ensure that definitions of NULL are consistent across the various C header files. respond to review: add in locale.h respond to review: make all changes AIX specific --- clang/lib/Headers/CMakeLists.txt | 38 +++++++++++++++++++++++++++++ clang/lib/Headers/dbm.h | 24 ++++++++++++++++++ clang/lib/Headers/locale.h | 24 ++++++++++++++++++ clang/lib/Headers/stdio.h | 24 ++++++++++++++++++ clang/lib/Headers/stdlib.h | 24 ++++++++++++++++++ clang/lib/Headers/string.h | 24 ++++++++++++++++++ clang/lib/Headers/sys/dir.h | 24 ++++++++++++++++++ clang/lib/Headers/sys/param.h | 24 ++++++++++++++++++ clang/lib/Headers/sys/types.h | 24 ++++++++++++++++++ clang/lib/Headers/time.h | 24 ++++++++++++++++++ clang/lib/Headers/unistd.h | 24 ++++++++++++++++++ clang/lib/Headers/wchar.h | 24 ++++++++++++++++++ clang/test/Headers/check-NULL-aix.c | 22 +++++++++++++++++ 13 files changed, 324 insertions(+) create mode 100644 clang/lib/Headers/dbm.h create mode 100644 clang/lib/Headers/locale.h create mode 100644 clang/lib/Headers/stdio.h create mode 100644 clang/lib/Headers/stdlib.h create mode 100644 clang/lib/Headers/string.h create mode 100644 clang/lib/Headers/sys/dir.h create mode 100644 clang/lib/Headers/sys/param.h create mode 100644 clang/lib/Headers/sys/types.h create mode 100644 clang/lib/Headers/time.h create mode 100644 clang/lib/Headers/unistd.h create mode 100644 clang/lib/Headers/wchar.h create mode 100644 clang/test/Headers/check-NULL-aix.c diff --git a/clang/lib/Headers/CMakeLists.txt b/clang/lib/Headers/CMakeLists.txt index dd52498bbef4c..6c238c2e8aee9 100644 --- a/clang/lib/Headers/CMakeLists.txt +++ b/clang/lib/Headers/CMakeLists.txt @@ -39,6 +39,26 @@ set(core_files varargs.h ) +set(aix_wrapper_files + dbm.h + locale.h + stdio.h + stdlib.h + string.h + time.h + unistd.h + wchar.h + ) +set(aix_sys_subdir_wrapper_files + sys/dir.h + sys/param.h + sys/types.h + ) +set(aix_files + ${aix_wrapper_files} + ${aix_sys_subdir_wrapper_files} + ) + set(arm_common_files # Headers shared by Arm and AArch64 arm_acle.h @@ -312,6 +332,7 @@ set(utility_files set(files ${core_files} + ${aix_files} ${arm_common_files} ${arm_only_files} ${aarch64_only_files} @@ -529,6 +550,7 @@ set_target_properties("clang-resource-headers" PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${output_dir}") add_dependencies("clang-resource-headers" "core-resource-headers" + "aix-resource-headers" "arm-common-resource-headers" "arm-resource-headers" "aarch64-resource-headers" @@ -557,6 +579,7 @@ add_header_target("core-resource-headers" ${core_files}) add_header_target("arm-common-resource-headers" "${arm_common_files};${arm_common_generated_files}") # Architecture/platform specific targets +add_header_target("aix-resource-headers" "${aix_files}") add_header_target("arm-resource-headers" "${arm_only_files};${arm_only_generated_files}") add_header_target("aarch64-resource-headers" "${aarch64_only_files};${aarch64_only_generated_files}") add_header_target("cuda-resource-headers" "${cuda_files};${cuda_wrapper_files};${cuda_wrapper_bits_files};${cuda_wrapper_utility_files}") @@ -644,6 +667,18 @@ install( EXCLUDE_FROM_ALL COMPONENT core-resource-headers) +install( + FILES ${aix_wrapper_files} + DESTINATION ${header_install_dir} + EXCLUDE_FROM_ALL + COMPONENT aix-resource-headers) + +install( + FILES ${aix_sys_subdir_wrapper_files} + DESTINATION ${header_install_dir}/sys + EXCLUDE_FROM_ALL + COMPONENT aix-resource-headers) + install( FILES ${arm_common_files} ${arm_common_generated_files} DESTINATION ${header_install_dir} @@ -837,6 +872,9 @@ if (NOT LLVM_ENABLE_IDE) add_llvm_install_targets(install-core-resource-headers DEPENDS core-resource-headers COMPONENT core-resource-headers) + add_llvm_install_targets(install-aix-resource-headers + DEPENDS aix-resource-headers + COMPONENT aix-resource-headers) add_llvm_install_targets(install-arm-common-resource-headers DEPENDS arm-common-resource-headers COMPONENT arm-common-resource-headers) diff --git a/clang/lib/Headers/dbm.h b/clang/lib/Headers/dbm.h new file mode 100644 index 0000000000000..6e2ff119e20fd --- /dev/null +++ b/clang/lib/Headers/dbm.h @@ -0,0 +1,24 @@ +/*===---- dbm.h - BSD header for database management ----------------------===*\ + * + * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. + * See https://llvm.org/LICENSE.txt for license information. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + * +\*===----------------------------------------------------------------------===*/ + +#if !defined(_AIX) + +#include_next + +#else + +#define __need_NULL +#include + +#include_next + +/* Ensure that the definition of NULL is as expected. */ +#define __need_NULL +#include + +#endif diff --git a/clang/lib/Headers/locale.h b/clang/lib/Headers/locale.h new file mode 100644 index 0000000000000..d1d755874c00e --- /dev/null +++ b/clang/lib/Headers/locale.h @@ -0,0 +1,24 @@ +/*===---- locale.h - Standard header for localization ---------------------===*\ + * + * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. + * See https://llvm.org/LICENSE.txt for license information. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + * +\*===----------------------------------------------------------------------===*/ + +#if !defined(_AIX) + +#include_next + +#else + +#define __need_NULL +#include + +#include_next + +/* Ensure that the definition of NULL is as expected. */ +#define __need_NULL +#include + +#endif diff --git a/clang/lib/Headers/stdio.h b/clang/lib/Headers/stdio.h new file mode 100644 index 0000000000000..775dabf42969b --- /dev/null +++ b/clang/lib/Headers/stdio.h @@ -0,0 +1,24 @@ +/*===---- stdio.h - Standard header for input and output-------------------===*\ + * + * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. + * See https://llvm.org/LICENSE.txt for license information. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + * +\*===----------------------------------------------------------------------===*/ + +#if !defined(_AIX) + +#include_next + +#else + +#define __need_NULL +#include + +#include_next + +/* Ensure that the definition of NULL is as expected. */ +#define __need_NULL +#include + +#endif diff --git a/clang/lib/Headers/stdlib.h b/clang/lib/Headers/stdlib.h new file mode 100644 index 0000000000000..823dfbfef3432 --- /dev/null +++ b/clang/lib/Headers/stdlib.h @@ -0,0 +1,24 @@ +/*===---- stdlib.h - Standard header for general utilities ----------------===*\ + * + * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. + * See https://llvm.org/LICENSE.txt for license information. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + * +\*===----------------------------------------------------------------------===*/ + +#if !defined(_AIX) + +#include_next + +#else + +#define __need_NULL +#include + +#include_next + +/* Ensure that the definition of NULL is as expected. */ +#define __need_NULL +#include + +#endif diff --git a/clang/lib/Headers/string.h b/clang/lib/Headers/string.h new file mode 100644 index 0000000000000..5ec59b3702af8 --- /dev/null +++ b/clang/lib/Headers/string.h @@ -0,0 +1,24 @@ +/*===---- string.h - Standard header for string handling ------------------===*\ + * + * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. + * See https://llvm.org/LICENSE.txt for license information. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + * +\*===----------------------------------------------------------------------===*/ + +#if !defined(_AIX) + +#include_next + +#else + +#define __need_NULL +#include + +#include_next + +/* Ensure that the definition of NULL is as expected. */ +#define __need_NULL +#include + +#endif diff --git a/clang/lib/Headers/sys/dir.h b/clang/lib/Headers/sys/dir.h new file mode 100644 index 0000000000000..9afb0b12eee93 --- /dev/null +++ b/clang/lib/Headers/sys/dir.h @@ -0,0 +1,24 @@ +/*===---- sys/dir.h - BSD header for directory handling -------------------===*\ + * + * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. + * See https://llvm.org/LICENSE.txt for license information. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + * +\*===----------------------------------------------------------------------===*/ + +#if !defined(_AIX) + +#include_next + +#else + +#define __need_NULL +#include + +#include_next + +/* Ensure that the definition of NULL is as expected. */ +#define __need_NULL +#include + +#endif diff --git a/clang/lib/Headers/sys/param.h b/clang/lib/Headers/sys/param.h new file mode 100644 index 0000000000000..b024b94f1c4db --- /dev/null +++ b/clang/lib/Headers/sys/param.h @@ -0,0 +1,24 @@ +/*===---- sys/param.h - BSD header ----------------------------------------===*\ + * + * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. + * See https://llvm.org/LICENSE.txt for license information. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + * +\*===----------------------------------------------------------------------===*/ + +#if !defined(_AIX) + +#include_next + +#else + +#define __need_NULL +#include + +#include_next + +/* Ensure that the definition of NULL is as expected. */ +#define __need_NULL +#include + +#endif diff --git a/clang/lib/Headers/sys/types.h b/clang/lib/Headers/sys/types.h new file mode 100644 index 0000000000000..1fd02d9ae79f8 --- /dev/null +++ b/clang/lib/Headers/sys/types.h @@ -0,0 +1,24 @@ +/*===---- sys/types.h - BSD header for types ------------------------------===*\ + * + * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. + * See https://llvm.org/LICENSE.txt for license information. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + * +\*===----------------------------------------------------------------------===*/ + +#if !defined(_AIX) + +#include_next + +#else + +#define __need_NULL +#include + +#include_next + +/* Ensure that the definition of NULL is as expected. */ +#define __need_NULL +#include + +#endif diff --git a/clang/lib/Headers/time.h b/clang/lib/Headers/time.h new file mode 100644 index 0000000000000..ec23f91142492 --- /dev/null +++ b/clang/lib/Headers/time.h @@ -0,0 +1,24 @@ +/*===---- time.h - Standard header for date and time handling -------------===*\ + * + * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. + * See https://llvm.org/LICENSE.txt for license information. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + * +\*===----------------------------------------------------------------------===*/ + +#if !defined(_AIX) + +#include_next + +#else + +#define __need_NULL +#include + +#include_next + +/* Ensure that the definition of NULL is as expected. */ +#define __need_NULL +#include + +#endif diff --git a/clang/lib/Headers/unistd.h b/clang/lib/Headers/unistd.h new file mode 100644 index 0000000000000..b12495976095f --- /dev/null +++ b/clang/lib/Headers/unistd.h @@ -0,0 +1,24 @@ +/*===---- unistd.h - Posix Standard header --------------------------------===*\ + * + * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. + * See https://llvm.org/LICENSE.txt for license information. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + * +\*===----------------------------------------------------------------------===*/ + +#if !defined(_AIX) + +#include_next + +#else + +#define __need_NULL +#include + +#include_next + +/* Ensure that the definition of NULL is as expected. */ +#define __need_NULL +#include + +#endif diff --git a/clang/lib/Headers/wchar.h b/clang/lib/Headers/wchar.h new file mode 100644 index 0000000000000..1694dec799794 --- /dev/null +++ b/clang/lib/Headers/wchar.h @@ -0,0 +1,24 @@ +/*===---- wchar.h - Standard header for string handling ------------------===*\ + * + * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. + * See https://llvm.org/LICENSE.txt for license information. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + * +\*===----------------------------------------------------------------------===*/ + +#if !defined(_AIX) + +#include_next + +#else + +#define __need_NULL +#include + +#include_next + +/* Ensure that the definition of NULL is as expected. */ +#define __need_NULL +#include + +#endif diff --git a/clang/test/Headers/check-NULL-aix.c b/clang/test/Headers/check-NULL-aix.c new file mode 100644 index 0000000000000..d5cc84c4331fa --- /dev/null +++ b/clang/test/Headers/check-NULL-aix.c @@ -0,0 +1,22 @@ +// There are at least 2 valid C null-pointer constants as defined +// by the C language standard. +// Test that the macro NULL is defined consistently by those system headers +// on AIX that have a macro definition for NULL. + +// REQUIRES: system-aix + +// RUN: %clang %s -Dheader="" -E | tail -1 | FileCheck %s +// RUN: %clang %s -Dheader="" -E | tail -1 | FileCheck %s +// RUN: %clang %s -Dheader="" -E | tail -1 | FileCheck %s +// RUN: %clang %s -Dheader="" -E | tail -1 | FileCheck %s +// RUN: %clang %s -Dheader="" -E | tail -1 | FileCheck %s +// RUN: %clang %s -Dheader="" -E | tail -1 | FileCheck %s +// RUN: %clang %s -Dheader="" -E | tail -1 | FileCheck %s +// RUN: %clang %s -Dheader="" -E | tail -1 | FileCheck %s +// RUN: %clang %s -Dheader="" -E | tail -1 | FileCheck %s +// RUN: %clang %s -Dheader="" -E | tail -1 | FileCheck %s +// RUN: %clang %s -Dheader="" -E | tail -1 | FileCheck %s + +#include header +void *p = NULL; +// CHECK: ({{ *}}({{ *}}void{{ *}}*{{ *}}){{ *}}0{{ *}})