Skip to content

Commit 5deeda0

Browse files
authored
Merge pull request github#3387 from geoffw0/tostringperf
C++: Eliminate recursion from toString().
2 parents f049945 + 3e2e69c commit 5deeda0

File tree

14 files changed

+74
-57
lines changed

14 files changed

+74
-57
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,12 @@ class Declaration extends Locatable, @declaration {
9898
this.hasQualifiedName(namespaceQualifier, "", baseName)
9999
}
100100

101-
override string toString() { result = this.getName() }
101+
/**
102+
* Gets a description of this `Declaration` for display purposes.
103+
*/
104+
string getDescription() { result = this.getName() }
105+
106+
final override string toString() { result = this.getDescription() }
102107

103108
/**
104109
* Gets the name of this declaration.

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

Lines changed: 8 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 " + this.getDeclaration().getDescription() }
154157
}
155158

156159
/**
@@ -169,7 +172,7 @@ 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() { result = "using namespace " + this.getNamespace().getFriendlyName() }
173176
}
174177

175178
/**
@@ -204,7 +207,7 @@ class GlobalNamespace extends Namespace {
204207
*/
205208
deprecated string getFullName() { result = this.getName() }
206209

207-
override string toString() { result = "(global namespace)" }
210+
override string getFriendlyName() { result = "(global namespace)" }
208211
}
209212

210213
/**

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+
private 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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class Closure extends Class {
8686
result.getName() = "operator()"
8787
}
8888

89-
override string toString() { result = "decltype([...](...){...})" }
89+
override string getDescription() { result = "decltype([...](...){...})" }
9090
}
9191

9292
/**
@@ -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

cpp/ql/test/library-tests/templates/isfromtemplateinstantiation/isfromtemplateinstantiation.ql

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import cpp
22

33
class FunctionMonkeyPatch extends Function {
44
language[monotonicAggregates]
5-
override string toString() {
5+
override string getDescription() {
66
exists(string name, string templateArgs, string args |
77
result = name + templateArgs + args and
88
name = this.getQualifiedName() and
@@ -30,7 +30,9 @@ class FunctionMonkeyPatch extends Function {
3030
}
3131

3232
class ParameterMonkeyPatch extends Parameter {
33-
override string toString() { result = super.getType().getName() + " " + super.toString() }
33+
override string getDescription() {
34+
result = super.getType().getName() + " " + super.getDescription()
35+
}
3436
}
3537

3638
from Element e, Element ti
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
| templates.cpp:9:5:9:14 | using c |
2-
| usings.cpp:8:1:8:11 | using nf |
3-
| usings.cpp:9:1:9:17 | using namespace N |
4-
| usings.cpp:18:3:18:13 | using bf |
5-
| usings.cpp:21:5:21:14 | using gf |
6-
| usings.cpp:34:3:34:20 | using tbf |
7-
| usings.cpp:42:5:42:22 | using foo |
1+
| templates.cpp:9:5:9:14 | using c | UsingDeclarationEntry, enclosingElement:std |
2+
| usings.cpp:8:1:8:11 | using nf | UsingDeclarationEntry, enclosingElement:(global namespace) |
3+
| usings.cpp:9:1:9:17 | using namespace N | UsingDirectiveEntry, enclosingElement:(global namespace) |
4+
| usings.cpp:18:3:18:13 | using bf | UsingDeclarationEntry, enclosingElement:D |
5+
| usings.cpp:21:5:21:14 | using gf | UsingDeclarationEntry, enclosingElement:{ ... } |
6+
| usings.cpp:34:3:34:20 | using tbf | UsingDeclarationEntry, enclosingElement:TD |
7+
| usings.cpp:42:5:42:22 | using foo | UsingDeclarationEntry, enclosingElement:nsbar |
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
import cpp
22

3+
string describe(UsingEntry ue) {
4+
ue instanceof UsingDeclarationEntry and
5+
result = "UsingDeclarationEntry"
6+
or
7+
ue instanceof UsingDirectiveEntry and
8+
result = "UsingDirectiveEntry"
9+
or
10+
result = "enclosingElement:" + ue.getEnclosingElement().toString()
11+
}
12+
313
from UsingEntry ue
4-
select ue
14+
select ue, concat(describe(ue), ", ")

cpp/ql/test/library-tests/usings/Usings2.expected

Lines changed: 0 additions & 7 deletions
This file was deleted.

cpp/ql/test/library-tests/usings/Usings2.ql

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)