[cfe-dev] [clang-tools-extra] A LibTooling project: Clang-cast

Ray Zhang via cfe-dev cfe-dev at lists.llvm.org
Tue Oct 20 21:52:10 PDT 2020


Hi everyone,

I've always wanted to contribute to Clang, and a few months ago I was
looking at the documentations online and found this
<https://clang.llvm.org/docs/ClangTools.html>:
Ideas for new Tools

   -

   C++ cast conversion tool. Will convert C-style casts ((type) value) to
   appropriate C++ cast (static_cast, const_cast or reinterpret_cast).

Well, I'm happy to say that after the few months I've finally finished the
tool and I'm excited to contribute this back to the community. I also made
sure to comply with clang's coding style and quality standards. I would
love it if anyone wants to review it/give it a try. Here's my PR on
Phabricator: https://reviews.llvm.org/D89765. I will write a .rst in the
docs section about how to use in the same PR, but I'm hoping to split the
patch into three sections first (strategy to split it is discussed in the
link).

Best,
Ray

*Addendum (A subset of functionalities)*
*Checking whether C-style cast was silently performing
static/reinterpret_casts/const_cast (or any combination)*
const float* const* i;
(int**) i;
error: C style cast can be replaced by 'const_cast<int
**>(reinterpret_cast<const int *const *>(i))'
    (int**) i;
    ^~~~~~~~~
    const_cast<int **>(reinterpret_cast<const int *const *>(i))

*Checking whether C-style casts were impossible to turn into C++ style
casts:*
on dependent type T emits the following:
error: C style cast cannot be converted into a C++ style cast
    (T) a;
    ^~~~~

on private inheritance specifier:
struct A {};
class B: A {};
error: C style cast cannot be converted into a C++ style cast
    (B*)(a);
    ^~~~~~~

*Checking whether C-style casts are no-ops and can be removed*
error: C style cast can be replaced by 'x'
    const int& y = (const int&) x;
                   ^~~~~~~~~~~~~~
                   x

*Fix all of the above*
note: FIX-IT applied suggested code changes

*... except in macros*
warning: C style cast can be replaced by 'static_cast<int>(3.5f)' (won't be
fixed in macro)
    CC(int, 3.5f);
~~~~^~~~~~~~~~~~
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20201020/94307aba/attachment-0001.html>


More information about the cfe-dev mailing list