Skip to content

Commit b8cff77

Browse files
authored
Merge pull request #20873 from github/shared-xml-discard
Share XML discard predicates
2 parents 50929ef + 6257bed commit b8cff77

File tree

12 files changed

+246
-136
lines changed

12 files changed

+246
-136
lines changed

config/identical-files.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,5 +276,12 @@
276276
"Python model summaries test extension": [
277277
"python/ql/test/library-tests/dataflow/model-summaries/InlineTaintTest.ext.yml",
278278
"python/ql/test/library-tests/dataflow/model-summaries/NormalDataflowTest.ext.yml"
279+
],
280+
"XML discard predicates": [
281+
"javascript/ql/lib/semmle/javascript/internal/OverlayXml.qll",
282+
"java/ql/lib/semmle/code/java/internal/OverlayXml.qll",
283+
"go/ql/lib/semmle/go/internal/OverlayXml.qll",
284+
"python/ql/lib/semmle/python/internal/OverlayXml.qll",
285+
"csharp/ql/lib/semmle/code/csharp/internal/OverlayXml.qll"
279286
]
280287
}

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

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* Defines entity discard predicates for C# overlay analysis.
33
*/
44

5+
private import OverlayXml
6+
57
/**
68
* Holds always for the overlay variant and never for the base variant.
79
* This local predicate is used to define local predicates that behave
@@ -110,36 +112,6 @@ private predicate discardLocation(@location_default loc) {
110112
exists(string path | discardableLocation(loc, path) | overlayChangedFiles(path))
111113
}
112114

113-
/**
114-
* A class of Xml locatables that can be discarded from the base.
115-
*/
116-
overlay[local]
117-
private class DiscardableXmlEntity extends DiscardableEntityBase instanceof @xmllocatable {
118-
/** Gets the path to the file in which this element occurs. */
119-
override string getFilePath() {
120-
exists(@location_default loc | result = getLocationFilePath(loc) | xmllocations(this, loc))
121-
}
122-
}
123-
124-
overlay[local]
125-
private predicate overlayXmlExtracted(string file) {
126-
exists(DiscardableXmlEntity dxe |
127-
dxe.existsInOverlay() and
128-
file = dxe.getFilePath() and
129-
not files(dxe, _) and
130-
not xmlNs(dxe, _, _, _)
131-
)
132-
}
133-
134-
overlay[discard_entity]
135-
private predicate discardXmlEntity(@xmllocatable xml) {
136-
overlayChangedFiles(xml.(DiscardableXmlEntity).getFilePath())
137-
or
138-
// The XML extractor is not incremental and may extract more
139-
// XML files than those included in overlayChangedFiles.
140-
overlayXmlExtracted(xml.(DiscardableXmlEntity).getFilePath())
141-
}
142-
143115
overlay[local]
144116
private class DiscardableAspEntity extends DiscardableEntityBase instanceof @asp_element {
145117
/** Gets the path to the file in which this element occurs. */
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
overlay[local]
2+
module;
3+
4+
/**
5+
* A local predicate that always holds for the overlay variant and never holds for the base variant.
6+
* This is used to define local predicates that behave differently for the base and overlay variant.
7+
*/
8+
private predicate isOverlay() { databaseMetadata("isOverlay", "true") }
9+
10+
private string getXmlFile(@xmllocatable locatable) {
11+
exists(@location_default location, @file file | xmllocations(locatable, location) |
12+
locations_default(location, file, _, _, _, _) and
13+
files(file, result)
14+
)
15+
}
16+
17+
private string getXmlFileInBase(@xmllocatable locatable) {
18+
not isOverlay() and
19+
result = getXmlFile(locatable)
20+
}
21+
22+
/**
23+
* Holds if the given `file` was extracted as part of the overlay and was extracted by the HTML/XML
24+
* extractor.
25+
*/
26+
private predicate overlayXmlExtracted(string file) {
27+
isOverlay() and
28+
exists(@xmllocatable locatable |
29+
not files(locatable, _) and not xmlNs(locatable, _, _, _) and file = getXmlFile(locatable)
30+
)
31+
}
32+
33+
/**
34+
* Holds if the given XML `locatable` should be discarded, because it is part of the overlay base
35+
* and is in a file that was also extracted as part of the overlay database.
36+
*/
37+
overlay[discard_entity]
38+
private predicate discardXmlLocatable(@xmllocatable locatable) {
39+
exists(string file | file = getXmlFileInBase(locatable) |
40+
overlayChangedFiles(file)
41+
or
42+
// The HTML/XML extractor is currently not incremental and may extract more files than those
43+
// included in overlayChangedFiles.
44+
overlayXmlExtracted(file)
45+
)
46+
}

go/ql/lib/semmle/go/Overlay.qll

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
overlay[local]
55
module;
66

7+
private import internal.OverlayXml
8+
79
/**
810
* A local predicate that always holds for the overlay variant and never holds for the base variant.
911
* This is used to define local predicates that behave differently for the base and overlay variant.
@@ -52,40 +54,3 @@ private predicate discardLocatable(@locatable locatable) {
5254
discardableLocatable(file, locatable) and discardableFile(path)
5355
)
5456
}
55-
56-
private @file getXmlFile(@xmllocatable locatable) {
57-
exists(@location_default location | xmllocations(locatable, location) |
58-
locations_default(location, result, _, _, _, _)
59-
)
60-
}
61-
62-
private @file getXmlFileInBase(@xmllocatable locatable) {
63-
not isOverlay() and
64-
result = getXmlFile(locatable)
65-
}
66-
67-
/**
68-
* Holds if the given `file` was extracted as part of the overlay and was extracted by the HTML/XML
69-
* extractor.
70-
*/
71-
private predicate overlayXmlExtracted(@file file) {
72-
isOverlay() and
73-
exists(@xmllocatable locatable |
74-
not files(locatable, _) and not xmlNs(locatable, _, _, _) and file = getXmlFile(locatable)
75-
)
76-
}
77-
78-
/**
79-
* Holds if the given XML `locatable` should be discarded, because it is part of the overlay base
80-
* and is in a file that was also extracted as part of the overlay database.
81-
*/
82-
overlay[discard_entity]
83-
private predicate discardXmlLocatable(@xmllocatable locatable) {
84-
exists(@file file | file = getXmlFileInBase(locatable) |
85-
exists(string path | files(file, path) | overlayChangedFiles(path))
86-
or
87-
// The HTML/XML extractor is currently not incremental and may extract more files than those
88-
// included in overlayChangedFiles.
89-
overlayXmlExtracted(file)
90-
)
91-
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
overlay[local]
2+
module;
3+
4+
/**
5+
* A local predicate that always holds for the overlay variant and never holds for the base variant.
6+
* This is used to define local predicates that behave differently for the base and overlay variant.
7+
*/
8+
private predicate isOverlay() { databaseMetadata("isOverlay", "true") }
9+
10+
private string getXmlFile(@xmllocatable locatable) {
11+
exists(@location_default location, @file file | xmllocations(locatable, location) |
12+
locations_default(location, file, _, _, _, _) and
13+
files(file, result)
14+
)
15+
}
16+
17+
private string getXmlFileInBase(@xmllocatable locatable) {
18+
not isOverlay() and
19+
result = getXmlFile(locatable)
20+
}
21+
22+
/**
23+
* Holds if the given `file` was extracted as part of the overlay and was extracted by the HTML/XML
24+
* extractor.
25+
*/
26+
private predicate overlayXmlExtracted(string file) {
27+
isOverlay() and
28+
exists(@xmllocatable locatable |
29+
not files(locatable, _) and not xmlNs(locatable, _, _, _) and file = getXmlFile(locatable)
30+
)
31+
}
32+
33+
/**
34+
* Holds if the given XML `locatable` should be discarded, because it is part of the overlay base
35+
* and is in a file that was also extracted as part of the overlay database.
36+
*/
37+
overlay[discard_entity]
38+
private predicate discardXmlLocatable(@xmllocatable locatable) {
39+
exists(string file | file = getXmlFileInBase(locatable) |
40+
overlayChangedFiles(file)
41+
or
42+
// The HTML/XML extractor is currently not incremental and may extract more files than those
43+
// included in overlayChangedFiles.
44+
overlayXmlExtracted(file)
45+
)
46+
}

java/ql/lib/semmle/code/java/Overlay.qll

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ overlay[local?]
55
module;
66

77
import java
8+
private import internal.OverlayXml
89

910
/**
1011
* A local predicate that always holds for the overlay variant and
@@ -18,7 +19,7 @@ predicate isOverlay() { databaseMetadata("isOverlay", "true") }
1819
overlay[local]
1920
string getRawFile(@locatable el) {
2021
exists(@location loc, @file file |
21-
(hasLocation(el, loc) or xmllocations(el, loc)) and
22+
hasLocation(el, loc) and
2223
locations_default(loc, file, _, _, _, _) and
2324
files(file, result)
2425
)
@@ -102,31 +103,3 @@ private predicate discardBaseConfigLocatable(@configLocatable el) {
102103
// property files than those included in overlayChangedFiles.
103104
overlayConfigExtracted(baseConfigLocatable(el))
104105
}
105-
106-
/**
107-
* An `@xmllocatable` that should be discarded in the base variant if its file is
108-
* extracted in the overlay variant.
109-
*/
110-
overlay[local]
111-
abstract class DiscardableXmlLocatable extends @xmllocatable {
112-
/** Gets the raw file for an xmllocatable in base. */
113-
string getRawFileInBase() { not isOverlay() and result = getRawFile(this) }
114-
115-
/** Gets a textual representation of this discardable xmllocatable. */
116-
string toString() { none() }
117-
}
118-
119-
overlay[local]
120-
private predicate overlayXmlExtracted(string file) {
121-
isOverlay() and
122-
exists(@xmllocatable el | not files(el, _) and not xmlNs(el, _, _, _) and file = getRawFile(el))
123-
}
124-
125-
overlay[discard_entity]
126-
private predicate discardXmlLocatable(@xmllocatable el) {
127-
overlayChangedFiles(el.(DiscardableXmlLocatable).getRawFileInBase())
128-
or
129-
// The XML extractor is currently not incremental and may extract more
130-
// XML files than those included in overlayChangedFiles.
131-
overlayXmlExtracted(el.(DiscardableXmlLocatable).getRawFileInBase())
132-
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
overlay[local]
2+
module;
3+
4+
/**
5+
* A local predicate that always holds for the overlay variant and never holds for the base variant.
6+
* This is used to define local predicates that behave differently for the base and overlay variant.
7+
*/
8+
private predicate isOverlay() { databaseMetadata("isOverlay", "true") }
9+
10+
private string getXmlFile(@xmllocatable locatable) {
11+
exists(@location_default location, @file file | xmllocations(locatable, location) |
12+
locations_default(location, file, _, _, _, _) and
13+
files(file, result)
14+
)
15+
}
16+
17+
private string getXmlFileInBase(@xmllocatable locatable) {
18+
not isOverlay() and
19+
result = getXmlFile(locatable)
20+
}
21+
22+
/**
23+
* Holds if the given `file` was extracted as part of the overlay and was extracted by the HTML/XML
24+
* extractor.
25+
*/
26+
private predicate overlayXmlExtracted(string file) {
27+
isOverlay() and
28+
exists(@xmllocatable locatable |
29+
not files(locatable, _) and not xmlNs(locatable, _, _, _) and file = getXmlFile(locatable)
30+
)
31+
}
32+
33+
/**
34+
* Holds if the given XML `locatable` should be discarded, because it is part of the overlay base
35+
* and is in a file that was also extracted as part of the overlay database.
36+
*/
37+
overlay[discard_entity]
38+
private predicate discardXmlLocatable(@xmllocatable locatable) {
39+
exists(string file | file = getXmlFileInBase(locatable) |
40+
overlayChangedFiles(file)
41+
or
42+
// The HTML/XML extractor is currently not incremental and may extract more files than those
43+
// included in overlayChangedFiles.
44+
overlayXmlExtracted(file)
45+
)
46+
}

java/ql/lib/semmle/code/xml/XML.qll

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ module;
66

77
import semmle.files.FileSystem
88
private import codeql.xml.Xml
9-
private import semmle.code.java.Overlay
109

1110
private module Input implements InputSig<File, Location> {
1211
class XmlLocatableBase = @xmllocatable or @xmlnamespaceable;
@@ -70,13 +69,3 @@ private module Input implements InputSig<File, Location> {
7069
}
7170

7271
import Make<File, Location, Input>
73-
74-
private class DiscardableXmlAttribute extends DiscardableXmlLocatable, @xmlattribute { }
75-
76-
private class DiscardableXmlElement extends DiscardableXmlLocatable, @xmlelement { }
77-
78-
private class DiscardableXmlComment extends DiscardableXmlLocatable, @xmlcomment { }
79-
80-
private class DiscardableXmlCharacters extends DiscardableXmlLocatable, @xmlcharacters { }
81-
82-
private class DiscardableXmlDtd extends DiscardableXmlLocatable, @xmldtd { }

javascript/ql/lib/semmle/javascript/internal/Overlay.qll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
private import javascript
2+
private import OverlayXml
23

34
/** Holds if the database is an overlay. */
45
overlay[local]
@@ -12,8 +13,6 @@ private string getFileFromEntity(@locatable node) {
1213
json_locations(node, loc)
1314
or
1415
yaml_locations(node, loc)
15-
or
16-
xmllocations(node, loc)
1716
|
1817
result = getFileFromLocation(loc)
1918
)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
overlay[local]
2+
module;
3+
4+
/**
5+
* A local predicate that always holds for the overlay variant and never holds for the base variant.
6+
* This is used to define local predicates that behave differently for the base and overlay variant.
7+
*/
8+
private predicate isOverlay() { databaseMetadata("isOverlay", "true") }
9+
10+
private string getXmlFile(@xmllocatable locatable) {
11+
exists(@location_default location, @file file | xmllocations(locatable, location) |
12+
locations_default(location, file, _, _, _, _) and
13+
files(file, result)
14+
)
15+
}
16+
17+
private string getXmlFileInBase(@xmllocatable locatable) {
18+
not isOverlay() and
19+
result = getXmlFile(locatable)
20+
}
21+
22+
/**
23+
* Holds if the given `file` was extracted as part of the overlay and was extracted by the HTML/XML
24+
* extractor.
25+
*/
26+
private predicate overlayXmlExtracted(string file) {
27+
isOverlay() and
28+
exists(@xmllocatable locatable |
29+
not files(locatable, _) and not xmlNs(locatable, _, _, _) and file = getXmlFile(locatable)
30+
)
31+
}
32+
33+
/**
34+
* Holds if the given XML `locatable` should be discarded, because it is part of the overlay base
35+
* and is in a file that was also extracted as part of the overlay database.
36+
*/
37+
overlay[discard_entity]
38+
private predicate discardXmlLocatable(@xmllocatable locatable) {
39+
exists(string file | file = getXmlFileInBase(locatable) |
40+
overlayChangedFiles(file)
41+
or
42+
// The HTML/XML extractor is currently not incremental and may extract more files than those
43+
// included in overlayChangedFiles.
44+
overlayXmlExtracted(file)
45+
)
46+
}

0 commit comments

Comments
 (0)