Skip to content

Commit 46332d4

Browse files
committed
C++: Eliminate recursion from toString().
1 parent 33f4503 commit 46332d4

File tree

4 files changed

+39
-25
lines changed

4 files changed

+39
-25
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ class Namespace extends NameQualifyingElement, @namespace {
7979
/** Gets the metric namespace. */
8080
MetricNamespace getMetrics() { result = this }
8181

82-
override string toString() { result = this.getQualifiedName() }
82+
/** Gets a version of the `QualifiedName` that is more suitable for display purposes. */
83+
string getFriendlyName() { result = this.getQualifiedName() }
84+
85+
final override string toString() { result = getFriendlyName() }
8386

8487
/** Gets a declaration of (part of) this namespace. */
8588
NamespaceDeclarationEntry getADeclarationEntry() { result.getNamespace() = this }
@@ -104,7 +107,7 @@ class NamespaceDeclarationEntry extends Locatable, @namespace_decl {
104107
namespace_decls(underlyingElement(this), unresolveElement(result), _, _)
105108
}
106109

107-
override string toString() { result = this.getNamespace().toString() }
110+
override string toString() { result = this.getNamespace().getFriendlyName() }
108111

109112
/**
110113
* Gets the location of the token preceding the namespace declaration
@@ -150,7 +153,7 @@ class UsingDeclarationEntry extends UsingEntry {
150153
*/
151154
Declaration getDeclaration() { usings(underlyingElement(this), unresolveElement(result), _) }
152155

153-
override string toString() { result = "using " + this.getDeclaration().toString() }
156+
override string toString() { result = "using declaration" }
154157
}
155158

156159
/**
@@ -169,7 +172,9 @@ class UsingDirectiveEntry extends UsingEntry {
169172
*/
170173
Namespace getNamespace() { usings(underlyingElement(this), unresolveElement(result), _) }
171174

172-
override string toString() { result = "using namespace " + this.getNamespace().toString() }
175+
override string toString() {
176+
result = "using namespace " + this.getNamespace().getFriendlyName()
177+
}
173178
}
174179

175180
/**
@@ -204,7 +209,7 @@ class GlobalNamespace extends Namespace {
204209
*/
205210
deprecated string getFullName() { result = this.getName() }
206211

207-
override string toString() { result = "(global namespace)" }
212+
override string getFriendlyName() { result = "(global namespace)" }
208213
}
209214

210215
/**

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

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -260,24 +260,33 @@ class ParameterDeclarationEntry extends VariableDeclarationEntry {
260260
*/
261261
int getIndex() { param_decl_bind(underlyingElement(this), result, _) }
262262

263+
string getAnonymousParameterDescription() {
264+
not exists(getName()) and
265+
exists(string idx |
266+
idx =
267+
((getIndex() + 1).toString() + "th")
268+
.replaceAll("1th", "1st")
269+
.replaceAll("2th", "2nd")
270+
.replaceAll("3th", "3rd")
271+
.replaceAll("11st", "11th")
272+
.replaceAll("12nd", "12th")
273+
.replaceAll("13rd", "13th") and
274+
if exists(getCanonicalName())
275+
then result = "declaration of " + getCanonicalName() + " as anonymous " + idx + " parameter"
276+
else result = "declaration of " + idx + " parameter"
277+
)
278+
}
279+
263280
override string toString() {
264-
if exists(getName())
265-
then result = super.toString()
266-
else
267-
exists(string idx |
268-
idx =
269-
((getIndex() + 1).toString() + "th")
270-
.replaceAll("1th", "1st")
271-
.replaceAll("2th", "2nd")
272-
.replaceAll("3th", "3rd")
273-
.replaceAll("11st", "11th")
274-
.replaceAll("12nd", "12th")
275-
.replaceAll("13rd", "13th")
276-
|
277-
if exists(getCanonicalName())
278-
then result = "declaration of " + getCanonicalName() + " as anonymous " + idx + " parameter"
279-
else result = "declaration of " + idx + " parameter"
280-
)
281+
isDefinition() and
282+
result = "definition of " + getName()
283+
or
284+
not isDefinition() and
285+
if getName() = getCanonicalName()
286+
then result = "declaration of " + getName()
287+
else result = "declaration of " + getCanonicalName() + " as " + getName()
288+
or
289+
result = getAnonymousParameterDescription()
281290
}
282291

283292
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class XMLFile extends XMLParent, File {
116116
XMLFile() { xmlEncoding(this, _) }
117117

118118
/** Gets a printable representation of this XML file. */
119-
override string toString() { result = XMLParent.super.toString() }
119+
override string toString() { result = getName() }
120120

121121
/** Gets the name of this XML file. */
122122
override string getName() { result = File.super.getAbsolutePath() }
@@ -236,7 +236,7 @@ class XMLElement extends @xmlelement, XMLParent, XMLLocatable {
236236
string getAttributeValue(string name) { result = this.getAttribute(name).getValue() }
237237

238238
/** Gets a printable representation of this XML element. */
239-
override string toString() { result = XMLParent.super.toString() }
239+
override string toString() { result = getName() }
240240
}
241241

242242
/**

cpp/ql/src/semmle/code/cpp/exprs/Lambda.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class Closure extends Class {
9999
* ```
100100
*/
101101
class LambdaCapture extends Locatable, @lambdacapture {
102-
override string toString() { result = getField().toString() }
102+
override string toString() { result = getField().getName() }
103103

104104
override string getCanonicalQLClass() { result = "LambdaCapture" }
105105

0 commit comments

Comments
 (0)