Skip to content

Commit 06ae0c4

Browse files
authored
Merge pull request #864 from jbj/ir-TIRVariable-shared
Approved by dave-bartolomeo
2 parents dc20924 + 3e03835 commit 06ae0c4

File tree

6 files changed

+50
-121
lines changed

6 files changed

+50
-121
lines changed

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

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,7 @@ import FunctionIR
33
import cpp
44
import semmle.code.cpp.ir.implementation.TempVariableTag
55
private import semmle.code.cpp.ir.internal.TempVariableTag
6-
7-
private newtype TIRVariable =
8-
TIRAutomaticUserVariable(LocalScopeVariable var, FunctionIR funcIR) {
9-
exists(Function func |
10-
func = funcIR.getFunction() and
11-
(
12-
var.getFunction() = func or
13-
var.(Parameter).getCatchBlock().getEnclosingFunction() = func
14-
)
15-
)
16-
} or
17-
TIRStaticUserVariable(Variable var, FunctionIR funcIR) {
18-
(
19-
var instanceof GlobalOrNamespaceVariable or
20-
var instanceof MemberVariable and not var instanceof Field
21-
) and
22-
exists(VariableAccess access |
23-
access.getTarget() = var and
24-
access.getEnclosingFunction() = funcIR.getFunction()
25-
)
26-
} or
27-
TIRTempVariable(FunctionIR funcIR, Locatable ast, TempVariableTag tag,
28-
Type type) {
29-
Construction::hasTempVariable(funcIR.getFunction(), ast, tag, type)
30-
}
6+
private import semmle.code.cpp.ir.internal.TIRVariable
317

328
IRUserVariable getIRUserVariable(Function func, Variable var) {
339
result.getVariable() = var and
@@ -40,7 +16,7 @@ IRUserVariable getIRUserVariable(Function func, Variable var) {
4016
* generated by the AST-to-IR translation (`IRTempVariable`).
4117
*/
4218
abstract class IRVariable extends TIRVariable {
43-
FunctionIR funcIR;
19+
Function func;
4420

4521
abstract string toString();
4622

@@ -72,14 +48,14 @@ abstract class IRVariable extends TIRVariable {
7248
* Gets the IR for the function that references this variable.
7349
*/
7450
final FunctionIR getFunctionIR() {
75-
result = funcIR
51+
result.getFunction() = func
7652
}
7753

7854
/**
7955
* Gets the function that references this variable.
8056
*/
8157
final Function getFunction() {
82-
result = funcIR.getFunction()
58+
result = func
8359
}
8460
}
8561

@@ -126,7 +102,7 @@ class IRAutomaticUserVariable extends IRUserVariable, IRAutomaticVariable,
126102
LocalScopeVariable localVar;
127103

128104
IRAutomaticUserVariable() {
129-
this = TIRAutomaticUserVariable(localVar, funcIR) and
105+
this = TIRAutomaticUserVariable(localVar, func) and
130106
var = localVar
131107
}
132108

@@ -137,7 +113,7 @@ class IRAutomaticUserVariable extends IRUserVariable, IRAutomaticVariable,
137113

138114
class IRStaticUserVariable extends IRUserVariable, TIRStaticUserVariable {
139115
IRStaticUserVariable() {
140-
this = TIRStaticUserVariable(var, funcIR)
116+
this = TIRStaticUserVariable(var, func)
141117
}
142118
}
143119

@@ -152,7 +128,7 @@ class IRTempVariable extends IRVariable, IRAutomaticVariable, TIRTempVariable {
152128
Type type;
153129

154130
IRTempVariable() {
155-
this = TIRTempVariable(funcIR, ast, tag, type)
131+
this = TIRTempVariable(func, ast, tag, type)
156132
}
157133

158134
override final Type getType() {

cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/internal/SSAConstruction.qll

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,8 @@ cached private module Cached {
4040
}
4141

4242
private IRVariable getNewIRVariable(OldIR::IRVariable var) {
43-
result.getFunction() = var.getFunction() and
44-
(
45-
exists(OldIR::IRUserVariable userVar, IRUserVariable newUserVar |
46-
userVar = var and
47-
newUserVar.getVariable() = userVar.getVariable() and
48-
result = newUserVar
49-
) or
50-
exists(OldIR::IRTempVariable tempVar, IRTempVariable newTempVar |
51-
tempVar = var and
52-
newTempVar.getAST() = tempVar.getAST() and
53-
newTempVar.getTag() = tempVar.getTag() and
54-
result = newTempVar
55-
)
56-
)
43+
// This is just a type cast. Both classes derive from the same newtype.
44+
result = var
5745
}
5846

5947
cached newtype TInstruction =

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

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,7 @@ import FunctionIR
33
import cpp
44
import semmle.code.cpp.ir.implementation.TempVariableTag
55
private import semmle.code.cpp.ir.internal.TempVariableTag
6-
7-
private newtype TIRVariable =
8-
TIRAutomaticUserVariable(LocalScopeVariable var, FunctionIR funcIR) {
9-
exists(Function func |
10-
func = funcIR.getFunction() and
11-
(
12-
var.getFunction() = func or
13-
var.(Parameter).getCatchBlock().getEnclosingFunction() = func
14-
)
15-
)
16-
} or
17-
TIRStaticUserVariable(Variable var, FunctionIR funcIR) {
18-
(
19-
var instanceof GlobalOrNamespaceVariable or
20-
var instanceof MemberVariable and not var instanceof Field
21-
) and
22-
exists(VariableAccess access |
23-
access.getTarget() = var and
24-
access.getEnclosingFunction() = funcIR.getFunction()
25-
)
26-
} or
27-
TIRTempVariable(FunctionIR funcIR, Locatable ast, TempVariableTag tag,
28-
Type type) {
29-
Construction::hasTempVariable(funcIR.getFunction(), ast, tag, type)
30-
}
6+
private import semmle.code.cpp.ir.internal.TIRVariable
317

328
IRUserVariable getIRUserVariable(Function func, Variable var) {
339
result.getVariable() = var and
@@ -40,7 +16,7 @@ IRUserVariable getIRUserVariable(Function func, Variable var) {
4016
* generated by the AST-to-IR translation (`IRTempVariable`).
4117
*/
4218
abstract class IRVariable extends TIRVariable {
43-
FunctionIR funcIR;
19+
Function func;
4420

4521
abstract string toString();
4622

@@ -72,14 +48,14 @@ abstract class IRVariable extends TIRVariable {
7248
* Gets the IR for the function that references this variable.
7349
*/
7450
final FunctionIR getFunctionIR() {
75-
result = funcIR
51+
result.getFunction() = func
7652
}
7753

7854
/**
7955
* Gets the function that references this variable.
8056
*/
8157
final Function getFunction() {
82-
result = funcIR.getFunction()
58+
result = func
8359
}
8460
}
8561

@@ -126,7 +102,7 @@ class IRAutomaticUserVariable extends IRUserVariable, IRAutomaticVariable,
126102
LocalScopeVariable localVar;
127103

128104
IRAutomaticUserVariable() {
129-
this = TIRAutomaticUserVariable(localVar, funcIR) and
105+
this = TIRAutomaticUserVariable(localVar, func) and
130106
var = localVar
131107
}
132108

@@ -137,7 +113,7 @@ class IRAutomaticUserVariable extends IRUserVariable, IRAutomaticVariable,
137113

138114
class IRStaticUserVariable extends IRUserVariable, TIRStaticUserVariable {
139115
IRStaticUserVariable() {
140-
this = TIRStaticUserVariable(var, funcIR)
116+
this = TIRStaticUserVariable(var, func)
141117
}
142118
}
143119

@@ -152,7 +128,7 @@ class IRTempVariable extends IRVariable, IRAutomaticVariable, TIRTempVariable {
152128
Type type;
153129

154130
IRTempVariable() {
155-
this = TIRTempVariable(funcIR, ast, tag, type)
131+
this = TIRTempVariable(func, ast, tag, type)
156132
}
157133

158134
override final Type getType() {

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

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,7 @@ import FunctionIR
33
import cpp
44
import semmle.code.cpp.ir.implementation.TempVariableTag
55
private import semmle.code.cpp.ir.internal.TempVariableTag
6-
7-
private newtype TIRVariable =
8-
TIRAutomaticUserVariable(LocalScopeVariable var, FunctionIR funcIR) {
9-
exists(Function func |
10-
func = funcIR.getFunction() and
11-
(
12-
var.getFunction() = func or
13-
var.(Parameter).getCatchBlock().getEnclosingFunction() = func
14-
)
15-
)
16-
} or
17-
TIRStaticUserVariable(Variable var, FunctionIR funcIR) {
18-
(
19-
var instanceof GlobalOrNamespaceVariable or
20-
var instanceof MemberVariable and not var instanceof Field
21-
) and
22-
exists(VariableAccess access |
23-
access.getTarget() = var and
24-
access.getEnclosingFunction() = funcIR.getFunction()
25-
)
26-
} or
27-
TIRTempVariable(FunctionIR funcIR, Locatable ast, TempVariableTag tag,
28-
Type type) {
29-
Construction::hasTempVariable(funcIR.getFunction(), ast, tag, type)
30-
}
6+
private import semmle.code.cpp.ir.internal.TIRVariable
317

328
IRUserVariable getIRUserVariable(Function func, Variable var) {
339
result.getVariable() = var and
@@ -40,7 +16,7 @@ IRUserVariable getIRUserVariable(Function func, Variable var) {
4016
* generated by the AST-to-IR translation (`IRTempVariable`).
4117
*/
4218
abstract class IRVariable extends TIRVariable {
43-
FunctionIR funcIR;
19+
Function func;
4420

4521
abstract string toString();
4622

@@ -72,14 +48,14 @@ abstract class IRVariable extends TIRVariable {
7248
* Gets the IR for the function that references this variable.
7349
*/
7450
final FunctionIR getFunctionIR() {
75-
result = funcIR
51+
result.getFunction() = func
7652
}
7753

7854
/**
7955
* Gets the function that references this variable.
8056
*/
8157
final Function getFunction() {
82-
result = funcIR.getFunction()
58+
result = func
8359
}
8460
}
8561

@@ -126,7 +102,7 @@ class IRAutomaticUserVariable extends IRUserVariable, IRAutomaticVariable,
126102
LocalScopeVariable localVar;
127103

128104
IRAutomaticUserVariable() {
129-
this = TIRAutomaticUserVariable(localVar, funcIR) and
105+
this = TIRAutomaticUserVariable(localVar, func) and
130106
var = localVar
131107
}
132108

@@ -137,7 +113,7 @@ class IRAutomaticUserVariable extends IRUserVariable, IRAutomaticVariable,
137113

138114
class IRStaticUserVariable extends IRUserVariable, TIRStaticUserVariable {
139115
IRStaticUserVariable() {
140-
this = TIRStaticUserVariable(var, funcIR)
116+
this = TIRStaticUserVariable(var, func)
141117
}
142118
}
143119

@@ -152,7 +128,7 @@ class IRTempVariable extends IRVariable, IRAutomaticVariable, TIRTempVariable {
152128
Type type;
153129

154130
IRTempVariable() {
155-
this = TIRTempVariable(funcIR, ast, tag, type)
131+
this = TIRTempVariable(func, ast, tag, type)
156132
}
157133

158134
override final Type getType() {

cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/internal/SSAConstruction.qll

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,8 @@ cached private module Cached {
4040
}
4141

4242
private IRVariable getNewIRVariable(OldIR::IRVariable var) {
43-
result.getFunction() = var.getFunction() and
44-
(
45-
exists(OldIR::IRUserVariable userVar, IRUserVariable newUserVar |
46-
userVar = var and
47-
newUserVar.getVariable() = userVar.getVariable() and
48-
result = newUserVar
49-
) or
50-
exists(OldIR::IRTempVariable tempVar, IRTempVariable newTempVar |
51-
tempVar = var and
52-
newTempVar.getAST() = tempVar.getAST() and
53-
newTempVar.getTag() = tempVar.getTag() and
54-
result = newTempVar
55-
)
56-
)
43+
// This is just a type cast. Both classes derive from the same newtype.
44+
result = var
5745
}
5846

5947
cached newtype TInstruction =
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
private import cpp
2+
private import semmle.code.cpp.ir.implementation.TempVariableTag
3+
private import semmle.code.cpp.ir.implementation.raw.internal.IRConstruction as Construction
4+
5+
newtype TIRVariable =
6+
TIRAutomaticUserVariable(LocalScopeVariable var, Function func) {
7+
Construction::functionHasIR(func) and
8+
var.getFunction() = func or
9+
var.(Parameter).getCatchBlock().getEnclosingFunction() = func
10+
} or
11+
TIRStaticUserVariable(Variable var, Function func) {
12+
Construction::functionHasIR(func) and
13+
(
14+
var instanceof GlobalOrNamespaceVariable or
15+
var instanceof MemberVariable and not var instanceof Field
16+
) and
17+
exists(VariableAccess access |
18+
access.getTarget() = var and
19+
access.getEnclosingFunction() = func
20+
)
21+
} or
22+
TIRTempVariable(Function func, Locatable ast, TempVariableTag tag, Type type) {
23+
Construction::hasTempVariable(func, ast, tag, type)
24+
}
25+

0 commit comments

Comments
 (0)