Skip to content

Commit 2a8ff87

Browse files
authored
C#: Add AST printing (#4038)
1 parent ec7a657 commit 2a8ff87

File tree

95 files changed

+11501
-18
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+11501
-18
lines changed

csharp/ql/src/printAst.ql

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @name Print AST
3+
* @description Outputs a representation of a file's Abstract Syntax Tree. This
4+
* query is used by the VS Code extension.
5+
* @id csharp/print-ast
6+
* @kind graph
7+
* @tags ide-contextual-queries/print-ast
8+
*/
9+
10+
import csharp
11+
import semmle.code.csharp.PrintAst
12+
import definitions
13+
14+
/**
15+
* The source file to generate an AST from.
16+
*/
17+
external string selectedSourceFile();
18+
19+
class PrintAstConfigurationOverride extends PrintAstConfiguration {
20+
/**
21+
* Holds if the location matches the selected file in the VS Code extension and
22+
* the element is `fromSource`.
23+
*/
24+
override predicate shouldPrint(Element e, Location l) {
25+
super.shouldPrint(e, l) and
26+
l.getFile() = getEncodedFile(selectedSourceFile())
27+
}
28+
}

csharp/ql/src/semmle/code/csharp/Attribute.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,6 @@ class Attribute extends TopLevelExprParent, @attribute {
9393
result = "[" + name + "(...)]"
9494
)
9595
}
96+
97+
override string getAPrimaryQlClass() { result = "Attribute" }
9698
}

csharp/ql/src/semmle/code/csharp/Callable.qll

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,8 @@ class Method extends Callable, Virtualizable, Attributable, @method {
284284
override Parameter getRawParameter(int i) {
285285
if this.isStatic() then result = this.getParameter(i) else result = this.getParameter(i - 1)
286286
}
287+
288+
override string getAPrimaryQlClass() { result = "Method" }
287289
}
288290

289291
/**
@@ -302,6 +304,8 @@ class ExtensionMethod extends Method {
302304

303305
/** Gets the type being extended by this method. */
304306
Type getExtendedType() { result = getParameter(0).getType() }
307+
308+
override string getAPrimaryQlClass() { result = "ExtensionMethod" }
305309
}
306310

307311
/**
@@ -371,6 +375,8 @@ class StaticConstructor extends Constructor {
371375
StaticConstructor() { this.isStatic() }
372376

373377
override string getUndecoratedName() { result = ".cctor" }
378+
379+
override string getAPrimaryQlClass() { result = "StaticConstructor" }
374380
}
375381

376382
/**
@@ -385,6 +391,8 @@ class StaticConstructor extends Constructor {
385391
*/
386392
class InstanceConstructor extends Constructor {
387393
InstanceConstructor() { not this.isStatic() }
394+
395+
override string getAPrimaryQlClass() { result = "InstanceConstructor" }
388396
}
389397

390398
/**
@@ -413,6 +421,8 @@ class Destructor extends DotNet::Destructor, Callable, Member, Attributable, @de
413421
override Location getALocation() { destructor_location(this, result) }
414422

415423
override string toString() { result = Callable.super.toString() }
424+
425+
override string getAPrimaryQlClass() { result = "Destructor" }
416426
}
417427

418428
/**
@@ -471,6 +481,8 @@ class PlusOperator extends UnaryOperator {
471481
PlusOperator() { this.getName() = "+" }
472482

473483
override string getFunctionName() { result = "op_UnaryPlus" }
484+
485+
override string getAPrimaryQlClass() { result = "PlusOperator" }
474486
}
475487

476488
/**
@@ -486,6 +498,8 @@ class MinusOperator extends UnaryOperator {
486498
MinusOperator() { this.getName() = "-" }
487499

488500
override string getFunctionName() { result = "op_UnaryNegation" }
501+
502+
override string getAPrimaryQlClass() { result = "MinusOperator" }
489503
}
490504

491505
/**
@@ -501,6 +515,8 @@ class NotOperator extends UnaryOperator {
501515
NotOperator() { this.getName() = "!" }
502516

503517
override string getFunctionName() { result = "op_LogicalNot" }
518+
519+
override string getAPrimaryQlClass() { result = "NotOperator" }
504520
}
505521

506522
/**
@@ -516,6 +532,8 @@ class ComplementOperator extends UnaryOperator {
516532
ComplementOperator() { this.getName() = "~" }
517533

518534
override string getFunctionName() { result = "op_OnesComplement" }
535+
536+
override string getAPrimaryQlClass() { result = "ComplementOperator" }
519537
}
520538

521539
/**
@@ -531,6 +549,8 @@ class IncrementOperator extends UnaryOperator {
531549
IncrementOperator() { this.getName() = "++" }
532550

533551
override string getFunctionName() { result = "op_Increment" }
552+
553+
override string getAPrimaryQlClass() { result = "IncrementOperator" }
534554
}
535555

536556
/**
@@ -546,6 +566,8 @@ class DecrementOperator extends UnaryOperator {
546566
DecrementOperator() { this.getName() = "--" }
547567

548568
override string getFunctionName() { result = "op_Decrement" }
569+
570+
override string getAPrimaryQlClass() { result = "DecrementOperator" }
549571
}
550572

551573
/**
@@ -561,6 +583,8 @@ class FalseOperator extends UnaryOperator {
561583
FalseOperator() { this.getName() = "false" }
562584

563585
override string getFunctionName() { result = "op_False" }
586+
587+
override string getAPrimaryQlClass() { result = "FalseOperator" }
564588
}
565589

566590
/**
@@ -576,6 +600,8 @@ class TrueOperator extends UnaryOperator {
576600
TrueOperator() { this.getName() = "true" }
577601

578602
override string getFunctionName() { result = "op_True" }
603+
604+
override string getAPrimaryQlClass() { result = "TrueOperator" }
579605
}
580606

581607
/**
@@ -608,6 +634,8 @@ class AddOperator extends BinaryOperator {
608634
AddOperator() { this.getName() = "+" }
609635

610636
override string getFunctionName() { result = "op_Addition" }
637+
638+
override string getAPrimaryQlClass() { result = "AddOperator" }
611639
}
612640

613641
/**
@@ -623,6 +651,8 @@ class SubOperator extends BinaryOperator {
623651
SubOperator() { this.getName() = "-" }
624652

625653
override string getFunctionName() { result = "op_Subtraction" }
654+
655+
override string getAPrimaryQlClass() { result = "SubOperator" }
626656
}
627657

628658
/**
@@ -638,6 +668,8 @@ class MulOperator extends BinaryOperator {
638668
MulOperator() { this.getName() = "*" }
639669

640670
override string getFunctionName() { result = "op_Multiply" }
671+
672+
override string getAPrimaryQlClass() { result = "MulOperator" }
641673
}
642674

643675
/**
@@ -653,6 +685,8 @@ class DivOperator extends BinaryOperator {
653685
DivOperator() { this.getName() = "/" }
654686

655687
override string getFunctionName() { result = "op_Division" }
688+
689+
override string getAPrimaryQlClass() { result = "DivOperator" }
656690
}
657691

658692
/**
@@ -668,6 +702,8 @@ class RemOperator extends BinaryOperator {
668702
RemOperator() { this.getName() = "%" }
669703

670704
override string getFunctionName() { result = "op_Modulus" }
705+
706+
override string getAPrimaryQlClass() { result = "RemOperator" }
671707
}
672708

673709
/**
@@ -683,6 +719,8 @@ class AndOperator extends BinaryOperator {
683719
AndOperator() { this.getName() = "&" }
684720

685721
override string getFunctionName() { result = "op_BitwiseAnd" }
722+
723+
override string getAPrimaryQlClass() { result = "AndOperator" }
686724
}
687725

688726
/**
@@ -698,6 +736,8 @@ class OrOperator extends BinaryOperator {
698736
OrOperator() { this.getName() = "|" }
699737

700738
override string getFunctionName() { result = "op_BitwiseOr" }
739+
740+
override string getAPrimaryQlClass() { result = "OrOperator" }
701741
}
702742

703743
/**
@@ -713,6 +753,8 @@ class XorOperator extends BinaryOperator {
713753
XorOperator() { this.getName() = "^" }
714754

715755
override string getFunctionName() { result = "op_ExclusiveOr" }
756+
757+
override string getAPrimaryQlClass() { result = "XorOperator" }
716758
}
717759

718760
/**
@@ -728,6 +770,8 @@ class LShiftOperator extends BinaryOperator {
728770
LShiftOperator() { this.getName() = "<<" }
729771

730772
override string getFunctionName() { result = "op_LeftShift" }
773+
774+
override string getAPrimaryQlClass() { result = "LShiftOperator" }
731775
}
732776

733777
/**
@@ -743,6 +787,8 @@ class RShiftOperator extends BinaryOperator {
743787
RShiftOperator() { this.getName() = ">>" }
744788

745789
override string getFunctionName() { result = "op_RightShift" }
790+
791+
override string getAPrimaryQlClass() { result = "RShiftOperator" }
746792
}
747793

748794
/**
@@ -758,6 +804,8 @@ class EQOperator extends BinaryOperator {
758804
EQOperator() { this.getName() = "==" }
759805

760806
override string getFunctionName() { result = "op_Equality" }
807+
808+
override string getAPrimaryQlClass() { result = "EQOperator" }
761809
}
762810

763811
/**
@@ -773,6 +821,8 @@ class NEOperator extends BinaryOperator {
773821
NEOperator() { this.getName() = "!=" }
774822

775823
override string getFunctionName() { result = "op_Inequality" }
824+
825+
override string getAPrimaryQlClass() { result = "NEOperator" }
776826
}
777827

778828
/**
@@ -788,6 +838,8 @@ class LTOperator extends BinaryOperator {
788838
LTOperator() { this.getName() = "<" }
789839

790840
override string getFunctionName() { result = "op_LessThan" }
841+
842+
override string getAPrimaryQlClass() { result = "LTOperator" }
791843
}
792844

793845
/**
@@ -803,6 +855,8 @@ class GTOperator extends BinaryOperator {
803855
GTOperator() { this.getName() = ">" }
804856

805857
override string getFunctionName() { result = "op_GreaterThan" }
858+
859+
override string getAPrimaryQlClass() { result = "GTOperator" }
806860
}
807861

808862
/**
@@ -818,6 +872,8 @@ class LEOperator extends BinaryOperator {
818872
LEOperator() { this.getName() = "<=" }
819873

820874
override string getFunctionName() { result = "op_LessThanOrEqual" }
875+
876+
override string getAPrimaryQlClass() { result = "LEOperator" }
821877
}
822878

823879
/**
@@ -833,6 +889,8 @@ class GEOperator extends BinaryOperator {
833889
GEOperator() { this.getName() = ">=" }
834890

835891
override string getFunctionName() { result = "op_GreaterThanOrEqual" }
892+
893+
override string getAPrimaryQlClass() { result = "GEOperator" }
836894
}
837895

838896
/**
@@ -870,6 +928,8 @@ class ImplicitConversionOperator extends ConversionOperator {
870928
ImplicitConversionOperator() { this.getName() = "implicit conversion" }
871929

872930
override string getFunctionName() { result = "op_Implicit" }
931+
932+
override string getAPrimaryQlClass() { result = "ImplicitConversionOperator" }
873933
}
874934

875935
/**
@@ -885,6 +945,8 @@ class ExplicitConversionOperator extends ConversionOperator {
885945
ExplicitConversionOperator() { this.getName() = "explicit conversion" }
886946

887947
override string getFunctionName() { result = "op_Explicit" }
948+
949+
override string getAPrimaryQlClass() { result = "ExplicitConversionOperator" }
888950
}
889951

890952
/**
@@ -923,4 +985,6 @@ class LocalFunction extends Callable, Modifiable, @local_function {
923985
override Location getALocation() { result = getStatement().getALocation() }
924986

925987
override Parameter getRawParameter(int i) { result = getParameter(i) }
988+
989+
override string getAPrimaryQlClass() { result = "LocalFunction" }
926990
}

csharp/ql/src/semmle/code/csharp/Element.qll

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,17 @@ class Element extends DotNet::Element, @element {
4848
* other children (zero-based).
4949
*/
5050
int getIndex() { exists(Element parent | parent.getChild(result) = this) }
51+
52+
/**
53+
* Gets the name of a primary CodeQL class to which this element belongs.
54+
*
55+
* For most elements, this is simply the most precise syntactic category to
56+
* which they belong; for example, `AddExpr` is a primary class, but
57+
* `BinaryOperation` is not.
58+
*
59+
* This predicate always has a result. If no primary class can be
60+
* determined, the result is `"???"`. If multiple primary classes match,
61+
* this predicate can have multiple results.
62+
*/
63+
string getAPrimaryQlClass() { result = "???" }
5164
}

csharp/ql/src/semmle/code/csharp/Event.qll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ class Event extends DeclarationWithAccessors, @event {
6161
}
6262

6363
override Location getALocation() { event_location(this, result) }
64+
65+
override string getAPrimaryQlClass() { result = "Event" }
6466
}
6567

6668
/**
@@ -107,6 +109,8 @@ class EventAccessor extends Accessor, @event_accessor {
107109
*/
108110
class AddEventAccessor extends EventAccessor, @add_event_accessor {
109111
override string getName() { result = "add" + "_" + getDeclaration().getName() }
112+
113+
override string getAPrimaryQlClass() { result = "AddEventAccessor" }
110114
}
111115

112116
/**
@@ -124,4 +128,6 @@ class AddEventAccessor extends EventAccessor, @add_event_accessor {
124128
*/
125129
class RemoveEventAccessor extends EventAccessor, @remove_event_accessor {
126130
override string getName() { result = "remove" + "_" + getDeclaration().getName() }
131+
132+
override string getAPrimaryQlClass() { result = "RemoveEventAccessor" }
127133
}

csharp/ql/src/semmle/code/csharp/Generics.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ class TypeParameter extends DotNet::TypeParameter, Type, @type_parameter {
183183

184184
/** Gets the generic that defines this type parameter. */
185185
UnboundGeneric getGeneric() { type_parameters(this, _, result, _) }
186+
187+
override string getAPrimaryQlClass() { result = "TypeParameter" }
186188
}
187189

188190
/**

csharp/ql/src/semmle/code/csharp/Namespace.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,6 @@ class NamespaceDeclaration extends Element, @namespace_declaration {
207207
override Location getALocation() { namespace_declaration_location(this, result) }
208208

209209
override string toString() { result = "namespace ... { ... }" }
210+
211+
override string getAPrimaryQlClass() { result = "NamespaceDeclaration" }
210212
}

0 commit comments

Comments
 (0)