Define entity discard predicates for Java

This commit is contained in:
Kasper Svendsen
2025-01-09 09:35:56 +01:00
parent 6454521957
commit f8373c1f19
8 changed files with 129 additions and 0 deletions

View File

@@ -26,6 +26,7 @@ import semmle.code.java.KotlinType
import semmle.code.java.Member
import semmle.code.java.Modifier
import semmle.code.java.Modules
import semmle.code.java.Overlay
import semmle.code.java.Package
import semmle.code.java.Statement
import semmle.code.java.Type

View File

@@ -221,3 +221,17 @@ private predicate fixedHasLocation(Top l, Location loc, File f) {
not hasSourceLocation(l, _, _) and
locations_default(loc, f, _, _, _, _)
}
overlay[local]
pragma[nomagic]
predicate discardableLocation(string file, @location l) {
not hasOverlay() and
file = getRawFileForLoc(l) and
not exists(@file f | hasLocation(f, l))
}
overlay[discard_entity]
pragma[nomagic]
predicate discardLocation(@location l) {
exists(string file | discardableLocation(file, l) and discardFile(file))
}

View File

@@ -2701,3 +2701,16 @@ class RecordPatternExpr extends Expr, @recordpatternexpr {
)
}
}
overlay[local]
pragma[nomagic]
predicate discardableExpr(string file, @expr e) {
not hasOverlay() and
file = getRawFile(e)
}
overlay[discard_entity]
pragma[nomagic]
predicate discardExpr(@expr e) {
exists(string file | discardableExpr(file, e) and discardFile(file))
}

View File

@@ -196,3 +196,16 @@ class KtCommentSection extends @ktcommentsection {
/** Gets the string representation of this section. */
string toString() { result = this.getContent() }
}
overlay[local]
pragma[nomagic]
predicate discardableJavadoc(string file, @javadoc d) {
not hasOverlay() and
exists(@member m | file = getRawFile(m) and hasJavadoc(m, d))
}
overlay[discard_entity]
pragma[nomagic]
predicate discardJavadoc(@javadoc d) {
exists(string file | discardableJavadoc(file, d) and discardFile(file))
}

View File

@@ -897,3 +897,36 @@ class ExtensionMethod extends Method {
else result = 0
}
}
overlay[local]
pragma[nomagic]
predicate discardableMethod(string file, @method m) {
not hasOverlay() and
file = getRawFile(m) and
exists(@classorinterface c | methods(m, _, _, _, c, _) and isAnonymClass(c, _))
}
overlay[discard_entity]
pragma[nomagic]
predicate discardAnonMethod(@method m) {
exists(string file | discardableMethod(file, m) and discardFile(file))
}
overlay[local]
pragma[nomagic]
predicate discardableBaseMethod(string file, @method m) {
not hasOverlay() and
file = getRawFile(m)
}
overlay[local]
pragma[nomagic]
predicate usedOverlayMethod(@method m) { hasOverlay() and methods(m, _, _, _, _, _) }
overlay[discard_entity]
pragma[nomagic]
predicate discardMethod(@method m) {
exists(string file |
discardableBaseMethod(file, m) and discardFile(file) and not usedOverlayMethod(m)
)
}

View File

@@ -0,0 +1,29 @@
overlay[local?]
module;
import java
overlay[local]
pragma[nomagic]
predicate hasOverlay() { databaseMetadata("isOverlay", "true") }
overlay[local]
string getRawFile(@locatable el) {
exists(@location loc, @file file |
hasLocation(el, loc) and
locations_default(loc, file, _, _, _, _) and
files(file, result)
)
}
overlay[local]
string getRawFileForLoc(@location l) {
exists(@file f | locations_default(l, f, _, _, _, _) and files(f, result))
}
overlay[local]
pragma[nomagic]
predicate discardFile(string file) {
hasOverlay() and
exists(@expr e | callableEnclosingExpr(e, _) and file = getRawFile(e))
}

View File

@@ -987,3 +987,16 @@ class SuperConstructorInvocationStmt extends Stmt, ConstructorCall, @superconstr
override string getAPrimaryQlClass() { result = "SuperConstructorInvocationStmt" }
}
overlay[local]
pragma[nomagic]
predicate discardableStmt(string file, @stmt s) {
not hasOverlay() and
file = getRawFile(s)
}
overlay[discard_entity]
pragma[nomagic]
predicate discardStmt(@stmt s) {
exists(string file | discardableStmt(file, s) and discardFile(file))
}

View File

@@ -133,3 +133,16 @@ class Parameter extends Element, @param, LocalScopeVariable {
/** Holds if this is an anonymous parameter, `_` */
predicate isAnonymous() { this.getName() = "" }
}
overlay[local]
pragma[nomagic]
predicate discardableLocalVarDecl(string file, @localscopevariable l) {
not hasOverlay() and
file = getRawFile(l)
}
overlay[discard_entity]
pragma[nomagic]
predicate discardLocalVarDecl(@localscopevariable l) {
exists(string file | discardableLocalVarDecl(file, l) and discardFile(file))
}