Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b6287b9

Browse files
author
AndreiDiaconu1
committed
Preliminary refactoring
Some preliminary refactoring of the TranslatedDeclaration.qll file
1 parent 1e4b3fa commit b6287b9

6 files changed

Lines changed: 79 additions & 134 deletions

File tree

csharp/ql/src/semmle/code/csharp/ir/implementation/raw/internal/TranslatedDeclarationEntry.qll

Lines changed: 25 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -12,87 +12,44 @@ private import semmle.code.csharp.ir.internal.IRCSharpLanguage as Language
1212
* Gets the `TranslatedDeclarationEntry` that represents the declaration
1313
* `entry`.
1414
*/
15-
TranslatedDeclarationEntry getTranslatedDeclarationEntry(Declaration entry) {
16-
result.getAST() = entry
15+
TranslatedLocalDeclaration getTranslatedLocalDeclaration(LocalVariableDeclExpr declExpr) {
16+
result.getAST() = declExpr
1717
}
1818

1919
/**
2020
* Represents the IR translation of a declaration within the body of a function.
21-
* Most often, this is the declaration of an automatic local variable, although
22-
* it can also be the declaration of a static local variable, an extern
23-
* variable, or an extern function.
2421
*/
25-
// TODO: Make sure local decls are handeld correctly (seem to be)
26-
abstract class TranslatedDeclarationEntry extends TranslatedElement, TTranslatedDeclarationEntry {
27-
Declaration entry;
22+
abstract class TranslatedLocalDeclaration extends TranslatedElement, TTranslatedDeclarationEntry {
23+
LocalVariableDeclExpr expr;
2824

29-
TranslatedDeclarationEntry() {
30-
this = TTranslatedDeclarationEntry(entry)
25+
TranslatedLocalDeclaration() {
26+
this = TTranslatedDeclarationEntry(expr)
3127
}
3228

3329
override final Callable getFunction() {
34-
exists(LocalVariableDeclExpr expr |
35-
expr.getVariable() = entry and
36-
result = expr.getEnclosingCallable()
37-
)
30+
result = expr.getEnclosingCallable()
3831
}
3932

4033
override final string toString() {
41-
result = entry.toString()
34+
result = expr.toString()
4235
}
4336

4437
override final Language::AST getAST() {
45-
result = entry
46-
}
47-
}
48-
49-
/**
50-
* Represents the IR translation of a declaration within the body of a function,
51-
* for declarations other than local variables. Since these have no semantic
52-
* effect, they are translated as `NoOp`.
53-
*/
54-
class TranslatedNonVariableDeclarationEntry extends TranslatedDeclarationEntry {
55-
TranslatedNonVariableDeclarationEntry() {
56-
not entry instanceof LocalVariable
57-
}
58-
59-
override predicate hasInstruction(Opcode opcode, InstructionTag tag,
60-
Type resultType, boolean isLValue) {
61-
opcode instanceof Opcode::NoOp and
62-
tag = OnlyInstructionTag() and
63-
resultType instanceof Language::UnknownType and
64-
isLValue = false
65-
}
66-
67-
override Instruction getFirstInstruction() {
68-
result = getInstruction(OnlyInstructionTag())
69-
}
70-
71-
override TranslatedElement getChild(int id) {
72-
none()
73-
}
74-
75-
override Instruction getInstructionSuccessor(InstructionTag tag,
76-
EdgeKind kind) {
77-
tag = OnlyInstructionTag() and
78-
result = getParent().getChildSuccessor(this) and
79-
kind instanceof GotoEdge
80-
}
81-
82-
override Instruction getChildSuccessor(TranslatedElement child) {
83-
none()
38+
result = expr
8439
}
8540
}
8641

8742
/**
8843
* Represents the IR translation of the declaration of a local variable,
8944
* including its initialization, if any.
9045
*/
91-
abstract class TranslatedVariableDeclaration extends TranslatedElement, InitializationContext {
92-
/**
93-
* Gets the local variable being declared.
94-
*/
95-
abstract LocalVariable getVariable();
46+
class TranslatedLocalVariableDeclaration extends TranslatedLocalDeclaration,
47+
InitializationContext {
48+
LocalVariable var;
49+
50+
TranslatedLocalVariableDeclaration() {
51+
var = expr.getVariable()
52+
}
9653

9754
override TranslatedElement getChild(int id) {
9855
id = 0 and result = getInitialization()
@@ -107,14 +64,14 @@ abstract class TranslatedVariableDeclaration extends TranslatedElement, Initiali
10764
(
10865
tag = InitializerVariableAddressTag() and
10966
opcode instanceof Opcode::VariableAddress and
110-
resultType = getVariableType(getVariable()) and
67+
resultType = getVariableType(var) and
11168
isLValue = true
11269
) or
11370
(
11471
hasUninitializedInstruction() and
11572
tag = InitializerStoreTag() and
11673
opcode instanceof Opcode::Uninitialized and
117-
resultType = getVariableType(getVariable()) and
74+
resultType = getVariableType(var) and
11875
isLValue = false
11976
)
12077
}
@@ -149,7 +106,7 @@ abstract class TranslatedVariableDeclaration extends TranslatedElement, Initiali
149106
tag = InitializerVariableAddressTag() or
150107
hasUninitializedInstruction() and tag = InitializerStoreTag()
151108
) and
152-
result = getIRUserVariable(getFunction(), getVariable())
109+
result = getIRUserVariable(getFunction(), var)
153110
}
154111

155112
override Instruction getInstructionOperand(InstructionTag tag, OperandTag operandTag) {
@@ -164,18 +121,18 @@ abstract class TranslatedVariableDeclaration extends TranslatedElement, Initiali
164121
}
165122

166123
override Type getTargetType() {
167-
result = getVariableType(getVariable())
124+
result = getVariableType(var)
168125
}
169126

170127
// TODO: All declarations which use an initializer will need a special case here
171128
private TranslatedInitialization getInitialization() {
172129
// First complex initializations
173-
if (getVariable().getInitializer() instanceof ArrayCreation) then
174-
result = getTranslatedInitialization(getVariable().getInitializer().(ArrayCreation).getInitializer())
175-
else if (getVariable().getInitializer() instanceof ObjectCreation) then
176-
result = getTranslatedInitialization(getVariable().getInitializer())
130+
if (var.getInitializer() instanceof ArrayCreation) then
131+
result = getTranslatedInitialization(var.getInitializer().(ArrayCreation).getInitializer())
132+
else if (var.getInitializer() instanceof ObjectCreation) then
133+
result = getTranslatedInitialization(var.getInitializer())
177134
else // then the simple variable initialization
178-
result = getTranslatedInitialization(getVariable().getInitializer())
135+
result = getTranslatedInitialization(var.getInitializer())
179136
}
180137

181138
private predicate hasUninitializedInstruction() {
@@ -184,22 +141,6 @@ abstract class TranslatedVariableDeclaration extends TranslatedElement, Initiali
184141
}
185142
}
186143

187-
/**
188-
* Represents the IR translation of a local variable declaration within a declaration statement.
189-
*/
190-
class TranslatedVariableDeclarationEntry extends TranslatedVariableDeclaration,
191-
TranslatedDeclarationEntry {
192-
LocalVariable var;
193-
194-
TranslatedVariableDeclarationEntry() {
195-
var = entry
196-
}
197-
198-
override LocalVariable getVariable() {
199-
result = var
200-
}
201-
}
202-
203144
///**
204145
// * Gets the `TranslatedRangeBasedForVariableDeclaration` that represents the declaration of
205146
// * `var`.

csharp/ql/src/semmle/code/csharp/ir/implementation/raw/internal/TranslatedElement.qll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,7 @@ newtype TTranslatedElement =
334334
)
335335
} or
336336
// A local declaration
337-
TTranslatedDeclarationEntry(Declaration entry) {
338-
exists(LocalVariableDeclExpr declExpr | declExpr.getVariable() = entry)
339-
}
337+
TTranslatedDeclarationEntry(LocalVariableDeclExpr entry)
340338

341339
/**
342340
* Gets the index of the first explicitly initialized element in `initList`

csharp/ql/src/semmle/code/csharp/ir/implementation/raw/internal/TranslatedExpr.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import csharp
22
private import semmle.code.csharp.ir.implementation.Opcode
33
private import semmle.code.csharp.ir.implementation.internal.OperandTag
44
private import semmle.code.csharp.ir.internal.TempVariableTag
5+
private import semmle.code.csharp.ir.internal.IRUtilities
56
private import InstructionTag
67
private import TranslatedCondition
78
private import TranslatedDeclarationEntry

csharp/ql/src/semmle/code/csharp/ir/implementation/raw/internal/TranslatedInitialization.qll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* Class that deals with variable initializations.
3+
* Separated from `TranslatedExpr` for clarity.
4+
*/
5+
16
import csharp
27
private import semmle.code.csharp.ir.implementation.Opcode
38
private import semmle.code.csharp.ir.implementation.internal.OperandTag

csharp/ql/src/semmle/code/csharp/ir/implementation/raw/internal/TranslatedStmt.qll

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class TranslatedDeclStmt extends TranslatedStmt {
7474
override LocalVariableDeclStmt stmt;
7575

7676
override TranslatedElement getChild(int id) {
77-
result = getDeclarationEntry(id)
77+
result = getLocalDeclaration(id)
7878
}
7979

8080
override predicate hasInstruction(Opcode opcode, InstructionTag tag,
@@ -83,15 +83,15 @@ class TranslatedDeclStmt extends TranslatedStmt {
8383
}
8484

8585
override Instruction getFirstInstruction() {
86-
result = getDeclarationEntry(0).getFirstInstruction() //REVIEW: Empty?
86+
result = getLocalDeclaration(0).getFirstInstruction() //REVIEW: Empty?
8787
}
8888

8989
private int getChildCount() {
9090
result = count(stmt.getAVariableDeclExpr())
9191
}
9292

93-
private TranslatedDeclarationEntry getDeclarationEntry(int index) {
94-
result = getTranslatedDeclarationEntry(stmt.getVariableDeclExpr(index).getVariable())
93+
private TranslatedLocalDeclaration getLocalDeclaration(int index) {
94+
result = getTranslatedLocalDeclaration(stmt.getVariableDeclExpr(index))
9595
}
9696

9797
override Instruction getInstructionSuccessor(InstructionTag tag,
@@ -101,11 +101,11 @@ class TranslatedDeclStmt extends TranslatedStmt {
101101

102102
override Instruction getChildSuccessor(TranslatedElement child) {
103103
exists(int index |
104-
child = getDeclarationEntry(index) and
104+
child = getLocalDeclaration(index) and
105105
if index = (getChildCount() - 1) then
106106
result = getParent().getChildSuccessor(this)
107107
else
108-
result = getDeclarationEntry(index + 1).getFirstInstruction()
108+
result = getLocalDeclaration(index + 1).getFirstInstruction()
109109
)
110110
}
111111
}
@@ -429,8 +429,8 @@ class TranslatedCatchByTypeClause extends TranslatedClause {
429429
result = stmt.(SpecificCatchClause).getVariable().getType()
430430
}
431431

432-
private TranslatedDeclarationEntry getParameter() {
433-
result = getTranslatedDeclarationEntry(stmt.(SpecificCatchClause).getVariable())
432+
private TranslatedLocalDeclaration getParameter() {
433+
result = getTranslatedLocalDeclaration(stmt.(SpecificCatchClause).getVariableDeclExpr())
434434
}
435435
}
436436

csharp/ql/test/library-tests/ir/ir/raw_ir.expected

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -136,46 +136,46 @@ array.cs:
136136
# 9| v0_86(Void) = ExitFunction :
137137

138138
crement.cs:
139-
# 2| CrementOpsTest.Main()
139+
# 2| Main
140140
# 2| Block 0
141-
# 2| v0_0(Void) = EnterFunction :
142-
# 2| mu0_1(Object) = AliasedDefinition :
143-
# 2| mu0_2(Object) = UnmodeledDefinition :
144-
# 2| r0_3(lval<CrementOpsTest>) = InitializeThis :
145-
# 3| r0_4(lval<Int32>) = VariableAddress[x] :
146-
# 3| r0_5(Int32) = Constant[10] :
147-
# 3| mu0_6(Int32) = Store : &:r0_4, r0_5
148-
# 4| r0_7(lval<Int32>) = VariableAddress[a] :
149-
# 4| r0_8(lval<Int32>) = VariableAddress[x] :
150-
# 4| r0_9(Int32) = Load : &:r0_8, ~mu0_2
151-
# 4| r0_10(Int32) = Constant[1] :
152-
# 4| r0_11(Int32) = Add : r0_9, r0_10
153-
# 4| mu0_12(Int32) = Store : &:r0_8, r0_11
154-
# 4| mu0_13(Int32) = Store : &:r0_7, r0_9
155-
# 5| r0_14(lval<Int32>) = VariableAddress[b] :
156-
# 5| r0_15(lval<Int32>) = VariableAddress[x] :
157-
# 5| r0_16(Int32) = Load : &:r0_15, ~mu0_2
158-
# 5| r0_17(Int32) = Constant[1] :
159-
# 5| r0_18(Int32) = Sub : r0_16, r0_17
160-
# 5| mu0_19(Int32) = Store : &:r0_15, r0_18
161-
# 5| mu0_20(Int32) = Store : &:r0_14, r0_18
162-
# 6| r0_21(lval<Int32>) = VariableAddress[c] :
163-
# 6| r0_22(lval<Int32>) = VariableAddress[x] :
164-
# 6| r0_23(Int32) = Load : &:r0_22, ~mu0_2
165-
# 6| r0_24(Int32) = Constant[1] :
166-
# 6| r0_25(Int32) = Add : r0_23, r0_24
167-
# 6| mu0_26(Int32) = Store : &:r0_22, r0_25
168-
# 6| mu0_27(Int32) = Store : &:r0_21, r0_25
169-
# 7| r0_28(lval<Int32>) = VariableAddress[x] :
170-
# 7| r0_29(Int32) = Load : &:r0_28, ~mu0_2
171-
# 7| r0_30(Int32) = Constant[1] :
172-
# 7| r0_31(Int32) = Sub : r0_29, r0_30
173-
# 7| mu0_32(Int32) = Store : &:r0_28, r0_31
174-
# 7| r0_33(lval<Int32>) = VariableAddress[x] :
175-
# 7| mu0_34(Int32) = Store : &:r0_33, r0_29
176-
# 2| v0_35(Void) = ReturnVoid :
177-
# 2| v0_36(Void) = UnmodeledUse : mu*
178-
# 2| v0_37(Void) = ExitFunction :
141+
# 2| v0_0(Void) = EnterFunction :
142+
# 2| mu0_1(null) = AliasedDefinition :
143+
# 2| mu0_2(null) = UnmodeledDefinition :
144+
# 2| r0_3(glval<CrementOpsTest>) = InitializeThis :
145+
# 3| r0_4(glval<Int32>) = VariableAddress[x] :
146+
# 3| r0_5(Int32) = Constant[10] :
147+
# 3| mu0_6(Int32) = Store : &:r0_4, r0_5
148+
# 4| r0_7(glval<Int32>) = VariableAddress[a] :
149+
# 4| r0_8(glval<Int32>) = VariableAddress[x] :
150+
# 4| r0_9(Int32) = Load : &:r0_8, ~mu0_2
151+
# 4| r0_10(Int32) = Constant[1] :
152+
# 4| r0_11(Int32) = Add : r0_9, r0_10
153+
# 4| mu0_12(Int32) = Store : &:r0_8, r0_11
154+
# 4| mu0_13(Int32) = Store : &:r0_7, r0_9
155+
# 5| r0_14(glval<Int32>) = VariableAddress[b] :
156+
# 5| r0_15(glval<Int32>) = VariableAddress[x] :
157+
# 5| r0_16(Int32) = Load : &:r0_15, ~mu0_2
158+
# 5| r0_17(Int32) = Constant[1] :
159+
# 5| r0_18(Int32) = Sub : r0_16, r0_17
160+
# 5| mu0_19(Int32) = Store : &:r0_15, r0_18
161+
# 5| mu0_20(Int32) = Store : &:r0_14, r0_18
162+
# 6| r0_21(glval<Int32>) = VariableAddress[c] :
163+
# 6| r0_22(glval<Int32>) = VariableAddress[x] :
164+
# 6| r0_23(Int32) = Load : &:r0_22, ~mu0_2
165+
# 6| r0_24(Int32) = Constant[1] :
166+
# 6| r0_25(Int32) = Add : r0_23, r0_24
167+
# 6| mu0_26(Int32) = Store : &:r0_22, r0_25
168+
# 6| mu0_27(Int32) = Store : &:r0_21, r0_25
169+
# 7| r0_28(glval<Int32>) = VariableAddress[x] :
170+
# 7| r0_29(Int32) = Load : &:r0_28, ~mu0_2
171+
# 7| r0_30(Int32) = Constant[1] :
172+
# 7| r0_31(Int32) = Sub : r0_29, r0_30
173+
# 7| mu0_32(Int32) = Store : &:r0_28, r0_31
174+
# 7| r0_33(glval<Int32>) = VariableAddress[x] :
175+
# 7| mu0_34(Int32) = Store : &:r0_33, r0_29
176+
# 2| v0_35(Void) = ReturnVoid :
177+
# 2| v0_36(Void) = UnmodeledUse : mu*
178+
# 2| v0_37(Void) = ExitFunction :
179179

180180
func_with_param_call.cs:
181181
# 4| f

0 commit comments

Comments
 (0)