[clang] 0d45944 - Mark ATOMIC_VAR_INIT and ATOMIC_FLAG_INIT as deprecated
Aaron Ballman via cfe-commits
cfe-commits at lists.llvm.org
Tue Jan 18 10:42:08 PST 2022
Author: Aaron Ballman
Date: 2022-01-18T13:41:56-05:00
New Revision: 0d459444e5105d689e6388612784b89a93054944
URL: https://github.com/llvm/llvm-project/commit/0d459444e5105d689e6388612784b89a93054944
DIFF: https://github.com/llvm/llvm-project/commit/0d459444e5105d689e6388612784b89a93054944.diff
LOG: Mark ATOMIC_VAR_INIT and ATOMIC_FLAG_INIT as deprecated
C17 deprecated ATOMIC_VAR_INIT with the resolution of DR 485. C++
followed suit when adopting P0883R2 for C++20, but additionally chose
to deprecate ATOMIC_FLAG_INIT at the same time despite the macro still
being required in C. This patch marks both macros as deprecated when
appropriate to do so.
Added:
clang/test/Headers/stdatomic-deprecations.c
Modified:
clang/docs/ReleaseNotes.rst
clang/docs/UsersManual.rst
clang/lib/Headers/stdatomic.h
Removed:
################################################################################
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 9fd22297ae423..de395b5d035ec 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -169,6 +169,11 @@ C Language Changes in Clang
which adds ``*_WIDTH`` macros to limits.h and stdint.h to report the bit
width of various integer datatypes.
+- The ``ATOMIC_VAR_INIT`` macro from ``<stdatomic.h>`` is now diagnosed as
+ deprecated in C17 and later. The diagnostic can be disabled by defining the
+ ``_CLANG_DISABLE_CRT_DEPRECATION_WARNINGS`` macro prior to including the
+ header.
+
C++ Language Changes in Clang
-----------------------------
@@ -176,7 +181,15 @@ C++ Language Changes in Clang
C++20 Feature Support
^^^^^^^^^^^^^^^^^^^^^
-...
+
+- The ``ATOMIC_VAR_INIT`` and ``ATOMIC_FLAG_INIT`` macros from the C standard
+ library ``<stdatomic.h>`` header are now diagnosed as deprecated in C++20 and
+ later. Note, the behavior is specific to the inclusion of ``<stdatomic.h>``
+ in C++ code; the STL ``<atomic>`` header also controls the behavior of these
+ macros and is not affected by these changes. The ``<stdatomic.h>`` diagnostic
+ can be disabled by defining the ``_CLANG_DISABLE_CRT_DEPRECATION_WARNINGS``
+ macro prior to including the header.
+
C++2b Feature Support
^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/docs/UsersManual.rst b/clang/docs/UsersManual.rst
index bdb9705dac637..227884f6aad6a 100644
--- a/clang/docs/UsersManual.rst
+++ b/clang/docs/UsersManual.rst
@@ -1132,10 +1132,28 @@ A ``#include`` directive which finds a file relative to the current
directory is treated as including a system header if the including file
is treated as a system header.
+Controlling Deprecation Diagnostics in Clang-Provided C Runtime Headers
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+Clang is responsible for providing some of the C runtime headers that cannot be
+provided by a platform CRT, such as implementation limits or when compiling in
+freestanding mode. Define the ``_CLANG_DISABLE_CRT_DEPRECATION_WARNINGS`` macro
+prior to including such a C runtime header to disable the deprecation warnings.
+Note that the C Standard Library headers are allowed to transitively include
+other standard library headers (see 7.1.2p5), and so the most appropriate use
+of this macro is to set it within the build system using ``-D`` or before any
+include directives in the translation unit.
+
+.. code-block:: c
+
+ #define _CLANG_DISABLE_CRT_DEPRECATION_WARNINGS
+ #include <stdint.h> // Clang CRT deprecation warnings are disabled.
+ #include <stdatomic.h> // Clang CRT deprecation warnings are disabled.
+
.. _diagnostics_enable_everything:
Enabling All Diagnostics
-^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+^^^^^^^^^^^^^^^^^^^^^^^^
In addition to the traditional ``-W`` flags, one can enable **all** diagnostics
by passing :option:`-Weverything`. This works as expected with
diff --git a/clang/lib/Headers/stdatomic.h b/clang/lib/Headers/stdatomic.h
index 1e47bcb2bacfc..780bcc2dfea17 100644
--- a/clang/lib/Headers/stdatomic.h
+++ b/clang/lib/Headers/stdatomic.h
@@ -44,6 +44,11 @@ extern "C" {
/* 7.17.2 Initialization */
#define ATOMIC_VAR_INIT(value) (value)
+#if (__STDC_VERSION__ >= 201710L || __cplusplus >= 202002L) && \
+ !defined(_CLANG_DISABLE_CRT_DEPRECATION_WARNINGS)
+/* ATOMIC_VAR_INIT was deprecated in C17 and C++20. */
+#pragma clang deprecated(ATOMIC_VAR_INIT)
+#endif
#define atomic_init __c11_atomic_init
/* 7.17.3 Order and consistency */
@@ -153,6 +158,10 @@ typedef _Atomic(uintmax_t) atomic_uintmax_t;
typedef struct atomic_flag { atomic_bool _Value; } atomic_flag;
#define ATOMIC_FLAG_INIT { 0 }
+#if __cplusplus >= 202002L && !defined(_CLANG_DISABLE_CRT_DEPRECATION_WARNINGS)
+/* ATOMIC_FLAG_INIT was deprecated in C++20 but is not deprecated in C. */
+#pragma clang deprecated(ATOMIC_FLAG_INIT)
+#endif
/* These should be provided by the libc implementation. */
#ifdef __cplusplus
diff --git a/clang/test/Headers/stdatomic-deprecations.c b/clang/test/Headers/stdatomic-deprecations.c
new file mode 100644
index 0000000000000..56ee33e16f51a
--- /dev/null
+++ b/clang/test/Headers/stdatomic-deprecations.c
@@ -0,0 +1,18 @@
+// RUN: %clang_cc1 -fsyntax-only -std=c11 %s -verify=okay
+// RUN: %clang_cc1 -fsyntax-only -std=c17 %s -verify
+// RUN: %clang_cc1 -fsyntax-only -std=c++11 -x c++ %s -verify=okay
+// RUN: %clang_cc1 -fsyntax-only -std=c++20 -x c++ %s -verify=cxx,expected
+// RUN: %clang_cc1 -fsyntax-only -std=c++20 -x c++ %s -verify=okay -D_CLANG_DISABLE_CRT_DEPRECATION_WARNINGS
+// RUN: %clang_cc1 -fsyntax-only -std=c17 %s -verify=okay -D_CLANG_DISABLE_CRT_DEPRECATION_WARNINGS
+//
+// okay-no-diagnostics
+
+#include <stdatomic.h>
+
+void func(void) {
+ _Atomic int i = ATOMIC_VAR_INIT(12); // expected-warning {{macro 'ATOMIC_VAR_INIT' has been marked as deprecated}} \
+ // expected-note at stdatomic.h:* {{macro marked 'deprecated' here}}
+ #if defined(ATOMIC_FLAG_INIT) // cxx-warning {{macro 'ATOMIC_FLAG_INIT' has been marked as deprecated}} \
+ // cxx-note at stdatomic.h:* {{macro marked 'deprecated' here}}
+ #endif
+}
More information about the cfe-commits
mailing list