如何在开发过程中检测代码复制?

我们有一个相当大的代码库,400K 的 C + + LOC,代码复制是一个问题。是否有任何工具可以有效地检测重复的代码块?

理想情况下,开发人员可以在开发过程中使用它,而不是只是偶尔运行以查看问题出在哪里。如果我们能够将这样一个工具与 CruiseControl 集成在一起,在每次签入后提供一个报告,那也是很好的。

前段时间我看了一下 双倍,它显示了一个很好的图形,但是使用它需要一个 Smalltalk 环境,这使得自动运行它相当困难。

免费的工具是不错,但如果有一些好的商业工具,我也会感兴趣。

31415 次浏览

Simian detects duplicate code in C++ projects.

Update: Also works with Java, C#, C, COBOL, Ruby, JSP, ASP, HTML, XML, Visual Basic, Groovy source code and even plain text files

Look at the PMD project.

I've never used it, but have always wanted to.

CCFinderX is a free (for in-house use) cloned code detector that supports multiple programming languages (Java, C, C++, COBOL, VB, C#).

TeamCity has a powerful code duplication engine for .NET and java, that can effortlessly run as part of your build system.

I've used PMD's Copy-and-Paste-Detector and integrated it into CruiseControl by using the following wrapper script (be sure to have the pmd jar in the classpath).

Our check runs nightly. If you wish to limit output to list only files from the current change set you might need some custom programming (idea: check all and list only duplicates where one of the changed files is involved. You have to check all files because a change could use some code from a non-changed file). Should be doable by using XML output and parsing the result. Don't forget to post that script when it's done ;)

For starters the "Text" output should be ok, but you will want to display the results in a user-friendly way, for which i use a perl script to generate HTML files from the "xml" output of CPD. Those are accessible by posting them to the tomcat where cruise's reporting jsp resides. The developers can view them from there and see the results of their dirty hacking :)

It runs quite fast, less than 2 seconds on 150 KLoc code (empty lines and comments not counted in that number).

duplicatecheck.xml:

<project name="duplicatecheck" default="cpd">


<property name="files.dir" value="dir containing your sources"/>
<property name="output.dir" value="dir containing results for publishing"/>


<target name="cpd">
<taskdef name="cpd" classname="net.sourceforge.pmd.cpd.CPDTask"/>
<cpd minimumTokenCount="100"
language="cpp"
outputFile="${output.dir}/duplicates.txt"
ignoreLiterals="false"
ignoreIdentifiers="false"
format="text">
<fileset dir="${files.dir}/">
<include name="**/*.h"/>
<include name="**/*.cpp"/>
<!-- exclude third-party stuff -->
<exclude name="boost/"/>
<exclude name="cppunit/"/>
</fileset>
</cpd>
</target>

Finding "identical" code snippets is relatively easy, there are existing tool that already do this (see other answers).

Sometimes it's a good thing, sometimes it's not; it can bog down development time if done at a too fine "level"; i.e. trying to refactor so much code, you loose your goal (and probably bust your milestones and schedules).

What is harder is to find multiple function/method that do the same thing but with different (but similar) inputs and/or algorithm without proper documentation.

If you have to two or different methods to do the same thing and the programmer try to fix one instance but forget (or does not know they exist) to fix the other ones, you will increase the risk to your software.

duplo appears to be a C implementation of the algorithm used in Duploc. It is simple to compile and install, and while the options are limited it seems to more or less work out-of-the-box.

Well, you can run a clone detector on your source code base every night.

Many clone detectors work by comparing source lines, and can only find exact duplicate code.

CCFinder, above, works by comparing language tokens, so it isn't sensitive to white space changes. It can detect clones which are variants of the original code if there only single token changes (e.g, change a variable X to Y in the clone).

Ideally what you want is the above, but the ability to find clones where the variations are allowed to be relatively arbitrary, e.g., replace a variable by an expression, a statement by a block, etc.

Our CloneDR clone detector does this for Java, C#, C++, COBOL, VB.net, VB6, Fortran and a variety of other languages. It can be seen at: http://www.semdesigns.com/Products/Clone/index.html

As well as being able to handle multiple languages, CloneDR engine is capable of handling a variety of input encoding styles, including ASCII, ISO-8859-1, UTF8, UTF16, EBCDIC, a number of Microsoft encodings, and (Japanese) Shift-JIS.

The site has several clone detection run example reports, including one for C++.

EDIT Feb 2014: Now handles all of C++14.

Same (http://sourceforge.net/projects/same/) is extremely plain, but it works on text lines instead of tokens, which is useful if you're using a language that isn't supported by one of the fancier clone finders.

There is also Simian which supports Java, C#, C++, C, Objective-C, JavaScript...

It's supported by Hudson (like CPD).

Unless you're an open source project, you must pay for Simian.

These Debian packages seem to do something along these lines:

P.S. There ought to be a debtags tag for all tools related for finding [near] duplication. (But what would it be called?)

ConQAT is a great tool which suports C++ code analysis. Can find duplicates ignoring whitespace. Has extreamly handy gui and console interfaces. Because of it's flexibility it is not an easy to to setup. I've found this blog post very useful for setting up c++ project.

You can use our SourceMeter tool for detecting code duplication. It is a command line tool (very similar to compilers), so you can it easily integrate into continuous integration tools, like CruiseControl your mentioned, or Jenkins.