[cfe-dev] C++ algorithm analysis tool

Artem Dergachev via cfe-dev cfe-dev at lists.llvm.org
Tue Apr 4 01:31:22 PDT 2017


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




More information about the cfe-dev mailing list