views:

1823

answers:

11

We have a fairly large code base, 400K LOC of C++, and code duplication is something of a problem. Are there any tools which can effectively detect duplicated blocks of code?

Ideally this would be something that developers could use during development rather than just run occasionally to see where the problems are. It would also be nice if we could integrate such a tool with CruiseControl to give a report after each check in.

I had a look at Duploc some time ago, it showed a nice graph but requires a smalltalk environment to use it, which makes running it automatically rather difficult.

Free tools would be nice, but if there are some good commercial tools I would also be interested.

+13  A: 

Simian detects duplicate code in C++ projects.

Simon Steele
+1  A: 

Look at the PMD project.

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

Andy Lester
I thought that PMD was just for Java, but I now see that CPD (which is part of PMD) can be used for C++ as well.
David Dibben
+1  A: 

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

bk1e
Thanks for this link. I will definitely look at it. What is even better is that there is a Japanese version (all other developers on the project apart from me are Japanese)
David Dibben
A: 

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

ripper234
Neither .Net or Java is C++, so while this may be effortless to run, it is also fruitless.
Tom
+4  A: 

I've used PMD's Copy-and-Paste-Detector (http://pmd.sourceforge.net/cpd.html) 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>

+3  A: 

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.

Max
... and as a practical matter, you aren't going to be able to detect that two pieces of code *do* the same thing if they are implemented differently. There's a Turing machine standing in your way.
Ira Baxter
"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."Right. And if they DO the same thing, they should be NAMED the same, since the name should describe why that code exists in the first place.So step one might be to make sure that all functions/methods are accurately named and documented. If the name truly describes what it does, similarities and identities will quickly become obvious.
mickeyf
+3  A: 

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.

benno
+1  A: 

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, 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++.

Ira Baxter
A: 

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.

Sean McMillan
A: 

In most cases you won't have too many instances of "identical" code in a real life system. "Cut'n paste" development involves in most cases renaming of variables, using different constant values, inserting/deleting code and re-formatting of the original code snippet. If you take all these into account you would be surprised to find out how much duplication could exist. The tool I recommend if you want the job properly done is the Source Code Duplication Detector (SolidSDD). It works on C/C++, Java and C#. It targets not only developers, but also architects and managers, so it can be handy at more places in the organization.

Lucian Voinea
A: 

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.

Wernight