Skip to content

Commit 9ecbb4a

Browse files
author
AndreiDiaconu1
committed
More fixes for the PR comments
1 parent fe3645f commit 9ecbb4a

26 files changed

+762
-1187
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ private module Cached {
139139
// Compiler generated foreach while loop:
140140
// Same as above
141141
exists(TranslatedForeachWhile s |
142-
s instanceof TranslatedForeachWhile and
143142
result = s.getFirstInstruction() and
144143
exists(TranslatedElement inBody, InstructionTag tag |
145144
result = inBody.getInstructionSuccessor(tag, kind) and

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import csharp
22
import semmle.code.csharp.ir.Util
33

4-
//private predicate fieldIsInitialized(Field field) {
5-
// exists(field.getInitializer())
6-
//}
74
private predicate elementIsInitialized(int elementIndex) {
85
exists(ArrayInitWithMod initList | initList.isInitialized(elementIndex))
96
}

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

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,18 @@ private import InstructionTag
55
private import TranslatedElement
66
private import TranslatedExpr
77
private import semmle.code.csharp.ir.Util
8-
private import semmle.code.csharp.ir.implementation.raw.internal.common.TranslatedCallBlueprint
8+
private import semmle.code.csharp.ir.implementation.raw.internal.common.TranslatedCallBase
99
private import semmle.code.csharp.ir.internal.IRCSharpLanguage as Language
1010

1111
/**
1212
* The IR translation of a call to a function. The function can be a normal function
13-
* (eg. `MethodCall`) or a constructor call (eg. `ObjectCreation`). Notice that the
13+
* (ie. `MethodCall`) or a constructor call (ie. `ObjectCreation`). Notice that the
1414
* AST generated translated calls are tied to an expression (unlike compiler generated ones,
1515
* which can be attached to either a statement or an expression).
1616
*/
17+
abstract class TranslatedCall extends TranslatedExpr, TranslatedCallBase {
18+
final override Instruction getResult() { result = TranslatedCallBase.super.getResult() }
1719

18-
abstract class TranslatedCall extends TranslatedExpr, TranslatedCallBlueprint {
19-
final override Instruction getResult() {
20-
result = TranslatedCallBlueprint.super.getResult()
21-
}
22-
2320
override Instruction getUnmodeledDefinitionInstruction() {
2421
result = this.getEnclosingFunction().getUnmodeledDefinitionInstruction()
2522
}
@@ -30,11 +27,10 @@ abstract class TranslatedCall extends TranslatedExpr, TranslatedCallBlueprint {
3027
* `MethodCall`, `LocalFunctionCall`, `AccessorCall`, `OperatorCall`.
3128
* Note that `DelegateCall`s are not treated here since they need to be desugared.
3229
*/
33-
3430
class TranslatedFunctionCall extends TranslatedNonConstantExpr, TranslatedCall {
3531
override Call expr;
36-
37-
TranslatedFunctionCall() {
32+
33+
TranslatedFunctionCall() {
3834
expr instanceof MethodCall or
3935
expr instanceof LocalFunctionCall or
4036
expr instanceof AccessorCall or
@@ -45,27 +41,19 @@ class TranslatedFunctionCall extends TranslatedNonConstantExpr, TranslatedCall {
4541
tag = CallTargetTag() and result = expr.getTarget()
4642
}
4743

48-
override predicate hasArguments() {
49-
exists(expr.getArgument(0))
50-
}
51-
5244
override TranslatedExpr getArgument(int index) {
5345
result = getTranslatedExpr(expr.getArgument(index))
5446
}
55-
47+
5648
override TranslatedExpr getQualifier() {
5749
expr instanceof QualifiableExpr and
5850
result = getTranslatedExpr(expr.(QualifiableExpr).getQualifier())
5951
}
60-
61-
override Instruction getQualifierResult() {
62-
result = this.getQualifier().getResult()
63-
}
64-
65-
override Type getCallResultType() {
66-
result = expr.getTarget().getReturnType()
67-
}
68-
52+
53+
override Instruction getQualifierResult() { result = this.getQualifier().getResult() }
54+
55+
override Type getCallResultType() { result = expr.getTarget().getReturnType() }
56+
6957
override predicate hasReadSideEffect() {
7058
not expr.getTarget().(SideEffectFunction).neverReadsMemory()
7159
}
@@ -83,34 +71,26 @@ class TranslatedFunctionCall extends TranslatedNonConstantExpr, TranslatedCall {
8371
*/
8472
class TranslatedConstructorCall extends TranslatedNonConstantExpr, TranslatedCall {
8573
override Call expr;
86-
87-
TranslatedConstructorCall() {
74+
75+
TranslatedConstructorCall() {
8876
expr instanceof ObjectCreation or
8977
expr instanceof ConstructorInitializer
9078
}
9179

9280
override Callable getInstructionFunction(InstructionTag tag) {
9381
tag = CallTargetTag() and result = expr.getTarget()
9482
}
95-
96-
override predicate hasArguments() {
97-
exists(expr.getArgument(0))
98-
}
99-
83+
10084
override TranslatedExpr getArgument(int index) {
10185
result = getTranslatedExpr(expr.getArgument(index))
10286
}
103-
87+
10488
// The qualifier for a constructor call has already been generated
10589
// (the `NewObj` instruction)
106-
override TranslatedExpr getQualifier() {
107-
none()
108-
}
109-
110-
override Type getCallResultType() {
111-
result instanceof VoidType
112-
}
113-
90+
override TranslatedExpr getQualifier() { none() }
91+
92+
override Type getCallResultType() { result instanceof VoidType }
93+
11494
override Instruction getQualifierResult() {
11595
exists(ConstructorCallContext context |
11696
context = this.getParent() and

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

Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,19 @@ private import semmle.code.csharp.ir.implementation.internal.OperandTag
44
private import InstructionTag
55
private import TranslatedElement
66
private import TranslatedExpr
7-
private import common.TranslatedConditionBlueprint
7+
private import common.TranslatedConditionBase
88
private import semmle.code.csharp.ir.internal.IRCSharpLanguage as Language
99

10-
TranslatedCondition getTranslatedCondition(Expr expr) {
11-
result.getExpr() = expr
12-
}
10+
TranslatedCondition getTranslatedCondition(Expr expr) { result.getExpr() = expr }
1311

14-
abstract class TranslatedCondition extends ConditionBlueprint {
12+
abstract class TranslatedCondition extends ConditionBase {
1513
Expr expr;
1614

1715
final override string toString() { result = expr.toString() }
1816

1917
final override Language::AST getAST() { result = expr }
2018

21-
final Expr getExpr() {
22-
result = expr
23-
}
19+
final Expr getExpr() { result = expr }
2420

2521
final override Callable getFunction() { result = expr.getEnclosingCallable() }
2622

@@ -53,12 +49,12 @@ abstract class TranslatedFlexibleCondition extends TranslatedCondition, Conditio
5349
class TranslatedParenthesisCondition extends TranslatedFlexibleCondition {
5450
override ParenthesizedExpr expr;
5551

56-
final override Instruction getChildTrueSuccessor(ConditionBlueprint child) {
52+
final override Instruction getChildTrueSuccessor(ConditionBase child) {
5753
child = this.getOperand() and
5854
result = this.getConditionContext().getChildTrueSuccessor(this)
5955
}
6056

61-
final override Instruction getChildFalseSuccessor(ConditionBlueprint child) {
57+
final override Instruction getChildFalseSuccessor(ConditionBase child) {
6258
child = this.getOperand() and
6359
result = this.getConditionContext().getChildFalseSuccessor(this)
6460
}
@@ -71,12 +67,12 @@ class TranslatedParenthesisCondition extends TranslatedFlexibleCondition {
7167
class TranslatedNotCondition extends TranslatedFlexibleCondition {
7268
override LogicalNotExpr expr;
7369

74-
override Instruction getChildTrueSuccessor(ConditionBlueprint child) {
70+
override Instruction getChildTrueSuccessor(ConditionBase child) {
7571
child = this.getOperand() and
7672
result = this.getConditionContext().getChildFalseSuccessor(this)
7773
}
7874

79-
override Instruction getChildFalseSuccessor(ConditionBlueprint child) {
75+
override Instruction getChildFalseSuccessor(ConditionBase child) {
8076
child = this.getOperand() and
8177
result = this.getConditionContext().getChildTrueSuccessor(this)
8278
}
@@ -128,18 +124,15 @@ abstract class TranslatedBinaryLogicalOperation extends TranslatedNativeConditio
128124
class TranslatedLogicalAndExpr extends TranslatedBinaryLogicalOperation {
129125
TranslatedLogicalAndExpr() { expr instanceof LogicalAndExpr }
130126

131-
override Instruction getChildTrueSuccessor(ConditionBlueprint child) {
132-
(
133-
child = this.getLeftOperand() and
134-
result = this.getRightOperand().getFirstInstruction()
135-
) or
136-
(
137-
child = this.getRightOperand() and
138-
result = this.getConditionContext().getChildTrueSuccessor(this)
139-
)
127+
override Instruction getChildTrueSuccessor(ConditionBase child) {
128+
child = this.getLeftOperand() and
129+
result = this.getRightOperand().getFirstInstruction()
130+
or
131+
child = this.getRightOperand() and
132+
result = this.getConditionContext().getChildTrueSuccessor(this)
140133
}
141134

142-
override Instruction getChildFalseSuccessor(ConditionBlueprint child) {
135+
override Instruction getChildFalseSuccessor(ConditionBase child) {
143136
child = this.getAnOperand() and
144137
result = this.getConditionContext().getChildFalseSuccessor(this)
145138
}
@@ -148,34 +141,25 @@ class TranslatedLogicalAndExpr extends TranslatedBinaryLogicalOperation {
148141
class TranslatedLogicalOrExpr extends TranslatedBinaryLogicalOperation {
149142
override LogicalOrExpr expr;
150143

151-
override Instruction getChildTrueSuccessor(ConditionBlueprint child) {
144+
override Instruction getChildTrueSuccessor(ConditionBase child) {
152145
child = getAnOperand() and
153146
result = this.getConditionContext().getChildTrueSuccessor(this)
154147
}
155148

156-
override Instruction getChildFalseSuccessor(ConditionBlueprint child) {
157-
(
158-
child = this.getLeftOperand() and
159-
result = getRightOperand().getFirstInstruction()
160-
) or
161-
(
162-
child = this.getRightOperand() and
163-
result = this.getConditionContext().getChildFalseSuccessor(this)
164-
)
149+
override Instruction getChildFalseSuccessor(ConditionBase child) {
150+
child = this.getLeftOperand() and
151+
result = getRightOperand().getFirstInstruction()
152+
or
153+
child = this.getRightOperand() and
154+
result = this.getConditionContext().getChildFalseSuccessor(this)
165155
}
166156
}
167157

168-
class TranslatedValueCondition extends TranslatedCondition, ValueConditionBlueprint,
169-
TTranslatedValueCondition {
170-
TranslatedValueCondition() {
171-
this = TTranslatedValueCondition(expr)
172-
}
173-
174-
override TranslatedExpr getValueExpr() {
175-
result = getTranslatedExpr(expr)
176-
}
177-
178-
override Instruction valueExprResult() {
179-
result = this.getValueExpr().getResult()
180-
}
158+
class TranslatedValueCondition extends TranslatedCondition, ValueConditionBase,
159+
TTranslatedValueCondition {
160+
TranslatedValueCondition() { this = TTranslatedValueCondition(expr) }
161+
162+
override TranslatedExpr getValueExpr() { result = getTranslatedExpr(expr) }
163+
164+
override Instruction valueExprResult() { result = this.getValueExpr().getResult() }
181165
}

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

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ private import TranslatedElement
77
private import TranslatedExpr
88
private import TranslatedInitialization
99
private import semmle.code.csharp.ir.internal.IRCSharpLanguage as Language
10-
private import common.TranslatedDeclarationBlueprint
10+
private import common.TranslatedDeclarationBase
1111

1212
/**
1313
* Gets the `TranslatedDeclaration` that represents the declaration
@@ -36,29 +36,21 @@ abstract class TranslatedLocalDeclaration extends TranslatedElement, TTranslated
3636
* Represents the IR translation of the declaration of a local variable,
3737
* including its initialization, if any.
3838
*/
39-
class TranslatedLocalVariableDeclaration extends TranslatedLocalDeclaration, LocalVariableDeclarationBlueprint,
40-
InitializationContext {
39+
class TranslatedLocalVariableDeclaration extends TranslatedLocalDeclaration,
40+
LocalVariableDeclarationBase, InitializationContext {
4141
LocalVariable var;
42-
43-
TranslatedLocalVariableDeclaration() {
44-
var = expr.getVariable()
45-
}
46-
42+
43+
TranslatedLocalVariableDeclaration() { var = expr.getVariable() }
44+
4745
override Instruction getTargetAddress() {
4846
result = this.getInstruction(InitializerVariableAddressTag())
4947
}
5048

51-
override LocalVariable getDeclVar() {
52-
result = var
53-
}
49+
override LocalVariable getDeclVar() { result = var }
5450

55-
override Type getVarType() {
56-
result = getVariableType(getDeclVar())
57-
}
51+
override Type getVarType() { result = getVariableType(getDeclVar()) }
5852

59-
override Type getTargetType() {
60-
result = getVariableType(var)
61-
}
53+
override Type getTargetType() { result = getVariableType(var) }
6254

6355
override IRVariable getInstructionVariable(InstructionTag tag) {
6456
(
@@ -68,7 +60,7 @@ class TranslatedLocalVariableDeclaration extends TranslatedLocalDeclaration, Loc
6860
) and
6961
result = getIRUserVariable(getFunction(), getDeclVar())
7062
}
71-
63+
7264
override TranslatedInitialization getInitialization() {
7365
// First complex initializations
7466
if var.getInitializer() instanceof ArrayCreation
@@ -81,7 +73,5 @@ class TranslatedLocalVariableDeclaration extends TranslatedLocalDeclaration, Loc
8173
result = getTranslatedInitialization(var.getInitializer())
8274
}
8375

84-
override predicate isInitializedByElement() {
85-
expr.getParent() instanceof IsExpr
86-
}
76+
override predicate isInitializedByElement() { expr.getParent() instanceof IsExpr }
8777
}

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,17 @@ ArrayType getArrayOfDim(int dim, Type type) {
2222
result.getElementType() = type
2323
}
2424

25-
predicate canCreateCompilerGeneratedElement (int nth) {
25+
private predicate canCreateCompilerGeneratedElement(Element generatedBy, int nth) {
26+
(
27+
generatedBy instanceof ForeachStmt or
28+
generatedBy instanceof LockStmt or
29+
generatedBy instanceof DelegateCreation or
30+
generatedBy instanceof DelegateCall
31+
) and
2632
// For now we allow a max of 15 compiler generated elements
2733
nth in [0 .. 14]
2834
}
2935

30-
3136
/**
3237
* Gets the "real" parent of `expr`. This predicate treats conversions as if
3338
* they were explicit nodes in the expression tree, rather than as implicit
@@ -63,10 +68,8 @@ private predicate ignoreExprAndDescendants(Expr expr) {
6368
or
6469
// Ignore the local declaration done by a `ForeachStmt`
6570
// since we desugar it
66-
(
67-
expr instanceof LocalVariableDeclExpr and
68-
expr.getParent().getParent() instanceof ForeachStmt
69-
)
71+
expr instanceof LocalVariableDeclExpr and
72+
expr.getParent() instanceof ForeachStmt
7073
or
7174
ignoreExprAndDescendants(getRealParent(expr)) // recursive case
7275
}
@@ -272,14 +275,14 @@ newtype TTranslatedElement =
272275
TTranslatedDeclaration(LocalVariableDeclExpr entry) {
273276
// foreach var decl and init is treated separately,
274277
// because foreach needs desugaring
275-
not entry.getParent() instanceof ForeachStmt
278+
not ignoreExprAndDescendants(entry)
276279
} or
277-
// A compiler generated element, generated by `generatedBy` during the
280+
// A compiler generated element, generated by `generatedBy` during the
278281
// desugaring process
279282
TTranslatedCompilerGeneratedElement(Element generatedBy, int index) {
280-
canCreateCompilerGeneratedElement(index)
283+
canCreateCompilerGeneratedElement(generatedBy, index)
281284
}
282-
285+
283286
/**
284287
* Gets the index of the first explicitly initialized element in `initList`
285288
* whose index is greater than `afterElementIndex`, where `afterElementIndex`

0 commit comments

Comments
 (0)