<html><head><meta http-equiv="Content-Type" content="text/html charset=windows-1252"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">On Feb 7, 2014, at 4:49 AM, Vassil Vassilev <<a href="mailto:vvasilev@cern.ch">vvasilev@cern.ch</a>> wrote:<br><div><br class="Apple-interchange-newline"><blockquote type="cite">
<meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type">
<div bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix">On 05/02/14 21:32, Nick Lewycky wrote:<br>
</div>
<blockquote cite="mid:CADbEz-jjNkbA3NR5x9+jpy2QYzizQEYaHzjdyCcQ4fU4pDWe0w@mail.gmail.com" type="cite">
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
<div dir="ltr">
<div class="gmail_extra">
<div class="gmail_quote">On 3 February 2014 14:08, Richard <span dir="ltr"><<a moz-do-not-send="true" href="mailto:legalize@xmission.com" target="_blank">legalize@xmission.com</a>></span>
wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px
0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><br>
In article <<a moz-do-not-send="true" href="mailto:CAENS6EsgzhXWfANFze8VAp68qDGHnrHNZJaaLmi28YJtnQwOmw@mail.gmail.com" target="_blank">CAENS6EsgzhXWfANFze8VAp68qDGHnrHNZJaaLmi28YJtnQwOmw@mail.gmail.com</a>>,<br>
<div> David Blaikie <<a moz-do-not-send="true" href="mailto:dblaikie@gmail.com" target="_blank">dblaikie@gmail.com</a>>
writes:<br>
<br>
> On Mon, Feb 3, 2014 at 3:06 AM, Vassil Vassilev
<<a moz-do-not-send="true" href="mailto:vvasilev@cern.ch" target="_blank">vvasilev@cern.ch</a>>
wrote:<br>
><br>
</div>
<div>> > A few months ago I was looking for a
copy-paste detector for a C++<br>
> > project. I didn't find such a feature of
clang's static analyzer. Is this<br>
> > the case?<br>
><br>
> copy-paste detector? As in plagarism detection?<br>
<br>
</div>
I don't think plagiarism is the concern. The conern is
that<br>
copy/paste of blocks of code where the pasted block needs
to be<br>
updated in several places, but not all of the updates were
performed.<br>
</blockquote>
<div><br>
</div>
<div>I've implemented this sort of thing, but it's only 80%
finished and has been kicking around on the low-priority
end of my todo list for the past couple of years. Patch
attached. It'd be great if someone were interested in
finishing this off. I won't get to it soon.</div>
<div><br>
</div>
<div>Note that it's a warning instead of a static analysis
check which means that it must have an aggressively low
number of false positives, and that it must be run
quickly. The implementation I have analyzes conditional
operators and if/elseif chains, but doesn't collect all
the expressions through something like a && b
&&c && a. That would be the next thing to
add.</div>
<div><br>
</div>
<div>It does have some really cool properties that we can
only get because clang integrates closely with its
preprocessor. Consider this sample from the testcase:</div>
<div><br>
#define num_cpus() (1)<br>
#define max_omp_threads() (1)<br>
int test8(int expr) {<br>
if (expr) {<br>
return num_cpus();<br>
} else {<br>
return max_omp_threads();<br>
}<br>
}</div>
<div><br>
</div>
<div>We know better than to warn on that, even though the
AST looks the same. If you instead write "return
num_cpus();" twice, we warn on that (that's test9 in the
testsuite).</div>
<div><br>
</div>
<div>Nick</div>
</div>
</div>
</div>
</blockquote>
Thanks this looks very interesting. This may be a good start for a
student. IIUC a non-unique expr is the ones that have same source
ranges and same FileIDs, right? Could this be upgraded to AST-node
(structural) comparison?</div></blockquote><div><br></div><div>I’d love to see a tool with this kind of functionality as part of llvm.</div><div><div>There is a commercial tool called “Pattern Insight” that does stuff like this. <a href="http://patterninsight.com/">http://patterninsight.com/</a></div><div><br></div></div><div>Here’s another use case for you.</div></div><div><br></div><div>I’ve been in groups that have used it in the past (careful locution; I haven’t personally used it), and occasionally it finds some amazing things.</div><div>The best example (from our use):</div><div><br></div><div>Code block #1 is about 50 lines of code, with references to a global variable (global1, global1, global1, global1, global1).</div><div>Code block #2 is an obviously duplicated and edited block of code, with references to (global2, global2, global2, global1, global2).</div><div><br></div><div>Pattern Insight, while looking through this code base, emitted a message to the effect “Are you sure you don’t mean ‘global2’ here?”</div><div>(and was correct)</div><div><br></div><div>— Marshall</div><div><br></div></body></html>