Left Accent
UAB CIS Header

ATL Use Case - CoCloRep: A DSL to Represent Code Clones
ATL Logo

This work presents a preliminary investigation into a representative Domain-Specific Language (DSL) for code clones. The objective is to perform analysis and suggest refactoring opportunities on the clones that are represented by the DSL.

Keywords

Clone Detection, Clone Analysis.

Overview

In this work, we develop a DSL that can represent both exact and near exact clones of a subset of the Java programming language. Once the clones have been represented, mechanisms can be developed to analyze these clones for the purpose of refactoring to reduce the number of clones in the program. With this in mind, the DSL is divided into three sections: clone groups, clone instances, and commands. An example instance of the DSL and the clones that it represents is given in Figure 1. The DSL is described below.


Figure 1: Example DSL instance representing clones

Clone Groups contain the main representation of clones. Parameterized clones (clones that differ only in variable names) are represented by passing the variable names as parameters into the clone group. In the above example, two variable names are passed as parameters into the clone group. These parameters replace "placeholder" variables (ie. variables starting with a '$') in the source code in the clone group.

To represent near exact clones (clones that differ in the addition or deletion of some statements), the source code in the clone group contain "boxes" (ie. marked by "{{" and "}}") that can be replaced by additional code. The example in Figure 1 contains one box called "section."

Clone Instances represent each clone that is part of a clone group. In the example above, the three clones on the left are represented by the clone instances "r," "s," and "t." Each instance provides the actual variable names that will be passed to the clone group and any additional code that should be placed in designated boxes in the clone group source code. Clone instance "t" contains some additional code that will be placed in the "section" box.

Clone Commands perform specific functions on the clones that are represented by the clone groups and instances. The variables command generates a listing of all variables in the clone group, which are categorized based on whether they are declared and whether their value changes in the group. An example output for is given in Figure 2. Notice that the variable i is only in clone instance t. The three "expand" commands in the example above will regenerate the clones' original source code. This command would not be used for analysis per se. It was an initial command used to test the process of performing a task on the clone representation. The commands are placed in a separate source file from the groups and instances. This will allow the representation of the clones to be separated from any subsequent analysis and management code.


Figure 2: Output of variables command

In Figure 3, a megamodel of the process is displayed. Initially the code clones and commands are injected from the EBNF technical space (on the left) to the Model-Driven Engineering (MDE) technical space to generate their respective models and used as source models (step 1). Model transformation can then be performed on both models (step 2). The transformation is based on the commands that are defined in the Commands model. The transformations will yield target models Variables and Expand. The final step is the extraction of these target models from the MDE technical space to the EBNF technical space (on the right) to generate the desired output (step 3).


Figure 3: Megamodel

The DSL is developed in the AMMA platform. The concrete syntax of the DSL, which is represented by Code Clones and Commands is implemented in TCS. The abstract syntax of the DSL is specified in KM3.

Figure 4 provides another example of an instance of the DSL where three function-level clones are represented. Similar to the first example, two clones are identical with only variable names that are different. The last clone is similar with one extra line in the function.


Figure 4: Another example of function-level clones representation

Related Use Cases

None at the current time.

References

Tairas, R., Liu, S., Jouault, F., and Gray, J. "CoCloRep: A DSL for Code Clones," In Proceedings of the International Workshop on Software Language Engineering (ATEM), International Conference on Model Driven Engineering Languages and Systems (MoDELS), Nashville, TN, October 2007. [ Paper ]

Download

Complete scenario

All files for CoCloRep (metamodels, examples, transformations).

Clones metamodel

Full Clones metamodel specified in KM3.

Commands metamodel

Full Commands metamodel specified in KM3.

Variables metamodel

Full Variables metamodel specified in KM3.

Expand metamodel

Full Expand metamodel specified in KM3.

Variables transformation

Full Variables transformation specified in ATL.

Expand transformation

Full Expand transformation specified in ATL.
This project is supported by NSF grant CPA-0702764

Accent Right