<div dir="ltr"><div>Hi everyone,</div><div><br></div><div>I've always wanted to contribute to Clang, and a few months ago I was looking at the documentations online and found <a href="https://clang.llvm.org/docs/ClangTools.html ">this</a>:<br><h2>Ideas for new Tools</h2>
<ul><li><p class="gmail-first">C++ cast conversion tool. Will convert C-style casts (<code class="gmail-docutils gmail-literal gmail-notranslate"><span class="gmail-pre">(type)</span> <span class="gmail-pre">value</span></code>) to
appropriate C++ cast (<code class="gmail-docutils gmail-literal gmail-notranslate"><span class="gmail-pre">static_cast</span></code>, <code class="gmail-docutils gmail-literal gmail-notranslate"><span class="gmail-pre">const_cast</span></code> or
<code class="gmail-docutils gmail-literal gmail-notranslate"><span class="gmail-pre">reinterpret_cast</span></code>).</p>
</li></ul></div><div>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: <a href="https://reviews.llvm.org/D89765">https://reviews.llvm.org/D89765</a>. 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).</div><div><br></div><div>Best,</div><div>Ray<br></div><div><b><br></b></div><div><b>Addendum (A subset of functionalities)</b><br></div><div><b>Checking whether C-style cast was silently performing static/reinterpret_casts/const_cast (or any combination)</b></div><div>const float* const* i;<br>(int**) i;</div><div>error: C style cast can be replaced by 'const_cast<int **>(reinterpret_cast<const int *const *>(i))'<br> (int**) i;<br> ^~~~~~~~~<br> const_cast<int **>(reinterpret_cast<const int *const *>(i))</div><div><br></div><div><b>Checking whether C-style casts were impossible to turn into C++ style casts:</b></div><div>on dependent type T emits the following:<br></div><div>error: C style cast cannot be converted into a C++ style cast<br> (T) a;<br> ^~~~~</div><div><br></div><div>on private inheritance specifier:<br></div><div>struct A {};<br>class B: A {};</div><div>error: C style cast cannot be converted into a C++ style cast<br> (B*)(a);<br> ^~~~~~~<br></div><div><br></div><div><b>Checking whether C-style casts are no-ops and can be removed</b></div><div>error: C style cast can be replaced by 'x'<br> const int& y = (const int&) x;<br> ^~~~~~~~~~~~~~<br> x<br></div><div><br></div><div><b>Fix all of the above</b></div><div>note: FIX-IT applied suggested code changes</div><div><b><br></b></div><div><b>... except in macros</b></div><div>warning: C style cast can be replaced by 'static_cast<int>(3.5f)' (won't be fixed in macro)<br> CC(int, 3.5f);<br>~~~~^~~~~~~~~~~~</div></div>