Skip to content

Commit f8373c1

Browse files
committed
Define entity discard predicates for Java
1 parent 6454521 commit f8373c1

File tree

8 files changed

+129
-0
lines changed

8 files changed

+129
-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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,17 @@ 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+
pragma[nomagic]
227+
predicate discardableLocation(string file, @location l) {
228+
not hasOverlay() and
229+
file = getRawFileForLoc(l) and
230+
not exists(@file f | hasLocation(f, l))
231+
}
232+
233+
overlay[discard_entity]
234+
pragma[nomagic]
235+
predicate discardLocation(@location l) {
236+
exists(string file | discardableLocation(file, l) and discardFile(file))
237+
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2701,3 +2701,16 @@ class RecordPatternExpr extends Expr, @recordpatternexpr {
27012701
)
27022702
}
27032703
}
2704+
2705+
overlay[local]
2706+
pragma[nomagic]
2707+
predicate discardableExpr(string file, @expr e) {
2708+
not hasOverlay() and
2709+
file = getRawFile(e)
2710+
}
2711+
2712+
overlay[discard_entity]
2713+
pragma[nomagic]
2714+
predicate discardExpr(@expr e) {
2715+
exists(string file | discardableExpr(file, e) and discardFile(file))
2716+
}

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

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

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,3 +897,36 @@ class ExtensionMethod extends Method {
897897
else result = 0
898898
}
899899
}
900+
901+
overlay[local]
902+
pragma[nomagic]
903+
predicate discardableMethod(string file, @method m) {
904+
not hasOverlay() and
905+
file = getRawFile(m) and
906+
exists(@classorinterface c | methods(m, _, _, _, c, _) and isAnonymClass(c, _))
907+
}
908+
909+
overlay[discard_entity]
910+
pragma[nomagic]
911+
predicate discardAnonMethod(@method m) {
912+
exists(string file | discardableMethod(file, m) and discardFile(file))
913+
}
914+
915+
overlay[local]
916+
pragma[nomagic]
917+
predicate discardableBaseMethod(string file, @method m) {
918+
not hasOverlay() and
919+
file = getRawFile(m)
920+
}
921+
922+
overlay[local]
923+
pragma[nomagic]
924+
predicate usedOverlayMethod(@method m) { hasOverlay() and methods(m, _, _, _, _, _) }
925+
926+
overlay[discard_entity]
927+
pragma[nomagic]
928+
predicate discardMethod(@method m) {
929+
exists(string file |
930+
discardableBaseMethod(file, m) and discardFile(file) and not usedOverlayMethod(m)
931+
)
932+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
overlay[local?]
2+
module;
3+
4+
import java
5+
6+
overlay[local]
7+
pragma[nomagic]
8+
predicate hasOverlay() { databaseMetadata("isOverlay", "true") }
9+
10+
overlay[local]
11+
string getRawFile(@locatable el) {
12+
exists(@location loc, @file file |
13+
hasLocation(el, loc) and
14+
locations_default(loc, file, _, _, _, _) and
15+
files(file, result)
16+
)
17+
}
18+
19+
overlay[local]
20+
string getRawFileForLoc(@location l) {
21+
exists(@file f | locations_default(l, f, _, _, _, _) and files(f, result))
22+
}
23+
24+
overlay[local]
25+
pragma[nomagic]
26+
predicate discardFile(string file) {
27+
hasOverlay() and
28+
exists(@expr e | callableEnclosingExpr(e, _) and file = getRawFile(e))
29+
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,3 +987,16 @@ class SuperConstructorInvocationStmt extends Stmt, ConstructorCall, @superconstr
987987

988988
override string getAPrimaryQlClass() { result = "SuperConstructorInvocationStmt" }
989989
}
990+
991+
overlay[local]
992+
pragma[nomagic]
993+
predicate discardableStmt(string file, @stmt s) {
994+
not hasOverlay() and
995+
file = getRawFile(s)
996+
}
997+
998+
overlay[discard_entity]
999+
pragma[nomagic]
1000+
predicate discardStmt(@stmt s) {
1001+
exists(string file | discardableStmt(file, s) and discardFile(file))
1002+
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,16 @@ 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+
pragma[nomagic]
139+
predicate discardableLocalVarDecl(string file, @localscopevariable l) {
140+
not hasOverlay() and
141+
file = getRawFile(l)
142+
}
143+
144+
overlay[discard_entity]
145+
pragma[nomagic]
146+
predicate discardLocalVarDecl(@localscopevariable l) {
147+
exists(string file | discardableLocalVarDecl(file, l) and discardFile(file))
148+
}

0 commit comments

Comments
 (0)