From b2571b538e569ba5cba6afa4704f7259448cf8e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Darius=20Nea=C8=9Bu?= Date: Wed, 15 Jul 2026 21:04:59 +0300 Subject: [PATCH 1/3] [beman-tidy] Implement cpp.no_flag_forking check (#61) --- beman_tidy/lib/checks/beman_standard/cpp.py | 70 ++++++++++++++++++- .../invalid/uses_elif_bare_feature_test.hpp | 12 ++++ .../invalid/uses_feature_test_macro.hpp | 16 +++++ .../cpp/no_flag_forking/valid/uses_config.hpp | 14 ++++ .../lib/checks/beman_standard/cpp/test_cpp.py | 31 +++++++- 5 files changed, 140 insertions(+), 3 deletions(-) create mode 100644 tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/uses_elif_bare_feature_test.hpp create mode 100644 tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/uses_feature_test_macro.hpp create mode 100644 tests/lib/checks/beman_standard/cpp/no_flag_forking/valid/uses_config.hpp diff --git a/beman_tidy/lib/checks/beman_standard/cpp.py b/beman_tidy/lib/checks/beman_standard/cpp.py index 8de4e4d8..a0514635 100644 --- a/beman_tidy/lib/checks/beman_standard/cpp.py +++ b/beman_tidy/lib/checks/beman_standard/cpp.py @@ -5,7 +5,9 @@ from beman_tidy.lib.checks.base.base_check import BaseCheck from beman_tidy.lib.checks.base.file_base_check import FileBaseCheck, BatchFileBaseCheck from beman_tidy.lib.checks.system.registry import register_beman_standard_check -from beman_tidy.lib.utils.file import get_beman_include_headers +from beman_tidy.lib.utils.comments import determine_comment_type +from beman_tidy.lib.utils.file import get_beman_include_headers, get_non_test_cpp_files +from beman_tidy.lib.utils.string import normalize_path_for_display @register_beman_standard_check("cpp.namespace") class CppNamespaceCheck(BatchFileBaseCheck): @@ -102,7 +104,71 @@ def fix(self): return True -# TODO cpp.no_flag_forking +@register_beman_standard_check("cpp.no_flag_forking") +class CppNoFlagForkingCheck(BatchFileBaseCheck): + """ + [cpp.no_flag_forking] + Requirement: C++ preprocessing must produce identical output regardless of compiler flags. + Feature test macros such as __cpp_* must not be used directly in #if/#elif. + """ + + _FLAG_FORKING_PATTERNS = ( + re.compile(r"defined\s*\(\s*__cpp_"), + re.compile(r"#\s*if\s+__cpp_"), + re.compile(r"#\s*elif\s+defined\s*\(\s*__cpp_"), + re.compile(r"#\s*elif\s+__cpp_"), + ) + + def __init__(self, repo_info, beman_standard_check_config): + super().__init__(repo_info, beman_standard_check_config) + self.file_check_class = self.CppNoFlagForkingCheckImpl + self.file_path_generator = get_non_test_cpp_files + + class CppNoFlagForkingCheckImpl(FileBaseCheck): + """ + Implementation of the "cpp.no_flag_forking" check for a single file. + """ + + def __init__(self, repo_info, beman_standard_check_config, relative_path): + super().__init__( + repo_info, beman_standard_check_config, relative_path, name="cpp.no_flag_forking" + ) + + def _is_in_comment(self, lines, line_index): + return determine_comment_type(lines, line_index) is not None + + _PREPROCESSOR_LINE_RE = re.compile(r"^\s*#") + + def check(self): + lines = self.read_lines() + display_path = normalize_path_for_display(self.path, self.repo_path) + + for line_index, line in enumerate(lines): + if self._is_in_comment(lines, line_index): + continue + if not self._PREPROCESSOR_LINE_RE.match(line): + continue + + for pattern in CppNoFlagForkingCheck._FLAG_FORKING_PATTERNS: + if pattern.search(line): + self.log( + f"Direct use of compiler feature-test macro in {display_path} at line {line_index + 1}. " + f"Use CMake-detected config.hpp macros instead of __cpp_* in #if/#elif. " + f"See https://github.com/bemanproject/beman/blob/main/docs/beman_standard.md#cppno_flag_forking" + ) + return False + + return True + + def fix(self): + display_path = normalize_path_for_display(self.path, self.repo_path) + self.log( + f"Please manually replace __cpp_* feature-test macros in {display_path} with " + f"CMake-detected config.hpp macros. " + f"See https://github.com/bemanproject/beman/blob/main/docs/beman_standard.md#cppno_flag_forking" + ) + return False + @register_beman_standard_check("cpp.extension_identifiers") class CppExtensionIdentifiersCheck(BaseCheck): diff --git a/tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/uses_elif_bare_feature_test.hpp b/tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/uses_elif_bare_feature_test.hpp new file mode 100644 index 00000000..cb7c265f --- /dev/null +++ b/tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/uses_elif_bare_feature_test.hpp @@ -0,0 +1,12 @@ +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#ifndef USES_ELIF_BARE_FEATURE_TEST_HPP +#define USES_ELIF_BARE_FEATURE_TEST_HPP + +#if defined(BEMAN_MY_LIB_USE_DEDUCING_THIS) +void use_deducing_this(); +#elif __cpp_concepts +void use_concepts(); +#endif + +#endif diff --git a/tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/uses_feature_test_macro.hpp b/tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/uses_feature_test_macro.hpp new file mode 100644 index 00000000..09cbe2e3 --- /dev/null +++ b/tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/uses_feature_test_macro.hpp @@ -0,0 +1,16 @@ +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#ifndef USES_FEATURE_TEST_MACRO_HPP +#define USES_FEATURE_TEST_MACRO_HPP + +#if defined(__cpp_explicit_this_parameter) +void use_deducing_this(); +#elif defined(__cpp_concepts) +void use_concepts(); +#else +#if __cpp_lib_format >= 201907L +void use_format(); +#endif +#endif + +#endif diff --git a/tests/lib/checks/beman_standard/cpp/no_flag_forking/valid/uses_config.hpp b/tests/lib/checks/beman_standard/cpp/no_flag_forking/valid/uses_config.hpp new file mode 100644 index 00000000..d09b20b5 --- /dev/null +++ b/tests/lib/checks/beman_standard/cpp/no_flag_forking/valid/uses_config.hpp @@ -0,0 +1,14 @@ +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#ifndef USES_CONFIG_HPP +#define USES_CONFIG_HPP + +#include + +#if BEMAN_MY_LIB_USE_DEDUCING_THIS +void use_deducing_this(); +#endif + +// Do not flag __cpp_* mentions in comments, e.g. __cpp_explicit_this_parameter + +#endif diff --git a/tests/lib/checks/beman_standard/cpp/test_cpp.py b/tests/lib/checks/beman_standard/cpp/test_cpp.py index f50cf899..f2024ff9 100644 --- a/tests/lib/checks/beman_standard/cpp/test_cpp.py +++ b/tests/lib/checks/beman_standard/cpp/test_cpp.py @@ -1,10 +1,15 @@ #!/usr/bin/env python3 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +import pytest import shutil from pathlib import Path -from beman_tidy.lib.checks.beman_standard.cpp import CppNamespaceCheck, CppExtensionIdentifiersCheck +from beman_tidy.lib.checks.beman_standard.cpp import ( + CppNamespaceCheck, + CppNoFlagForkingCheck, + CppExtensionIdentifiersCheck, +) test_data_prefix = Path("tests/lib/checks/beman_standard/cpp/namespace") valid_prefix = test_data_prefix / "valid" @@ -39,6 +44,30 @@ def test__cpp_namespace__fix_inplace(repo_info, beman_standard_check_config, tmp assert check.check() is True +no_flag_forking_prefix = Path("tests/lib/checks/beman_standard/cpp/no_flag_forking") +no_flag_forking_valid_prefix = no_flag_forking_prefix / "valid" +no_flag_forking_invalid_prefix = no_flag_forking_prefix / "invalid" + + +def test__cpp_no_flag_forking__valid(repo_info, beman_standard_check_config): + repo_info["top_level"] = no_flag_forking_valid_prefix + + check = CppNoFlagForkingCheck(repo_info, beman_standard_check_config) + assert check.check() is True + + +def test__cpp_no_flag_forking__invalid(repo_info, beman_standard_check_config): + repo_info["top_level"] = no_flag_forking_invalid_prefix + + check = CppNoFlagForkingCheck(repo_info, beman_standard_check_config) + assert check.check() is False + + +@pytest.mark.skip(reason="not implemented") +def test__cpp_no_flag_forking__fix_inplace(repo_info, beman_standard_check_config): + pass + + def test__cpp_extension_identifiers__is_always_skipped(repo_info, beman_standard_check_config): """ Test that cpp.extension_identifiers is always skipped. From 8dd41176d78b109d73c7cb11ae076d82b7b2cbc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Darius=20Nea=C8=9Bu?= Date: Thu, 19 Nov 2026 00:05:03 +0200 Subject: [PATCH 2/3] [beman-tidy] Catch all __cpp_* uses in cpp.no_flag_forking conditionals Cover #ifdef/#ifndef, compound and continued conditions, ignore comments, and exempt the library's own include/beman//config.hpp fallback. --- beman_tidy/lib/checks/beman_standard/cpp.py | 76 ++++++++++++------- .../include/beman/exemplar/uses_compound.hpp | 5 ++ .../beman/exemplar/uses_continuation.hpp | 6 ++ .../exemplar}/uses_feature_test_macro.hpp | 0 .../include/beman/exemplar/detail/config.hpp | 6 ++ .../exemplar}/uses_elif_bare_feature_test.hpp | 0 .../invalid/example/examples/example.cpp | 5 ++ .../include/beman/exemplar/uses_ifdef.hpp | 5 ++ .../include/beman/exemplar/uses_ifndef.hpp | 5 ++ .../include/beman/exemplar/uses_indented.hpp | 7 ++ .../valid/include/beman/exemplar/config.hpp | 18 +++++ .../include/beman/exemplar/uses_config.hpp | 37 +++++++++ .../exemplar/uses_feature_test_macro.test.cpp | 5 ++ .../cpp/no_flag_forking/valid/uses_config.hpp | 14 ---- .../lib/checks/beman_standard/cpp/test_cpp.py | 25 +++++- 15 files changed, 172 insertions(+), 42 deletions(-) create mode 100644 tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/compound/include/beman/exemplar/uses_compound.hpp create mode 100644 tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/continuation/include/beman/exemplar/uses_continuation.hpp rename tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/{ => defined/include/beman/exemplar}/uses_feature_test_macro.hpp (100%) create mode 100644 tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/detail_config/include/beman/exemplar/detail/config.hpp rename tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/{ => elif_bare/include/beman/exemplar}/uses_elif_bare_feature_test.hpp (100%) create mode 100644 tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/example/examples/example.cpp create mode 100644 tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/ifdef/include/beman/exemplar/uses_ifdef.hpp create mode 100644 tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/ifndef/include/beman/exemplar/uses_ifndef.hpp create mode 100644 tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/indented/include/beman/exemplar/uses_indented.hpp create mode 100644 tests/lib/checks/beman_standard/cpp/no_flag_forking/valid/include/beman/exemplar/config.hpp create mode 100644 tests/lib/checks/beman_standard/cpp/no_flag_forking/valid/include/beman/exemplar/uses_config.hpp create mode 100644 tests/lib/checks/beman_standard/cpp/no_flag_forking/valid/tests/beman/exemplar/uses_feature_test_macro.test.cpp delete mode 100644 tests/lib/checks/beman_standard/cpp/no_flag_forking/valid/uses_config.hpp diff --git a/beman_tidy/lib/checks/beman_standard/cpp.py b/beman_tidy/lib/checks/beman_standard/cpp.py index 96b8f8c2..4efe3b6f 100644 --- a/beman_tidy/lib/checks/beman_standard/cpp.py +++ b/beman_tidy/lib/checks/beman_standard/cpp.py @@ -2,6 +2,7 @@ # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception import re +from pathlib import Path from beman_tidy.lib.checks.base.base_check import BaseCheck from beman_tidy.lib.checks.base.file_base_check import FileBaseCheck, BatchFileBaseCheck from beman_tidy.lib.checks.beman_standard.readme import ReadmeBaseCheck @@ -110,15 +111,18 @@ class CppNoFlagForkingCheck(BatchFileBaseCheck): """ [cpp.no_flag_forking] Requirement: C++ preprocessing must produce identical output regardless of compiler flags. - Feature test macros such as __cpp_* must not be used directly in #if/#elif. + Feature test macros such as __cpp_* must not be used directly in preprocessor conditionals. + + The library's own include/beman//config.hpp is exempt: the Beman Standard + designates it as the place that wraps config_generated.hpp and provides fallback + defaults for users who vendor the include/ directory. """ - _FLAG_FORKING_PATTERNS = ( - re.compile(r"defined\s*\(\s*__cpp_"), - re.compile(r"#\s*if\s+__cpp_"), - re.compile(r"#\s*elif\s+defined\s*\(\s*__cpp_"), - re.compile(r"#\s*elif\s+__cpp_"), - ) + # #if, #elif, #ifdef, #ifndef, #elifdef, #elifndef (C++23) + _CONDITIONAL_DIRECTIVE_RE = re.compile(r"^\s*#\s*(?:if|elif|ifdef|ifndef|elifdef|elifndef)\b") + _FEATURE_TEST_MACRO_RE = re.compile(r"\b__cpp_\w+") + _BLOCK_COMMENT_RE = re.compile(r"/\*.*?\*/") + _LINE_COMMENT_RE = re.compile(r"//.*$") def __init__(self, repo_info, beman_standard_check_config): super().__init__(repo_info, beman_standard_check_config) @@ -135,31 +139,51 @@ def __init__(self, repo_info, beman_standard_check_config, relative_path): repo_info, beman_standard_check_config, relative_path, name="cpp.no_flag_forking" ) - def _is_in_comment(self, lines, line_index): - return determine_comment_type(lines, line_index) is not None + def _is_config_header(self): + return self.relative_path == Path("include") / "beman" / self.short_name / "config.hpp" - _PREPROCESSOR_LINE_RE = re.compile(r"^\s*#") - - def check(self): + def _find_violations(self): + """ + Returns a list of (line_number, macro) for each conditional directive that uses a + __cpp_* feature test macro. Continuation lines are joined and comments are ignored. + """ + outer = CppNoFlagForkingCheck lines = self.read_lines() - display_path = normalize_path_for_display(self.path, self.repo_path) - - for line_index, line in enumerate(lines): - if self._is_in_comment(lines, line_index): + violations = [] + line_index = 0 + while line_index < len(lines): + start_index = line_index + directive = lines[line_index].rstrip("\n") + while directive.rstrip().endswith("\\") and line_index + 1 < len(lines): + line_index += 1 + directive = directive.rstrip()[:-1] + " " + lines[line_index].rstrip("\n") + line_index += 1 + + if not outer._CONDITIONAL_DIRECTIVE_RE.match(directive): continue - if not self._PREPROCESSOR_LINE_RE.match(line): + if determine_comment_type(lines, start_index) is not None: continue - for pattern in CppNoFlagForkingCheck._FLAG_FORKING_PATTERNS: - if pattern.search(line): - self.log( - f"Direct use of compiler feature-test macro in {display_path} at line {line_index + 1}. " - f"Use CMake-detected config.hpp macros instead of __cpp_* in #if/#elif. " - f"See https://github.com/bemanproject/beman/blob/main/docs/beman_standard.md#cppno_flag_forking" - ) - return False + directive = outer._BLOCK_COMMENT_RE.sub(" ", directive) + directive = outer._LINE_COMMENT_RE.sub("", directive) + match = outer._FEATURE_TEST_MACRO_RE.search(directive) + if match: + violations.append((start_index + 1, match.group(0))) + return violations - return True + def check(self): + if self._is_config_header(): + return True + + display_path = normalize_path_for_display(self.path, self.repo_path) + violations = self._find_violations() + for line_number, macro in violations: + self.log( + f"Direct use of compiler feature-test macro {macro} in {display_path} at line {line_number}. " + f"Use CMake-detected config.hpp macros instead of __cpp_* in preprocessor conditionals. " + f"See https://github.com/bemanproject/beman/blob/main/docs/beman_standard.md#cppno_flag_forking" + ) + return not violations def fix(self): display_path = normalize_path_for_display(self.path, self.repo_path) diff --git a/tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/compound/include/beman/exemplar/uses_compound.hpp b/tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/compound/include/beman/exemplar/uses_compound.hpp new file mode 100644 index 00000000..ba2ce68a --- /dev/null +++ b/tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/compound/include/beman/exemplar/uses_compound.hpp @@ -0,0 +1,5 @@ +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#if __cplusplus >= 202302L && __cpp_lib_ranges >= 202207L // P2494R2 +void use_ranges(); +#endif diff --git a/tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/continuation/include/beman/exemplar/uses_continuation.hpp b/tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/continuation/include/beman/exemplar/uses_continuation.hpp new file mode 100644 index 00000000..1e83855d --- /dev/null +++ b/tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/continuation/include/beman/exemplar/uses_continuation.hpp @@ -0,0 +1,6 @@ +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#if defined(BEMAN_EXEMPLAR_USE_CONCEPTS) && \ + defined(__cpp_lib_ranges) +void use_ranges(); +#endif diff --git a/tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/uses_feature_test_macro.hpp b/tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/defined/include/beman/exemplar/uses_feature_test_macro.hpp similarity index 100% rename from tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/uses_feature_test_macro.hpp rename to tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/defined/include/beman/exemplar/uses_feature_test_macro.hpp diff --git a/tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/detail_config/include/beman/exemplar/detail/config.hpp b/tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/detail_config/include/beman/exemplar/detail/config.hpp new file mode 100644 index 00000000..a11f83f2 --- /dev/null +++ b/tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/detail_config/include/beman/exemplar/detail/config.hpp @@ -0,0 +1,6 @@ +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +// Only include/beman//config.hpp is exempt, not nested config headers. +#if defined(__cpp_lib_three_way_comparison) +#define BEMAN_EXEMPLAR_DETAIL_USE_SPACESHIP 1 +#endif diff --git a/tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/uses_elif_bare_feature_test.hpp b/tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/elif_bare/include/beman/exemplar/uses_elif_bare_feature_test.hpp similarity index 100% rename from tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/uses_elif_bare_feature_test.hpp rename to tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/elif_bare/include/beman/exemplar/uses_elif_bare_feature_test.hpp diff --git a/tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/example/examples/example.cpp b/tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/example/examples/example.cpp new file mode 100644 index 00000000..c0fb9b3e --- /dev/null +++ b/tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/example/examples/example.cpp @@ -0,0 +1,5 @@ +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#if __cpp_lib_print >= 202207L +int main() { return 0; } +#endif diff --git a/tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/ifdef/include/beman/exemplar/uses_ifdef.hpp b/tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/ifdef/include/beman/exemplar/uses_ifdef.hpp new file mode 100644 index 00000000..907f1f7d --- /dev/null +++ b/tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/ifdef/include/beman/exemplar/uses_ifdef.hpp @@ -0,0 +1,5 @@ +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#ifdef __cpp_lib_execution +void use_execution(); +#endif diff --git a/tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/ifndef/include/beman/exemplar/uses_ifndef.hpp b/tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/ifndef/include/beman/exemplar/uses_ifndef.hpp new file mode 100644 index 00000000..dc716604 --- /dev/null +++ b/tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/ifndef/include/beman/exemplar/uses_ifndef.hpp @@ -0,0 +1,5 @@ +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#ifndef __cpp_lib_jthread +void use_fallback_stop_token(); +#endif diff --git a/tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/indented/include/beman/exemplar/uses_indented.hpp b/tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/indented/include/beman/exemplar/uses_indented.hpp new file mode 100644 index 00000000..cb2551de --- /dev/null +++ b/tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/indented/include/beman/exemplar/uses_indented.hpp @@ -0,0 +1,7 @@ +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +namespace beman::exemplar { + # if __cpp_concepts > 202002L + void use_concepts(); + # endif +} diff --git a/tests/lib/checks/beman_standard/cpp/no_flag_forking/valid/include/beman/exemplar/config.hpp b/tests/lib/checks/beman_standard/cpp/no_flag_forking/valid/include/beman/exemplar/config.hpp new file mode 100644 index 00000000..dd9c075f --- /dev/null +++ b/tests/lib/checks/beman_standard/cpp/no_flag_forking/valid/include/beman/exemplar/config.hpp @@ -0,0 +1,18 @@ +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +#ifndef BEMAN_EXEMPLAR_CONFIG_HPP +#define BEMAN_EXEMPLAR_CONFIG_HPP + +// The library's config.hpp may probe feature test macros in its fallback for users +// who vendor the include/ directory without config_generated.hpp. +#if !defined(__has_include) || __has_include() + #include +#else + #include + #if defined(__cpp_explicit_this_parameter) && __cpp_explicit_this_parameter >= 202110L + #define BEMAN_EXEMPLAR_USE_DEDUCING_THIS() 1 + #else + #define BEMAN_EXEMPLAR_USE_DEDUCING_THIS() 0 + #endif +#endif + +#endif diff --git a/tests/lib/checks/beman_standard/cpp/no_flag_forking/valid/include/beman/exemplar/uses_config.hpp b/tests/lib/checks/beman_standard/cpp/no_flag_forking/valid/include/beman/exemplar/uses_config.hpp new file mode 100644 index 00000000..68ad60f9 --- /dev/null +++ b/tests/lib/checks/beman_standard/cpp/no_flag_forking/valid/include/beman/exemplar/uses_config.hpp @@ -0,0 +1,37 @@ +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#ifndef USES_CONFIG_HPP +#define USES_CONFIG_HPP + +#include + +#if BEMAN_MY_LIB_USE_DEDUCING_THIS +void use_deducing_this(); +#endif + +// Do not flag __cpp_* mentions in comments, e.g. __cpp_explicit_this_parameter +/* +#if __cpp_concepts +#endif +*/ + +#if BEMAN_MY_LIB_USE_RANGES // replaces __cpp_lib_ranges +void use_ranges(); +#endif + +#if BEMAN_MY_LIB_USE_FORMAT /* replaces __cpp_lib_format */ +void use_format(); +#endif + +// Identifiers that merely contain __cpp_ are not feature test macros. +#if 202207 <= disabled__cpp_lib_forward_like +void use_forward_like(); +#endif + +#if __cplusplus >= 202002L +void use_cpp20(); +#endif + +inline constexpr const char* ftm_name = "__cpp_concepts"; + +#endif diff --git a/tests/lib/checks/beman_standard/cpp/no_flag_forking/valid/tests/beman/exemplar/uses_feature_test_macro.test.cpp b/tests/lib/checks/beman_standard/cpp/no_flag_forking/valid/tests/beman/exemplar/uses_feature_test_macro.test.cpp new file mode 100644 index 00000000..84922c02 --- /dev/null +++ b/tests/lib/checks/beman_standard/cpp/no_flag_forking/valid/tests/beman/exemplar/uses_feature_test_macro.test.cpp @@ -0,0 +1,5 @@ +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// Test code is out of scope for cpp.no_flag_forking. +#if __cpp_lib_format >= 201907L +int main() { return 0; } +#endif diff --git a/tests/lib/checks/beman_standard/cpp/no_flag_forking/valid/uses_config.hpp b/tests/lib/checks/beman_standard/cpp/no_flag_forking/valid/uses_config.hpp deleted file mode 100644 index d09b20b5..00000000 --- a/tests/lib/checks/beman_standard/cpp/no_flag_forking/valid/uses_config.hpp +++ /dev/null @@ -1,14 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - -#ifndef USES_CONFIG_HPP -#define USES_CONFIG_HPP - -#include - -#if BEMAN_MY_LIB_USE_DEDUCING_THIS -void use_deducing_this(); -#endif - -// Do not flag __cpp_* mentions in comments, e.g. __cpp_explicit_this_parameter - -#endif diff --git a/tests/lib/checks/beman_standard/cpp/test_cpp.py b/tests/lib/checks/beman_standard/cpp/test_cpp.py index 66376cba..21a01e80 100644 --- a/tests/lib/checks/beman_standard/cpp/test_cpp.py +++ b/tests/lib/checks/beman_standard/cpp/test_cpp.py @@ -51,14 +51,35 @@ def test__cpp_namespace__fix_inplace(repo_info, beman_standard_check_config, tmp def test__cpp_no_flag_forking__valid(repo_info, beman_standard_check_config): + """ + Test that headers using config.hpp macros pass, that __cpp_* in comments, strings, tests/ + and in the library's own include/beman//config.hpp fallback is not flagged. + """ repo_info["top_level"] = no_flag_forking_valid_prefix check = CppNoFlagForkingCheck(repo_info, beman_standard_check_config) assert check.check() is True -def test__cpp_no_flag_forking__invalid(repo_info, beman_standard_check_config): - repo_info["top_level"] = no_flag_forking_invalid_prefix +@pytest.mark.parametrize( + "case", + [ + "defined", # #if defined(__cpp_...) / #elif defined(__cpp_...) + "elif_bare", # #elif __cpp_... + "ifdef", # #ifdef __cpp_... + "ifndef", # #ifndef __cpp_... + "compound", # #if __cplusplus >= ... && __cpp_... // comment + "continuation", # __cpp_... on a continuation line + "indented", # indented directive with space after '#' + "detail_config", # nested config.hpp is not exempt + "example", # examples/ are not exempt + ], +) +def test__cpp_no_flag_forking__invalid(repo_info, beman_standard_check_config, case): + """ + Test that each way of using a __cpp_* feature test macro in a preprocessor conditional fails. + """ + repo_info["top_level"] = no_flag_forking_invalid_prefix / case check = CppNoFlagForkingCheck(repo_info, beman_standard_check_config) assert check.check() is False From 96be608e20e511c8156d957da311e5958d42f5f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Darius=20Nea=C8=9Bu?= Date: Thu, 3 Dec 2026 21:43:13 +0200 Subject: [PATCH 3/3] [beman-tidy] Cover elifdef and open block comments in cpp.no_flag_forking Assert exact violation lines per case and the per-library config.hpp exemption. --- beman_tidy/lib/checks/beman_standard/cpp.py | 3 ++ .../include/beman/exemplar/uses_elifdef.hpp | 7 +++ .../include/beman/exemplar/uses_config.hpp | 10 ++++ .../lib/checks/beman_standard/cpp/test_cpp.py | 50 ++++++++++++++----- 4 files changed, 57 insertions(+), 13 deletions(-) create mode 100644 tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/elifdef/include/beman/exemplar/uses_elifdef.hpp diff --git a/beman_tidy/lib/checks/beman_standard/cpp.py b/beman_tidy/lib/checks/beman_standard/cpp.py index 4efe3b6f..2e61a320 100644 --- a/beman_tidy/lib/checks/beman_standard/cpp.py +++ b/beman_tidy/lib/checks/beman_standard/cpp.py @@ -122,6 +122,8 @@ class CppNoFlagForkingCheck(BatchFileBaseCheck): _CONDITIONAL_DIRECTIVE_RE = re.compile(r"^\s*#\s*(?:if|elif|ifdef|ifndef|elifdef|elifndef)\b") _FEATURE_TEST_MACRO_RE = re.compile(r"\b__cpp_\w+") _BLOCK_COMMENT_RE = re.compile(r"/\*.*?\*/") + # A block comment opened on the directive line and closed on a later line. + _UNTERMINATED_BLOCK_COMMENT_RE = re.compile(r"/\*.*$") _LINE_COMMENT_RE = re.compile(r"//.*$") def __init__(self, repo_info, beman_standard_check_config): @@ -166,6 +168,7 @@ def _find_violations(self): directive = outer._BLOCK_COMMENT_RE.sub(" ", directive) directive = outer._LINE_COMMENT_RE.sub("", directive) + directive = outer._UNTERMINATED_BLOCK_COMMENT_RE.sub("", directive) match = outer._FEATURE_TEST_MACRO_RE.search(directive) if match: violations.append((start_index + 1, match.group(0))) diff --git a/tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/elifdef/include/beman/exemplar/uses_elifdef.hpp b/tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/elifdef/include/beman/exemplar/uses_elifdef.hpp new file mode 100644 index 00000000..adcaac90 --- /dev/null +++ b/tests/lib/checks/beman_standard/cpp/no_flag_forking/invalid/elifdef/include/beman/exemplar/uses_elifdef.hpp @@ -0,0 +1,7 @@ +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#if defined(BEMAN_EXEMPLAR_USE_CONCEPTS) +void use_concepts(); +#elifdef __cpp_lib_ranges +void use_ranges(); +#endif diff --git a/tests/lib/checks/beman_standard/cpp/no_flag_forking/valid/include/beman/exemplar/uses_config.hpp b/tests/lib/checks/beman_standard/cpp/no_flag_forking/valid/include/beman/exemplar/uses_config.hpp index 68ad60f9..6f17ddc6 100644 --- a/tests/lib/checks/beman_standard/cpp/no_flag_forking/valid/include/beman/exemplar/uses_config.hpp +++ b/tests/lib/checks/beman_standard/cpp/no_flag_forking/valid/include/beman/exemplar/uses_config.hpp @@ -34,4 +34,14 @@ void use_cpp20(); inline constexpr const char* ftm_name = "__cpp_concepts"; +#if BEMAN_MY_LIB_USE_CONCEPTS /* replaces __cpp_concepts, + see config.hpp */ +void use_concepts(); +#endif + +// Other predefined macros and operators that merely contain "cpp" are fine. +#if defined(__has_cpp_attribute) && __has_cpp_attribute(nodiscard) +void use_nodiscard(); +#endif + #endif diff --git a/tests/lib/checks/beman_standard/cpp/test_cpp.py b/tests/lib/checks/beman_standard/cpp/test_cpp.py index 21a01e80..4311174f 100644 --- a/tests/lib/checks/beman_standard/cpp/test_cpp.py +++ b/tests/lib/checks/beman_standard/cpp/test_cpp.py @@ -60,30 +60,54 @@ def test__cpp_no_flag_forking__valid(repo_info, beman_standard_check_config): check = CppNoFlagForkingCheck(repo_info, beman_standard_check_config) assert check.check() is True + # The config.hpp exemption is per library: another library's config.hpp is not exempt. + repo_info["short_name"] = "other_library" + check = CppNoFlagForkingCheck(repo_info, beman_standard_check_config) + assert check.check() is False + @pytest.mark.parametrize( - "case", + "case, expected_violations", [ - "defined", # #if defined(__cpp_...) / #elif defined(__cpp_...) - "elif_bare", # #elif __cpp_... - "ifdef", # #ifdef __cpp_... - "ifndef", # #ifndef __cpp_... - "compound", # #if __cplusplus >= ... && __cpp_... // comment - "continuation", # __cpp_... on a continuation line - "indented", # indented directive with space after '#' - "detail_config", # nested config.hpp is not exempt - "example", # examples/ are not exempt + # #if defined(__cpp_...), #elif defined(__cpp_...), #if __cpp_... >= N: all reported + ( + "defined", + [ + (6, "__cpp_explicit_this_parameter"), + (8, "__cpp_concepts"), + (11, "__cpp_lib_format"), + ], + ), + ("elif_bare", [(8, "__cpp_concepts")]), # #elif __cpp_... + ("ifdef", [(3, "__cpp_lib_execution")]), # #ifdef __cpp_... + ("ifndef", [(3, "__cpp_lib_jthread")]), # #ifndef __cpp_... + ("elifdef", [(5, "__cpp_lib_ranges")]), # #elifdef __cpp_... (C++23) + ("compound", [(3, "__cpp_lib_ranges")]), # #if __cplusplus >= N && __cpp_... // comment + ("continuation", [(3, "__cpp_lib_ranges")]), # __cpp_... on a continuation line + ("indented", [(4, "__cpp_concepts")]), # indented directive with space after '#' + ("detail_config", [(4, "__cpp_lib_three_way_comparison")]), # nested config.hpp + ("example", [(3, "__cpp_lib_print")]), # examples/ are in scope ], ) -def test__cpp_no_flag_forking__invalid(repo_info, beman_standard_check_config, case): +def test__cpp_no_flag_forking__invalid( + repo_info, beman_standard_check_config, case, expected_violations +): """ - Test that each way of using a __cpp_* feature test macro in a preprocessor conditional fails. + Test that each way of using a __cpp_* feature test macro in a preprocessor conditional fails, + and that every offending directive is reported with its line number. """ - repo_info["top_level"] = no_flag_forking_invalid_prefix / case + case_prefix = no_flag_forking_invalid_prefix / case + repo_info["top_level"] = case_prefix check = CppNoFlagForkingCheck(repo_info, beman_standard_check_config) assert check.check() is False + (relative_path,) = [p.relative_to(case_prefix) for p in case_prefix.rglob("*") if p.is_file()] + file_check = CppNoFlagForkingCheck.CppNoFlagForkingCheckImpl( + repo_info, beman_standard_check_config, relative_path + ) + assert file_check._find_violations() == expected_violations + @pytest.mark.skip(reason="not implemented") def test__cpp_no_flag_forking__fix_inplace(repo_info, beman_standard_check_config):