Skip to content

Commit a06c6d9

Browse files
committed
C/C++ overlay: Address review comments
1 parent 244aa90 commit a06c6d9

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

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

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

13-
/** Gets the file path for a location. */
1413
overlay[local]
1514
private string getLocationFilePath(@location_default loc) {
1615
exists(@file file | locations_default(loc, file, _, _, _, _) | files(file, result))
1716
}
1817

18+
/**
19+
* An element with a single location. Discard if in a changed file.
20+
*/
1921
overlay[local]
20-
private class DiscardableEntityBase extends @element {
21-
/** Gets the path to the file in which this element occurs. */
22+
abstract private class Discardable extends @element {
2223
abstract string getFilePath();
2324

24-
/** Holds if this element exists in the base variant. */
2525
predicate existsInBase() { not isOverlay() }
2626

27-
/** Gets a textual representation of this discardable element. */
2827
string toString() { none() }
2928
}
3029

30+
overlay[discard_entity]
31+
private predicate discardable(@element e) {
32+
e = any(Discardable d | d.existsInBase() and overlayChangedFiles(d.getFilePath()))
33+
}
34+
3135
/**
32-
* Discard an entity from the base if all its locations are in changed files.
33-
* Entities with at least one location in an unchanged file are kept.
36+
* An element with potentially multiple locations, e.g., variables, functions and types.
37+
* Discard only if all locations are in changed files.
3438
*/
39+
overlay[local]
40+
abstract private class MultiDiscardable extends @element {
41+
abstract string getFilePath();
42+
43+
predicate existsInBase() { not isOverlay() }
44+
45+
string toString() { none() }
46+
}
47+
3548
overlay[discard_entity]
36-
private predicate discardEntity(@element e) {
49+
private predicate multiDiscardable(@element e) {
3750
e =
38-
any(DiscardableEntityBase de |
39-
de.existsInBase() and
40-
overlayChangedFiles(de.getFilePath()) and
41-
// Only discard if ALL file paths are in changed files
42-
forall(string path | path = de.getFilePath() | overlayChangedFiles(path))
51+
any(MultiDiscardable d |
52+
d.existsInBase() and
53+
forall(string path | path = d.getFilePath() | overlayChangedFiles(path))
4354
)
4455
}
4556

46-
/** A discardable variable declaration entry. */
4757
overlay[local]
48-
private class DiscardableVarDecl extends DiscardableEntityBase instanceof @var_decl {
58+
private class DiscardableVarDecl extends Discardable instanceof @var_decl {
4959
override string getFilePath() {
5060
exists(@location_default loc | var_decls(this, _, _, _, loc) |
5161
result = getLocationFilePath(loc)
5262
)
5363
}
5464
}
5565

56-
/** A discardable variable. */
5766
overlay[local]
58-
private class DiscardableVariable extends DiscardableEntityBase instanceof @variable {
67+
private class DiscardableVariable extends MultiDiscardable instanceof @variable {
5968
override string getFilePath() {
6069
exists(@var_decl vd, @location_default loc | var_decls(vd, this, _, _, loc) |
6170
result = getLocationFilePath(loc)

0 commit comments

Comments
 (0)