Skip to content

Commit c55c9f5

Browse files
authored
Merge pull request github#12680 from jcogs33/jcogs33/metrics-query-refactor-top500
Java: test GeneratedVsManualCoverage query on top 500 JDK APIs
2 parents b0daceb + cc92936 commit c55c9f5

File tree

143 files changed

+2535
-64
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

143 files changed

+2535
-64
lines changed

java/ql/src/Metrics/Summaries/GeneratedVsManualCoverage.ql

Lines changed: 7 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -7,71 +7,15 @@
77
*/
88

99
import java
10-
import semmle.code.java.dataflow.FlowSummary
11-
import utils.modelgenerator.internal.CaptureModels
12-
13-
/**
14-
* Returns the number of `DataFlowTargetApi`s with Summary MaD models
15-
* for a given package and provenance.
16-
*/
17-
bindingset[package]
18-
private int getNumMadModeledApis(string package, string provenance) {
19-
provenance in ["generated", "manual", "both"] and
20-
result =
21-
count(SummarizedCallable sc |
22-
package = sc.asCallable().getCompilationUnit().getPackage().getName() and
23-
sc.asCallable() instanceof DataFlowTargetApi and
24-
(
25-
// "auto-only"
26-
sc.isAutoGenerated() and
27-
provenance = "generated"
28-
or
29-
sc.isManual() and
30-
(
31-
if sc.hasProvenance(["generated", "ai-generated"])
32-
then
33-
// "both"
34-
provenance = "both"
35-
else
36-
// "manual-only"
37-
provenance = "manual"
38-
)
39-
)
40-
)
41-
}
42-
43-
/** Returns the total number of `DataFlowTargetApi`s for a given package. */
44-
private int getNumApis(string package) {
45-
result =
46-
strictcount(DataFlowTargetApi dataFlowTargApi |
47-
package = dataFlowTargApi.getCompilationUnit().getPackage().getName()
48-
)
49-
}
10+
import GeneratedVsManualCoverageQuery
5011

5112
from
52-
string package, int generatedOnly, int both, int manualOnly, int generated, int manual, int non,
53-
int all, float coverage, float generatedCoverage, float manualCoverage,
54-
float manualCoveredByGenerated, float generatedCoveredByManual, float match
13+
string package, int generatedOnly, int both, int manualOnly, int non, int all, float coverage,
14+
float generatedCoverage, float manualCoverage, float manualCoveredByGenerated,
15+
float generatedCoveredByManual, float match
5516
where
56-
// count the number of APIs with generated-only, both, and manual-only MaD models for each package
57-
generatedOnly = getNumMadModeledApis(package, "generated") and
58-
both = getNumMadModeledApis(package, "both") and
59-
manualOnly = getNumMadModeledApis(package, "manual") and
60-
// calculate the total generated and total manual numbers
61-
generated = generatedOnly + both and
62-
manual = manualOnly + both and
63-
// count the total number of `DataFlowTargetApi`s for each package
64-
all = getNumApis(package) and
65-
non = all - (generatedOnly + both + manualOnly) and
66-
// Proportion of coverage
67-
coverage = (generatedOnly + both + manualOnly).(float) / all and
68-
generatedCoverage = generated.(float) / all and
69-
manualCoverage = manual.(float) / all and
70-
// Proportion of manual models covered by generated ones
71-
manualCoveredByGenerated = both.(float) / (both + manualOnly) and
72-
// Proportion of generated models covered by manual ones
73-
generatedCoveredByManual = both.(float) / (both + generatedOnly) and
74-
// Proportion of data points that match
75-
match = (both.(float) + non) / all
17+
modelCoverageGenVsMan(package, generatedOnly, both, manualOnly, non, all, coverage,
18+
generatedCoverage, manualCoverage, manualCoveredByGenerated, generatedCoveredByManual, match,
19+
"allApis")
7620
select package, generatedOnly, both, manualOnly, non, all, coverage, generatedCoverage,
7721
manualCoverage, manualCoveredByGenerated, generatedCoveredByManual, match order by package
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
private import semmle.code.java.dataflow.FlowSummary
2+
private import utils.modelgenerator.internal.CaptureModels
3+
private import TopJdkApis
4+
5+
/**
6+
* Returns the number of `DataFlowTargetApi`s with Summary MaD models
7+
* for a given package and provenance.
8+
*/
9+
bindingset[package, apiSubset]
10+
private int getNumMadModeledApis(string package, string provenance, string apiSubset) {
11+
provenance in ["generated", "manual", "both"] and
12+
result =
13+
count(SummarizedCallable sc |
14+
callableSubset(sc.asCallable(), apiSubset) and
15+
package = sc.asCallable().getCompilationUnit().getPackage().getName() and
16+
sc.asCallable() instanceof DataFlowTargetApi and
17+
(
18+
// "auto-only"
19+
sc.isAutoGenerated() and
20+
provenance = "generated"
21+
or
22+
sc.isManual() and
23+
(
24+
if sc.hasProvenance(["generated", "ai-generated"])
25+
then
26+
// "both"
27+
provenance = "both"
28+
else
29+
// "manual-only"
30+
provenance = "manual"
31+
)
32+
)
33+
)
34+
}
35+
36+
/** Returns the total number of `DataFlowTargetApi`s for a given package. */
37+
private int getNumApis(string package, string apiSubset) {
38+
result =
39+
strictcount(DataFlowTargetApi dataFlowTargApi |
40+
callableSubset(dataFlowTargApi, apiSubset) and
41+
package = dataFlowTargApi.getCompilationUnit().getPackage().getName()
42+
)
43+
}
44+
45+
/** Holds if the given `callable` belongs to the specified `apiSubset`. */
46+
private predicate callableSubset(Callable callable, string apiSubset) {
47+
apiSubset in ["topJdkApis", "allApis"] and
48+
(
49+
if apiSubset = "topJdkApis"
50+
then exists(TopJdkApi topJdkApi | callable = topJdkApi.asCallable())
51+
else apiSubset = "allApis"
52+
)
53+
}
54+
55+
/**
56+
* Provides MaD summary model coverage information for the given `package`
57+
* on the given `apiSubset`.
58+
*/
59+
predicate modelCoverageGenVsMan(
60+
string package, int generatedOnly, int both, int manualOnly, int non, int all, float coverage,
61+
float generatedCoverage, float manualCoverage, float manualCoveredByGenerated,
62+
float generatedCoveredByManual, float match, string apiSubset
63+
) {
64+
exists(int generated, int manual |
65+
// count the number of APIs with generated-only, both, and manual-only MaD models for each package
66+
generatedOnly = getNumMadModeledApis(package, "generated", apiSubset) and
67+
both = getNumMadModeledApis(package, "both", apiSubset) and
68+
manualOnly = getNumMadModeledApis(package, "manual", apiSubset) and
69+
// calculate the total generated and total manual numbers
70+
generated = generatedOnly + both and
71+
manual = manualOnly + both and
72+
// count the total number of `DataFlowTargetApi`s for each package
73+
all = getNumApis(package, apiSubset) and
74+
non = all - (generatedOnly + both + manualOnly) and
75+
// Proportion of coverage
76+
coverage = (generatedOnly + both + manualOnly).(float) / all and
77+
generatedCoverage = generated.(float) / all and
78+
manualCoverage = manual.(float) / all and
79+
// Proportion of manual models covered by generated ones
80+
manualCoveredByGenerated = both.(float) / (both + manualOnly) and
81+
// Proportion of generated models covered by manual ones
82+
generatedCoveredByManual = both.(float) / (both + generatedOnly) and
83+
// Proportion of data points that match
84+
match = (both.(float) + non) / all
85+
)
86+
}

java/ql/test/ext/TopJdkApis/TopJdkApisTest.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import java
2-
import TopJdkApis
2+
import Metrics.Summaries.TopJdkApis
33

44
from string apiName, string message
55
where
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Generated automatically from java.awt.Container for testing purposes
2+
3+
package java.awt;
4+
5+
import java.awt.AWTEvent;
6+
import java.awt.AWTKeyStroke;
7+
import java.awt.Component;
8+
import java.awt.ComponentOrientation;
9+
import java.awt.Dimension;
10+
import java.awt.Event;
11+
import java.awt.FocusTraversalPolicy;
12+
import java.awt.Font;
13+
import java.awt.Graphics;
14+
import java.awt.Insets;
15+
import java.awt.LayoutManager;
16+
import java.awt.Point;
17+
import java.awt.event.ContainerEvent;
18+
import java.awt.event.ContainerListener;
19+
import java.beans.PropertyChangeListener;
20+
import java.io.PrintStream;
21+
import java.io.PrintWriter;
22+
import java.util.EventListener;
23+
import java.util.Set;
24+
25+
public class Container extends Component
26+
{
27+
public Component add(Component p0){ return null; } // manual summary
28+
public void add(Component p0, Object p1){} // manual summary
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Generated automatically from java.awt.Insets for testing purposes
2+
3+
package java.awt;
4+
5+
import java.io.Serializable;
6+
7+
public class Insets implements Cloneable, Serializable
8+
{
9+
protected Insets() {}
10+
public Insets(int p0, int p1, int p2, int p3){} // manual neutral
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Generated automatically from java.io.BufferedReader for testing purposes
2+
3+
package java.io;
4+
5+
import java.io.Reader;
6+
import java.util.stream.Stream;
7+
8+
public class BufferedReader
9+
{
10+
protected BufferedReader() {}
11+
public BufferedReader(Reader p0){} // manual summary
12+
public String readLine(){ return null; } // manual summary
13+
}

0 commit comments

Comments
 (0)