Repository navigation
fix(cuda.core): declare C++ bool returns as cpp_bool in _rt #3642
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
Merged
+117
−8
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 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 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 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """Reject Cython declarations that return ``bint`` for a C++ function returning ``bool``. | ||
|
|
||
| A consumer module calls a cimported function through a pointer of the type in | ||
| the ``.pxd``, not the type in the C++ header. ``bint`` is ``int``, but MSVC | ||
| sets only the low byte of the return register for a ``bool``, so on Windows | ||
| the caller can read ``false`` as ``true``. Declare such returns as:: | ||
|
|
||
| from libcpp cimport bool as cpp_bool | ||
|
|
||
| cdef cpp_bool f(...) noexcept nogil | ||
| """ | ||
|
|
||
| import re | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| REPO_ROOT = Path(__file__).resolve().parent.parent | ||
| SOURCE_ROOT = REPO_ROOT / "cuda_core" / "cuda" / "core" | ||
| CXX_SUFFIXES = (".hpp", ".h", ".cpp") | ||
| CYTHON_SUFFIXES = (".pyx", ".pxd", ".pxi") | ||
|
|
||
| # [inline|static|...] bool name( -- the return type may sit on its own line | ||
| _CXX_BOOL_FUNCTION = re.compile( | ||
| r"""(?:^|[;{}]|\]\])\s* | ||
| (?:(?:inline|static|constexpr|virtual|extern)\s+)* | ||
| bool\s+(?P<name>[A-Za-z_]\w*)\s*\(""", | ||
| re.MULTILINE | re.VERBOSE, | ||
| ) | ||
| # [cdef ][inline ]bint name ["c::name"] ( | ||
| _CYTHON_BINT_FUNCTION = re.compile( | ||
| r"""^[ \t]*(?:cdef\s+)?(?:inline\s+)? | ||
| bint\s+(?P<name>[A-Za-z_]\w*)\s* | ||
| (?:"(?P<cname>[^"]*)"\s*)?\(""", | ||
| re.MULTILINE | re.VERBOSE, | ||
| ) | ||
|
|
||
|
|
||
| def _blank(match): | ||
| return re.sub(r"[^\n]", " ", match.group(0)) | ||
|
|
||
|
|
||
| def _strip_cxx_comments(text): | ||
| text = re.sub(r"(?s)/\*.*?\*/", _blank, text) | ||
| return re.sub(r"//[^\n]*", _blank, text) | ||
|
|
||
|
|
||
| def _strip_cython_comments(text): | ||
| # Keep the quoted C names in extern declarations; only drop ``#`` comments | ||
| # and docstrings, whose examples could otherwise look like declarations. | ||
| text = re.sub(r'(?s)(?:"""|\'\'\').*?(?:"""|\'\'\')', _blank, text) | ||
| return re.sub(r"#[^\n]*", _blank, text) | ||
|
|
||
|
|
||
| def collect_cxx_bool_functions(root=SOURCE_ROOT): | ||
| names = set() | ||
| for path in root.rglob("*"): | ||
| if path.suffix in CXX_SUFFIXES: | ||
| text = _strip_cxx_comments(path.read_text(encoding="utf-8")) | ||
| names.update(m.group("name") for m in _CXX_BOOL_FUNCTION.finditer(text)) | ||
| return names | ||
|
|
||
|
|
||
| def find_violations(text, bool_functions): | ||
| """Yield ``(offset, message)`` for each ``bint`` declaration of a C++ ``bool`` function.""" | ||
| for match in _CYTHON_BINT_FUNCTION.finditer(_strip_cython_comments(text)): | ||
| name = match.group("name") | ||
| cname = match.group("cname") | ||
| target = cname.rsplit("::", 1)[-1] if cname else name | ||
| if target in bool_functions: | ||
| yield ( | ||
| match.start("name"), | ||
| f"{name!r} returns C++ bool; declare its return type as cpp_bool, not bint", | ||
| ) | ||
|
|
||
|
|
||
| def main(root=SOURCE_ROOT): | ||
| bool_functions = collect_cxx_bool_functions(root) | ||
| failed = False | ||
| for path in sorted(root.rglob("*")): | ||
| if path.suffix not in CYTHON_SUFFIXES: | ||
| continue | ||
| text = path.read_text(encoding="utf-8") | ||
| for offset, message in find_violations(text, bool_functions): | ||
| lineno = text.count("\n", 0, offset) + 1 | ||
| print(f"{path.relative_to(REPO_ROOT)}:{lineno}: {message}") | ||
| failed = True | ||
| return int(failed) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| sys.exit(main()) | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not identify C++ functions by their unqualified name alone.
If a header contains
flags::get()returningboolandcounts::get()returningint, a correct Cython declaration ofcounts::get()asbintfails this check. The collector stores onlyget, and this lookup ignores the extern block's namespace.Match the declaration to its C++ namespace and overload before rejecting its return type. Otherwise, the hook rejects valid bindings.