-
Notifications
You must be signed in to change notification settings - Fork 31
Add GetBinaryOperator
#319
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
Add GetBinaryOperator
#319
Conversation
lib/Interpreter/CppInterOp.cpp
Outdated
else if (op == "^") | ||
getSema().LookupOverloadedOperatorName(clang::OO_Caret, S, lookup); |
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.
What other operators should be here?
Should I include +=
, &=
, &&
, etc?
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.
We probably should create some enum with the supported operators.
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.
We need a test case for this PR.
lib/Interpreter/CppInterOp.cpp
Outdated
else if (op == "^") | ||
getSema().LookupOverloadedOperatorName(clang::OO_Caret, S, lookup); |
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.
We probably should create some enum with the supported operators.
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.
clang-tidy made some suggestions
lib/Interpreter/CppInterOp.cpp
Outdated
getSema().LookupOverloadedOperatorName(clang::OO_Caret, S, lookup); | ||
|
||
for (NamedDecl* x : lookup) { | ||
if (auto F = llvm::dyn_cast<Decl>(x)) { |
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.
warning: 'auto F' can be declared as 'auto *F' [llvm-qualified-auto]
if (auto F = llvm::dyn_cast<Decl>(x)) { | |
if (auto *F = llvm::dyn_cast<Decl>(x)) { |
lib/Interpreter/CppInterOp.cpp
Outdated
clang::UnresolvedSet<8> lookup; | ||
|
||
if (op == "+") | ||
getSema().LookupOverloadedOperatorName(clang::OO_Plus, S, lookup); |
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.
Looks like Sema::LookupBinOp
is a higher-level interface. We should probably use it instead...
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #319 +/- ##
==========================================
+ Coverage 73.38% 73.47% +0.09%
==========================================
Files 8 8
Lines 2979 3005 +26
==========================================
+ Hits 2186 2208 +22
- Misses 793 797 +4
|
clang-tidy review says "All clean, LGTM! 👍" |
@@ -465,7 +483,7 @@ namespace Cpp { | |||
TCppIndex_t param_index); | |||
|
|||
///\returns function that performs operation op on lc and rc | |||
TCppFunction_t GetBinaryOperator(TCppScope_t scope, const std::string& op, | |||
TCppFunction_t GetBinaryOperator(TCppScope_t scope, enum BinaryOperator op, |
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.
We don't need an elaborated type here. Dropping the enum should be fine.
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.
I was thinking of removing our definition of the enum and replacing it with using clang::BinaryOperatorKind;
. Then we will not require that long switch ... case ...
in CppInterOp.cpp::GetBinaryOperator
. Nor any kind of string comparison.
Any thoughts?
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.
We can't leak implementation details for clang. That means we can't include header files of llvm/clang in CppInterOp.h.
lib/Interpreter/CppInterOp.cpp
Outdated
@@ -3178,35 +3178,53 @@ namespace Cpp { | |||
|
|||
clang::UnresolvedSet<8> lookup; | |||
|
|||
if (op == "+") | |||
switch (op) { | |||
case Plus: | |||
getSema().LookupOverloadedOperatorName(clang::OO_Plus, S, lookup); |
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.
Can't we follow the enum ordering in clang. Eg clang::OO_Plus == BinaryOperator.Plus? Then this will be just a single call instead of a switch stmt.
clang-tidy review says "All clean, LGTM! 👍" |
clang-tidy review says "All clean, LGTM! 👍" |
Hi @vgvassilev, I have made all the changes you suggested. Please have a look. Valgrind test fails with |
clang-tidy review says "All clean, LGTM! 👍" |
clang-tidy review says "All clean, LGTM! 👍" |
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.
LGTM! See remaining comments.
lib/Interpreter/CppInterOp.cpp
Outdated
for (NamedDecl* x : lookup) { | ||
if (auto* F = llvm::dyn_cast<Decl>(x)) { |
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.
for (NamedDecl* x : lookup) { | |
if (auto* F = llvm::dyn_cast<Decl>(x)) { | |
for (NamedDecl* D : lookup) { | |
if (auto* FD = llvm::dyn_cast<Decl>(D)) { |
This is the common convention.
I have spent more than 3 hours trying to get the following to work. // C++ code
class MyClass {
public:
int x;
MyClass(int x) : x(x) {}
};
namespace MyScope {
MyClass operator*(MyClass lhs, MyClass rhs) {
return MyClass(lhs.x * rhs.x);
}
} // end namespace MyScope Look up the operator with Cpp::GetBinaryOperator(Cpp::GetScope("MyScope"), Cpp::BinaryOperator::Mul, "MyClass", "MyClass"); But it is not working. This is required for the |
clang-tidy review says "All clean, LGTM! 👍" |
Implements
GetBinaryOperator
to resolve binary operator in the given scope with specified arguments.