[libcxx-commits] [libcxx] Allow the use of `if constexpr` in dialects after C++11. (PR #76178)

via libcxx-commits libcxx-commits at lists.llvm.org
Thu Dec 21 12:06:47 PST 2023


https://github.com/EricWF created https://github.com/llvm/llvm-project/pull/76178

This change enables that by disabling the C++17 extensions warning
in the test suite and by adding documentation and testing for the use of
this extension.


>From 193d56903da6eb223d12eeae9abb961a2ca7e937 Mon Sep 17 00:00:00 2001
From: Eric Fiselier <eric at efcs.ca>
Date: Thu, 21 Dec 2023 15:05:38 -0500
Subject: [PATCH] Allow the use of `if constexpr` in dialects after C++11.

This change enables that by disabling the C++17 extensions warning
in the test suite and by adding documentation and testing for the use of
this extension.
---
 ...XX03Support.rst => ExtendedCXXSupport.rst} | 24 ++++++++++----
 libcxx/docs/index.rst                         |  2 +-
 .../extensions/test-cxx17-extensions.pass.cpp | 31 +++++++++++++++++++
 libcxx/utils/libcxx/test/params.py            |  2 ++
 4 files changed, 52 insertions(+), 7 deletions(-)
 rename libcxx/docs/DesignDocs/{ExtendedCXX03Support.rst => ExtendedCXXSupport.rst} (82%)
 create mode 100644 libcxx/test/libcxx/extensions/test-cxx17-extensions.pass.cpp

diff --git a/libcxx/docs/DesignDocs/ExtendedCXX03Support.rst b/libcxx/docs/DesignDocs/ExtendedCXXSupport.rst
similarity index 82%
rename from libcxx/docs/DesignDocs/ExtendedCXX03Support.rst
rename to libcxx/docs/DesignDocs/ExtendedCXXSupport.rst
index 8c18e563e81997..27d73a3c7a1b23 100644
--- a/libcxx/docs/DesignDocs/ExtendedCXX03Support.rst
+++ b/libcxx/docs/DesignDocs/ExtendedCXXSupport.rst
@@ -1,6 +1,6 @@
-=======================
-Extended C++03 Support
-=======================
+===================================
+Language and Library C++ Extensions
+===================================
 
 .. contents::
    :local:
@@ -10,11 +10,14 @@ Overview
 
 libc++ is an implementation of the C++ standard library targeting C++11 or later.
 
-In C++03, the library implements the C++11 standard using C++11 language extensions provided
+In C++03, the library provides the C++11 standard using C++11 language extensions provided
 by Clang.
 
-This document tracks the C++11 extensions libc++ requires, the C++11 extensions it provides,
-and how to write minimal C++11 inside libc++.
+In other dialects, the library may depend on language extensions provided by the compiler.
+
+This document tracks the C++ extensions libc++ requires from the compiler, and in what dialect.
+Additionally this documents the C++11 extensions libc++ provides, and how to write
+minimal C++11 inside libc++.
 
 Required C++11 Compiler Extensions
 ==================================
@@ -36,6 +39,15 @@ mode. These include:
 *  Trailing return types.
 * ``>>`` without a space.
 
+Required C++17 Compiler Extensions
+==================================
+
+Clang and GCC provide some language extensions in C++11 and later.
+The features libc++ expects Clang & GCC to provide are:
+
+* `if constexpr`
+
+This extension is not available in C++03.
 
 Provided C++11 Library Extensions
 =================================
diff --git a/libcxx/docs/index.rst b/libcxx/docs/index.rst
index c7769bae6bb17d..f4a472b9e622d1 100644
--- a/libcxx/docs/index.rst
+++ b/libcxx/docs/index.rst
@@ -186,7 +186,7 @@ Design Documents
    DesignDocs/AtomicDesign
    DesignDocs/CapturingConfigInfo
    DesignDocs/ExperimentalFeatures
-   DesignDocs/ExtendedCXX03Support
+   DesignDocs/ExtendedCXXSupport
    DesignDocs/FeatureTestMacros
    DesignDocs/FileTimeType
    DesignDocs/HeaderRemovalPolicy
diff --git a/libcxx/test/libcxx/extensions/test-cxx17-extensions.pass.cpp b/libcxx/test/libcxx/extensions/test-cxx17-extensions.pass.cpp
new file mode 100644
index 00000000000000..e38b1f32c2df43
--- /dev/null
+++ b/libcxx/test/libcxx/extensions/test-cxx17-extensions.pass.cpp
@@ -0,0 +1,31 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03
+
+// Test that `if constexpr` is provided as an extension by supported compilers
+// in all language dialects after C++03. Also further test that the test suite
+// has disabled -Wc++17-extension because we also disable system headers.
+
+#include <cassert>
+
+// if constexpr doesn't need to be used in a constexpr function, nor a dependent
+// one.
+bool CheckIfConstexpr() {
+  if constexpr (false) {
+    return false;
+  }
+  if constexpr (true) {
+    return true;
+  }
+}
+
+int main(int, char**) {
+  assert(CheckIfConstexpr());
+  return 0;
+}
diff --git a/libcxx/utils/libcxx/test/params.py b/libcxx/utils/libcxx/test/params.py
index 4e209901f43be8..8b4aff2bbdd656 100644
--- a/libcxx/utils/libcxx/test/params.py
+++ b/libcxx/utils/libcxx/test/params.py
@@ -57,6 +57,8 @@
     # Disable warnings for extensions used in C++03
     "-Wno-local-type-template-args",
     "-Wno-c++11-extensions",
+    # Allow the use of C++17 `if constexpr` as an extension after C++03
+    "-Wno-c++17-extensions",
 
     # TODO(philnik) This fails with the PSTL.
     "-Wno-unknown-pragmas",



More information about the libcxx-commits mailing list