CloneFinder version 2.02 is a low cost refactoring tool that quickly finds duplicated code within C, C++, Java and C# programs. CloneFinder is a proven tool that goes beyond simple searching and pattern matching to find blocks of code that have the same intents but that reside in more then one place.
See below for instructions for using CloneFinder.
- Fixed bug where CloneFinder was not reading project files with a .clone file extension properly. The system was erroneously deleting the last character of file names from the file and then complaining that it couldn’t find any files to analyze.
- Added the –eula command line option that will print out the End User License Agreement
- CloneFinder can now report what it finds via a series of .html files.
- New –wait command pauses the display of information until the users presses return, allowing error messages and other information to be viewed if CloneFinder is started in a new window.
- Instructions for use with Borland C++ Builder are now included.
- Substantial improvements have been made in CloneFinder’s ability to recognize the similarity in blocks of code that have the same intent but that have major differences.
- CloneFinder can now be told to ignore a section of code when it does it’s analysis with the following text added within a comment to the code: --cf:ignore. This allows the user who has identified a section of code as not being a problem to rerun CloneFinder without the code being reported each time.
- CloneFinder now supports the finding of clones with Java and C# in addition to the previous ability to analyze C and C++ programs.
- CloneFinder can now be ran from within IDE’s such as Microsoft Visual Studio and EMACS. These IDE’s allow the user to navigate to the lines of code reported by CloneFinder, which make it substantially easier to use CloneFinder.
- New command (.ext) allows CloneFinder to recognize file extensions other then the ones it would normally identify as source code (currently .c, .cpp, .cc, .cxx, .cs and .java).
- New commands (-d and –s) tell CloneFinder to analyze all source files found in a directory (-d) or a directory and all subdirectories (-s).
- CloneFinder can now read project files created by Microsoft Visual Studio (.dsp, .vcproj and .csproj) to determine which files it should analyze.
Windows (XP, 2000, NT, 98 etc)
After downloading and unzipping CloneFinder.zip (or CloneFinderDemo.zip) you will have three files, this ReadMe.html, Eula.htm (the End User License Agreement) file and either CloneFinder or CloneFinderDemo depending on whether you have purchase the product or have just downloaded the demo version from the net.
To run CloneFinder from the command line it would probably be best to copy CloneFinder.exe (or CloneFinderDemo.exe) into a directory that is included in the path.
If you are going to use CloneFinder from an IDE such as Microsoft Visual Studio or Borland C++ Builder then copy CloneFinder to a directory that is easy for you to find and remember.
Using CloneFinder
Okay, now that you have CloneFinder what do you do with it?
That’s pretty simple. CloneFinder works like most compilers and can be ran from the command line or from within an IDE (instructions for using within Microsoft’s Visual Studio and Borland C++ Builder are included later). From a command line you simply invoke CloneFinder and pass as arguments the name of the source file or files that you want to analyze.
For instance, if CloneFinder were installed in a directory included in your path statement and you had a source file a.cpp that you wanted to analyze you could do so by issuing the following statement at the command prompt:
The results would be displayed as in the following:
CloneFinder Ver 2.02 Copyright © 2001, 2005 by Studio501, Inc
--- Module a.cpp
Analyzing files
*** Clone Grouping 1 ***
a.cpp(90): lines 90 through 133
a.cpp(151): lines 151 through 194
Finished.
In the example shown CloneFinder analyzed the file (Module) a.cpp and found that the code on lines 90 through 133 was repeated on lines 151 through 194. If more then one clone grouping were found it would be reported as “*** Clone Grouping 2 ***” and so on.
CloneFinder can also be used to find duplicated code in multiple files at once. There are actually many ways to instruct CloneFinder which files to scan the simplest being to just include each file name on the command line as in:
This would analyze the source in files a.cpp and b.cpp found in the current directory.
If the files were in directories other then the current directory then the complete path would need to be entered when specifiying the names of the files.
CloneFinder can also analyze all of the source files in a directory or all of the source files in a directory and all subdirectories. To do this use command options –d for files in one directory or –s for all files in a directory and all subdirectories under it.
For example if you wanted to analyze all of the source files found in c:\src\test then you could issue the following command
Or if you wanted to check all of the source files in the directory c:\src and all of the subdirectories found under c:\src such as c:\src\test then you could issue the following command
If you want to analyze multiple files in a directory, but not all of them, or if the files you wish to analyze are contained in different directories you can do so by creating a file that contains the names of the files you want to analyze each with the full path and each on a separate line. Name the file with a .clone file extension (such as MyProject.clone) and call CloneFinder with the –proj command as in
CloneFinder will then analyze each of the files and report out any clones found.
CloneFinder can also read project files created by Microsoft Visual Studio and Microsoft Visual Studio.Net such as in the following examples
Or
Or
CloneFinder has a couple of other command line options and these are described here:
-fast When passed as an option CloneFinder will skip some of the more time consuming analysis that it would normally do, such as looking for blocks of code that are similar to each other with the exception of additional lines of code. For example if a source file had the following two blocks of code in them:
Block 1:
for ( int x = 0 ; x < 10 ; x++ )
{
int a = ResultOfSomeFunction( x );
printf( “The contents of a are %d\n”, a );
DoSomething(a);
DoSomethingElse(a);
DoYetSomethingElse(a);
}
Block 2:
for ( int x = 0 ; x < 10 ; x++ )
{
int a = ResultOfSomeFunction( x );
DoSomething(a);
DoSomethingElse(a);
DoYetSomethingElse(a);
}
CloneFinder would report these two blocks as possible clones without the –fast option, but wouldn’t report them as clones with the –fast option.
-strict When passed as an option CloneFinder will require blocks of code to be much more similar to each other before they are reported as being clones. For instance CloneFinder would normally consider a string literal to be a string literal and would as long as the rest of two blocks of code were similar it would consider the two blocks of code to be clones of each other. With the –strict option CloneFinder would consider the string “Meyers More Effective C++” different then the string “Meyers Effective STL” and would be less likely to report the two blocks of code containing them as clones. CloneFinder might still consider blocks to be clones even with these differences under the –strict guideline if enough other similarities were found. The –strict command is only considered a guideline.
-small The –small option tells CloneFinder to report clones that are only a few tokens or lines line that are often rather trivial and usually not worth looking at. For instance consider the following, quite common, code:
Block 1:
for ( int x = 0 ; x < 5 ; x++ )
{
DoSomething(x);
}
Block 2:
for ( int z = 0 ; z < 8 ; z++ )
{
DoSomething(z);
}
CloneFinder would normally consider these two blocks of code too small to report, even though they are similar. With the –small option CloneFinder will report these two blocks of code to be clones.
CloneFinder will normally only analyze source code that has one of the following extensions:
.cpp
.c
.cc
.cxx
.cs
.java
If you use a file extension other then one of these you need to tell CloneFinder about it by using the –ext command. For instance if you use .ccc as a file extension you could instruct CloneFinder to analyze files of this type by issuing the command:
Which would instruct CloneFinder to analyze all of the files found in the file MyProject.clone that have any of the standard file extensions listed above, or the file extension .ccc
Even on Unix boxes CloneFinder treats file extensions case insensitively so that a file with the extension .CC would be considered the same as a file with the extension of .cc
You can instruct CloneFinder to not consider a block of code to be a clone by inserting the following text within a comment in your code:
--cf:ignore
This text must be in lower case for CloneFinder to recognize it, and when it does recognize it, no code that includes it will be reported as a possible clone. This will allow you to repeatedly run CloneFinder on a project and have CloneFinder report only clones that you haven’t already dismissed as not being a problem.
-html directoryname
The –html command tells CloneFinder to output html files that report the clones found. The argument to the command should be an existing directory that can be written to. Each time CloneFinder runs with this parameter it will overwrite any files in this directory that it has created before.
The files that it will create are named CloneFinderReport*.html
The file CloneFinderReport.html will be the file to view as it will have the main report and links to all of the other files created.
On Windows operating system CloneFinder will start the default web browser and point it to the main report file when the –html option is used.
-wait
The –wait command tells CloneFinder to wait for the user to press the <enter> key before exiting. This allows the user to view any error messages that might be displayed by CloneFinder. It is especially helpful if CloneFinder is started in a separate window that might disappear after CloneFinder is executed.
To use CloneFinder from within Visual Studio add an entry to the tools menu by using the Tools->Customize menu option and then the tools tab of the resulting dialog.
Add an entry called “CloneFinder” and in the command edit control enter the complete path of where you installed CloneFinder.
In the arguments edit control enter –proj $(FileDir)
And in the Initial directory enter $(FileDir)
Make sure that the “Use Output Window” check box is clicked and that the “Prompt for arguments” check box is not clicked.
This will tell Microsoft Visual Studio to have CloneFinder analyze the current project and use the output window for its results.
After this setup is done you can analyze your current project by selecting CloneFinder from the tools menu. The analysis of the project will then show up in the output window and you can use F4 to navigate through your code to view each of the clones reported.
To use CloneFinder from within Borland C++ Builder add an entry to the tools menu by using the Tools->Configure Tools menu option. In the Tool Properties dialog add the following:
|
Title: |
CloneFinder |
|
Program: |
The full path to the CloneFinder (or CloneFinderDemo) executable. |
|
Parameter: |
$SAVEALL –wait –html c:\html –d $PATH($EDNAME) |
Substitute c:\html with the name of a directory that CloneFinder can use to write html files.
This will tell Borland C++ Builder to save any source code that has been changed to disk and then execute CloneFinder with the following commands
-wait (CloneFinder will wait for the user to press <enter> before closing the command window that it uses. This will allow you to see any errors reported by CloneFinder and will help debug any problems)
-html c:\html (This tells CloneFinder to create html output files in the c:\html directory with information about the clones found. The directory named must already exist or CloneFinder will exit with an error.)
-d $PATH($EDNAME) (This command tells CloneFinder to analyze all of the source files found in the directory where the current file is located.
Visit our web site for updates and other information.