Skip to content

Commit 9d300e3

Browse files
committed
C#: Address comments in the QL implementation.
1 parent 1657dfb commit 9d300e3

File tree

3 files changed

+53
-58
lines changed

3 files changed

+53
-58
lines changed

csharp/ql/lib/csharp.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import semmle.code.csharp.controlflow.ControlFlowGraph
3636
import semmle.code.csharp.dataflow.DataFlow
3737
import semmle.code.csharp.dataflow.TaintTracking
3838
import semmle.code.csharp.dataflow.SSA
39-
private import semmle.code.csharp.Overlay
39+
private import semmle.code.csharp.internal.Overlay
4040

4141
/** Whether the source was extracted without a build command. */
4242
predicate extractionIsStandalone() { exists(SourceFile f | f.extractedStandalone()) }

csharp/ql/lib/semmle/code/csharp/Overlay.qll renamed to csharp/ql/lib/semmle/code/csharp/internal/Overlay.qll

Lines changed: 48 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,46 @@
1010
overlay[local]
1111
predicate isOverlay() { databaseMetadata("isOverlay", "true") }
1212

13+
overlay[local]
14+
private string getLocationFilePath(@location_default loc) {
15+
exists(@file file | locations_default(loc, file, _, _, _, _) | files(file, result))
16+
}
17+
1318
/**
1419
* An abstract base class for all elements that can be discarded from the base.
1520
*/
1621
overlay[local]
17-
abstract private class DiscardableEntity extends @locatable {
22+
private class DiscardableEntity extends @locatable {
1823
/** Gets the path to the file in which this element occurs. */
19-
abstract string getPath();
24+
string getFilePath() {
25+
exists(@location_default loc | result = getLocationFilePath(loc) |
26+
expr_location(this, loc) or
27+
stmt_location(this, loc) or
28+
using_directive_location(this, loc) or
29+
namespace_declaration_location(this, loc) or
30+
preprocessor_directive_location(this, loc) or
31+
type_location(this, loc) or
32+
attribute_location(this, loc) or
33+
type_parameter_constraints_location(this, loc) or
34+
property_location(this, loc) or
35+
indexer_location(this, loc) or
36+
accessor_location(this, loc) or
37+
event_location(this, loc) or
38+
event_accessor_location(this, loc) or
39+
operator_location(this, loc) or
40+
method_location(this, loc) or
41+
constructor_location(this, loc) or
42+
destructor_location(this, loc) or
43+
field_location(this, loc) or
44+
localvar_location(this, loc) or
45+
param_location(this, loc) or
46+
type_mention_location(this, loc) or
47+
commentline_location(this, loc) or
48+
commentblock_location(this, loc) or
49+
diagnostics(this, _, _, _, _, loc) or
50+
extractor_messages(this, _, _, _, _, loc, _)
51+
)
52+
}
2053

2154
/** Holds if this element exists in the base variant. */
2255
predicate existsInBase() { not isOverlay() and exists(this) }
@@ -34,74 +67,39 @@ abstract private class DiscardableEntity extends @locatable {
3467
*/
3568
overlay[local]
3669
private class StarEntity =
37-
@expr or @stmt or @diagnostic or @extractor_message or @using_directive or @type_mention;
70+
@expr or @stmt or @diagnostic or @extractor_message or @using_directive or @type_mention or
71+
@local_variable;
3872

3973
overlay[discard_entity]
4074
private predicate discardStarEntity(@locatable e) {
4175
e instanceof StarEntity and
4276
// Entities with *-ids can exist either in base or overlay, but not both.
43-
exists(DiscardableEntity de | de = e |
44-
overlayChangedFiles(de.getPath()) and
45-
de.existsInBase()
46-
)
77+
e =
78+
any(DiscardableEntity de |
79+
overlayChangedFiles(de.getFilePath()) and
80+
de.existsInBase()
81+
)
4782
}
4883

4984
overlay[discard_entity]
5085
private predicate discardNamedEntity(@locatable e) {
5186
not e instanceof StarEntity and
5287
// Entities with named IDs can exist both in base, overlay, or both.
53-
exists(DiscardableEntity de | de = e |
54-
overlayChangedFiles(de.getPath()) and
55-
not de.existsInOverlay()
56-
)
57-
}
58-
59-
overlay[local]
60-
private string getLocationPath(@location_default loc) {
61-
exists(@file file | locations_default(loc, file, _, _, _, _) | files(file, result))
88+
e =
89+
any(DiscardableEntity de |
90+
overlayChangedFiles(de.getFilePath()) and
91+
not de.existsInOverlay()
92+
)
6293
}
6394

6495
overlay[local]
6596
private predicate discardableLocation(@location_default loc, string path) {
6697
not isOverlay() and
67-
path = getLocationPath(loc)
98+
path = getLocationFilePath(loc)
6899
}
69100

70101
// Discard locations that are in changed files from the base variant.
71102
overlay[discard_entity]
72103
private predicate discardLocation(@location_default loc) {
73104
exists(string path | discardableLocation(loc, path) | overlayChangedFiles(path))
74105
}
75-
76-
overlay[local]
77-
private class PossibleDiscardableEntity extends DiscardableEntity instanceof @locatable {
78-
override string getPath() {
79-
exists(@location_default loc | result = getLocationPath(loc) |
80-
expr_location(this, loc) or
81-
stmt_location(this, loc) or
82-
using_directive_location(this, loc) or
83-
namespace_declaration_location(this, loc) or
84-
preprocessor_directive_location(this, loc) or
85-
type_location(this, loc) or
86-
attribute_location(this, loc) or
87-
type_parameter_constraints_location(this, loc) or
88-
property_location(this, loc) or
89-
indexer_location(this, loc) or
90-
accessor_location(this, loc) or
91-
event_location(this, loc) or
92-
event_accessor_location(this, loc) or
93-
operator_location(this, loc) or
94-
method_location(this, loc) or
95-
constructor_location(this, loc) or
96-
destructor_location(this, loc) or
97-
field_location(this, loc) or
98-
localvar_location(this, loc) or
99-
param_location(this, loc) or
100-
type_mention_location(this, loc) or
101-
commentline_location(this, loc) or
102-
commentblock_location(this, loc) or
103-
diagnostics(this, _, _, _, _, loc) or
104-
extractor_messages(this, _, _, _, _, loc, _)
105-
)
106-
}
107-
}

csharp/ql/test/library-tests/overlay/testlocations.ql

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,22 @@ import csharp
55
query predicate elementsWithMultipleSourceLocations(Element e, SourceLocation loc) {
66
e.fromSource() and
77
not e instanceof Namespace and
8-
count(SourceLocation l0 | l0 = e.getALocation()) > 1 and
8+
strictcount(SourceLocation l0 | l0 = e.getALocation()) > 1 and
99
loc = e.getALocation()
1010
}
1111

1212
query predicate typeMentionsWithMultipleSourceLocations(TypeMention tm, SourceLocation loc) {
13-
tm.getLocation().getFile().fromSource() and
14-
count(SourceLocation l0 | l0 = tm.getLocation()) > 1 and
13+
strictcount(SourceLocation l0 | l0 = tm.getLocation()) > 1 and
1514
loc = tm.getLocation()
1615
}
1716

1817
query predicate commentLinesWithMultipleSourceLocations(CommentLine cl, SourceLocation loc) {
19-
cl.getLocation().getFile().fromSource() and
20-
count(SourceLocation l0 | l0 = cl.getLocation()) > 1 and
18+
strictcount(SourceLocation l0 | l0 = cl.getLocation()) > 1 and
2119
loc = cl.getLocation()
2220
}
2321

2422
query predicate commentBlocksWithMultipleSourceLocations(CommentBlock cb, SourceLocation loc) {
25-
cb.getLocation().getFile().fromSource() and
26-
count(SourceLocation l0 | l0 = cb.getLocation()) > 1 and
23+
strictcount(SourceLocation l0 | l0 = cb.getLocation()) > 1 and
2724
loc = cb.getLocation()
2825
}
2926

0 commit comments

Comments
 (0)