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

Skip to content

Commit c4a167c

Browse files
committed
C#: Group cached predicates
1 parent 05e00ab commit c4a167c

3 files changed

Lines changed: 47 additions & 40 deletions

File tree

csharp/ql/src/semmle/code/csharp/Element.qll

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,3 @@ class Element extends DotNet::Element, @element {
5757
exists(Element parent | parent.getChild(result) = this)
5858
}
5959
}
60-
61-
/**
62-
* Gets the "best" location for element `e`. Where an element has locations in
63-
* source and assemblies, choose the source location. If there are multiple assembly
64-
* locations, choose only one.
65-
*/
66-
cached
67-
private Location bestLocation(Element e) {
68-
result = e.getALocation().(SourceLocation) and
69-
(mustHaveLocationInFile(e, _) implies mustHaveLocationInFile(e, result.getFile()))
70-
or
71-
(
72-
hasNoSourceLocation(e)
73-
and
74-
result = min(Location l | l = e.getALocation() | l order by l.getFile().toString())
75-
)
76-
}
77-
78-
private predicate hasNoSourceLocation(Element e) {
79-
not e.getALocation() instanceof SourceLocation
80-
}

csharp/ql/src/semmle/code/csharp/ExprOrStmtParent.qll

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,29 @@ private cached module Cached {
370370
t = getTopLevelDeclaringType(d) or d = t or d = p
371371
)
372372
}
373+
374+
private predicate hasNoSourceLocation(Element e) {
375+
not e.getALocation() instanceof SourceLocation
376+
}
377+
378+
/**
379+
* Gets the "best" location for element `e`. Where an element has locations in
380+
* source and assemblies, choose the source location. If there are multiple assembly
381+
* locations, choose only one.
382+
*/
383+
cached
384+
Location bestLocationCached(Element e) {
385+
result = e.getALocation().(SourceLocation) and
386+
(mustHaveLocationInFile(e, _) implies mustHaveLocationInFile(e, result.getFile()))
387+
or
388+
(
389+
hasNoSourceLocation(e)
390+
and
391+
result = min(Location l | l = e.getALocation() | l order by l.getFile().toString())
392+
)
393+
}
373394
}
374395
private import Cached
375396

376397
predicate mustHaveLocationInFile = mustHaveLocationInFileCached/2;
398+
predicate bestLocation = bestLocationCached/1;

csharp/ql/src/semmle/code/csharp/Stmt.qll

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -173,17 +173,7 @@ class SwitchStmt extends SelectionStmt, @switch_stmt {
173173
* }
174174
* ```
175175
*/
176-
cached
177-
CaseStmt getCase(int i) {
178-
exists(int index, int rankIndex |
179-
result = getChildStmt(index) and
180-
rankIndex = i + 1 and
181-
index = rank[rankIndex](int j, CaseStmt cs |
182-
cs = this.getChildStmt(j) |
183-
j
184-
)
185-
)
186-
}
176+
CaseStmt getCase(int i) { result = SwithStmtInternal::getCase(this, i) }
187177

188178
/** Gets a case of this `switch` statement. */
189179
CaseStmt getACase() { result = this.getCase(_) }
@@ -221,29 +211,45 @@ class SwitchStmt extends SelectionStmt, @switch_stmt {
221211
* Note that each non-`default` case is a labeled statement, so the statement
222212
* that follows is a child of the labeled statement, and not the `switch` block.
223213
*/
214+
Stmt getStmt(int i) { result = SwithStmtInternal::getStmt(this, i) }
215+
216+
/** Gets a statement in the body of this `switch` statement. */
217+
Stmt getAStmt() { result = this.getStmt(_) }
218+
}
219+
220+
private cached module SwithStmtInternal {
224221
cached
225-
Stmt getStmt(int i) {
222+
CaseStmt getCase(SwitchStmt ss, int i) {
226223
exists(int index, int rankIndex |
227-
result = getChildStmt(index) and
224+
result = ss.getChildStmt(index) and
225+
rankIndex = i + 1 and
226+
index = rank[rankIndex](int j, CaseStmt cs |
227+
cs = ss.getChildStmt(j) |
228+
j
229+
)
230+
)
231+
}
232+
233+
cached
234+
Stmt getStmt(SwitchStmt ss, int i) {
235+
exists(int index, int rankIndex |
236+
result = ss.getChildStmt(index) and
228237
rankIndex = i + 1 and
229238
index = rank[rankIndex](int j, Stmt s |
230239
// `getChild` includes both labeled statements and the targeted
231240
// statements of labeled statement as separate children, but we
232241
// only want the labeled statement
233-
s = getLabeledStmt(j) |
242+
s = getLabeledStmt(ss, j) |
234243
j
235244
)
236245
)
237246
}
238247

239-
private Stmt getLabeledStmt(int i) {
240-
result = this.getChildStmt(i) and
248+
private Stmt getLabeledStmt(SwitchStmt ss, int i) {
249+
result = ss.getChildStmt(i) and
241250
not result = any(ConstCase cc).getStmt() and
242251
not result = any(TypeCase tc).getStmt()
243252
}
244-
245-
/** Gets a statement in the body of this `switch` statement. */
246-
Stmt getAStmt() { result = this.getStmt(_) }
247253
}
248254

249255
/**

0 commit comments

Comments
 (0)