Skip to content

Commit

Permalink
C#: Update PrintAST query
Browse files Browse the repository at this point in the history
  • Loading branch information
hvitved committed Apr 3, 2024
1 parent 231a0fb commit 5eb4179
Show file tree
Hide file tree
Showing 18 changed files with 135 additions and 257 deletions.
26 changes: 6 additions & 20 deletions csharp/ql/lib/semmle/code/csharp/PrintAst.qll
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ private predicate shouldPrint(Element e, Location l) {
}

private predicate isImplicitExpression(ControlFlowElement element) {
element.(Expr).isImplicit() and not exists(element.getAChild())
element.(Expr).isImplicit() and
not element instanceof CastExpr and
not element.(OperatorCall).getTarget() instanceof ImplicitConversionOperator and
not element instanceof ElementInitializer
}

private predicate isFilteredCompilerGenerated(Declaration d) {
Expand Down Expand Up @@ -291,18 +294,6 @@ class ControlFlowElementNode extends ElementNode {
controlFlowElement = element and
// Removing implicit expressions
not isImplicitExpression(element) and
// Removing extra nodes that are generated for an `AssignOperation`
not exists(AssignOperation ao |
ao.hasExpandedAssignment() and
(
ao.getExpandedAssignment() = controlFlowElement or
ao.getExpandedAssignment().getRValue() = controlFlowElement or
ao.getExpandedAssignment().getRValue().(BinaryOperation).getLeftOperand() =
controlFlowElement.getParent*() or
ao.getExpandedAssignment().getRValue().(OperatorCall).getChild(0) =
controlFlowElement.getParent*()
)
) and
not isNotNeeded(element.getParent+()) and
// LambdaExpr is both a Callable and a ControlFlowElement,
// print it with the more specific CallableNode
Expand Down Expand Up @@ -429,7 +420,7 @@ final class DeclarationWithAccessorsNode extends ElementNode {
result.(ParametersNode).getParameterizable() = declaration
or
childIndex = 2 and
result.(ElementNode).getElement() = declaration.(Property).getInitializer().getParent()
result.(ElementNode).getElement() = declaration.(Property).getInitializer()
or
result.(ElementNode).getElement() =
rank[childIndex - 2](Element a, string file, int line, int column, string name |
Expand Down Expand Up @@ -462,12 +453,7 @@ final class FieldNode extends ElementNode {
result.(AttributesNode).getAttributable() = field
or
childIndex = 1 and
field.hasInitializer() and
(
if field.getDeclaringType() instanceof Enum
then result.(ElementNode).getElement() = field.getInitializer()
else result.(ElementNode).getElement() = field.getInitializer().getParent()
)
result.(ElementNode).getElement() = field.getInitializer()
}
}

Expand Down
4 changes: 1 addition & 3 deletions csharp/ql/lib/semmle/code/csharp/Variable.qll
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ class Field extends Variable, AssignableMember, Attributable, TopLevelExprParent
* }
* ```
*/
override Expr getInitializer() { result = this.getChildExpr(0).getChildExpr(0) }
final override Expr getInitializer() { result = this.getChildExpr(0).getChildExpr(0) }

/**
* Holds if this field has an initial value. For example, the initial
Expand Down Expand Up @@ -515,6 +515,4 @@ class EnumConstant extends MemberConstant {
* ```
*/
predicate hasExplicitValue() { exists(this.getInitializer()) }

override Expr getInitializer() { result = this.getChildExpr(0) }
}
6 changes: 5 additions & 1 deletion csharp/ql/lib/semmle/code/csharp/exprs/Expr.qll
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ class Expr extends ControlFlowElement, @expr {
* Holds if this expression is generated by the compiler and does not appear
* explicitly in the source code.
*/
predicate isImplicit() { compiler_generated(this) }
final predicate isImplicit() {
compiler_generated(this) or
this =
any(AssignOperation op).getExpandedAssignment().getRValue().getChildExpr(0).getAChildExpr+()
}

/**
* Gets an expression that is the result of stripping (recursively) all
Expand Down
4 changes: 1 addition & 3 deletions csharp/ql/test/library-tests/attributes/PrintAst.expected
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,7 @@ attributes.cs:
# 67| 4: [BlockStmt] {...}
# 70| [Enum] E
# 70| 5: [Field] A
# 70| 1: [AssignExpr] ... = ...
# 70| 0: [MemberConstantAccess] access to constant A
# 70| 1: [IntLiteral] 42
# 70| 1: [IntLiteral] 42
# 72| [Class] ArgsAttribute
#-----| 3: (Base types)
# 72| 0: [TypeMention] Attribute
Expand Down
8 changes: 2 additions & 6 deletions csharp/ql/test/library-tests/comments/PrintAst.expected
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,10 @@ trivia.cs:
# 89| 4: [BlockStmt] {...}
# 92| 7: [Field] F1
# 92| -1: [TypeMention] int
# 92| 1: [AssignExpr] ... = ...
# 92| 0: [FieldAccess] access to field F1
# 94| 1: [IntLiteral] 10
# 94| 1: [IntLiteral] 10
# 98| 8: [Field] F2
# 98| -1: [TypeMention] int
# 98| 1: [AssignExpr] ... = ...
# 98| 0: [FieldAccess] access to field F2
# 98| 1: [IntLiteral] 0
# 98| 1: [IntLiteral] 0
# 100| 9: [Property] P1
# 100| -1: [TypeMention] int
# 102| 3: [Getter] get_P1
Expand Down
8 changes: 2 additions & 6 deletions csharp/ql/test/library-tests/csharp11/PrintAst.expected
Original file line number Diff line number Diff line change
Expand Up @@ -1111,18 +1111,14 @@ StaticInterfaceMembers.cs:
#-----| 3: (Base types)
# 28| 4: [Property] Real
# 28| -1: [TypeMention] double
# 28| 2: [AssignExpr] ... = ...
# 28| 0: [PropertyCall] access to property Real
# 28| 1: [DoubleLiteral] 0
# 28| 2: [DoubleLiteral] 0
# 28| 3: [Getter] get_Real
# 28| 4: [Setter] set_Real
#-----| 2: (Parameters)
# 28| 0: [Parameter] value
# 29| 5: [Property] Imaginary
# 29| -1: [TypeMention] double
# 29| 2: [AssignExpr] ... = ...
# 29| 0: [PropertyCall] access to property Imaginary
# 29| 1: [DoubleLiteral] 0
# 29| 2: [DoubleLiteral] 0
# 29| 3: [Getter] get_Imaginary
# 29| 4: [Setter] set_Imaginary
#-----| 2: (Parameters)
Expand Down
4 changes: 1 addition & 3 deletions csharp/ql/test/library-tests/csharp6/PrintAst.expected
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ csharp6.cs:
# 10| [Class] TestCSharp6
# 12| 6: [Property] Value
# 12| -1: [TypeMention] int
# 15| 2: [AssignExpr] ... = ...
# 12| 0: [PropertyCall] access to property Value
# 15| 1: [IntLiteral] 20
# 15| 2: [IntLiteral] 20
# 14| 3: [Getter] get_Value
# 17| 7: [Method] Fn
# 17| -1: [TypeMention] Void
Expand Down
8 changes: 2 additions & 6 deletions csharp/ql/test/library-tests/csharp7.2/PrintAst.expected
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,11 @@ csharp72.cs:
# 44| [Class] NumericLiterals
# 46| 5: [Field] binaryValue
# 46| -1: [TypeMention] int
# 46| 1: [AssignExpr] ... = ...
# 46| 0: [FieldAccess] access to field binaryValue
# 46| 1: [IntLiteral] 85
# 46| 1: [IntLiteral] 85
# 49| [Class] PrivateProtected
# 51| 5: [Field] X
# 51| -1: [TypeMention] int
# 51| 1: [AssignExpr] ... = ...
# 51| 0: [FieldAccess] access to field X
# 51| 1: [IntLiteral] 1
# 51| 1: [IntLiteral] 1
# 53| 6: [Method] F
# 53| -1: [TypeMention] Void
# 53| 4: [BlockStmt] {...}
16 changes: 4 additions & 12 deletions csharp/ql/test/library-tests/csharp7/PrintAst.expected
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,17 @@ CSharp7.cs:
# 5| [Class] Literals
# 7| 5: [Field] x
# 7| -1: [TypeMention] int
# 7| 1: [AssignExpr] ... = ...
# 7| 0: [FieldAccess] access to field x
# 7| 1: [IntLiteral] 11
# 7| 1: [IntLiteral] 11
# 8| 6: [Field] y
# 8| -1: [TypeMention] int
# 8| 1: [AssignExpr] ... = ...
# 8| 0: [FieldAccess] access to field y
# 8| 1: [IntLiteral] 123456
# 8| 1: [IntLiteral] 123456
# 9| 7: [Field] z
# 9| -1: [TypeMention] int
# 9| 1: [AssignExpr] ... = ...
# 9| 0: [FieldAccess] access to field z
# 9| 1: [IntLiteral] 128
# 9| 1: [IntLiteral] 128
# 12| [Class] ExpressionBodiedMembers
# 14| 4: [Field] field
# 14| -1: [TypeMention] int
# 14| 1: [AssignExpr] ... = ...
# 14| 0: [FieldAccess] access to field field
# 14| 1: [IntLiteral] 0
# 14| 1: [IntLiteral] 0
# 15| 5: [Method] Foo
# 15| -1: [TypeMention] int
# 15| 4: [FieldAccess] access to field field
Expand Down
16 changes: 6 additions & 10 deletions csharp/ql/test/library-tests/csharp8/PrintAst.expected
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@ AlternateInterpolatedStrings.cs:
# 3| [Class] AlternateInterpolatedStrings
# 5| 5: [Field] s1
# 5| -1: [TypeMention] string
# 5| 1: [AssignExpr] ... = ...
# 5| 0: [FieldAccess] access to field s1
# 5| 1: [InterpolatedStringExpr] $"..."
# 5| 0: [StringLiteralUtf16] "C:"
# 5| 1: [IntLiteral] 12
# 5| 1: [InterpolatedStringExpr] $"..."
# 5| 0: [StringLiteralUtf16] "C:"
# 5| 1: [IntLiteral] 12
# 6| 6: [Field] s2
# 6| -1: [TypeMention] string
# 6| 1: [AssignExpr] ... = ...
# 6| 0: [FieldAccess] access to field s2
# 6| 1: [InterpolatedStringExpr] $"..."
# 6| 0: [StringLiteralUtf16] "C:"
# 6| 1: [IntLiteral] 12
# 6| 1: [InterpolatedStringExpr] $"..."
# 6| 0: [StringLiteralUtf16] "C:"
# 6| 1: [IntLiteral] 12
AsyncStreams.cs:
# 6| [Class] AsyncStreams
# 8| 5: [Method] Items
Expand Down
12 changes: 4 additions & 8 deletions csharp/ql/test/library-tests/csharp9/PrintAst.expected
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ AnonymousObjectCreation.cs:
# 7| 5: [Field] l
# 7| -1: [TypeMention] List<AnonObj>
# 7| 1: [TypeMention] AnonObj
# 7| 1: [AssignExpr] ... = ...
# 7| 0: [FieldAccess] access to field l
# 7| 1: [CastExpr] (...) ...
# 7| 1: [ObjectCreation] object creation of type List<AnonObj>
# 7| 1: [CastExpr] (...) ...
# 7| 1: [ObjectCreation] object creation of type List<AnonObj>
# 9| 6: [Property] Prop1
# 9| -1: [TypeMention] int
# 9| 3: [Getter] get_Prop1
Expand Down Expand Up @@ -294,10 +292,8 @@ FunctionPointer.cs:
# 5| 5: [Class] Program
# 7| 5: [Field] pointer
# 7| -1: [TypeMention] delegate* default<Int32>
# 7| 1: [AssignExpr] ... = ...
# 7| 0: [FieldAccess] access to field pointer
# 7| 1: [AddressOfExpr] &...
# 7| 0: [MethodAccess] access to method M0
# 7| 1: [AddressOfExpr] &...
# 7| 0: [MethodAccess] access to method M0
# 9| 6: [Method] M0
# 9| -1: [TypeMention] int
# 10| 4: [BlockStmt] {...}
Expand Down
22 changes: 8 additions & 14 deletions csharp/ql/test/library-tests/definitions/PrintAst.expected
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,9 @@ definitions.cs:
# 9| 4: [BlockStmt] {...}
# 13| 2: [Enum] Enumeration
# 15| 5: [Field] e1
# 15| 1: [AssignExpr] ... = ...
# 15| 0: [MemberConstantAccess] access to constant e1
# 15| 1: [IntLiteral] 1
# 15| 1: [IntLiteral] 1
# 15| 6: [Field] e2
# 15| 1: [AssignExpr] ... = ...
# 15| 0: [MemberConstantAccess] access to constant e2
# 15| 1: [IntLiteral] 2
# 15| 1: [IntLiteral] 2
# 15| 7: [Field] e3
# 18| 3: [Class] C1
# 20| 4: [InstanceConstructor] C1
Expand Down Expand Up @@ -405,14 +401,12 @@ definitions.cs:
# 166| -1: [TypeMention] Nested<I4>
# 166| 1: [TypeMention] C4
# 166| 2: [TypeMention] I4
# 166| 1: [AssignExpr] ... = ...
# 166| 0: [FieldAccess] access to field f
# 166| 1: [MethodCall] call to method Create
# 166| -1: [TypeAccess] access to type Nested<I4>
# 166| -2: [TypeMention] Nested<I4>
# 166| 1: [TypeMention] I4
# 166| -1: [TypeAccess] access to type C4
# 166| 0: [TypeMention] C4
# 166| 1: [MethodCall] call to method Create
# 166| -1: [TypeAccess] access to type Nested<I4>
# 166| -2: [TypeMention] Nested<I4>
# 166| 1: [TypeMention] I4
# 166| -1: [TypeAccess] access to type C4
# 166| 0: [TypeMention] C4
# 167| 6: [Field] c1
# 167| -1: [TypeMention] C1
# 169| 7: [Method] M
Expand Down
34 changes: 12 additions & 22 deletions csharp/ql/test/library-tests/enums/PrintAst.expected
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,25 @@ enums.cs:
# 23| 3: [Enum] E
# 25| 4: [Enum] ValueColor
# 28| 5: [Field] OneRed
# 28| 1: [AssignExpr] ... = ...
# 28| 0: [MemberConstantAccess] access to constant OneRed
# 28| 1: [CastExpr] (...) ...
# 28| 1: [IntLiteral] 1
# 28| 1: [CastExpr] (...) ...
# 28| 1: [IntLiteral] 1
# 29| 6: [Field] TwoGreen
# 29| 1: [AssignExpr] ... = ...
# 29| 0: [MemberConstantAccess] access to constant TwoGreen
# 29| 1: [CastExpr] (...) ...
# 29| 1: [IntLiteral] 2
# 29| 1: [CastExpr] (...) ...
# 29| 1: [IntLiteral] 2
# 30| 7: [Field] FourBlue
# 30| 1: [AssignExpr] ... = ...
# 30| 0: [MemberConstantAccess] access to constant FourBlue
# 30| 1: [CastExpr] (...) ...
# 30| 1: [IntLiteral] 4
# 30| 1: [CastExpr] (...) ...
# 30| 1: [IntLiteral] 4
# 34| 5: [Enum] SparseColor
# 37| 5: [Field] Red
# 38| 6: [Field] Green
# 38| 1: [AssignExpr] ... = ...
# 38| 0: [MemberConstantAccess] access to constant Green
# 38| 1: [IntLiteral] 10
# 38| 1: [IntLiteral] 10
# 39| 7: [Field] Blue
# 40| 8: [Field] AnotherBlue
# 40| 1: [AssignExpr] ... = ...
# 40| 0: [MemberConstantAccess] access to constant AnotherBlue
# 40| 1: [AddExpr] ... + ...
# 40| 0: [CastExpr] (...) ...
# 40| 1: [MemberConstantAccess] access to constant Blue
# 40| 1: [CastExpr] (...) ...
# 40| 1: [MemberConstantAccess] access to constant Red
# 40| 1: [AddExpr] ... + ...
# 40| 0: [CastExpr] (...) ...
# 40| 1: [MemberConstantAccess] access to constant Blue
# 40| 1: [CastExpr] (...) ...
# 40| 1: [MemberConstantAccess] access to constant Red
# 44| 6: [Class] Test
# 47| 5: [Method] Main
# 47| -1: [TypeMention] Void
Expand Down
12 changes: 4 additions & 8 deletions csharp/ql/test/library-tests/events/PrintAst.expected
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,12 @@ events.cs:
# 50| 4: [Class] Control
# 53| 6: [Field] mouseDownEventKey
# 53| -1: [TypeMention] object
# 53| 1: [AssignExpr] ... = ...
# 53| 0: [FieldAccess] access to field mouseDownEventKey
# 53| 1: [ObjectCreation] object creation of type Object
# 53| 0: [TypeMention] object
# 53| 1: [ObjectCreation] object creation of type Object
# 53| 0: [TypeMention] object
# 54| 7: [Field] mouseUpEventKey
# 54| -1: [TypeMention] object
# 54| 1: [AssignExpr] ... = ...
# 54| 0: [FieldAccess] access to field mouseUpEventKey
# 54| 1: [ObjectCreation] object creation of type Object
# 54| 0: [TypeMention] object
# 54| 1: [ObjectCreation] object creation of type Object
# 54| 0: [TypeMention] object
# 57| 8: [Method] GetEventHandler
# 57| -1: [TypeMention] Delegate
#-----| 2: (Parameters)
Expand Down
Loading

0 comments on commit 5eb4179

Please sign in to comment.