Repository navigation
[Query] Code Clones
Generated from 'wiki-query.ts' on 2026-11-21, 19:58:49 UTC (v2.17.9), do not edit directly.
Code Clones Query [overview]
Finds code clones in all analyzed files with a configurable selection of AST, token, and fingerprint based algorithms.
This query is requested with the type code-clones.
Run in the REPL: :query @code-clones [baxter|gst|winnowing ...] [<code>]
Two functions that compute the same thing under different names are clones of each other, and the query finds such pairs. Given this R code:
normalize <- function(v) {
m <- mean(v)
(v - m) / sd(v)
}
scale_it <- function(values) {
mu <- mean(values)
(values - mu) / sd(values)
}The following query reports them:
[ { "type": "code-clones", "algorithms": [ "gst", "winnowing" ] } ]Results (prettified and summarized):
Query: code-clones (5 ms)
╰ 2 clone pairs in 1 file
╰ [gst 1.00] normalize <input 0>:1.14-4.1 ~ scale_it <input 0>:5.13-8.1
╰ [winnowing 1.00] normalize <input 0>:1.14-4.1 ~ scale_it <input 0>:5.13-8.1
Show Detailed Results as Json
The analysis ran (including parsing and normalization and the query) within the generation environment.
The JSON contains the ids of the nodes in question (see the Interface wiki page).
{"code-clones":{".meta":{},"pairs":[{"algorithm":"gst","similarity":1,"containment":[1,1],"a":{"file":"<input 0>","id":23,"loc":[1,14,4,1,"<input 0>"],"name":"normalize"},"b":{"file":"<input 0>","id":48,"loc":[5,13,8,1,"<input 0>"],"name":"scale_it"},"matches":[{"a":[1,14,3,16],"b":[5,13,7,27]}]},{"algorithm":"winnowing","similarity":1,"containment":[1,1],"a":{"file":"<input 0>","id":23,"loc":[1,14,4,1,"<input 0>"],"name":"normalize"},"b":{"file":"<input 0>","id":48,"loc":[5,13,8,1,"<input 0>"],"name":"scale_it"},"matches":[{"a":[2,3,3,16],"b":[6,3,7,20]}]}],"stats":{"files":1,"units":{"gst":2,"winnowing":2},"comparisons":{"gst":1,"winnowing":1}}},".meta":{}}This query reports pairs of code fragments that are clones of each other, that is, code that was copied and possibly adapted. It runs over all files of the analysis, for example a whole folder or package, with three published algorithms:
| algorithm | compares | after |
|---|---|---|
baxter |
all AST subtrees above a mass threshold, hashed by structure (ignoring identifiers and constants) and compared with 2S / (2S + L + R)
|
Baxter et al., Clone Detection Using Abstract Syntax Trees, ICSM 1998 |
gst |
the token strings of functions or files with Greedy String Tiling | as in JPlag, see Prechelt et al., Finding Plagiarisms among a Set of Programs with JPlag, 2002 |
winnowing |
k-gram fingerprints selected by robust winnowing, reported with the matched passages | Schleimer et al., Winnowing: Local Algorithms for Document Fingerprinting, SIGMOD 2003, the algorithm behind MOSS |
Before the detection, the query presents the code in one of three perspectives, chosen with perspective:
-
syntaxshows the code as written. -
canonical(the default) rewrites syntactic sugar and newer R features into one core form. Pipes become calls,\(x)becomesfunction(x), right and equal assignments become<-,(e)and{e}becomee,x[["a"]]becomesx$a,TandFbecomeTRUEandFALSE, and a tailreturn(e)becomese. Every rewritten node keeps the id of the node it stems from, so all findings point to the original source. -
dataflowadditionally inlines temporaries that are used exactly once along the dataflow graph, sot <- f(x)followed byg(t)looks likeg(f(x)). It only inlines where this cannot change what the code computes, and it is the only perspective that needs the dataflow analysis.
With exportIndex, the query also returns the clone index of the analyzed files (their normalized ASTs).
A later query can pass such indices as against to search new code in a corpus of pre-analyzed projects without analyzing them again.
Use config to tune the thresholds of each algorithm, and maxPairs to limit the number of reported pairs, as n copies of the same function already form n(n-1)/2 pairs.
In code, passing the query on its own to FlowrAnalyzer::query returns its result directly:
const { pairs } = await analyzer.query({ type: 'code-clones', algorithms: ['gst', 'winnowing'] });
for(const { algorithm, similarity, a, b } of pairs) {
console.log(`${algorithm} (${similarity.toFixed(2)}): ${a.name ?? a.file} ~ ${b.name ?? b.file}`);
}Note
The linter offers the same detection as its code-clones rule, which is not active by default. In the REPL, :query @code-clones gst winnowing <code> runs the query with the given algorithms.
Implementation Details
The Code Clones Query is executed by executeCodeClonesQuery.
Currently maintained by Florian Sihler and Oliver Gerstl at Ulm University
Email | GitHub | Penguins | Portfolio
- 🧑💻 Developer Onboarding
- 💻 Setup
- 👓 Overview
- 🪟 Interfacing with flowR
- 🌋 Core
- 🧹 Testing & Linting (Benchmark Page)
⁉️ FAQ- ℹ️ Extra Information