📜 ⬆️ ⬇️

Duplicate code search plugin for QtCreator

image

Duplicate code makes it difficult to make changes, understanding the source texts and their further maintenance. In order to avoid duplication, as well as to assess the quality of the code and its refactoring, some IDEs have built-in tools for searching for duplicate code fragments. Plugins are written for other IDEs. However, for the QtCreator development environment, to date, there has been no built-in tools or duplicate search plug-ins.
The article describes two solutions to the problem of automatically finding duplicates in this IDE: using the integration tool of third-party utilities and using the developed plugin, which I hope will be useful for C ++ programmers using QtCreator.

1 Integration of a third-party repeater code search utility


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

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

In conclusion, this file must be set to execute permissions.
2. Adding an external utility (external tool) to QtCreator
On the External Tools tab of the Environment settings section, we add a new category and the 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 launch arguments:
-failOnDuplication- -includes = ** / *. cpp -includes = ** / *. h -includes = *. cpp -includes = *. h

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

With these settings, the utility will search for repetitions in all files with the .cpp and .h extension in the project directory and all subdirectories.
Now there is a new tab in the External Tools menu.
')

When the Check menu item is selected, the simian utility is launched and its output is redirected to the message panel, and information about duplicates found (number, files, lines) is displayed. If used in the launch arguments option
-reportDuplicateText +
then duplicate code will be output. A complete list of simian launch options can be found at .
But this solution only automated the utility launch. It was necessary to open files with repetitions and search for the necessary lines "manually". But I wanted it to be like in other plugins and IDE: double clicking on the record opens the source file, goes to the necessary line and the text of the repetition is highlighted. Then it was decided to write a plugin.

2 Duplicate code search plugin for QtCreator


I didn’t have experience of developing plug-ins and the Ttdo Plugin for QtCreator and the System of Extensions Qt Creator provided indispensable help. I found the code for opening a file in the editor and switching to the right line in the ToDo plug-in source code. In general, with the documentation on writing plugins for QtCreator, things are pretty bad. It took a relatively long time to look for a solution to the task of highlighting the replay text. The answer is found in the source code of the plug-in to assess the degree of coverage with Code Coverage tests. In my opinion, in writing a plugin for QtCreator, the main help is the study of source codes. The plugin gallery on the Qt Project website contains links to most third-party plug-ins. Unclear questions can also be asked in the # qt-creator irc channel 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 Building and Installing the Plugin

2.1.1 Getting sources and building QtCreator

We will need the QtCreator sources for building both the IDE itself and the plug-ins for it. It also assumes that you have already installed Qt, QtCreator and all the necessary system variables are configured. The procedure for getting sources and builds is described in detail in 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 - one of the installed Qt distribution (we will call it “installed”) and one - assembled from source (let's call it “assembled”). At once I will make a reservation that in the future we will assemble the plugin 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 sources), then an error in resolving dependencies will occur.


In this case, the Core and ProjectExplorer plugins should have the same version as QtCreator. Also note that QtCreator from the distribution is built using Microsoft Visual Studio C ++ compiller . Thus, to build a plugin for QtCreator installed along with Qt, you need to get the QtCreator source code of a specific revision (the FromRevision line in the About window), you also need the Qt version for MSVC. I will not further describe the QtCreator build compiled by the MSVC compiler and assume that you will build (and continue to use in the work) the latest QtCreator version from source. If you are developing a plugin, you will also need two QtCreator assemblies (not necessarily different as above, you can have two of the same). In one, a plugin is developed and assembled, and in another it is copied and tested. Hint: when developing a plugin, when it comes together in the window that appears, select the exe file of QtCreatora, in which the plugin is tested, then QtCreator with the plugin will start automatically.


However, let's go back to the plugin.

2.1.2 Getting the sources and building the Simian plugin

Sources of the developed plugin are located on Sourceforge.net: QtCreator Similarity Analysis Plugin . You can download the archive with source codes or download them from the git-repository. After that, open the project (simian.pro) in the “installed” QtCreator. In the project file simian.pro you will need to change two lines indicating the path to the source directory and the QtCreator build 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 

Alternatively, set the system variables QTC_SOURCE and QTC_BUILD .

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

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

In this case, “Provider” is set to “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 (plugins that are part of QtCreator are located in this directory). Therefore, my nickname on Habré is used as the PROVIDER value.
Now you can try the plugin in action.

2.2 Using the plugin

2.2.1 Initial Setup and Startup

Before setting the plugin on the folders with your projects in the settings, you must specify the path to the executable file of the simian utility.
In Windows, to do this, in the settings dialog ( 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 system variable PATH, then in the Executable field you can simply specify the name of the exe-file without an extension (for example, for convenience of running in the console, I renamed the file simian-2.3.34.exe to simian.exe , included the path to the program to the PATH variable and text field using simian text).
For Unix-based systems, if you have already completed the steps to install the simian utility from section 1, you will also need to simply enter the name of the executable script “simian” (I remind you that it should be located in the PATH directory).
After saving the settings (the 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. Starting a search is performed either by selecting the Tools-> Find similarities menu item that appears after installing the plug-in or by clicking a button on the tab of the Simian panel. If you do not make changes in the application settings, then it starts with the default settings.
To check the operation of the plugin, we will create a new QI GUI project, in the header file of which we will declare 2 functions and in their implementation we will write the same code consisting of 6 (as the default Threshold setting value is 6) of the same lines of code, for example:
  int one; int two; int three; int four; int five; int six; 

Now select the Tools-> Find similarities action in the QtCreator menu (the created project must be active). After analyzing the sources, the Output Panel will display the output panel, in which there will be links to files in which repetitions are found, by clicking on which these files will open in the editor, and repetition fragments will be highlighted in color:


The plugin allows you to search not only for the exact correspondences of the fragments, but also to recognize variations of different kinds in fragments. To do this, you need additional settings for the search for duplicate code.

2.2.2 Advanced Setup

Settings for duplicate code search are in the Simian Behavior group:

When you change the settings in the Options dialog box and then save them in the current project directory, a text file of settings simian.ini is created . If the current project uses default settings, this file is not created. Although this approach adds one extra file to the project directory, it solves the problem of portability of settings by simple copying (the next time you open the settings window, the plugin checks for the presence of this file and loads the settings from it, or loads the default settings if there is no such file).
The plugin allows you to customize the severity of the search. Very rarely the copied text remains unchanged in the future: the values ​​of strings, numeric constants, variable names are modified. However, the structure of the code usually remains unchanged. The optimal settings for a specific project or quality requirements (admissible repeatability) of a code can be determined by changing the options, with settings with the minimum Threshold value (2 lines) and all included options (which allows for searching for repetitions the variation of constant values, identifiers and others), and on the other - with the most acceptable Threshold value and disabled settings options (which corresponds to the exact syntactic coincidence of the code so that it is considered a repetition). The options used and their description are presented in the table:
OptionDescription
ThresholdHow many lines must match to repeat a fragment (from 2 to 99).
Ignore character caseCompare case-insensitive constants ('A' is equivalent to 'a')
Ignore charactersFully ignore character constants ('A' is equivalent to 'Z')
Ignore string caseCompare strings without case ("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 is equivalent to 2.5)
Ignore identifier caseCompare case-less 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 line wrapping
Balance parenthesesConsider parentheses when moving lines

Of the presented settings, I was not entirely clear to me: Ignore overlapping blocks , Balance square brackets and Balance parentheses , so I will allow myself to leave their understanding of the reader as a home task.

Conclusion


In programming, as well as in any professional activity, it is always necessary to develop. At the same time, in my opinion, it is worthwhile not only to learn new technologies, design patterns or programming techniques, but also to improve your instrumental tools. Sometimes it is enough to look at your favorite IDE and compare it with another (even if intended for another programming language). Having noticed for yourself a new and interesting functionality, it is probably worth looking for it among existing plug-ins, but something you might want to implement yourself. Hopefully, 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. In the next article I will try to make a review of the most interesting and useful of existing plug-ins (which I met while writing the plug-in described above).
Plans for the development of the plugin:
1. Build and test the plug-in under Unix-based systems. The article describes only the order of action for Unix-systems, which "in theory" should work, but in practice has not been tested.
2. Expansion of the interface functionality (the “minimize-expand list” buttons, ordering by the number of files / rows of repetitions, setting the text selection color in the settings, etc.).
3. Adding the generation of the html file with the inspection report.
4. Adding other duplicate code search utilities with a choice of used ones.

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

Source: https://habr.com/ru/post/213785/


All Articles