-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[libc++] Adds additional FTM input validation. #138462
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
Open
mordante
wants to merge
1
commit into
llvm:main
Choose a base branch
from
mordante:review/ftm_add_more_input_validation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
+191
−1
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This ensure the input is properly sorted; when not gives a nice diagnostic.
@llvm/pr-subscribers-libcxx Author: Mark de Wever (mordante) ChangesThis ensure the input is properly sorted; when not gives a nice diagnostic. Full diff: https://github.com/llvm/llvm-project/pull/138462.diff 2 Files Affected:
diff --git a/libcxx/test/libcxx/feature_test_macro/invalid.sh.py b/libcxx/test/libcxx/feature_test_macro/invalid.sh.py
index ae457f6e1a545..588f6bec1521c 100644
--- a/libcxx/test/libcxx/feature_test_macro/invalid.sh.py
+++ b/libcxx/test/libcxx/feature_test_macro/invalid.sh.py
@@ -12,7 +12,7 @@
import json
sys.path.append(sys.argv[1])
-from generate_feature_test_macro_components import FeatureTestMacros
+from generate_feature_test_macro_components import FeatureTestMacros, DataNotSorted
def test(output, expected):
@@ -106,3 +106,170 @@ def test_error(data, type, message):
KeyError,
"'implemented'",
)
+
+test_error(
+ [
+ {
+ "name": "abc",
+ "values": {
+ "c++17": {
+ "197001": [
+ {
+ "implemented": False,
+ },
+ ],
+ },
+ },
+ "headers": [],
+ },
+ {
+ "name": "ghi",
+ "values": {
+ "c++17": {
+ "197001": [
+ {
+ "implemented": False,
+ },
+ ],
+ },
+ },
+ "headers": [],
+ },
+ { # This entry is in the wrong alphabetic order
+ "name": "def",
+ "values": {
+ "c++17": {
+ "197001": [
+ {
+ "implemented": False,
+ },
+ ],
+ },
+ },
+ "headers": [],
+ },
+ {
+ "name": "jkl",
+ "values": {
+ "c++17": {
+ "197001": [
+ {
+ "implemented": False,
+ },
+ ],
+ },
+ },
+ "headers": [],
+ },
+ ],
+ DataNotSorted,
+ """\
+The ftm names are not sorted.
+--- input data
++++ sorted data
+@@ -1,4 +1,4 @@
+ abc
++def
+ ghi
+-def
+ jkl
+""",
+)
+
+test_error(
+ [
+ {
+ "name": "abc",
+ "values": {
+ "c++14": {
+ "197001": [
+ {
+ "implemented": False,
+ },
+ ],
+ },
+ "c++23": {
+ "197001": [
+ {
+ "implemented": False,
+ },
+ ],
+ },
+ # This entry is in the wrong alphabetic order
+ # Note we don't use C++98, but C++03 instead so alphabetic order
+ # works this century.
+ "c++20": {
+ "197001": [
+ {
+ "implemented": False,
+ },
+ ],
+ },
+ },
+ "headers": [],
+ },
+ ],
+ DataNotSorted,
+ """\
+The C++ standard version numbers of ftm 'abc' are not sorted.
+--- input data
++++ sorted data
+@@ -1,3 +1,3 @@
+ c++14
++c++20
+ c++23
+-c++20
+""",
+)
+
+test_error(
+ [
+ {
+ "name": "abc",
+ "values": {
+ "c++14": {
+ "197001": [
+ {
+ "implemented": False,
+ },
+ ],
+ "197002": [
+ {
+ "implemented": False,
+ },
+ ],
+ "197004": [
+ {
+ "implemented": False,
+ },
+ ],
+ # This entry is in the wrong alphabetic order
+ "197003": [
+ {
+ "implemented": False,
+ },
+ ],
+ "197005": [
+ {
+ "implemented": False,
+ },
+ ],
+ },
+ },
+ "headers": [],
+ },
+ ],
+ DataNotSorted,
+ """\
+The value of the fmt 'abc' in c++14 are not sorted.
+--- input data
++++ sorted data
+@@ -1,5 +1,5 @@
+ 197001
+ 197002
++197003
+ 197004
+-197003
+ 197005
+""",
+)
diff --git a/libcxx/utils/generate_feature_test_macro_components.py b/libcxx/utils/generate_feature_test_macro_components.py
index cb92dc17ba707..cf69b39b294a5 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 dataclasses import dataclass
+from difflib import unified_diff
from functools import reduce
from typing import (
Any,
@@ -1987,6 +1988,25 @@ class VersionHeader:
condition: str = None
+class DataNotSorted(Exception):
+ pass
+
+def validate_sorted(name:str, data: List[str]) -> None:
+ sorted_data = sorted(data)
+ if data != sorted_data:
+ raise DataNotSorted(
+ f"The {name} are not sorted.\n"
+ + "\n".join(
+ unified_diff(
+ data,
+ sorted_data,
+ "input data",
+ "sorted data",
+ lineterm="",
+ )
+ ) + "\n"
+ )
+
def get_ftms(
data, std_dialects: List[Std], use_implemented_status: bool
) -> Dict[Ftm, Dict[Std, Optional[Value]]]:
@@ -1996,6 +2016,7 @@ def get_ftms(
last = None
entry = dict()
implemented = True
+ validate_sorted(f"C++ standard version numbers of ftm '{feature['name']}'", list(feature["values"].keys()))
for std in std_dialects:
if std not in feature["values"].keys():
if last == None:
@@ -2006,6 +2027,7 @@ def get_ftms(
if implemented:
values = feature["values"][std]
assert len(values) > 0, f"{feature['name']}[{std}] has no entries"
+ validate_sorted(f"value of the fmt '{feature['name']}' in {std}", list(values.keys()))
for value in values:
papers = list(values[value])
assert (
@@ -2024,6 +2046,7 @@ def get_ftms(
entry[std] = last
result[feature["name"]] = entry
+ validate_sorted("ftm names", list(result))
return result
|
You can test this locally with the following command:darker --check --diff -r HEAD~1...HEAD libcxx/test/libcxx/feature_test_macro/invalid.sh.py libcxx/utils/generate_feature_test_macro_components.py View the diff from darker here.--- test/libcxx/feature_test_macro/invalid.sh.py 2025-05-04 17:09:25.000000 +0000
+++ test/libcxx/feature_test_macro/invalid.sh.py 2025-05-04 17:13:45.263255 +0000
@@ -133,11 +133,11 @@
],
},
},
"headers": [],
},
- { # This entry is in the wrong alphabetic order
+ { # This entry is in the wrong alphabetic order
"name": "def",
"values": {
"c++17": {
"197001": [
{
--- utils/generate_feature_test_macro_components.py 2025-05-04 17:09:25.000000 +0000
+++ utils/generate_feature_test_macro_components.py 2025-05-04 17:13:45.894622 +0000
@@ -1989,11 +1989,12 @@
class DataNotSorted(Exception):
pass
-def validate_sorted(name:str, data: List[str]) -> None:
+
+def validate_sorted(name: str, data: List[str]) -> None:
sorted_data = sorted(data)
if data != sorted_data:
raise DataNotSorted(
f"The {name} are not sorted.\n"
+ "\n".join(
@@ -2002,34 +2003,42 @@
sorted_data,
"input data",
"sorted data",
lineterm="",
)
- ) + "\n"
+ )
+ + "\n"
)
+
def get_ftms(
data, std_dialects: List[Std], use_implemented_status: bool
) -> Dict[Ftm, Dict[Std, Optional[Value]]]:
"""Impementation for FeatureTestMacros.(standard|implemented)_ftms()."""
result = dict()
for feature in data:
last = None
entry = dict()
implemented = True
- validate_sorted(f"C++ standard version numbers of ftm '{feature['name']}'", list(feature["values"].keys()))
+ validate_sorted(
+ f"C++ standard version numbers of ftm '{feature['name']}'",
+ list(feature["values"].keys()),
+ )
for std in std_dialects:
if std not in feature["values"].keys():
if last == None:
continue
else:
entry[std] = last
else:
if implemented:
values = feature["values"][std]
assert len(values) > 0, f"{feature['name']}[{std}] has no entries"
- validate_sorted(f"value of the fmt '{feature['name']}' in {std}", list(values.keys()))
+ validate_sorted(
+ f"value of the fmt '{feature['name']}' in {std}",
+ list(values.keys()),
+ )
for value in values:
papers = list(values[value])
assert (
len(papers) > 0
), f"{feature['name']}[{std}][{value}] has no entries"
|
ldionne
approved these changes
May 6, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This ensure the input is properly sorted; when not gives a nice diagnostic.