Skip to content

Commit e09009c

Browse files
authored
Merge pull request #7118 from michaelnebel/csharp-primary-ql-class
C#: PrimaryQlClass
2 parents b51c855 + 8c6c8b0 commit e09009c

File tree

10 files changed

+74
-6
lines changed

10 files changed

+74
-6
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import csharp
2+
3+
query predicate missingPrimaryQlClass(Element e) {
4+
not exists(e.getAPrimaryQlClass()) and
5+
e.fromSource()
6+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ class TypeParameterConstraints extends Element, @type_parameter_constraints {
277277

278278
/** Gets a textual representation of these constraints. */
279279
override string toString() { result = "where " + this.getTypeParameter().getName() + ": ..." }
280+
281+
override string getAPrimaryQlClass() { result = "TypeParameterConstraints" }
280282
}
281283

282284
/**

csharp/ql/lib/semmle/code/csharp/Modifier.qll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@ class Modifier extends Element, @modifier {
1313
predicate hasName(string name) { name = this.getName() }
1414

1515
override string toString() { result = this.getName() }
16+
17+
override string getAPrimaryQlClass() { result = "Modifier" }
1618
}
1719

1820
/**
1921
* An access modifier: `public`, `private`, `internal` or `protected`.
2022
*/
2123
class AccessModifier extends Modifier {
2224
AccessModifier() { this.hasName(["public", "private", "internal", "protected"]) }
25+
26+
override string getAPrimaryQlClass() { result = "AccessModifier" }
2327
}

csharp/ql/lib/semmle/code/csharp/Type.qll

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,8 @@ class VoidType extends DotNet::ValueOrRefType, Type, @void_type {
413413
final override string getUndecoratedName() { result = "Void" }
414414

415415
override SystemNamespace getDeclaringNamespace() { any() }
416+
417+
override string getAPrimaryQlClass() { result = "VoidType" }
416418
}
417419

418420
/**
@@ -454,6 +456,8 @@ class BoolType extends SimpleType, @bool_type {
454456
override string toStringWithTypes() { result = "bool" }
455457

456458
override int getSize() { result = 1 }
459+
460+
override string getAPrimaryQlClass() { result = "BoolType" }
457461
}
458462

459463
/**
@@ -467,6 +471,8 @@ class CharType extends SimpleType, @char_type {
467471
override int minValue() { result = 0 }
468472

469473
override int maxValue() { result = 65535 }
474+
475+
override string getAPrimaryQlClass() { result = "CharType" }
470476
}
471477

472478
/**
@@ -506,6 +512,8 @@ class SByteType extends SignedIntegralType, @sbyte_type {
506512
override int minValue() { result = -128 }
507513

508514
override int maxValue() { result = 127 }
515+
516+
override string getAPrimaryQlClass() { result = "SByteType" }
509517
}
510518

511519
/**
@@ -519,6 +527,8 @@ class ShortType extends SignedIntegralType, @short_type {
519527
override int minValue() { result = -32768 }
520528

521529
override int maxValue() { result = 32767 }
530+
531+
override string getAPrimaryQlClass() { result = "ShortType" }
522532
}
523533

524534
/**
@@ -532,6 +542,8 @@ class IntType extends SignedIntegralType, @int_type {
532542
override int minValue() { result = -2147483647 - 1 }
533543

534544
override int maxValue() { result = 2147483647 }
545+
546+
override string getAPrimaryQlClass() { result = "IntType" }
535547
}
536548

537549
/**
@@ -541,6 +553,8 @@ class LongType extends SignedIntegralType, @long_type {
541553
override string toStringWithTypes() { result = "long" }
542554

543555
override int getSize() { result = 8 }
556+
557+
override string getAPrimaryQlClass() { result = "LongType" }
544558
}
545559

546560
/**
@@ -552,6 +566,8 @@ class ByteType extends UnsignedIntegralType, @byte_type {
552566
override int getSize() { result = 1 }
553567

554568
override int maxValue() { result = 255 }
569+
570+
override string getAPrimaryQlClass() { result = "ByteType" }
555571
}
556572

557573
/**
@@ -563,6 +579,8 @@ class UShortType extends UnsignedIntegralType, @ushort_type {
563579
override int getSize() { result = 2 }
564580

565581
override int maxValue() { result = 65535 }
582+
583+
override string getAPrimaryQlClass() { result = "UShortType" }
566584
}
567585

568586
/**
@@ -572,6 +590,8 @@ class UIntType extends UnsignedIntegralType, @uint_type {
572590
override string toStringWithTypes() { result = "uint" }
573591

574592
override int getSize() { result = 4 }
593+
594+
override string getAPrimaryQlClass() { result = "UIntType" }
575595
}
576596

577597
/**
@@ -581,6 +601,8 @@ class ULongType extends UnsignedIntegralType, @ulong_type {
581601
override string toStringWithTypes() { result = "ulong" }
582602

583603
override int getSize() { result = 8 }
604+
605+
override string getAPrimaryQlClass() { result = "ULongType" }
584606
}
585607

586608
/**
@@ -597,6 +619,8 @@ class FloatType extends FloatingPointType, @float_type {
597619
override string toStringWithTypes() { result = "float" }
598620

599621
override int getSize() { result = 4 }
622+
623+
override string getAPrimaryQlClass() { result = "FloatType" }
600624
}
601625

602626
/**
@@ -606,6 +630,8 @@ class DoubleType extends FloatingPointType, @double_type {
606630
override string toStringWithTypes() { result = "double" }
607631

608632
override int getSize() { result = 8 }
633+
634+
override string getAPrimaryQlClass() { result = "DoubleType" }
609635
}
610636

611637
/**
@@ -615,6 +641,8 @@ class DecimalType extends SimpleType, @decimal_type {
615641
override string toStringWithTypes() { result = "decimal" }
616642

617643
override int getSize() { result = 16 }
644+
645+
override string getAPrimaryQlClass() { result = "DecimalType" }
618646
}
619647

620648
/**
@@ -775,6 +803,8 @@ class ObjectType extends Class {
775803
ObjectType() { this.hasQualifiedName("System.Object") }
776804

777805
override string toStringWithTypes() { result = "object" }
806+
807+
override string getAPrimaryQlClass() { result = "ObjectType" }
778808
}
779809

780810
/**
@@ -784,6 +814,8 @@ class StringType extends Class {
784814
StringType() { this.hasQualifiedName("System.String") }
785815

786816
override string toStringWithTypes() { result = "string" }
817+
818+
override string getAPrimaryQlClass() { result = "StringType" }
787819
}
788820

789821
/**
@@ -910,7 +942,9 @@ class FunctionPointerType extends Type, Parameterizable, @function_pointer_type
910942
/**
911943
* The `null` type. The type of the `null` literal.
912944
*/
913-
class NullType extends RefType, @null_type { }
945+
class NullType extends RefType, @null_type {
946+
override string getAPrimaryQlClass() { result = "NullType" }
947+
}
914948

915949
/**
916950
* A nullable type, for example `int?`.
@@ -1124,6 +1158,8 @@ class TupleType extends ValueType, @tuple_type {
11241158
final override predicate hasQualifiedName(string qualifier, string name) {
11251159
this.getUnderlyingType().hasQualifiedName(qualifier, name)
11261160
}
1161+
1162+
override string getAPrimaryQlClass() { result = "TupleType" }
11271163
}
11281164

11291165
/**

csharp/ql/lib/semmle/code/csharp/Variable.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,8 @@ class LocalVariable extends LocalScopeVariable, @local_variable {
338338
override Type getType() { localvars(this, _, _, _, getTypeRef(result), _) }
339339

340340
override Location getALocation() { localvar_location(this, result) }
341+
342+
override string getAPrimaryQlClass() { result = "LocalVariable" }
341343
}
342344

343345
/**

csharp/ql/lib/semmle/code/csharp/exprs/Expr.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,8 @@ class DiscardExpr extends Expr, @discard_expr {
10841084

10851085
private class UnknownExpr extends Expr, @unknown_expr {
10861086
override string toString() { result = "Expression" }
1087+
1088+
override string getAPrimaryQlClass() { result = "UnknownExpr" }
10871089
}
10881090

10891091
/**

csharp/ql/lib/semmle/code/csharp/frameworks/System.qll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ class SystemBooleanStruct extends BoolType {
112112
result.getParameter(1).getType() instanceof BoolType and
113113
result.getReturnType() instanceof BoolType
114114
}
115+
116+
override string getAPrimaryQlClass() { result = "SystemBooleanStruct" }
115117
}
116118

117119
/** Data flow for `System.Boolean`. */
@@ -1092,6 +1094,8 @@ class SystemIntPtrType extends ValueType {
10921094
this = any(SystemNamespace n).getATypeDeclaration() and
10931095
this.hasName("IntPtr")
10941096
}
1097+
1098+
override string getAPrimaryQlClass() { result = "SystemIntPtrType" }
10951099
}
10961100

10971101
/** The `System.IDisposable` interface. */

csharp/ql/lib/semmle/code/dotnet/Element.qll

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,15 @@ class Element extends @dotnet_element {
4343

4444
/**
4545
* Gets a comma-separated list of the names of the primary CodeQL classes to which this element belongs.
46+
*
47+
* If no primary class can be determined, the result is `"???"`.
4648
*/
47-
final string getPrimaryQlClasses() { result = concat(this.getAPrimaryQlClass(), ",") }
49+
final string getPrimaryQlClasses() {
50+
result = strictconcat(this.getAPrimaryQlClass(), ",")
51+
or
52+
not exists(this.getAPrimaryQlClass()) and
53+
result = "???"
54+
}
4855

4956
/**
5057
* Gets the name of a primary CodeQL class to which this element belongs.
@@ -53,11 +60,12 @@ class Element extends @dotnet_element {
5360
* which they belong; for example, `AddExpr` is a primary class, but
5461
* `BinaryOperation` is not.
5562
*
56-
* This predicate always has a result. If no primary class can be
57-
* determined, the result is `"???"`. If multiple primary classes match,
58-
* this predicate can have multiple results.
63+
* If no primary classes match, this predicate has no result. If multiple
64+
* primary classes match, this predicate can have multiple results.
65+
*
66+
* See also `getPrimaryQlClasses`, which is better to use in most cases.
5967
*/
60-
string getAPrimaryQlClass() { result = "???" }
68+
string getAPrimaryQlClass() { none() }
6169
}
6270

6371
/** An element that has a name. */

csharp/ql/lib/semmle/code/dotnet/Namespace.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class Namespace extends Declaration, @namespace {
3939
final override string getName() { namespaces(this, result) }
4040

4141
final override string getUndecoratedName() { namespaces(this, result) }
42+
43+
override string getAPrimaryQlClass() { result = "Namespace" }
4244
}
4345

4446
/** The global namespace. */

csharp/ql/lib/semmle/code/dotnet/Type.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,6 @@ class ArrayType extends ValueOrRefType, @dotnet_array_type {
8989
final override string getLabel() { result = this.getElementType().getLabel() + "[]" }
9090

9191
override string toStringWithTypes() { result = this.getElementType().toStringWithTypes() + "[]" }
92+
93+
override string getAPrimaryQlClass() { result = "ArrayType" }
9294
}

0 commit comments

Comments
 (0)