Skip to content

Commit df4bd36

Browse files
authored
Merge pull request #236 from aschackmull/xml-qll/use-concat
Approved by hvitved, xiemaisi, yh-semmle
2 parents c36e7f0 + 9198f5b commit df4bd36

File tree

4 files changed

+185
-186
lines changed
  • cpp/ql/src/semmle/code/cpp
  • csharp/ql/src/semmle/code/csharp
  • javascript/ql/src/semmle/javascript
  • java/ql/src/semmle/code/xml

4 files changed

+185
-186
lines changed

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

Lines changed: 65 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
/**
2-
* A library for working with XML files and their content.
2+
* Provides classes and predicates for working with XML files and their content.
33
*/
44

55
import semmle.code.cpp.Location
66

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

1212
/**
13-
* Whether this element has the specified location information,
13+
* Holds if this element has the specified location information,
1414
* including file path, start line, start column, end line and end column.
1515
*/
1616
predicate hasLocationInfo(string filepath, int startline, int startcolumn, int endline, int endcolumn) {
@@ -20,7 +20,7 @@ abstract class XMLLocatable extends @xmllocatable {
2020
)
2121
}
2222

23-
/** A printable representation of this element. */
23+
/** Gets a printable representation of this element. */
2424
abstract string toString();
2525
}
2626

@@ -30,46 +30,49 @@ abstract class XMLLocatable extends @xmllocatable {
3030
*/
3131
class XMLParent extends @xmlparent {
3232
/**
33-
* A printable representation of this XML parent.
33+
* Gets a printable representation of this XML parent.
3434
* (Intended to be overridden in subclasses.)
3535
*/
3636
abstract string getName();
3737

38-
/** The file to which this XML parent belongs. */
38+
/** Gets the file to which this XML parent belongs. */
3939
XMLFile getFile() { result = this or xmlElements(this,_,_,_,result) }
4040

41-
/** The child element at a specified index of this XML parent. */
41+
/** Gets the child element at a specified index of this XML parent. */
4242
XMLElement getChild(int index) { xmlElements(result, _, this, index, _) }
4343

44-
/** A child element of this XML parent. */
44+
/** Gets a child element of this XML parent. */
4545
XMLElement getAChild() { xmlElements(result,_,this,_,_) }
4646

47-
/** A child element of this XML parent with the given `name`. */
47+
/** Gets a child element of this XML parent with the given `name`. */
4848
XMLElement getAChild(string name) { xmlElements(result,_,this,_,_) and result.hasName(name) }
4949

50-
/** A comment that is a child of this XML parent. */
50+
/** Gets a comment that is a child of this XML parent. */
5151
XMLComment getAComment() { xmlComments(result,_,this,_) }
5252

53-
/** A character sequence that is a child of this XML parent. */
53+
/** Gets a character sequence that is a child of this XML parent. */
5454
XMLCharacters getACharactersSet() { xmlChars(result,_,this,_,_,_) }
5555

56-
/** The depth in the tree. (Overridden in XMLElement.) */
56+
/** Gets the depth in the tree. (Overridden in XMLElement.) */
5757
int getDepth() { result = 0 }
5858

59-
/** The number of child XML elements of this XML parent. */
59+
/** Gets the number of child XML elements of this XML parent. */
6060
int getNumberOfChildren() {
6161
result = count(XMLElement e | xmlElements(e,_,this,_,_))
6262
}
6363

64-
/** The number of places in the body of this XML parent where text occurs. */
64+
/** Gets the number of places in the body of this XML parent where text occurs. */
6565
int getNumberOfCharacterSets() {
6666
result = count(int pos | xmlChars(_,_,this,pos,_,_))
6767
}
6868

6969
/**
70+
* DEPRECATED: Internal.
71+
*
7072
* Append the character sequences of this XML parent from left to right, separated by a space,
7173
* up to a specified (zero-based) index.
7274
*/
75+
deprecated
7376
string charsSetUpTo(int n) {
7477
(n = 0 and xmlChars(_,result,this,0,_,_)) or
7578
(n > 0 and exists(string chars | xmlChars(_,chars,this,n,_,_) |
@@ -78,18 +81,15 @@ class XMLParent extends @xmlparent {
7881

7982
/** Append all the character sequences of this XML parent from left to right, separated by a space. */
8083
string allCharactersString() {
81-
exists(int n | n = this.getNumberOfCharacterSets() |
82-
(n = 0 and result = "") or
83-
(n > 0 and result = this.charsSetUpTo(n-1))
84-
)
84+
result = concat(string chars, int pos | xmlChars(_, chars, this, pos, _, _) | chars, " " order by pos)
8585
}
8686

87-
/** The text value contained in this XML parent. */
87+
/** Gets the text value contained in this XML parent. */
8888
string getTextValue() {
8989
result = allCharactersString()
9090
}
9191

92-
/** A printable representation of this XML parent. */
92+
/** Gets a printable representation of this XML parent. */
9393
string toString() { result = this.getName() }
9494
}
9595

@@ -99,54 +99,54 @@ class XMLFile extends XMLParent, File {
9999
xmlEncoding(this,_)
100100
}
101101

102-
/** A printable representation of this XML file. */
102+
/** Gets a printable representation of this XML file. */
103103
override
104104
string toString() { result = XMLParent.super.toString() }
105105

106-
/** The name of this XML file. */
106+
/** Gets the name of this XML file. */
107107
override
108108
string getName() { files(this,result,_,_,_) }
109109

110-
/** The path of this XML file. */
110+
/** Gets the path of this XML file. */
111111
string getPath() { files(this,_,result,_,_) }
112112

113-
/** The path of the folder that contains this XML file. */
113+
/** Gets the path of the folder that contains this XML file. */
114114
string getFolder() {
115115
result = this.getPath().substring(0, this.getPath().length()-this.getName().length())
116116
}
117117

118-
/** The encoding of this XML file. */
118+
/** Gets the encoding of this XML file. */
119119
string getEncoding() { xmlEncoding(this,result) }
120120

121-
/** The XML file itself. */
121+
/** Gets the XML file itself. */
122122
override
123123
XMLFile getFile() { result = this }
124124

125-
/** A top-most element in an XML file. */
125+
/** Gets a top-most element in an XML file. */
126126
XMLElement getARootElement() { result = this.getAChild() }
127127

128-
/** A DTD associated with this XML file. */
128+
/** Gets a DTD associated with this XML file. */
129129
XMLDTD getADTD() { xmlDTDs(result,_,_,_,this) }
130130
}
131131

132132
/** A "Document Type Definition" of an XML file. */
133133
class XMLDTD extends @xmldtd {
134-
/** The name of the root element of this DTD. */
134+
/** Gets the name of the root element of this DTD. */
135135
string getRoot() { xmlDTDs(this,result,_,_,_) }
136136

137-
/** The public ID of this DTD. */
137+
/** Gets the public ID of this DTD. */
138138
string getPublicId() { xmlDTDs(this,_,result,_,_) }
139139

140-
/** The system ID of this DTD. */
140+
/** Gets the system ID of this DTD. */
141141
string getSystemId() { xmlDTDs(this,_,_,result,_) }
142142

143-
/** Whether this DTD is public. */
143+
/** Holds if this DTD is public. */
144144
predicate isPublic() { not xmlDTDs(this,_,"",_,_) }
145145

146-
/** The parent of this DTD. */
146+
/** Gets the parent of this DTD. */
147147
XMLParent getParent() { xmlDTDs(this,_,_,_,result) }
148148

149-
/** A printable representation of this DTD. */
149+
/** Gets a printable representation of this DTD. */
150150
string toString() {
151151
(this.isPublic() and result = this.getRoot() + " PUBLIC '" +
152152
this.getPublicId() + "' '" +
@@ -159,92 +159,92 @@ class XMLDTD extends @xmldtd {
159159

160160
/** An XML tag in an XML file. */
161161
class XMLElement extends @xmlelement, XMLParent, XMLLocatable {
162-
/** Whether this XML element has the given `name`. */
162+
/** Holds if this XML element has the given `name`. */
163163
predicate hasName(string name) { name = getName() }
164164

165-
/** The name of this XML element. */
165+
/** Gets the name of this XML element. */
166166
override
167167
string getName() { xmlElements(this,result,_,_,_) }
168168

169-
/** The XML file in which this XML element occurs. */
169+
/** Gets the XML file in which this XML element occurs. */
170170
override
171171
XMLFile getFile() { xmlElements(this,_,_,_,result) }
172172

173-
/** The parent of this XML element. */
173+
/** Gets the parent of this XML element. */
174174
XMLParent getParent() { xmlElements(this,_,result,_,_) }
175175

176-
/** The index of this XML element among its parent's children. */
176+
/** Gets the index of this XML element among its parent's children. */
177177
int getIndex() { xmlElements(this, _, _, result, _) }
178178

179-
/** Whether this XML element has a namespace. */
179+
/** Holds if this XML element has a namespace. */
180180
predicate hasNamespace() { xmlHasNs(this,_,_) }
181181

182-
/** The namespace of this XML element, if any. */
182+
/** Gets the namespace of this XML element, if any. */
183183
XMLNamespace getNamespace() { xmlHasNs(this,result,_) }
184184

185-
/** The index of this XML element among its parent's children. */
185+
/** Gets the index of this XML element among its parent's children. */
186186
int getElementPositionIndex() { xmlElements(this,_,_,result,_) }
187187

188-
/** The depth of this element within the XML file tree structure. */
188+
/** Gets the depth of this element within the XML file tree structure. */
189189
override
190190
int getDepth() { result = this.getParent().getDepth() + 1 }
191191

192-
/** An XML attribute of this XML element. */
192+
/** Gets an XML attribute of this XML element. */
193193
XMLAttribute getAnAttribute() { result.getElement() = this }
194194

195-
/** The attribute with the specified `name`, if any. */
195+
/** Gets the attribute with the specified `name`, if any. */
196196
XMLAttribute getAttribute(string name) {
197197
result.getElement() = this and result.getName() = name
198198
}
199199

200-
/** Whether this XML element has an attribute with the specified `name`. */
200+
/** Holds if this XML element has an attribute with the specified `name`. */
201201
predicate hasAttribute(string name) {
202202
exists(XMLAttribute a| a = this.getAttribute(name))
203203
}
204204

205-
/** The value of the attribute with the specified `name`, if any. */
205+
/** Gets the value of the attribute with the specified `name`, if any. */
206206
string getAttributeValue(string name) {
207207
result = this.getAttribute(name).getValue()
208208
}
209209

210-
/** A printable representation of this XML element. */
210+
/** Gets a printable representation of this XML element. */
211211
override
212212
string toString() { result = XMLParent.super.toString() }
213213
}
214214

215215
/** An attribute that occurs inside an XML element. */
216216
class XMLAttribute extends @xmlattribute, XMLLocatable {
217-
/** The name of this attribute. */
217+
/** Gets the name of this attribute. */
218218
string getName() { xmlAttrs(this,_,result,_,_,_) }
219219

220-
/** The XML element to which this attribute belongs. */
220+
/** Gets the XML element to which this attribute belongs. */
221221
XMLElement getElement() { xmlAttrs(this,result,_,_,_,_) }
222222

223-
/** Whether this attribute has a namespace. */
223+
/** Holds if this attribute has a namespace. */
224224
predicate hasNamespace() { xmlHasNs(this,_,_) }
225225

226-
/** The namespace of this attribute, if any. */
226+
/** Gets the namespace of this attribute, if any. */
227227
XMLNamespace getNamespace() { xmlHasNs(this,result,_) }
228228

229-
/** The value of this attribute. */
229+
/** Gets the value of this attribute. */
230230
string getValue() { xmlAttrs(this,_,_,result,_,_) }
231231

232-
/** A printable representation of this XML attribute. */
232+
/** Gets a printable representation of this XML attribute. */
233233
override string toString() { result = this.getName() + "=" + this.getValue() }
234234
}
235235

236236
/** A namespace used in an XML file */
237237
class XMLNamespace extends @xmlnamespace {
238-
/** The prefix of this namespace. */
238+
/** Gets the prefix of this namespace. */
239239
string getPrefix() { xmlNs(this,result,_,_) }
240240

241-
/** The URI of this namespace. */
241+
/** Gets the URI of this namespace. */
242242
string getURI() { xmlNs(this,_,result,_) }
243243

244-
/** Whether this namespace has no prefix. */
244+
/** Holds if this namespace has no prefix. */
245245
predicate isDefault() { this.getPrefix() = "" }
246246

247-
/** A printable representation of this XML namespace. */
247+
/** Gets a printable representation of this XML namespace. */
248248
string toString() {
249249
(this.isDefault() and result = this.getURI()) or
250250
(not this.isDefault() and result = this.getPrefix() + ":" + this.getURI())
@@ -253,13 +253,13 @@ class XMLNamespace extends @xmlnamespace {
253253

254254
/** A comment of the form `<!-- ... -->` is an XML comment. */
255255
class XMLComment extends @xmlcomment, XMLLocatable {
256-
/** The text content of this XML comment. */
256+
/** Gets the text content of this XML comment. */
257257
string getText() { xmlComments(this,result,_,_) }
258258

259-
/** The parent of this XML comment. */
259+
/** Gets the parent of this XML comment. */
260260
XMLParent getParent() { xmlComments(this,_,result,_) }
261261

262-
/** A printable representation of this XML comment. */
262+
/** Gets a printable representation of this XML comment. */
263263
override string toString() { result = this.getText() }
264264
}
265265

@@ -268,15 +268,15 @@ class XMLComment extends @xmlcomment, XMLLocatable {
268268
* closing tags of an XML element, excluding other elements.
269269
*/
270270
class XMLCharacters extends @xmlcharacters, XMLLocatable {
271-
/** The content of this character sequence. */
271+
/** Gets the content of this character sequence. */
272272
string getCharacters() { xmlChars(this,result,_,_,_,_) }
273273

274-
/** The parent of this character sequence. */
274+
/** Gets the parent of this character sequence. */
275275
XMLParent getParent() { xmlChars(this,_,result,_,_,_) }
276276

277-
/** Whether this character sequence is CDATA. */
277+
/** Holds if this character sequence is CDATA. */
278278
predicate isCDATA() { xmlChars(this,_,_,_,1,_) }
279279

280-
/** A printable representation of this XML character sequence. */
280+
/** Gets a printable representation of this XML character sequence. */
281281
override string toString() { result = this.getCharacters() }
282282
}

0 commit comments

Comments
 (0)