[cfe-dev] C++ algorithm analysis tool

Alexander Kornienko via cfe-dev cfe-dev at lists.llvm.org
Tue Apr 4 05:20:38 PDT 2017


As Artem noted, there are already a few clang-tidy checks that try to
suggest using more compact coding patterns like range-for-loops
<https://clang.llvm.org/extra/clang-tidy/checks/modernize-loop-convert.html>
, more suitable algorithms
<https://clang.llvm.org/extra/clang-tidy/checks/misc-inefficient-algorithm.html>,
shrink_to_fit()
<https://clang.llvm.org/extra/clang-tidy/checks/modernize-shrink-to-fit.html>,
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 modernize-loop-convert
<https://github.com/Microsoft/clang-tools-extra/blob/master/clang-tidy/modernize/LoopConvertCheck.cpp>,
for example. If you would like to try this, you can start here
<http://clang.llvm.org/extra/clang-tidy/#getting-involved>.

On Tue, Apr 4, 2017 at 10:31 AM, Artem Dergachev via cfe-dev <
cfe-dev at lists.llvm.org> wrote:

> Hmm, missed this one. Adding to Kirill's answer.
>
> 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.
>
> 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.
>
>
>
>
> On 4/1/17 2:42 AM, Christopher Di Bella via cfe-dev wrote:
>
>> Hey everyone,
>>
>> Just wondering if there's a clang tool that can analyse an algorithm to
>> suggest where standard algorithms can be replace handwritten algos?
>> E.g.
>>
>> int i = 0;
>> for (; i < v.size(); ++i)
>>    if (v[i] == expected)
>>       break;
>> if (i != v.size())
>>    some_function(v[i]);
>>
>> Can be rewritten to
>>
>> auto i = find(v.begin(), v.end(), expected);
>> if (i != v.end())
>>    some_function(*i);
>>
>> or in C++17:
>>
>> if (auto i = find(v.begin(), v.end(), expected); i != v.end())
>>    some_function(*i);
>>
>> 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).
>>
>> Cheers,
>>
>> Chris
>>
>>
>> _______________________________________________
>> cfe-dev mailing list
>> cfe-dev at lists.llvm.org
>> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20170404/f23b240d/attachment.html>


More information about the cfe-dev mailing list