Skip to content
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

Fix spurious warning about cast of unrelated types #241

Merged
merged 1 commit into from
Jul 1, 2024

Conversation

DutChen18
Copy link
Member

@DutChen18 DutChen18 commented Jul 1, 2024

Fixed a spurious warning about cast of unrelated types, as seen in this example program (Commenting out the namespace declaration on line 9 causes the warning not to appear):

#include <cheerp/types.h>

class CString {
public:
        [[cheerp::genericjs]]
        explicit operator client::String*() const;
};

namespace [[cheerp::genericjs]] client {}

[[cheerp::genericjs]]
client::Object* foo() {
        CString string;
        return static_cast<client::String*>(string);
}

Produces the warning:

test.cpp:14:16: warning: Cheerp: Using values cast to unrelated types is undefined behaviour unless the destination type is the actual type of the value [-Wcheerp-unsafe]
        return static_cast<client::String*>(string);
               ^

I believe the issue occurs because the client::String type spelled in the declaration of operator client::String* on line 6 is not the exact same type as the one in the static_cast on line 14. However, they do have the same canonical type.

I looked at a few different functions in ASTContext to compare the types in a better way. The one that seems to make the most sense is hasSameType, which internally calls getCanonicalType on both arguments and just does an == comparison on the resulting pointers.

@DutChen18 DutChen18 requested a review from yuri91 July 1, 2024 11:39
@yuri91
Copy link
Member

yuri91 commented Jul 1, 2024

hasSameType looks like the correct choice. i saw it used in a bunch of places too.

@yuri91 yuri91 merged commit 910a8a2 into master Jul 1, 2024
1 check passed
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.

2 participants