Skip to content

Commit 0993d36

Browse files
committed
Java: Add entity discard predicates
1 parent 58ad97c commit 0993d36

File tree

7 files changed

+136
-0
lines changed

7 files changed

+136
-0
lines changed

java/ql/lib/semmle/code/Location.qll

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module;
88

99
import FileSystem
1010
import semmle.code.java.Element
11+
private import semmle.code.java.Overlay
1112
private import semmle.code.SMAP
1213

1314
/** Holds if element `e` has name `name`. */
@@ -221,3 +222,16 @@ private predicate fixedHasLocation(Top l, Location loc, File f) {
221222
not hasSourceLocation(l, _, _) and
222223
locations_default(loc, f, _, _, _, _)
223224
}
225+
226+
overlay[local]
227+
private predicate discardableLocation(string file, @location l) {
228+
not isOverlay() and
229+
file = getRawFileForLoc(l) and
230+
not exists(@file f | hasLocation(f, l))
231+
}
232+
233+
/** Discard base locations in files fully extracted in the overlay. */
234+
overlay[discard_entity]
235+
private predicate discardLocation(@location l) {
236+
exists(string file | discardableLocation(file, l) and extractedInOverlay(file))
237+
}

java/ql/lib/semmle/code/java/Expr.qll

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module;
77
import java
88
private import semmle.code.java.frameworks.android.Compose
99
private import semmle.code.java.Constants
10+
private import semmle.code.java.Overlay
1011

1112
/** A common super-class that represents all kinds of expressions. */
1213
class Expr extends ExprParent, @expr {
@@ -2701,3 +2702,15 @@ class RecordPatternExpr extends Expr, @recordpatternexpr {
27012702
)
27022703
}
27032704
}
2705+
2706+
overlay[local]
2707+
private predicate discardableExpr(string file, @expr e) {
2708+
not isOverlay() and
2709+
file = getRawFile(e)
2710+
}
2711+
2712+
/** Discard base expressions in files fully extracted in the overlay. */
2713+
overlay[discard_entity]
2714+
private predicate discardExpr(@expr e) {
2715+
exists(string file | discardableExpr(file, e) and extractedInOverlay(file))
2716+
}

java/ql/lib/semmle/code/java/Javadoc.qll

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ overlay[local?]
55
module;
66

77
import semmle.code.Location
8+
private import semmle.code.java.Overlay
89

910
/** A Javadoc parent is an element whose child can be some Javadoc documentation. */
1011
class JavadocParent extends @javadocParent, Top {
@@ -196,3 +197,15 @@ class KtCommentSection extends @ktcommentsection {
196197
/** Gets the string representation of this section. */
197198
string toString() { result = this.getContent() }
198199
}
200+
201+
overlay[local]
202+
private predicate discardableJavadoc(string file, @javadoc d) {
203+
not isOverlay() and
204+
exists(@member m | file = getRawFile(m) and hasJavadoc(m, d))
205+
}
206+
207+
/** Discard javadoc entities in files fully extracted in the overlay. */
208+
overlay[discard_entity]
209+
private predicate discardJavadoc(@javadoc d) {
210+
exists(string file | discardableJavadoc(file, d) and extractedInOverlay(file))
211+
}

java/ql/lib/semmle/code/java/Member.qll

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Annotation
1111
import Exception
1212
import metrics.MetricField
1313
private import dispatch.VirtualDispatch
14+
private import semmle.code.java.Overlay
1415

1516
/**
1617
* A common abstraction for type member declarations,
@@ -897,3 +898,33 @@ class ExtensionMethod extends Method {
897898
else result = 0
898899
}
899900
}
901+
902+
overlay[local]
903+
private predicate discardableMethod(string file, @method m) {
904+
not isOverlay() and
905+
file = getRawFile(m) and
906+
exists(@classorinterface c | methods(m, _, _, _, c, _) and isAnonymClass(c, _))
907+
}
908+
909+
/** Discard base methods on anonymous classes in files fully extracted in the overlay. */
910+
overlay[discard_entity]
911+
private predicate discardAnonMethod(@method m) {
912+
exists(string file | discardableMethod(file, m) and extractedInOverlay(file))
913+
}
914+
915+
overlay[local]
916+
private predicate discardableBaseMethod(string file, @method m) {
917+
not isOverlay() and
918+
file = getRawFile(m)
919+
}
920+
921+
overlay[local]
922+
private predicate usedOverlayMethod(@method m) { isOverlay() and methods(m, _, _, _, _, _) }
923+
924+
/** Discard base methods in files fully extracted in the overlay that were not extracted in the overlay. */
925+
overlay[discard_entity]
926+
private predicate discardMethod(@method m) {
927+
exists(string file |
928+
discardableBaseMethod(file, m) and extractedInOverlay(file) and not usedOverlayMethod(m)
929+
)
930+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
overlay[local?]
2+
module;
3+
4+
import java
5+
6+
/**
7+
* A local predicate that always holds for the overlay variant and
8+
* never holds for the base variant. This is used to define local
9+
* predicates that behave differently for the base and overlay variant.
10+
*/
11+
overlay[local]
12+
predicate isOverlay() { databaseMetadata("isOverlay", "true") }
13+
14+
/** Gets the raw file for a locatable. */
15+
overlay[local]
16+
string getRawFile(@locatable el) {
17+
exists(@location loc, @file file |
18+
hasLocation(el, loc) and
19+
locations_default(loc, file, _, _, _, _) and
20+
files(file, result)
21+
)
22+
}
23+
24+
/** Gets the raw file for a location. */
25+
overlay[local]
26+
string getRawFileForLoc(@location l) {
27+
exists(@file f | locations_default(l, f, _, _, _, _) and files(f, result))
28+
}
29+
30+
/** Holds for files fully extracted in the overlay. */
31+
overlay[local]
32+
predicate extractedInOverlay(string file) {
33+
isOverlay() and
34+
// The incremental Java extractor extracts skeletons without method
35+
// bodies for dependencies. To approximate fully extracted files in
36+
// the overlay, we restrict attention to files that contain an expression
37+
// with an enclosing callable.
38+
exists(@expr e | callableEnclosingExpr(e, _) and file = getRawFile(e))
39+
}

java/ql/lib/semmle/code/java/Statement.qll

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module;
66

77
import Expr
88
import metrics.MetricStmt
9+
private import semmle.code.java.Overlay
910

1011
/** A common super-class of all statements. */
1112
class Stmt extends StmtParent, ExprParent, @stmt {
@@ -987,3 +988,15 @@ class SuperConstructorInvocationStmt extends Stmt, ConstructorCall, @superconstr
987988

988989
override string getAPrimaryQlClass() { result = "SuperConstructorInvocationStmt" }
989990
}
991+
992+
overlay[local]
993+
private predicate discardableStmt(string file, @stmt s) {
994+
not isOverlay() and
995+
file = getRawFile(s)
996+
}
997+
998+
/** Discard base statements in files fully extracted in the overlay. */
999+
overlay[discard_entity]
1000+
private predicate discardStmt(@stmt s) {
1001+
exists(string file | discardableStmt(file, s) and extractedInOverlay(file))
1002+
}

java/ql/lib/semmle/code/java/Variable.qll

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ overlay[local?]
55
module;
66

77
import Element
8+
private import semmle.code.java.Overlay
89

910
/** A variable is a field, a local variable or a parameter. */
1011
class Variable extends @variable, Annotatable, Element, Modifiable {
@@ -133,3 +134,15 @@ class Parameter extends Element, @param, LocalScopeVariable {
133134
/** Holds if this is an anonymous parameter, `_` */
134135
predicate isAnonymous() { this.getName() = "" }
135136
}
137+
138+
overlay[local]
139+
private predicate discardableLocalVarDecl(string file, @localscopevariable l) {
140+
not isOverlay() and
141+
file = getRawFile(l)
142+
}
143+
144+
/** Discard base local scoped variables in files fully extracted in the overlay. */
145+
overlay[discard_entity]
146+
private predicate discardLocalVarDecl(@localscopevariable l) {
147+
exists(string file | discardableLocalVarDecl(file, l) and extractedInOverlay(file))
148+
}

0 commit comments

Comments
 (0)