Skip to content

Commit 05e37a7

Browse files
committed
C#: Promote existing ad-hoc consistency checks to consistency queries
1 parent 43b5d50 commit 05e37a7

File tree

9 files changed

+55
-117
lines changed

9 files changed

+55
-117
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import csharp
2+
3+
query predicate missingLocation(Element e) {
4+
(
5+
e instanceof Declaration or
6+
e instanceof Expr or
7+
e instanceof Stmt
8+
) and
9+
not e instanceof ImplicitAccessorParameter and
10+
not e instanceof NullType and
11+
not e instanceof Parameter and // Bug in Roslyn - params occasionally lack locations
12+
not e.(Operator).getDeclaringType() instanceof IntType and // Roslyn quirk
13+
not e instanceof Constructor and
14+
not e instanceof ArrayType and
15+
not e instanceof UnknownType and
16+
not e instanceof ArglistType and
17+
not exists(TupleType t | e = t or e = t.getAField()) and
18+
not exists(e.getLocation())
19+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import csharp
2+
3+
query predicate missingUnbound(ConstructedGeneric g) { not exists(g.getUnboundGeneric()) }
4+
5+
query predicate missingArgs(ConstructedGeneric g) { g.getNumberOfTypeArguments() = 0 }
6+
7+
query predicate inconsistentArgCount(ConstructedGeneric g) {
8+
g.getUnboundGeneric().getNumberOfTypeParameters() != g.getNumberOfTypeArguments()
9+
}

csharp/ql/consistency-queries/SsaConsistency.ql

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import csharp
22
import semmle.code.csharp.dataflow.internal.SsaImplCommon::Consistency
3+
import Ssa
34

45
class MyRelevantDefinition extends RelevantDefinition, Ssa::Definition {
56
override predicate hasLocationInfo(
@@ -8,3 +9,18 @@ class MyRelevantDefinition extends RelevantDefinition, Ssa::Definition {
89
this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
910
}
1011
}
12+
13+
query predicate localDeclWithSsaDef(LocalVariableDeclExpr d) {
14+
// Local variables in C# must be initialized before every use, so uninitialized
15+
// local variables should not have an SSA definition, as that would imply that
16+
// the declaration is live (can reach a use without passing through a definition)
17+
exists(ExplicitDefinition def |
18+
d = def.getADefinition().(AssignableDefinitions::LocalVariableDefinition).getDeclaration()
19+
|
20+
not d = any(ForeachStmt fs).getVariableDeclExpr() and
21+
not d = any(SpecificCatchClause scc).getVariableDeclExpr() and
22+
not d.getVariable().getType() instanceof Struct and
23+
not d instanceof PatternExpr and
24+
not d.getVariable().isCaptured()
25+
)
26+
}

csharp/ql/lib/semmle/code/csharp/commons/ConsistencyChecks.qll

Lines changed: 0 additions & 102 deletions
This file was deleted.

csharp/ql/lib/semmle/code/csharp/dataflow/internal/SsaImplCommon.qll

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,4 +659,15 @@ module Consistency {
659659
not phiHasInputFromBlock(_, def, _) and
660660
not uncertainWriteDefinitionInput(_, def)
661661
}
662+
663+
query predicate notDominatedByDef(RelevantDefinition def, SourceVariable v, BasicBlock bb, int i) {
664+
exists(BasicBlock bbDef, int iDef | def.definesAt(v, bbDef, iDef) |
665+
ssaDefReachesReadWithinBlock(v, def, bb, i) and
666+
(bb != bbDef or i < iDef)
667+
or
668+
ssaDefReachesRead(v, def, bb, i) and
669+
not ssaDefReachesReadWithinBlock(v, def, bb, i) and
670+
not def.definesAt(v, getImmediateBasicBlockDominator*(bb), _)
671+
)
672+
}
662673
}

csharp/ql/test/library-tests/dataflow/ssa/SsaConsistency.expected

Whitespace-only changes.

csharp/ql/test/library-tests/dataflow/ssa/SsaConsistency.ql

Lines changed: 0 additions & 6 deletions
This file was deleted.

csharp/ql/test/library-tests/generics/ConsistencyChecks.expected

Whitespace-only changes.

csharp/ql/test/library-tests/generics/ConsistencyChecks.ql

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)