Commit c25c700a authored by Keishi Hattori's avatar Keishi Hattori Committed by Commit Bot

Add .get() to CheckedPtr when comparing with a std::string

When using a binary operand with CheckedPtr and std::string, you get an invalid operands to binary expression error.
With this CL, the rewriter matches CheckedPtr used with a std::string and adds .get() so the std::string/char* operator overrides gets used.

Bug: 1073933
Change-Id: I4854d4d272ba1d9a107e7e346bfbe363e6861c06
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2535908Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarŁukasz Anforowicz <lukasza@chromium.org>
Commit-Queue: Keishi Hattori <keishi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#827861}
parent 83b51704
...@@ -1046,6 +1046,26 @@ int main(int argc, const char* argv[]) { ...@@ -1046,6 +1046,26 @@ int main(int argc, const char* argv[]) {
match_finder.addMatcher(affected_ternary_operator_arg_matcher, match_finder.addMatcher(affected_ternary_operator_arg_matcher,
&affected_expr_rewriter); &affected_expr_rewriter);
// Affected string binary operator =========
// Given
// struct S { const char* y; }
// void foo(const S& s) {
// std::string other;
// bool v1 = s.y == other;
// std::string v2 = s.y + other;
// }
// binds the |s.y| expr if it matches the |affected_expr_matcher| above.
//
// See also testcases in tests/affected-expr-original.cc
auto std_string_expr_matcher =
expr(hasType(cxxRecordDecl(hasName("::std::basic_string"))));
auto affected_string_binary_operator_arg_matcher = cxxOperatorCallExpr(
hasAnyOverloadedOperatorName("+", "==", "!=", "<", "<=", ">", ">="),
hasAnyArgument(std_string_expr_matcher),
forEachArgumentWithParam(affected_expr_matcher, parmVarDecl()));
match_finder.addMatcher(affected_string_binary_operator_arg_matcher,
&affected_expr_rewriter);
// Calls to templated functions ========= // Calls to templated functions =========
// Given // Given
// struct S { int* y; }; // struct S { int* y; };
......
...@@ -174,6 +174,34 @@ void foo(int x) { ...@@ -174,6 +174,34 @@ void foo(int x) {
} // namespace ternary_operator_tests } // namespace ternary_operator_tests
namespace string_comparison_operator_tests {
void foo(int x) {
MyStruct my_struct;
std::string other_str = "other";
// To avoid the following error type:
// error: invalid operands to binary expression ... basic_string ... and ...
// CheckedPtr ...
// we need to append |.get()| to |my_struct.const_char_ptr| below.
//
// Expected rewrite: ... my_struct.const_char_ptr.get() ...
bool v1 = my_struct.const_char_ptr.get() == other_str;
bool v2 = other_str == my_struct.const_char_ptr.get();
bool v3 = my_struct.const_char_ptr.get() > other_str;
bool v4 = other_str > my_struct.const_char_ptr.get();
bool v5 = my_struct.const_char_ptr.get() >= other_str;
bool v6 = other_str >= my_struct.const_char_ptr.get();
bool v7 = my_struct.const_char_ptr.get() < other_str;
bool v8 = other_str < my_struct.const_char_ptr.get();
bool v9 = my_struct.const_char_ptr.get() <= other_str;
bool v10 = other_str <= my_struct.const_char_ptr.get();
std::string v11 = my_struct.const_char_ptr.get() + other_str;
std::string v12 = other_str + my_struct.const_char_ptr.get();
}
} // namespace string_comparison_operator_tests
namespace templated_functions { namespace templated_functions {
template <typename T> template <typename T>
......
...@@ -172,6 +172,34 @@ void foo(int x) { ...@@ -172,6 +172,34 @@ void foo(int x) {
} // namespace ternary_operator_tests } // namespace ternary_operator_tests
namespace string_comparison_operator_tests {
void foo(int x) {
MyStruct my_struct;
std::string other_str = "other";
// To avoid the following error type:
// error: invalid operands to binary expression ... basic_string ... and ...
// CheckedPtr ...
// we need to append |.get()| to |my_struct.const_char_ptr| below.
//
// Expected rewrite: ... my_struct.const_char_ptr.get() ...
bool v1 = my_struct.const_char_ptr == other_str;
bool v2 = other_str == my_struct.const_char_ptr;
bool v3 = my_struct.const_char_ptr > other_str;
bool v4 = other_str > my_struct.const_char_ptr;
bool v5 = my_struct.const_char_ptr >= other_str;
bool v6 = other_str >= my_struct.const_char_ptr;
bool v7 = my_struct.const_char_ptr < other_str;
bool v8 = other_str < my_struct.const_char_ptr;
bool v9 = my_struct.const_char_ptr <= other_str;
bool v10 = other_str <= my_struct.const_char_ptr;
std::string v11 = my_struct.const_char_ptr + other_str;
std::string v12 = other_str + my_struct.const_char_ptr;
}
} // namespace string_comparison_operator_tests
namespace templated_functions { namespace templated_functions {
template <typename T> template <typename T>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment