Skip to content

Commit bcadf31

Browse files
committed
Java: Add abstraction for discardable locatables
1 parent 0ee6a78 commit bcadf31

File tree

6 files changed

+53
-65
lines changed

6 files changed

+53
-65
lines changed

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2704,13 +2704,4 @@ class RecordPatternExpr extends Expr, @recordpatternexpr {
27042704
}
27052705

27062706
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-
}
2707+
private class DiscardableExpr extends DiscardableLocatable, @expr { }

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,4 @@ class KtCommentSection extends @ktcommentsection {
199199
}
200200

201201
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-
}
202+
private class DiscardableJavadoc extends DiscardableLocatable, @javadoc { }

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

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -906,31 +906,12 @@ class ExtensionMethod extends Method {
906906
}
907907

908908
overlay[local]
909-
private predicate discardableMethod(string file, @method m) {
910-
not isOverlay() and
911-
file = getRawFile(m) and
912-
exists(@classorinterface c | methods(m, _, _, _, c, _) and isAnonymClass(c, _))
913-
}
914-
915-
/** Discard base methods on anonymous classes in files fully extracted in the overlay. */
916-
overlay[discard_entity]
917-
private predicate discardAnonMethod(@method m) {
918-
exists(string file | discardableMethod(file, m) and extractedInOverlay(file))
919-
}
920-
921-
overlay[local]
922-
private predicate discardableBaseMethod(string file, @method m) {
923-
not isOverlay() and
924-
file = getRawFile(m)
909+
private class DiscardableAnonymousMethod extends DiscardableLocatable, @method {
910+
DiscardableAnonymousMethod() {
911+
not isOverlay() and
912+
exists(@classorinterface c | methods(this, _, _, _, c, _) and isAnonymClass(c, _))
913+
}
925914
}
926915

927916
overlay[local]
928-
private predicate usedOverlayMethod(@method m) { isOverlay() and methods(m, _, _, _, _, _) }
929-
930-
/** Discard base methods in files fully extracted in the overlay that were not extracted in the overlay. */
931-
overlay[discard_entity]
932-
private predicate discardMethod(@method m) {
933-
exists(string file |
934-
discardableBaseMethod(file, m) and extractedInOverlay(file) and not usedOverlayMethod(m)
935-
)
936-
}
917+
private class DiscardableMethod extends DiscardableReferableLocatable, @method { }

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,46 @@ predicate extractedInOverlay(string file) {
3535
// ignore skeleton extracted files in the overlay
3636
exists(@locatable l | numlines(l, _, _, _) and file = getRawFile(l))
3737
}
38+
39+
/**
40+
* A `@locatable` in the base variant that should be discarded if its file is
41+
* extracted in the overlay variant.
42+
*/
43+
overlay[local]
44+
abstract class DiscardableLocatable extends @locatable {
45+
/** Gets the raw file for a locatable in base. */
46+
string getRawFileInBase() { not isOverlay() and result = getRawFile(this) }
47+
48+
/** Gets a textual representation of this discardable locatable. */
49+
string toString() { none() }
50+
}
51+
52+
overlay[discard_entity]
53+
private predicate discardLocatable(@locatable el) {
54+
extractedInOverlay(el.(DiscardableLocatable).getRawFileInBase())
55+
}
56+
57+
/**
58+
* A `@locatable` in the base variant that should be discarded if its file is
59+
* extracted in the overlay variant and it is itself not extracted in the
60+
* overlay, that is, it is deleted in the overlay.
61+
*/
62+
overlay[local]
63+
abstract class DiscardableReferableLocatable extends @locatable {
64+
/** Gets the raw file for a locatable in base. */
65+
string getRawFileInBase() { not isOverlay() and result = getRawFile(this) }
66+
67+
/** Holds if the locatable exists in the overlay. */
68+
predicate existsInOverlay() { isOverlay() and exists(this) }
69+
70+
/** Gets a textual representation of this discardable locatable. */
71+
string toString() { none() }
72+
}
73+
74+
overlay[discard_entity]
75+
private predicate discardReferableLocatable(@locatable el) {
76+
exists(DiscardableReferableLocatable drl | drl = el |
77+
extractedInOverlay(drl.getRawFileInBase()) and
78+
not drl.existsInOverlay()
79+
)
80+
}

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -990,13 +990,4 @@ class SuperConstructorInvocationStmt extends Stmt, ConstructorCall, @superconstr
990990
}
991991

992992
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-
}
993+
private class DiscardableStmt extends DiscardableLocatable, @stmt { }

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,4 @@ class Parameter extends Element, @param, LocalScopeVariable {
136136
}
137137

138138
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-
}
139+
private class DiscardableLocalScopeVariable extends DiscardableLocatable, @localscopevariable { }

0 commit comments

Comments
 (0)