Duplicate Code Search Plugin for QtCreator

image

Duplicate code complicates the introduction of changes, understanding of the source texts and their further maintenance. In order to avoid duplication, as well as to evaluate the quality of the code and its refactoring, some IDEs have built-in tools for finding duplicate code fragments. Plugins are written for other IDEs. However, for the QtCreator development environment, so far there have been no built-in tools or retry search plugins.
The article describes two solutions to the automatic duplicate search task in this IDE: using a third-party utility integration tool and using a developed plug-in that I hope will be useful to C ++ programmers using QtCreator.

1 Integration of a third-party duplicate code retrieval utility


The integration mechanism of third-party utilities is a powerful means of expanding the functionality of QtCreator. The setup process is described in detail in the manual . The simian console utility - Similarity Analyzer , which will also be used in the plugin, was chosen as a duplicate search tool . To integrate it into QtCreator, you must perform the following steps:
1. Install the application.
Windows:
For Windows systems, we will use an exe application ( download page from the author’s site . Unpack the archive with the application in the directory you need (for example, D: / Development / Simian).
Unix-based:
To run on Unix systems (supporting JREs) simian is also implemented as a Java application. For some Unix systems, simian can be installed from a package system (for example, for Arch Linux ). If this application does not have packages in the system, then you need to download the archive with the application from the link , unpack it into the necessary directory (for example / usr / share / java / simian /), then create the file “simian in the PATH directory (for example / usr / bin) "Containing the application startup script:

#!/bin/bash
java -jar /usr/share/java/simian/simian.jar $@

Finally, this file must be set to execute.
2. Adding an external tool to QtCreator
On the External Tools tab of the Environment settings section, add a new category and utility ( Add Category, Add Tool ), an example is shown in the screenshot below.


In the Executable field, specify the path to the executable file of the program (or in the case of Unix-based systems, enter “simian”), in the Arguments field, specify the startup arguments:
-failOnDuplication- -includes = ** / *. cpp -includes = ** / *. h -includes = *. cpp -includes = *. h

In the Working Directory field, specify the template for the directory of the current project:
% {CurrentProject: Path}

With these settings, the utility will search for duplicates in all files with the extension .cpp and .h in the project directory and all subdirectories.
Now a new tab has appeared in the External Tools menu.


When you select the Check menu item , the simian utility starts and its output is redirected to the message panel, and information about the found duplicates (number, files, lines) is displayed. If you use the option in the startup arguments
-reportDuplicateText +
, then duplicate code will also be displayed. A complete list of simian launch options is available at .
But this solution only automated the launch of the utility. To open files with repetitions and to search for the necessary lines it was necessary "manually". And I wanted it to be like in other plugins and IDEs: double-clicking on an entry opens the source file, it jumps to the desired line and the repeat text is highlighted. Then it was decided to write a plugin.

2 Duplicate Code Search Plugin for QtCreator


I had no experience with plug-in development, and TODO Plugin articles for QtCreator and the Qt Creator Extensions System provided indispensable help . I found the code to open the file in the editor and go to the desired line in the source code of the ToDo plugin. In general, with the documentation for writing plugins for QtCreator, things are quite bad. I had a relatively long search for a solution to the problem of highlighting the text of the replay. Found the answer in the source code of the plug-in for assessing the degree of coverage by Code Coverage tests . In my opinion, in writing a plug-in for QtCreator, the main help is to study the source. In the plugin gallery on the Qt Project websiteLinks to most third-party plugins have been compiled. Incomprehensible questions can also be asked in the irc channel # qt-creator on freenode.net. If you have an idea for a plugin, be sure to try to implement it. Writing plugins is an exciting experience.

2.1 Assembly and installation of the plugin

2.1.1 Getting Sources and Building QtCreator

We will need the source code of QtCreator to build both the IDE itself and the plugins for it. It is also assumed that you already have Qt, QtCreator installed and all the necessary system variables configured. The procedure for obtaining sources and assemblies is described in detail in the article Building Qt Creator from git . After successfully completing this step, we will have 2 directories: qt-creator, in which the sources are located, and qt-creator-build, where the assembled QtCreator is located.
A small digression:
Thus, we will now have two QtCreator'a - one of the composition of the installed Qt distribution (we will call it "installed") and one - assembled from the sources (let's call it "assembled"). I’ll make a reservation right away that in the future we will build the plugin specifically for the “assembled” creator. The fact is that if you try to use the binary files of the plugin compiled for a newer version of QtCreator (obtained from the source), then a dependency resolution error will occur.


In this case, the Core and ProjectExplorer plugins must have the same version as QtCreator. It’s also worth noting that the QtCreator from the distribution is built using the Microsoft Visual Studio C ++ compiler . Thus, in order to build a plug-in for QtCreator installed with Qt, you need to get the source code for a specific revision of QtCreator ( FromRevision linein the About window), a Qt version for MSVC is also required. I will not further describe the assembly for QtCreator compiled by the MSVC compiler and I assume that you will collect (and will use in the future) the latest version of QtCreator from the source. If you are developing a plugin, then you will also need two QtCreator assemblies (not necessarily different as above, you can have two of them the same). In one, a plugin is developed and assembled, and in another it is copied and tested. Hint: when developing a plug-in, when it gathers in the window that appears, select the QtCreatora exe file in which the plug-in is tested, then QtCreator with the plug-in will start automatically.


However, let's get back to the plugin.

2.1.2 Getting the sources and building the Simian plugin

Sources of the developed plugin are available at Sourceforge.net: QtCreator Similarity Analysis Plugin . You can download the archive with the sources or download them from the git repository. After that, open the project (simian.pro) in the “installed” QtCreator. In the simian.pro project file, you will need to change two lines indicating the path to the source directory and the QtCreator assembly on your computer:
## set the QTC_SOURCE environment variable to override the setting here
QTCREATOR_SOURCES = $$(QTC_SOURCE)
isEmpty(QTCREATOR_SOURCES):QTCREATOR_SOURCES=D:/Sources/QtCreator/qt-creator
## set the QTC_BUILD environment variable to override the setting here
IDE_BUILD_TREE = $$(QTC_BUILD)
isEmpty(IDE_BUILD_TREE):IDE_BUILD_TREE=D:/Sources/QtCreator/qt-creator-build

Alternative way: set the system variables QTC_SOURCE and QTC_BUILD .

We assemble the release version of the plugin (Ctrl + R), which, after a successful assembly phase, is copied to
D: \ Sources \ QtCreator \ qt-creator-build \ lib \ qtcreator \ plugins \ SnaSoftware

The next line in the simian.pro project file is responsible for copying the plugin to the SnaSoftware folder .
PROVIDER = SnaSoftware

In this case, “Provider” means “plugin provider”. If you comment out this line, the plugins will be copied to the QtProject directory, which in my opinion is not entirely correct (the plugins that are part of QtCreator are located in this directory). Therefore, as a value for PROVIDER , my nickname on Habré was used.
Now you can try the plugin in business.

2.2 Using the plugin

2.2.1 Initial Setup and Startup

Before you set the plugin on folders with your projects in the settings, you must specify the path to the simian utility executable file .
In Windows, for this, in the settings dialog box ( Tools-> Options ) in the Simian section that appears, select the utility exe file using the file selection dialog ( Browse ... button in the Simian Executable Path area . You can also enter the path to the program directory simian into the PATH system variable, then in the Executable field you can simply specify the name of the exe file without an extension (for example, for convenience of launching in the console, I renamed the simian-2.3.34.exe file to simian.exe, included the path to the program in the PATH variable and in the text field I use the text simian ).
For Unix-based systems, if you have already completed the steps to install the simian utility from section 1, you will also just need to enter the name of the executable script “simian” (I remind you that it must be in the PATH directory).
After saving the settings ( Ok or Apply button ), the plugin can be used. To do this, open an existing or create a new project and start the search for repetitions. The search is launched either by selecting the menu item Tools-> Find similarities that appeared after installing the pluginor by clicking the button on the Simian panel tab . If you do not make changes to the application settings, then it starts with the default settings.
To test the plug-in, we will create a new Qt-project GUI, in the header file of which we will declare 2 functions and in their implementation we will write the same code consisting of 6 (since the default value of the Threshold setting is 6) of the same lines of code, for example:
    int one;
    int two;
    int three;
    int four;
    int five;
    int six;

Now in the QtCreator menu, select the action Tools-> Find similarities (in this case, the created project must be active). After analyzing the sources, a panel for outputting the results of the plug-in (Output Panel) will appear, in which there will be links to files in which duplicates are found, clicking on which these files will open in the editor, and fragments of repeats are highlighted in color:


The plugin allows you to search not only for exact matches of fragments, but also to recognize various kinds of fragments in fragments. To do this, you need to additionally configure duplicate code search parameters.

2.2.2 Advanced Setup

Duplicate code search settings are in the Simian Behavior group :

When changing the settings in the Options dialog box and then saving them in the directory of the current project, a simian.ini settings text file is created . If the default settings are used for the current project, this file is not created. Although this approach adds one extra file to the project directory, they solve the problem of portability of settings by simple copying (the next time the settings window is opened, the plugin checks for the presence of this file and loads the settings from it, or it loads the default settings if there is no such file).
The plugin allows you to adjust the severity of the search. Very rarely, copied text remains unchanged in the future: the values ​​of strings, digital constants, and variable names are modified. However, the code structure usually remains unchanged. It is possible to determine the optimal settings for a specific project or requirements for the quality (permissible repeatability) of the code by changing options, at the same time settings with a minimum Threshold value (2 lines) and all options enabled (which allows varying constants when searching for repetitions, are located on one pole) identifiers and etc.), and on the other with the most acceptable Threshold valueand disabled settings options (which corresponds to the exact syntactic match of the code so that it is considered a repeat). The options used and their description are presented in the table:
OptionDescription
ThresholdHow many lines must match to count a fragment as a repeat (2 to 99).
Ignore character caseCompare case-insensitive character constants ('A' is equivalent to 'a')
Ignore charactersIgnore character constants completely ('A' is equivalent to 'Z')
Ignore string caseCompare case insensitive strings (“foo” is equivalent to “FOO”)
Ignore stringsIgnore the contents of the lines (“foo” is equivalent to “bar”)
Ignore numbersIgnore the value of numeric constants (1 equals 2.5)
Ignore identifier caseCompare case-insensitive identifier names (int foo; equivalent to int FOO;)
Ignore identifier caseIgnore identifier names (int foo; equivalent to int bar;)
Ignore modifiersIgnore modifiers (public, private, static, etc)
Ignore curly bracesIgnore braces
Ignore overlapping blocksIgnore overlapping blocks
Balance square bracketsInclude square brackets when wrapping strings
Balance parenthesesTake parentheses into line breaks

Of the settings presented, it’s personally not entirely clear to me: Ignore overlapping blocks , Balance square brackets and Balance parentheses , so I will allow myself to leave their understanding as a homework by the reader.

Conclusion


In programming, however, as in any professional activity, it is always necessary to develop. At the same time, in my opinion, it is worth not only studying new technologies, design patterns or programming techniques, but also improving your instrumental tools. Sometimes it’s enough to take a look at your favorite IDE and compare it with another one (even if it is intended for a different programming language). Having noticed a new and interesting functionality for yourself, it might be worth looking for it among existing plugins, and you might want to implement something yourself. I hope that over time there will be good documentation on the QtCreator plugin system and then we will have even more convenient and useful tools at our disposal.
The plans for the development of the plugin:
1. Build and test the plugin under Unix-based systems. The article describes only the procedure for Unix-systems, which "in theory" should work, but has not been tested in practice.
2. Extension of the interface functionality (the “collapse-expand the list” buttons, ordering by the number of files / lines of repetitions, setting the color for highlighting text in the settings, etc.).
3. Adding html file generation with report on inspection results.
4. Adding other duplicate code search utilities with a choice of which ones to use.

Please write all wishes, remarks and error messages either in the ticket system or in the Feature Request / Bug Report forum section .
UPD: brought the screenshot and text of the test case in accordance with the text of the article (see discussion in the comments)

Also popular now: