Commit eb8dfa5c authored by Lukasz Anforowicz's avatar Lukasz Anforowicz Committed by Commit Bot

Rewriting |const_cast<...>(s.ptr_field)| into |...s.ptr_field.get()...|.

Bug: 1069567
Change-Id: I0e83dd3966c88bb9d5dda4744596bfa3729a606d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2159789
Commit-Queue: Łukasz Anforowicz <lukasza@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Cr-Commit-Position: refs/heads/master@{#774845}
parent 1480a9a1
...@@ -502,8 +502,19 @@ int main(int argc, const char* argv[]) { ...@@ -502,8 +502,19 @@ int main(int argc, const char* argv[]) {
auto affected_printf_arg_matcher = auto affected_printf_arg_matcher =
expr(allOf(affected_implicit_expr_matcher, expr(allOf(affected_implicit_expr_matcher,
hasParent(callExpr(callee(functionDecl(isVariadic())))))); hasParent(callExpr(callee(functionDecl(isVariadic()))))));
AffectedExprRewriter printf_arg_rewriter(&replacements_printer); AffectedExprRewriter affected_expr_rewriter(&replacements_printer);
match_finder.addMatcher(affected_printf_arg_matcher, &printf_arg_rewriter); match_finder.addMatcher(affected_printf_arg_matcher, &affected_expr_rewriter);
// some_cast<...>(expr) =========
// Given
// const_cast<...>(s.y)
// reinterpret_cast<...>(s.y)
// matches the |s.y| expr if it matches the |affected_implicit_expr_matcher|
// above.
auto affected_cast_arg_matcher = expr(allOf(
affected_implicit_expr_matcher,
hasParent(expr(anyOf(cxxConstCastExpr(), cxxReinterpretCastExpr())))));
match_finder.addMatcher(affected_cast_arg_matcher, &affected_expr_rewriter);
// Prepare and run the tool. // Prepare and run the tool.
std::unique_ptr<clang::tooling::FrontendActionFactory> factory = std::unique_ptr<clang::tooling::FrontendActionFactory> factory =
......
...@@ -2,15 +2,21 @@ ...@@ -2,15 +2,21 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include <stdint.h> // for uintptr_t
#include "base/memory/checked_ptr.h" #include "base/memory/checked_ptr.h"
class SomeClass; class SomeClass {};
class DerivedClass : public SomeClass {};
struct MyStruct { struct MyStruct {
CheckedPtr<SomeClass> ptr; CheckedPtr<SomeClass> ptr;
CheckedPtr<SomeClass> ptr2; CheckedPtr<SomeClass> ptr2;
CheckedPtr<const SomeClass> const_ptr;
}; };
namespace printf_tests {
int ConvertSomeClassToInt(SomeClass* some_class) { int ConvertSomeClassToInt(SomeClass* some_class) {
return 123; return 123;
} }
...@@ -35,3 +41,31 @@ void foo() { ...@@ -35,3 +41,31 @@ void foo() {
// No rewrite expected. // No rewrite expected.
MyPrintf("%d", ConvertSomeClassToInt(s.ptr)); MyPrintf("%d", ConvertSomeClassToInt(s.ptr));
} }
} // namespace printf_tests
namespace cast_tests {
void foo() {
MyStruct my_struct;
// To get |const_cast<...>(...)| to compile after the rewrite we
// need to rewrite the casted expression.
// Expected rewrite: const_cast<SomeClass*>(my_struct.const_ptr.get());
SomeClass* v = const_cast<SomeClass*>(my_struct.const_ptr.get());
// Expected rewrite: const_cast<const SomeClass*>(my_struct.ptr.get());
const SomeClass* v2 = const_cast<const SomeClass*>(my_struct.ptr.get());
// To get |reinterpret_cast<uintptr_t>(...)| to compile after the rewrite we
// need to rewrite the casted expression.
// Expected rewrite: reinterpret_cast<uintptr_t>(my_struct.ptr.get());
uintptr_t u = reinterpret_cast<uintptr_t>(my_struct.ptr.get());
// There is no need to append |.get()| inside static_cast - unlike the
// const_cast and reinterpret_cast examples above, static_cast will compile
// just fine.
DerivedClass* d = static_cast<DerivedClass*>(my_struct.ptr);
void* void_var = static_cast<void*>(my_struct.ptr);
}
} // namespace cast_tests
...@@ -2,13 +2,19 @@ ...@@ -2,13 +2,19 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
class SomeClass; #include <stdint.h> // for uintptr_t
class SomeClass {};
class DerivedClass : public SomeClass {};
struct MyStruct { struct MyStruct {
SomeClass* ptr; SomeClass* ptr;
SomeClass* ptr2; SomeClass* ptr2;
const SomeClass* const_ptr;
}; };
namespace printf_tests {
int ConvertSomeClassToInt(SomeClass* some_class) { int ConvertSomeClassToInt(SomeClass* some_class) {
return 123; return 123;
} }
...@@ -33,3 +39,31 @@ void foo() { ...@@ -33,3 +39,31 @@ void foo() {
// No rewrite expected. // No rewrite expected.
MyPrintf("%d", ConvertSomeClassToInt(s.ptr)); MyPrintf("%d", ConvertSomeClassToInt(s.ptr));
} }
} // namespace printf_tests
namespace cast_tests {
void foo() {
MyStruct my_struct;
// To get |const_cast<...>(...)| to compile after the rewrite we
// need to rewrite the casted expression.
// Expected rewrite: const_cast<SomeClass*>(my_struct.const_ptr.get());
SomeClass* v = const_cast<SomeClass*>(my_struct.const_ptr);
// Expected rewrite: const_cast<const SomeClass*>(my_struct.ptr.get());
const SomeClass* v2 = const_cast<const SomeClass*>(my_struct.ptr);
// To get |reinterpret_cast<uintptr_t>(...)| to compile after the rewrite we
// need to rewrite the casted expression.
// Expected rewrite: reinterpret_cast<uintptr_t>(my_struct.ptr.get());
uintptr_t u = reinterpret_cast<uintptr_t>(my_struct.ptr);
// There is no need to append |.get()| inside static_cast - unlike the
// const_cast and reinterpret_cast examples above, static_cast will compile
// just fine.
DerivedClass* d = static_cast<DerivedClass*>(my_struct.ptr);
void* void_var = static_cast<void*>(my_struct.ptr);
}
} // namespace cast_tests
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