Skip to content

Look up operator overloads by CXXOperatorName in GetFunctionsUsingName#996

Open
guitargeek wants to merge 1 commit into
compiler-research:mainfrom
guitargeek:cppyy-219
Open

Look up operator overloads by CXXOperatorName in GetFunctionsUsingName#996
guitargeek wants to merge 1 commit into
compiler-research:mainfrom
guitargeek:cppyy-219

Conversation

@guitargeek
Copy link
Copy Markdown

GetFunctionsUsingName built its lookup key only as an identifier, so queries like "operator==" never matched: operator overloads live in the AST under CXXOperatorName, not under an Identifier. cppyy's namespace getattr fallback (Cppyy::GetMethodsFromName -> Cpp::GetFunctionsUsingName) therefore returned no candidates and getattr(ns, 'operator==') raised AttributeError.

Detect names that start with "operator" followed by a non-identifier character and match the trimmed suffix against OperatorKinds.def. When it matches, build the DeclarationName via getCXXOperatorName(OO_...); otherwise fall back to the identifier lookup. Covers symbolic and multi-token overloads (==, +, (), [], new, delete, new[], delete[], ...). Names that merely start with "operator" (e.g. "operators_count") are left on the identifier path because the next character is an identifier-continue.

Fixes compiler-research/cppyy#219.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 11, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.80%. Comparing base (1ea28e2) to head (b43f9a6).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #996      +/-   ##
==========================================
+ Coverage   82.78%   82.80%   +0.02%     
==========================================
  Files          15       15              
  Lines        5048     5055       +7     
==========================================
+ Hits         4179     4186       +7     
  Misses        869      869              
Files with missing lines Coverage Δ
lib/CppInterOp/CppInterOp.cpp 88.98% <100.00%> (+0.02%) ⬆️
Files with missing lines Coverage Δ
lib/CppInterOp/CppInterOp.cpp 88.98% <100.00%> (+0.02%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

}

if (!DName)
DName = &Ctx.Idents.get(name);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we use LookupOperatorName instead of the string comparisons?

Something similar to:

clang/lib/Sema/SemaCoroutine.cpp-829-ExprResult Sema::BuildOperatorCoawaitLookupExpr(Scope *S, SourceLocation Loc) {
clang/lib/Sema/SemaCoroutine.cpp-830-  DeclarationName OpName =
clang/lib/Sema/SemaCoroutine.cpp-831-      Context.DeclarationNames.getCXXOperatorName(OO_Coawait);
clang/lib/Sema/SemaCoroutine.cpp-832-  LookupResult Operators(*this, OpName, SourceLocation(),
clang/lib/Sema/SemaCoroutine.cpp:833:                         Sema::LookupOperatorName);
clang/lib/Sema/SemaCoroutine.cpp-834-  LookupName(Operators, S);

GetFunctionsUsingName built its lookup key only as an identifier, so
queries like "operator==" never matched: operator overloads live in
the AST under CXXOperatorName, not under an Identifier. cppyy's
namespace getattr fallback (Cppyy::GetMethodsFromName ->
Cpp::GetFunctionsUsingName) therefore returned no candidates and
getattr(ns, 'operator==') raised AttributeError.

Detect names that start with "operator" followed by a non-identifier
character and match the trimmed suffix against OperatorKinds.def. When
it matches, build the DeclarationName via getCXXOperatorName(OO_...);
otherwise fall back to the identifier lookup. Covers symbolic and
multi-token overloads (==, +, (), [], new, delete, new[], delete[],
...). Names that merely start with "operator" (e.g. "operators_count")
are left on the identifier path because the next character is an
identifier-continue.

Fixes compiler-research/cppyy#219.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Regression in directly obtaining operator== as attribute

2 participants