Skip to content

Commit 36b42de

Browse files
authored
Merge pull request #1841 from ian-semmle/valuetext
C++: Split valuetext off into its own table
2 parents 11da14c + c08eb7e commit 36b42de

File tree

8 files changed

+4063
-254
lines changed

8 files changed

+4063
-254
lines changed

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,32 @@ class Expr extends StmtParent, @expr {
8585
override string toString() { none() }
8686

8787
/** Gets the value of this expression, if it is a constant. */
88-
string getValue() { exists(@value v | values(v,result,_) and valuebind(v,underlyingElement(this))) }
88+
string getValue() { exists(@value v | values(v,result) and valuebind(v,underlyingElement(this))) }
89+
90+
/** Gets the value text of this expression that's in the database. */
91+
private string getDbValueText() {
92+
exists(@value v | valuebind(v,underlyingElement(this)) and valuetext(v, result))
93+
}
94+
95+
/**
96+
* Gets the value text of `this`. If it doesn't have one, then instead
97+
* gets the value text is `this`'s nearest compatible conversion, if any.
98+
*/
99+
private string getValueTextFollowingConversions() {
100+
if exists(this.getDbValueText())
101+
then result = this.getDbValueText()
102+
else exists(Expr e |
103+
e = this.getConversion() and
104+
e.getValue() = this.getValue() and
105+
result = e.getValueTextFollowingConversions())
106+
}
89107

90108
/** Gets the source text for the value of this expression, if it is a constant. */
91-
string getValueText() { exists(@value v | values(v,_,result) and valuebind(v,underlyingElement(this))) }
109+
string getValueText() {
110+
if exists(this.getValueTextFollowingConversions())
111+
then result = this.getValueTextFollowingConversions()
112+
else result = this.getValue()
113+
}
92114

93115
/** Holds if this expression has a value that can be determined at compile time. */
94116
cached

cpp/ql/src/semmlecode.cpp.dbscheme

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,11 +1237,15 @@ expr_cond_false(
12371237
int false: @expr ref
12381238
);
12391239

1240-
// the second field is a string representation of the value
1241-
// the third field is the actual text in the source or the same as the second field
1240+
/** A string representation of the value. */
12421241
values(
12431242
unique int id: @value,
1244-
string str: string ref,
1243+
string str: string ref
1244+
);
1245+
1246+
/** The actual text in the source code for the value, if any. */
1247+
valuetext(
1248+
unique int id: @value ref,
12451249
string text: string ref
12461250
);
12471251

0 commit comments

Comments
 (0)