Skip to main content

Detecting Code Clones

Requirements:

  • CODESYS Static Analysis is installed.

  • A CODESYS project is open. The project includes the same code block in at least two programming objects.

  1. Click Build → Static Analysis → Detect Clones .

    The Clone detection results view opens.

  2. Click _san_icon_result.png Results.

    The detected code clones are displayed as a tree view in a window.

    If, in addition to identical code clones, there are also code clones with deviations, then these child nodes are highlighted in color in the tree view.

  3. In the tree view, select two code clones of a child node and click the Show selected clones button.

    The two programming objects containing this duplicated code are opened in the editor and displayed in the upper part of the view.

    The identical code clones are highlighted in light yellow and the code clones with deviations, such as the variable names ivar3 and ivarx in this example, are highlighted in red.

    _san_img_example_codeclone.png
  4. To create a function from the identical code clone of the programming object POU_1, first double-click in the tree view on the row with the code clone of Object POU_1.

    The programming object POU_1 opens in the editor, and in the implementation part the four rows are highlighted which contain the duplicated code.

  5. When you click the Extract function command in the context menu of the highlighted code block, the code block is extracted from POU_1 and a new method is created from it. The code block in the implementation part of POU_1 is replaced automatically by the call of the method.

    In the second programming object, you have to remove the code duplicate manually and replace the call of the method.

    Tip

    In a later version, the functionality will be extended so that the duplicated code block is extracted automatically from both programming objects, and it can be replaced by the correct function call.