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

Skip to content

Commit 367827a

Browse files
authored
Merge pull request #2541 from max-schaefer/unify-xml-qlls
C++/Java/JavaScript/Python: Unify XML libraries.
2 parents 66d49a4 + 81f51e4 commit 367827a

5 files changed

Lines changed: 328 additions & 179 deletions

File tree

config/identical-files.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,5 +265,11 @@
265265
"C# IR ValueNumberingImports": [
266266
"csharp/ql/src/semmle/code/csharp/ir/implementation/raw/gvn/internal/ValueNumberingImports.qll",
267267
"csharp/ql/src/semmle/code/csharp/ir/implementation/unaliased_ssa/gvn/internal/ValueNumberingImports.qll"
268+
],
269+
"XML": [
270+
"cpp/ql/src/semmle/code/cpp/XML.qll",
271+
"java/ql/src/semmle/code/xml/XML.qll",
272+
"javascript/ql/src/semmle/javascript/XML.qll",
273+
"python/ql/src/semmle/python/xml/XML.qll"
268274
]
269275
}

cpp/ql/src/semmle/code/cpp/XML.qll

Lines changed: 77 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,30 @@
22
* Provides classes and predicates for working with XML files and their content.
33
*/
44

5-
import semmle.code.cpp.Location
5+
import semmle.files.FileSystem
66

77
/** An XML element that has a location. */
88
abstract class XMLLocatable extends @xmllocatable {
99
/** Gets the source location for this element. */
1010
Location getLocation() { xmllocations(this, result) }
1111

1212
/**
13-
* Holds if this element has the specified location information,
14-
* including file path, start line, start column, end line and end column.
13+
* Holds if this element is at the specified location.
14+
* The location spans column `startcolumn` of line `startline` to
15+
* column `endcolumn` of line `endline` in file `filepath`.
16+
* For more information, see
17+
* [Locations](https://help.semmle.com/QL/learn-ql/ql/locations.html).
1518
*/
1619
predicate hasLocationInfo(
1720
string filepath, int startline, int startcolumn, int endline, int endcolumn
1821
) {
1922
exists(File f, Location l | l = this.getLocation() |
20-
locations_default(l, unresolveElement(f), startline, startcolumn, endline, endcolumn) and
23+
locations_default(l, f, startline, startcolumn, endline, endcolumn) and
2124
filepath = f.getAbsolutePath()
2225
)
2326
}
2427

25-
/** Gets a printable representation of this element. */
28+
/** Gets a textual representation of this element. */
2629
abstract string toString();
2730
}
2831

@@ -112,15 +115,21 @@ class XMLFile extends XMLParent, File {
112115
override string toString() { result = XMLParent.super.toString() }
113116

114117
/** Gets the name of this XML file. */
115-
override string getName() { files(this, result, _, _, _) }
118+
override string getName() { result = File.super.getAbsolutePath() }
116119

117-
/** Gets the path of this XML file. */
118-
string getPath() { files(this, _, result, _, _) }
120+
/**
121+
* DEPRECATED: Use `getAbsolutePath()` instead.
122+
*
123+
* Gets the path of this XML file.
124+
*/
125+
deprecated string getPath() { result = getAbsolutePath() }
119126

120-
/** Gets the path of the folder that contains this XML file. */
121-
string getFolder() {
122-
result = this.getPath().substring(0, this.getPath().length() - this.getName().length())
123-
}
127+
/**
128+
* DEPRECATED: Use `getParentContainer().getAbsolutePath()` instead.
129+
*
130+
* Gets the path of the folder that contains this XML file.
131+
*/
132+
deprecated string getFolder() { result = getParentContainer().getAbsolutePath() }
124133

125134
/** Gets the encoding of this XML file. */
126135
string getEncoding() { xmlEncoding(this, result) }
@@ -135,7 +144,17 @@ class XMLFile extends XMLParent, File {
135144
XMLDTD getADTD() { xmlDTDs(result, _, _, _, this) }
136145
}
137146

138-
/** A "Document Type Definition" of an XML file. */
147+
/**
148+
* An XML document type definition (DTD).
149+
*
150+
* Example:
151+
*
152+
* ```
153+
* <!ELEMENT person (firstName, lastName?)>
154+
* <!ELEMENT firstName (#PCDATA)>
155+
* <!ELEMENT lastName (#PCDATA)>
156+
* ```
157+
*/
139158
class XMLDTD extends @xmldtd {
140159
/** Gets the name of the root element of this DTD. */
141160
string getRoot() { xmlDTDs(this, result, _, _, _) }
@@ -162,7 +181,17 @@ class XMLDTD extends @xmldtd {
162181
}
163182
}
164183

165-
/** An XML tag in an XML file. */
184+
/**
185+
* An XML element in an XML file.
186+
*
187+
* Example:
188+
*
189+
* ```
190+
* <manifest xmlns:android="http://schemas.android.com/apk/res/android"
191+
* package="com.example.exampleapp" android:versionCode="1">
192+
* </manifest>
193+
* ```
194+
*/
166195
class XMLElement extends @xmlelement, XMLParent, XMLLocatable {
167196
/** Holds if this XML element has the given `name`. */
168197
predicate hasName(string name) { name = getName() }
@@ -207,7 +236,16 @@ class XMLElement extends @xmlelement, XMLParent, XMLLocatable {
207236
override string toString() { result = XMLParent.super.toString() }
208237
}
209238

210-
/** An attribute that occurs inside an XML element. */
239+
/**
240+
* An attribute that occurs inside an XML element.
241+
*
242+
* Examples:
243+
*
244+
* ```
245+
* package="com.example.exampleapp"
246+
* android:versionCode="1"
247+
* ```
248+
*/
211249
class XMLAttribute extends @xmlattribute, XMLLocatable {
212250
/** Gets the name of this attribute. */
213251
string getName() { xmlAttrs(this, _, result, _, _, _) }
@@ -228,7 +266,15 @@ class XMLAttribute extends @xmlattribute, XMLLocatable {
228266
override string toString() { result = this.getName() + "=" + this.getValue() }
229267
}
230268

231-
/** A namespace used in an XML file */
269+
/**
270+
* A namespace used in an XML file.
271+
*
272+
* Example:
273+
*
274+
* ```
275+
* xmlns:android="http://schemas.android.com/apk/res/android"
276+
* ```
277+
*/
232278
class XMLNamespace extends @xmlnamespace {
233279
/** Gets the prefix of this namespace. */
234280
string getPrefix() { xmlNs(this, result, _, _) }
@@ -247,7 +293,15 @@ class XMLNamespace extends @xmlnamespace {
247293
}
248294
}
249295

250-
/** A comment of the form `<!-- ... -->` is an XML comment. */
296+
/**
297+
* A comment in an XML file.
298+
*
299+
* Example:
300+
*
301+
* ```
302+
* <!-- This is a comment. -->
303+
* ```
304+
*/
251305
class XMLComment extends @xmlcomment, XMLLocatable {
252306
/** Gets the text content of this XML comment. */
253307
string getText() { xmlComments(this, result, _, _) }
@@ -262,6 +316,12 @@ class XMLComment extends @xmlcomment, XMLLocatable {
262316
/**
263317
* A sequence of characters that occurs between opening and
264318
* closing tags of an XML element, excluding other elements.
319+
*
320+
* Example:
321+
*
322+
* ```
323+
* <content>This is a sequence of characters.</content>
324+
* ```
265325
*/
266326
class XMLCharacters extends @xmlcharacters, XMLLocatable {
267327
/** Gets the content of this character sequence. */

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Provides classes and predicates for working with XML files and their content.
33
*/
44

5-
import semmle.code.Location
5+
import semmle.files.FileSystem
66

77
/** An XML element that has a location. */
88
abstract class XMLLocatable extends @xmllocatable {
@@ -88,7 +88,10 @@ class XMLParent extends @xmlparent {
8888
)
8989
}
9090

91-
/** Append all the character sequences of this XML parent from left to right, separated by a space. */
91+
/**
92+
* Gets the result of appending all the character sequences of this XML parent from
93+
* left to right, separated by a space.
94+
*/
9295
string allCharactersString() {
9396
result = concat(string chars, int pos |
9497
xmlChars(_, chars, this, pos, _, _)
@@ -114,6 +117,20 @@ class XMLFile extends XMLParent, File {
114117
/** Gets the name of this XML file. */
115118
override string getName() { result = File.super.getAbsolutePath() }
116119

120+
/**
121+
* DEPRECATED: Use `getAbsolutePath()` instead.
122+
*
123+
* Gets the path of this XML file.
124+
*/
125+
deprecated string getPath() { result = getAbsolutePath() }
126+
127+
/**
128+
* DEPRECATED: Use `getParentContainer().getAbsolutePath()` instead.
129+
*
130+
* Gets the path of the folder that contains this XML file.
131+
*/
132+
deprecated string getFolder() { result = getParentContainer().getAbsolutePath() }
133+
117134
/** Gets the encoding of this XML file. */
118135
string getEncoding() { xmlEncoding(this, result) }
119136

javascript/ql/src/semmle/javascript/XML.qll

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Provides classes and predicates for working with XML files and their content.
33
*/
44

5-
import javascript
5+
import semmle.files.FileSystem
66

77
/** An XML element that has a location. */
88
abstract class XMLLocatable extends @xmllocatable {
@@ -88,7 +88,10 @@ class XMLParent extends @xmlparent {
8888
)
8989
}
9090

91-
/** Append all the character sequences of this XML parent from left to right, separated by a space. */
91+
/**
92+
* Gets the result of appending all the character sequences of this XML parent from
93+
* left to right, separated by a space.
94+
*/
9295
string allCharactersString() {
9396
result = concat(string chars, int pos |
9497
xmlChars(_, chars, this, pos, _, _)
@@ -114,6 +117,20 @@ class XMLFile extends XMLParent, File {
114117
/** Gets the name of this XML file. */
115118
override string getName() { result = File.super.getAbsolutePath() }
116119

120+
/**
121+
* DEPRECATED: Use `getAbsolutePath()` instead.
122+
*
123+
* Gets the path of this XML file.
124+
*/
125+
deprecated string getPath() { result = getAbsolutePath() }
126+
127+
/**
128+
* DEPRECATED: Use `getParentContainer().getAbsolutePath()` instead.
129+
*
130+
* Gets the path of the folder that contains this XML file.
131+
*/
132+
deprecated string getFolder() { result = getParentContainer().getAbsolutePath() }
133+
117134
/** Gets the encoding of this XML file. */
118135
string getEncoding() { xmlEncoding(this, result) }
119136

0 commit comments

Comments
 (0)