Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[libc++] Adds a new feature-test macro generator class. #90889

Merged
merged 4 commits into from
Jul 5, 2024

Conversation

mordante
Copy link
Member

@mordante mordante commented May 2, 2024

The new feature-test macro generator uses a JSON file as input. Separating the
code from the data allows for testing the code. The generator has several tests.

This JSON format is based on the new format proposed in #88630

At the moment the FTM script has the existing code and an unused new
generator. Followup patches will complete the generator and convert the existing
Python dict to the new JSON format. Since that conversion is a manual job and
quite a bit of work the transition path has some unused code for some time.

@mordante mordante requested a review from a team as a code owner May 2, 2024 18:52
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label May 2, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented May 2, 2024

@llvm/pr-subscribers-libcxx

Author: Mark de Wever (mordante)

Changes

This is a proof-of-concept how we can test the script. Instead of storing the data in the script it's stored in a JSON file so a different file can be used for testing.

This is related to the RFC #89499


Full diff: https://github.com/llvm/llvm-project/pull/90889.diff

3 Files Affected:

  • (added) libcxx/test/libcxx/feature_test_macro_csv.sh.py (+35)
  • (added) libcxx/utils/data/feature_test_macros/test_data.json (+136)
  • (modified) libcxx/utils/generate_feature_test_macro_components.py (+33)
diff --git a/libcxx/test/libcxx/feature_test_macro_csv.sh.py b/libcxx/test/libcxx/feature_test_macro_csv.sh.py
new file mode 100644
index 00000000000000..2d80bfad63c859
--- /dev/null
+++ b/libcxx/test/libcxx/feature_test_macro_csv.sh.py
@@ -0,0 +1,35 @@
+# RUN: %{python} %s %{libcxx-dir}/utils %{libcxx-dir}/utils/data/feature_test_macros/test_data.json
+
+import sys
+import json
+
+sys.path.append(sys.argv[1])
+from generate_feature_test_macro_components import get_table
+
+
+data = json.load(open(f"{sys.argv[2]}"))
+table = get_table(data)
+
+expected = {
+    "__cpp_lib_any": {
+        "c++17": "201606L",
+        "c++20": "201606L",
+        "c++23": "201606L",
+        "c++26": "201606L",
+    },
+    "__cpp_lib_barrier": {"c++20": "201907L", "c++23": "201907L", "c++26": "201907L"},
+    "__cpp_lib_format": {
+        "c++20": "",
+        "c++23": "",
+        "c++26": "",
+    },
+    "__cpp_lib_variant": {
+        "c++17": "202102L",
+        "c++20": "202102L",
+        "c++23": "202102L",
+        "c++26": "202102L",
+    },
+}
+
+
+assert table == expected, f"expected\n{expected}\n\nresult\n{table}"
diff --git a/libcxx/utils/data/feature_test_macros/test_data.json b/libcxx/utils/data/feature_test_macros/test_data.json
new file mode 100644
index 00000000000000..5a98fba6403c09
--- /dev/null
+++ b/libcxx/utils/data/feature_test_macros/test_data.json
@@ -0,0 +1,136 @@
+[
+  {
+    "name": "__cpp_lib_any",
+    "values": {
+      "c++17": {
+        "201606": [
+          {
+            "implemented": true
+          }
+        ]
+      }
+    },
+    "headers": [
+      "any"
+    ]
+  },
+  {
+    "name": "__cpp_lib_barrier",
+    "values": {
+      "c++20": {
+        "201907": [
+          {
+            "implemented": true
+          }
+        ]
+      }
+    },
+    "headers": [
+      "barrier"
+    ],
+    "test_suite_guard":
+        "!defined(_LIBCPP_HAS_NO_THREADS) && (!defined(_LIBCPP_VERSION) || _LIBCPP_AVAILABILITY_HAS_SYNC)",
+    "libcxx_guard": "!defined(_LIBCPP_HAS_NO_THREADS) && _LIBCPP_AVAILABILITY_HAS_SYNC"
+  },
+  {
+    "name": "__cpp_lib_format",
+    "values": {
+      "c++20": {
+        "201907": [
+          {
+            "number": "P0645R10",
+            "title": "Text Formatting",
+            "implemented": true
+          },
+          {
+            "number": "P1361R2",
+            "title": "Integration of chrono with text formatting",
+            "implemented": false
+          }
+        ],
+        "202106": [
+          {
+            "number": "P2216R3",
+            "title": "std::format improvements",
+            "implemented": true
+          }
+        ],
+        "202110": [
+          {
+            "number": "P2372R3",
+            "title": "Fixing locale handling in chrono formatters",
+            "implemented": false
+          },
+          {
+            "number": "P2418R2",
+            "title": "FAdd support for std::generator-like types to std::format",
+            "implemented": true
+          }
+        ]
+      },
+      "c++23": {
+        "202207": [
+          {
+            "number": "P2419R2",
+            "title": "Clarify handling of encodings in localized formatting of chrono types",
+            "implemented": false
+          }
+        ]
+      },
+      "c++26": {
+        "202306": [
+          {
+            "number": "P2637R3",
+            "title": "Member Visit",
+            "implemented": true
+          }
+        ],
+        "202311": [
+          {
+            "number": "P2918R2",
+            "title": "Runtime format strings II",
+            "implemented": true
+          }
+        ]
+      }
+    },
+    "headers": [
+      "format"
+    ]
+  },
+  {
+    "name": "__cpp_lib_variant",
+    "values": {
+      "c++17": {
+        "202102": [
+          {
+            "number": "",
+            "title": "``std::visit`` for classes derived from ``std::variant``",
+            "implemented": true
+          }
+        ]
+      },
+      "c++20": {
+        "202106": [
+          {
+            "number": "",
+            "title": "Fully constexpr ``std::variant``",
+            "implemented": false
+          }
+        ]
+      },
+      "c++26": {
+        "202306": [
+          {
+            "number": "",
+            "title": "Member visit",
+            "implemented": true
+          }
+        ]
+      }
+    },
+    "headers": [
+      "variant"
+    ]
+  }
+]
diff --git a/libcxx/utils/generate_feature_test_macro_components.py b/libcxx/utils/generate_feature_test_macro_components.py
index f2b8d55c0e11b0..04a8a8786b1a35 100755
--- a/libcxx/utils/generate_feature_test_macro_components.py
+++ b/libcxx/utils/generate_feature_test_macro_components.py
@@ -3,6 +3,7 @@
 import os
 from builtins import range
 from functools import reduce
+import json
 
 
 def get_libcxx_paths():
@@ -1857,6 +1858,38 @@ def produce_docs():
         f.write(doc_str)
 
 
+def get_table(data):
+    result = dict()
+    for feature in data:
+        last = None
+        entry = dict()
+        implemented = True
+        for std in get_std_dialects():
+            if std not in feature["values"].keys():
+                if last == None:
+                    continue
+                else:
+                    entry[std] = last
+            else:
+                if last == None:
+                    last = ""
+                if implemented:
+                    for value in feature["values"][std]:
+                        for paper in list(feature["values"][std][value]):
+                            if not paper["implemented"]:
+                                implemented = False
+                                break
+                        if implemented:
+                            last = f"{value}L"
+                        else:
+                            break
+
+                entry[std] = last
+        result[feature["name"]] = entry
+
+    return result
+
+
 def main():
     produce_version_header()
     produce_tests()

@mordante mordante requested a review from EricWF May 2, 2024 18:53
Copy link
Member

@EricWF EricWF left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

Copy link
Member

@ldionne ldionne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The approach LGTM but I'd like to see this again!

libcxx/test/libcxx/feature_test_macro_csv.sh.py Outdated Show resolved Hide resolved
libcxx/test/libcxx/feature_test_macro_csv.sh.py Outdated Show resolved Hide resolved
libcxx/utils/generate_feature_test_macro_components.py Outdated Show resolved Hide resolved
@mordante mordante changed the title [RFC][libc++] Testing feature test macro script. [libc++] Adds a new feature-test macro generator class. May 9, 2024
Copy link
Member

@ldionne ldionne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks nice! I have some comments but this should be good to go then.

libcxx/utils/data/feature_test_macros/test_data.json Outdated Show resolved Hide resolved
libcxx/utils/generate_feature_test_macro_components.py Outdated Show resolved Hide resolved
libcxx/utils/generate_feature_test_macro_components.py Outdated Show resolved Hide resolved
libcxx/utils/generate_feature_test_macro_components.py Outdated Show resolved Hide resolved
libcxx/utils/generate_feature_test_macro_components.py Outdated Show resolved Hide resolved
libcxx/utils/generate_feature_test_macro_components.py Outdated Show resolved Hide resolved
libcxx/utils/generate_feature_test_macro_components.py Outdated Show resolved Hide resolved
@mordante mordante force-pushed the users/mordante/tests_feature_test_macro_script branch 2 times, most recently from 3f7a465 to 0a37d12 Compare June 12, 2024 18:17
Copy link
Member

@ldionne ldionne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM w/ comments.

libcxx/utils/data/feature_test_macros/test_data.json Outdated Show resolved Hide resolved
libcxx/utils/generate_feature_test_macro_components.py Outdated Show resolved Hide resolved
This is a proof-of-concept how we can test the script. Instead of storing
the data in the script it's stored in a JSON file so a different file can
be used for testing.

This is related to the RFC #89499
@mordante mordante force-pushed the users/mordante/tests_feature_test_macro_script branch from 0a37d12 to c8dd9ad Compare July 4, 2024 12:09
@mordante mordante force-pushed the users/mordante/tests_feature_test_macro_script branch from c8dd9ad to 2d68109 Compare July 4, 2024 15:39
@mordante mordante merged commit d2dd4b5 into main Jul 5, 2024
52 of 54 checks passed
@mordante mordante deleted the users/mordante/tests_feature_test_macro_script branch July 5, 2024 05:54
kbluck pushed a commit to kbluck/llvm-project that referenced this pull request Jul 6, 2024
The new feature-test macro generator uses a JSON file as input.
Separating the
code from the data allows for testing the code. The generator has
several tests.

This JSON format is based on the new format proposed in llvm#88630

At the moment the FTM script has the existing code and an unused new
generator. Followup patches will complete the generator and convert the
existing
Python `dict` to the new JSON format. Since that conversion is a manual
job and
quite a bit of work the transition path has some unused code for some
time.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants