Skip to content

Commit 8bd2ded

Browse files
committed
Define entity discard predicates for Java
1 parent d0feec9 commit 8bd2ded

File tree

8 files changed

+131
-0
lines changed

8 files changed

+131
-0
lines changed

java/ql/lib/java.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import semmle.code.java.KotlinType
2626
import semmle.code.java.Member
2727
import semmle.code.java.Modifier
2828
import semmle.code.java.Modules
29+
import semmle.code.java.Overlay
2930
import semmle.code.java.Package
3031
import semmle.code.java.Statement
3132
import semmle.code.java.Type

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,16 @@ private predicate fixedHasLocation(Top l, Location loc, File f) {
221221
not hasSourceLocation(l, _, _) and
222222
locations_default(loc, f, _, _, _, _)
223223
}
224+
225+
overlay[local]
226+
private predicate discardableLocation(string file, @location l) {
227+
not isOverlay() and
228+
file = getRawFileForLoc(l) and
229+
not exists(@file f | hasLocation(f, l))
230+
}
231+
232+
/** Discard base locations in files fully extracted in the overlay. */
233+
overlay[discard_entity]
234+
private predicate discardLocation(@location l) {
235+
exists(string file | discardableLocation(file, l) and extractedInOverlay(file))
236+
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2701,3 +2701,15 @@ class RecordPatternExpr extends Expr, @recordpatternexpr {
27012701
)
27022702
}
27032703
}
2704+
2705+
overlay[local]
2706+
private predicate discardableExpr(string file, @expr e) {
2707+
not isOverlay() and
2708+
file = getRawFile(e)
2709+
}
2710+
2711+
/** Discard base expressions in files fully extracted in the overlay. */
2712+
overlay[discard_entity]
2713+
private predicate discardExpr(@expr e) {
2714+
exists(string file | discardableExpr(file, e) and extractedInOverlay(file))
2715+
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,15 @@ class KtCommentSection extends @ktcommentsection {
196196
/** Gets the string representation of this section. */
197197
string toString() { result = this.getContent() }
198198
}
199+
200+
overlay[local]
201+
private predicate discardableJavadoc(string file, @javadoc d) {
202+
not isOverlay() and
203+
exists(@member m | file = getRawFile(m) and hasJavadoc(m, d))
204+
}
205+
206+
/** Discard javadoc entities in files fully extracted in the overlay. */
207+
overlay[discard_entity]
208+
private predicate discardJavadoc(@javadoc d) {
209+
exists(string file | discardableJavadoc(file, d) and extractedInOverlay(file))
210+
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,3 +897,33 @@ class ExtensionMethod extends Method {
897897
else result = 0
898898
}
899899
}
900+
901+
overlay[local]
902+
private predicate discardableMethod(string file, @method m) {
903+
not isOverlay() and
904+
file = getRawFile(m) and
905+
exists(@classorinterface c | methods(m, _, _, _, c, _) and isAnonymClass(c, _))
906+
}
907+
908+
/** Discard base methods on anonymous classes in files fully extracted in the overlay. */
909+
overlay[discard_entity]
910+
private predicate discardAnonMethod(@method m) {
911+
exists(string file | discardableMethod(file, m) and extractedInOverlay(file))
912+
}
913+
914+
overlay[local]
915+
private predicate discardableBaseMethod(string file, @method m) {
916+
not isOverlay() and
917+
file = getRawFile(m)
918+
}
919+
920+
overlay[local]
921+
private predicate usedOverlayMethod(@method m) { isOverlay() and methods(m, _, _, _, _, _) }
922+
923+
/** Discard base methods in files fully extracted in the overlay that were not extracted in the overlay. */
924+
overlay[discard_entity]
925+
private predicate discardMethod(@method m) {
926+
exists(string file |
927+
discardableBaseMethod(file, m) and extractedInOverlay(file) and not usedOverlayMethod(m)
928+
)
929+
}
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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,3 +987,15 @@ class SuperConstructorInvocationStmt extends Stmt, ConstructorCall, @superconstr
987987

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

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,15 @@ class Parameter extends Element, @param, LocalScopeVariable {
133133
/** Holds if this is an anonymous parameter, `_` */
134134
predicate isAnonymous() { this.getName() = "" }
135135
}
136+
137+
overlay[local]
138+
private predicate discardableLocalVarDecl(string file, @localscopevariable l) {
139+
not isOverlay() and
140+
file = getRawFile(l)
141+
}
142+
143+
/** Discard base local scoped variables in files fully extracted in the overlay. */
144+
overlay[discard_entity]
145+
private predicate discardLocalVarDecl(@localscopevariable l) {
146+
exists(string file | discardableLocalVarDecl(file, l) and extractedInOverlay(file))
147+
}

0 commit comments

Comments
 (0)