Skip to content

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

Merged
merged 8 commits into from
Sep 5, 2024

Conversation

Vipul-Cariappa
Copy link
Collaborator

Implements GetBinaryOperator to resolve binary operator in the given scope with specified arguments.

Comment on lines 3205 to 3206
else if (op == "^")
getSema().LookupOverloadedOperatorName(clang::OO_Caret, S, lookup);
Copy link
Collaborator Author

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?

Copy link
Contributor

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.

Copy link
Contributor

@vgvassilev vgvassilev left a 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.

Comment on lines 3205 to 3206
else if (op == "^")
getSema().LookupOverloadedOperatorName(clang::OO_Caret, S, lookup);
Copy link
Contributor

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.

Copy link
Contributor

@github-actions github-actions bot left a 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

getSema().LookupOverloadedOperatorName(clang::OO_Caret, S, lookup);

for (NamedDecl* x : lookup) {
if (auto F = llvm::dyn_cast<Decl>(x)) {
Copy link
Contributor

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]

Suggested change
if (auto F = llvm::dyn_cast<Decl>(x)) {
if (auto *F = llvm::dyn_cast<Decl>(x)) {

clang::UnresolvedSet<8> lookup;

if (op == "+")
getSema().LookupOverloadedOperatorName(clang::OO_Plus, S, lookup);
Copy link
Contributor

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...

Copy link

codecov bot commented Aug 31, 2024

Codecov Report

Attention: Patch coverage is 84.61538% with 4 lines in your changes missing coverage. Please review.

Project coverage is 73.47%. Comparing base (91e392d) to head (ead101e).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
lib/Interpreter/CppInterOp.cpp 84.61% 4 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            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     
Files with missing lines Coverage Δ
include/clang/Interpreter/CppInterOp.h 96.00% <ø> (ø)
lib/Interpreter/CppInterOp.cpp 79.20% <84.61%> (+0.08%) ⬆️
Files with missing lines Coverage Δ
include/clang/Interpreter/CppInterOp.h 96.00% <ø> (ø)
lib/Interpreter/CppInterOp.cpp 79.20% <84.61%> (+0.08%) ⬆️

Copy link
Contributor

github-actions bot commented Sep 3, 2024

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,
Copy link
Contributor

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.

Copy link
Collaborator Author

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?

Copy link
Contributor

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.

@@ -3178,35 +3178,53 @@ namespace Cpp {

clang::UnresolvedSet<8> lookup;

if (op == "+")
switch (op) {
case Plus:
getSema().LookupOverloadedOperatorName(clang::OO_Plus, S, lookup);
Copy link
Contributor

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.

Copy link
Contributor

github-actions bot commented Sep 3, 2024

clang-tidy review says "All clean, LGTM! 👍"

Copy link
Contributor

github-actions bot commented Sep 3, 2024

clang-tidy review says "All clean, LGTM! 👍"

@Vipul-Cariappa
Copy link
Collaborator Author

Hi @vgvassilev, I have made all the changes you suggested. Please have a look.

Valgrind test fails with Conditional jump or move depends on uninitialised value. I am not sure about the cause here.

Copy link
Contributor

github-actions bot commented Sep 3, 2024

clang-tidy review says "All clean, LGTM! 👍"

Copy link
Contributor

github-actions bot commented Sep 5, 2024

clang-tidy review says "All clean, LGTM! 👍"

Copy link
Contributor

@vgvassilev vgvassilev left a 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.

Comment on lines 3184 to 3185
for (NamedDecl* x : lookup) {
if (auto* F = llvm::dyn_cast<Decl>(x)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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.

@Vipul-Cariappa
Copy link
Collaborator Author

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 operator+ for std::string; it is defined within the std namespace.

Copy link
Contributor

github-actions bot commented Sep 5, 2024

clang-tidy review says "All clean, LGTM! 👍"

@vgvassilev vgvassilev merged commit 9bd4678 into compiler-research:main Sep 5, 2024
59 checks passed
@Vipul-Cariappa Vipul-Cariappa deleted the GetBinaryOperator branch September 6, 2024 09:56
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.

3 participants