Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e3f828c

Browse files
committed
Java: refine ranges in java/alert-suppression-annotations
1 parent b2bc838 commit e3f828c

2 files changed

Lines changed: 31 additions & 4 deletions

File tree

java/ql/src/AlertSuppressionAnnotations.ql

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,39 @@ class SuppressionAnnotation extends SuppressWarningsAnnotation {
3030
/** Gets the LGTM suppression annotation in this Java annotation. */
3131
string getAnnotation() { result = annotation }
3232

33+
private Annotation getASiblingAnnotation() {
34+
result = getAnnotatedElement().(Annotatable).getAnAnnotation() and
35+
(getAnnotatedElement() instanceof Callable or getAnnotatedElement() instanceof RefType)
36+
}
37+
38+
private Annotation firstAnnotation() {
39+
exists(Annotation m, int i |
40+
result = m and
41+
m = getASiblingAnnotation() and
42+
i = rankOfAnnotation(m) and
43+
not exists(Annotation other | other = getASiblingAnnotation() | rankOfAnnotation(other) < i)
44+
)
45+
}
46+
47+
private int rankOfAnnotation(Annotation m) {
48+
this.getASiblingAnnotation() = m and
49+
exists(Location mLoc, File f, int maxCol | mLoc = m.getLocation() |
50+
f = mLoc.getFile() and
51+
maxCol = max(Location loc | loc.getFile() = f | loc.getStartColumn()) and
52+
result = mLoc.getStartLine() * maxCol + mLoc.getStartColumn()
53+
)
54+
}
55+
3356
/**
3457
* Holds if this annotation applies to the range from column `startcolumn` of line `startline`
3558
* to column `endcolumn` of line `endline` in file `filepath`.
3659
*/
3760
predicate covers(string filepath, int startline, int startcolumn, int endline, int endcolumn) {
38-
getAnnotatedElement().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
61+
if firstAnnotation().hasLocationInfo(filepath, _, _, _, _)
62+
then
63+
getAnnotatedElement().hasLocationInfo(filepath, _, _, endline, endcolumn) and
64+
firstAnnotation().hasLocationInfo(filepath, startline, startcolumn, _, _)
65+
else getAnnotatedElement().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
3966
}
4067

4168
/** Gets the scope of this suppression. */
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
| TestSuppressWarnings.java:2:1:2:49 | SuppressWarnings | lgtm[java/non-sync-override] | lgtm[java/non-sync-override] | TestSuppressWarnings.java:4:7:17:5 | suppression range |
2-
| TestSuppressWarnings.java:5:5:5:31 | SuppressWarnings | lgtm[] | lgtm[] | TestSuppressWarnings.java:6:17:8:5 | suppression range |
3-
| TestSuppressWarnings.java:10:5:10:57 | SuppressWarnings | lgtm[java/confusing-method-name] | lgtm[java/confusing-method-name] | TestSuppressWarnings.java:11:17:13:5 | suppression range |
1+
| TestSuppressWarnings.java:2:1:2:49 | SuppressWarnings | lgtm[java/non-sync-override] | lgtm[java/non-sync-override] | TestSuppressWarnings.java:2:1:17:5 | suppression range |
2+
| TestSuppressWarnings.java:5:5:5:31 | SuppressWarnings | lgtm[] | lgtm[] | TestSuppressWarnings.java:5:5:8:5 | suppression range |
3+
| TestSuppressWarnings.java:10:5:10:57 | SuppressWarnings | lgtm[java/confusing-method-name] | lgtm[java/confusing-method-name] | TestSuppressWarnings.java:9:5:13:5 | suppression range |

0 commit comments

Comments
 (0)