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

Nathan James via cfe-dev cfe-dev at lists.llvm.org
Wed Oct 21 10:04:07 PDT 2020


Hi,

Can I just ask what are the differences between this new tool and the
'cppcoreguidelines-pro-type-cstyle-cast' and 'google-readability-
casting' checks inside clang-tidy?

~Nathan

On Tue, 2020-10-20 at 21:52 -0700, Ray Zhang via cfe-dev wrote:
> 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:
> 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);
> ~~~~^~~~~~~~~~~~
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev



More information about the cfe-dev mailing list