<div dir="ltr"><div>As Artem noted, there are already a few clang-tidy checks that try to suggest using more compact coding patterns like <a href="https://clang.llvm.org/extra/clang-tidy/checks/modernize-loop-convert.html">range-for-loops</a>, <a href="https://clang.llvm.org/extra/clang-tidy/checks/misc-inefficient-algorithm.html">more suitable algorithms</a>, <a href="https://clang.llvm.org/extra/clang-tidy/checks/modernize-shrink-to-fit.html">shrink_to_fit()</a>, etc. It should be equally possible to detect certain hand-rolled algorithms and replace them with STL. As for the amount of work it might take, you can take a look at the implementation of <a href="https://github.com/Microsoft/clang-tools-extra/blob/master/clang-tidy/modernize/LoopConvertCheck.cpp">modernize-loop-convert</a>, for example. If you would like to try this, you can start <a href="http://clang.llvm.org/extra/clang-tidy/#getting-involved">here</a>.<br></div><div><br></div><div><div class="gmail_extra"><div class="gmail_quote">On Tue, Apr 4, 2017 at 10:31 AM, Artem Dergachev via cfe-dev <span dir="ltr"><<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hmm, missed this one. Adding to Kirill's answer.<br>
<br>
Yep, clang-tidy seems to be the the right place for such check. They already have the `modernize' package which suggests using tricks from higher versions of the C++ standard. They support fix-it hints to suggest how to rewrite the highlighted code. They are also eagerly accepting checks that are not highlighting bugs, unlike Clang Static Analyzer, where, at least for now, the reputation requires to only include critical warnings that most likely require immediate attention. And your check doesn't seem to require symbolic execution, so relying on the Analyzer's path-sensitive engine is not necessary.<br>
<br>
You should be able to write particular checks easily using ASTMatchers; with this awesome mechanism, the code for matching a particular pattern wouldn't be much bigger than the pattern itself, and you'd be able to re-use sub-patterns.<div><div class="gmail-h5"><br>
<br>
<br>
<br>
On 4/1/17 2:42 AM, Christopher Di Bella via cfe-dev wrote:<br>
</div></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div class="gmail-h5">
Hey everyone,<br>
<br>
Just wondering if there's a clang tool that can analyse an algorithm to suggest where standard algorithms can be replace handwritten algos?<br>
E.g.<br>
<br>
int i = 0;<br>
for (; i < v.size(); ++i)<br>
   if (v[i] == expected)<br>
      break;<br>
if (i != v.size())<br>
   some_function(v[i]);<br>
<br>
Can be rewritten to<br>
<br>
auto i = find(v.begin(), v.end(), expected);<br>
if (i != v.end())<br>
   some_function(*i);<br>
<br>
or in C++17:<br>
<br>
if (auto i = find(v.begin(), v.end(), expected); i != v.end())<br>
   some_function(*i);<br>
<br>
If not, how difficult a task is it to write such a tool? Is there anything that one should take into special consideration while writing this tool? Do you think it would have a lot of use-cases? (I do, based on my company's code base, and code I have seen while marking assignments).<br>
<br>
Cheers,<br>
<br>
Chris<br>
<br>
<br></div></div><span class="gmail-">
______________________________<wbr>_________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/cfe-dev</a><br>
</span></blockquote><div class="gmail-HOEnZb"><div class="gmail-h5">
<br>
______________________________<wbr>_________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/cfe-dev</a><br>
</div></div></blockquote></div><br></div></div></div>