Skip to content

Commit bc48c25

Browse files
author
Dave Bartolomeo
committed
C++/C#: Make IRVariable and its derived classes non-abstract
1 parent 12daa76 commit bc48c25

File tree

5 files changed

+270
-45
lines changed

5 files changed

+270
-45
lines changed

cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/IRVariable.qll

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,16 @@ IRUserVariable getIRUserVariable(Language::Function func, Language::Variable var
1717
* be a user-declared variable (`IRUserVariable`) or a temporary variable
1818
* generated by the AST-to-IR translation (`IRTempVariable`).
1919
*/
20-
abstract class IRVariable extends TIRVariable {
20+
class IRVariable extends TIRVariable {
2121
Language::Function func;
2222

23-
abstract string toString();
23+
IRVariable() {
24+
this = TIRUserVariable(_, _, func) or
25+
this = TIRTempVariable(func, _, _, _) or
26+
this = TIRStringLiteral(func, _, _, _)
27+
}
28+
29+
string toString() { none() }
2430

2531
/**
2632
* Holds if this variable's value cannot be changed within a function. Currently used for string
@@ -41,19 +47,19 @@ abstract class IRVariable extends TIRVariable {
4147
/**
4248
* Gets the type of the variable.
4349
*/
44-
abstract Language::LanguageType getLanguageType();
50+
Language::LanguageType getLanguageType() { none() }
4551

4652
/**
4753
* Gets the AST node that declared this variable, or that introduced this
4854
* variable as part of the AST-to-IR translation.
4955
*/
50-
abstract Language::AST getAST();
56+
Language::AST getAST() { none() }
5157

5258
/**
5359
* Gets an identifier string for the variable. This identifier is unique
5460
* within the function.
5561
*/
56-
abstract string getUniqueId();
62+
string getUniqueId() { none() }
5763

5864
/**
5965
* Gets the source location of this variable.
@@ -101,16 +107,31 @@ class IRUserVariable extends IRVariable, TIRUserVariable {
101107
* stack. This includes all parameters, non-static local variables, and
102108
* temporary variables.
103109
*/
104-
abstract class IRAutomaticVariable extends IRVariable { }
110+
class IRAutomaticVariable extends IRVariable {
111+
IRAutomaticVariable() {
112+
exists(Language::Variable var |
113+
this = TIRUserVariable(var, _, func) and
114+
Language::isVariableAutomatic(var)
115+
) or
116+
this = TIRTempVariable(func, _, _, _)
117+
}
118+
}
105119

120+
/**
121+
* Represents a user-declared variable that is allocated on the stack. This
122+
* includes all parameters and non-static local variables.
123+
*/
106124
class IRAutomaticUserVariable extends IRUserVariable, IRAutomaticVariable {
107125
override Language::AutomaticVariable var;
108126

109-
IRAutomaticUserVariable() { Language::isVariableAutomatic(var) }
110-
111127
final override Language::AutomaticVariable getVariable() { result = var }
112128
}
113129

130+
/**
131+
* Represents a user-declared variable that is not allocated on the stack. This
132+
* includes all global variables, namespace-scope variables, static fields, and
133+
* static local variables.
134+
*/
114135
class IRStaticUserVariable extends IRUserVariable {
115136
override Language::StaticVariable var;
116137

@@ -119,10 +140,19 @@ class IRStaticUserVariable extends IRUserVariable {
119140
final override Language::StaticVariable getVariable() { result = var }
120141
}
121142

122-
abstract class IRGeneratedVariable extends IRVariable {
143+
/**
144+
* Represents a variable that is not user-declared. This includes temporary
145+
* variables generated as part of IR construction, as well as string literals.
146+
*/
147+
class IRGeneratedVariable extends IRVariable {
123148
Language::AST ast;
124149
Language::LanguageType type;
125150

151+
IRGeneratedVariable() {
152+
this = TIRTempVariable(func, ast, _, type) or
153+
this = TIRStringLiteral(func, ast, type, _)
154+
}
155+
126156
final override Language::LanguageType getLanguageType() { result = type }
127157

128158
final override Language::AST getAST() { result = ast }
@@ -144,6 +174,11 @@ IRTempVariable getIRTempVariable(Language::AST ast, TempVariableTag tag) {
144174
result.getTag() = tag
145175
}
146176

177+
/**
178+
* Represents a temporary variable introduced by IR construction. The most common examples are the
179+
* variable generated to hold the return value of afunction, or the variable generated to hold the
180+
* result of a condition operator (`a ? b : c`).
181+
*/
147182
class IRTempVariable extends IRGeneratedVariable, IRAutomaticVariable, TIRTempVariable {
148183
TempVariableTag tag;
149184

@@ -158,18 +193,28 @@ class IRTempVariable extends IRGeneratedVariable, IRAutomaticVariable, TIRTempVa
158193
override string getBaseString() { result = "#temp" }
159194
}
160195

196+
/**
197+
* The temporary variable generated to hold the return value of a function.
198+
*/
161199
class IRReturnVariable extends IRTempVariable {
162200
IRReturnVariable() { tag = ReturnValueTempVar() }
163201

164202
final override string toString() { result = "#return" }
165203
}
166204

205+
/**
206+
* The temporary variable generated to hold the exception thrown by a `ThrowValue` instruction.
207+
*/
167208
class IRThrowVariable extends IRTempVariable {
168209
IRThrowVariable() { tag = ThrowTempVar() }
169210

170211
override string getBaseString() { result = "#throw" }
171212
}
172213

214+
/**
215+
* The variable generated to represent the contents of a string literal. This variable acts much
216+
* like a read-only global variable.
217+
*/
173218
class IRStringLiteral extends IRGeneratedVariable, TIRStringLiteral {
174219
Language::StringLiteral literal;
175220

cpp/ql/src/semmle/code/cpp/ir/implementation/raw/IRVariable.qll

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,16 @@ IRUserVariable getIRUserVariable(Language::Function func, Language::Variable var
1717
* be a user-declared variable (`IRUserVariable`) or a temporary variable
1818
* generated by the AST-to-IR translation (`IRTempVariable`).
1919
*/
20-
abstract class IRVariable extends TIRVariable {
20+
class IRVariable extends TIRVariable {
2121
Language::Function func;
2222

23-
abstract string toString();
23+
IRVariable() {
24+
this = TIRUserVariable(_, _, func) or
25+
this = TIRTempVariable(func, _, _, _) or
26+
this = TIRStringLiteral(func, _, _, _)
27+
}
28+
29+
string toString() { none() }
2430

2531
/**
2632
* Holds if this variable's value cannot be changed within a function. Currently used for string
@@ -41,19 +47,19 @@ abstract class IRVariable extends TIRVariable {
4147
/**
4248
* Gets the type of the variable.
4349
*/
44-
abstract Language::LanguageType getLanguageType();
50+
Language::LanguageType getLanguageType() { none() }
4551

4652
/**
4753
* Gets the AST node that declared this variable, or that introduced this
4854
* variable as part of the AST-to-IR translation.
4955
*/
50-
abstract Language::AST getAST();
56+
Language::AST getAST() { none() }
5157

5258
/**
5359
* Gets an identifier string for the variable. This identifier is unique
5460
* within the function.
5561
*/
56-
abstract string getUniqueId();
62+
string getUniqueId() { none() }
5763

5864
/**
5965
* Gets the source location of this variable.
@@ -101,16 +107,31 @@ class IRUserVariable extends IRVariable, TIRUserVariable {
101107
* stack. This includes all parameters, non-static local variables, and
102108
* temporary variables.
103109
*/
104-
abstract class IRAutomaticVariable extends IRVariable { }
110+
class IRAutomaticVariable extends IRVariable {
111+
IRAutomaticVariable() {
112+
exists(Language::Variable var |
113+
this = TIRUserVariable(var, _, func) and
114+
Language::isVariableAutomatic(var)
115+
) or
116+
this = TIRTempVariable(func, _, _, _)
117+
}
118+
}
105119

120+
/**
121+
* Represents a user-declared variable that is allocated on the stack. This
122+
* includes all parameters and non-static local variables.
123+
*/
106124
class IRAutomaticUserVariable extends IRUserVariable, IRAutomaticVariable {
107125
override Language::AutomaticVariable var;
108126

109-
IRAutomaticUserVariable() { Language::isVariableAutomatic(var) }
110-
111127
final override Language::AutomaticVariable getVariable() { result = var }
112128
}
113129

130+
/**
131+
* Represents a user-declared variable that is not allocated on the stack. This
132+
* includes all global variables, namespace-scope variables, static fields, and
133+
* static local variables.
134+
*/
114135
class IRStaticUserVariable extends IRUserVariable {
115136
override Language::StaticVariable var;
116137

@@ -119,10 +140,19 @@ class IRStaticUserVariable extends IRUserVariable {
119140
final override Language::StaticVariable getVariable() { result = var }
120141
}
121142

122-
abstract class IRGeneratedVariable extends IRVariable {
143+
/**
144+
* Represents a variable that is not user-declared. This includes temporary
145+
* variables generated as part of IR construction, as well as string literals.
146+
*/
147+
class IRGeneratedVariable extends IRVariable {
123148
Language::AST ast;
124149
Language::LanguageType type;
125150

151+
IRGeneratedVariable() {
152+
this = TIRTempVariable(func, ast, _, type) or
153+
this = TIRStringLiteral(func, ast, type, _)
154+
}
155+
126156
final override Language::LanguageType getLanguageType() { result = type }
127157

128158
final override Language::AST getAST() { result = ast }
@@ -144,6 +174,11 @@ IRTempVariable getIRTempVariable(Language::AST ast, TempVariableTag tag) {
144174
result.getTag() = tag
145175
}
146176

177+
/**
178+
* Represents a temporary variable introduced by IR construction. The most common examples are the
179+
* variable generated to hold the return value of afunction, or the variable generated to hold the
180+
* result of a condition operator (`a ? b : c`).
181+
*/
147182
class IRTempVariable extends IRGeneratedVariable, IRAutomaticVariable, TIRTempVariable {
148183
TempVariableTag tag;
149184

@@ -158,18 +193,28 @@ class IRTempVariable extends IRGeneratedVariable, IRAutomaticVariable, TIRTempVa
158193
override string getBaseString() { result = "#temp" }
159194
}
160195

196+
/**
197+
* The temporary variable generated to hold the return value of a function.
198+
*/
161199
class IRReturnVariable extends IRTempVariable {
162200
IRReturnVariable() { tag = ReturnValueTempVar() }
163201

164202
final override string toString() { result = "#return" }
165203
}
166204

205+
/**
206+
* The temporary variable generated to hold the exception thrown by a `ThrowValue` instruction.
207+
*/
167208
class IRThrowVariable extends IRTempVariable {
168209
IRThrowVariable() { tag = ThrowTempVar() }
169210

170211
override string getBaseString() { result = "#throw" }
171212
}
172213

214+
/**
215+
* The variable generated to represent the contents of a string literal. This variable acts much
216+
* like a read-only global variable.
217+
*/
173218
class IRStringLiteral extends IRGeneratedVariable, TIRStringLiteral {
174219
Language::StringLiteral literal;
175220

0 commit comments

Comments
 (0)