Skip to content

Commit 1624c09

Browse files
committed
C/C++ overlay: Address review comments
Split the discard predicate into two: one for single-location elements and one for multi-location elements.
1 parent 3d69286 commit 1624c09

File tree

1 file changed

+37
-39
lines changed

1 file changed

+37
-39
lines changed

cpp/ql/lib/semmle/code/cpp/internal/Overlay.qll

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,58 +16,56 @@ private string getLocationFilePath(@location_default loc) {
1616
}
1717

1818
/**
19-
* An element with a single location. Discard if in a changed file.
19+
* Gets the file path for an element with a single location.
2020
*/
2121
overlay[local]
22-
abstract private class Discardable extends @element {
23-
abstract string getFilePath();
22+
private string getSingleLocationFilePath(@element e) {
23+
// @var_decl has a direct location in the var_decls relation
24+
exists(@location_default loc | var_decls(e, _, _, _, loc) |
25+
result = getLocationFilePath(loc)
26+
)
27+
//TODO: add other kinds of elements with single locations
28+
}
2429

25-
predicate existsInBase() { not isOverlay() }
30+
/**
31+
* Gets the file path for an element with potentially multiple locations.
32+
*/
33+
overlay[local]
34+
private string getMultiLocationFilePath(@element e) {
35+
// @variable gets its location(s) from its @var_decl(s)
36+
exists(@var_decl vd, @location_default loc | var_decls(vd, e, _, _, loc) |
37+
result = getLocationFilePath(loc)
38+
)
39+
//TODO: add other kinds of elements with multiple locations
40+
}
2641

27-
string toString() { none() }
42+
/** Holds if `e` exists in the base variant. */
43+
overlay[local]
44+
private predicate existsInBase(@element e) {
45+
not isOverlay() and
46+
(exists(getSingleLocationFilePath(e)) or exists(getMultiLocationFilePath(e)))
2847
}
2948

49+
/**
50+
* Discard an element with a single location if it is in a changed file.
51+
*/
3052
overlay[discard_entity]
31-
private predicate discardable(@element e) {
32-
e = any(Discardable d | d.existsInBase() and overlayChangedFiles(d.getFilePath()))
53+
private predicate discardSingleLocationElement(@element e) {
54+
existsInBase(e) and
55+
overlayChangedFiles(getSingleLocationFilePath(e))
3356
}
3457

3558
/**
36-
* An element with potentially multiple locations, e.g., variables, functions and types.
37-
* Discard only if all locations are in changed files.
59+
* Discard an element with multiple locations only if all its locations are in changed files.
3860
*/
39-
overlay[local]
40-
abstract private class MultiDiscardable extends @element {
41-
abstract string getFilePath();
61+
overlay[discard_entity]
62+
private predicate discardMultiLocationElement(@element e) {
63+
existsInBase(e) and
64+
exists(getMultiLocationFilePath(e)) and
65+
forall(string path | path = getMultiLocationFilePath(e) | overlayChangedFiles(path))
66+
}
4267

43-
predicate existsInBase() { not isOverlay() }
4468

45-
string toString() { none() }
46-
}
4769

48-
overlay[discard_entity]
49-
private predicate multiDiscardable(@element e) {
50-
e =
51-
any(MultiDiscardable d |
52-
d.existsInBase() and
53-
forall(string path | path = d.getFilePath() | overlayChangedFiles(path))
54-
)
55-
}
5670

57-
overlay[local]
58-
private class DiscardableVarDecl extends Discardable instanceof @var_decl {
59-
override string getFilePath() {
60-
exists(@location_default loc | var_decls(this, _, _, _, loc) |
61-
result = getLocationFilePath(loc)
62-
)
63-
}
64-
}
6571

66-
overlay[local]
67-
private class DiscardableVariable extends MultiDiscardable instanceof @variable {
68-
override string getFilePath() {
69-
exists(@var_decl vd, @location_default loc | var_decls(vd, this, _, _, loc) |
70-
result = getLocationFilePath(loc)
71-
)
72-
}
73-
}

0 commit comments

Comments
 (0)