Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions csharp/ql/consistency-queries/PrimaryQlClass.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import csharp

query predicate missingPrimaryQlClass(Element e) {
not exists(e.getAPrimaryQlClass()) and
e.fromSource()
}
2 changes: 2 additions & 0 deletions csharp/ql/lib/semmle/code/csharp/Generics.qll
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ class TypeParameterConstraints extends Element, @type_parameter_constraints {

/** Gets a textual representation of these constraints. */
override string toString() { result = "where " + this.getTypeParameter().getName() + ": ..." }

override string getAPrimaryQlClass() { result = "TypeParameterConstraints" }
}

/**
Expand Down
4 changes: 4 additions & 0 deletions csharp/ql/lib/semmle/code/csharp/Modifier.qll
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ class Modifier extends Element, @modifier {
predicate hasName(string name) { name = this.getName() }

override string toString() { result = this.getName() }

override string getAPrimaryQlClass() { result = "Modifier" }
}

/**
* An access modifier: `public`, `private`, `internal` or `protected`.
*/
class AccessModifier extends Modifier {
AccessModifier() { this.hasName(["public", "private", "internal", "protected"]) }

override string getAPrimaryQlClass() { result = "AccessModifier" }
}
38 changes: 37 additions & 1 deletion csharp/ql/lib/semmle/code/csharp/Type.qll
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,8 @@ class VoidType extends DotNet::ValueOrRefType, Type, @void_type {
final override string getUndecoratedName() { result = "Void" }

override SystemNamespace getDeclaringNamespace() { any() }

override string getAPrimaryQlClass() { result = "VoidType" }
}

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

override int getSize() { result = 1 }

override string getAPrimaryQlClass() { result = "BoolType" }
}

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

override int maxValue() { result = 65535 }

override string getAPrimaryQlClass() { result = "CharType" }
}

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

override int maxValue() { result = 127 }

override string getAPrimaryQlClass() { result = "SByteType" }
}

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

override int maxValue() { result = 32767 }

override string getAPrimaryQlClass() { result = "ShortType" }
}

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

override int maxValue() { result = 2147483647 }

override string getAPrimaryQlClass() { result = "IntType" }
}

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

override int getSize() { result = 8 }

override string getAPrimaryQlClass() { result = "LongType" }
}

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

override int maxValue() { result = 255 }

override string getAPrimaryQlClass() { result = "ByteType" }
}

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

override int maxValue() { result = 65535 }

override string getAPrimaryQlClass() { result = "UShortType" }
}

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

override int getSize() { result = 4 }

override string getAPrimaryQlClass() { result = "UIntType" }
}

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

override int getSize() { result = 8 }

override string getAPrimaryQlClass() { result = "ULongType" }
}

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

override int getSize() { result = 4 }

override string getAPrimaryQlClass() { result = "FloatType" }
}

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

override int getSize() { result = 8 }

override string getAPrimaryQlClass() { result = "DoubleType" }
}

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

override int getSize() { result = 16 }

override string getAPrimaryQlClass() { result = "DecimalType" }
}

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

override string toStringWithTypes() { result = "object" }

override string getAPrimaryQlClass() { result = "ObjectType" }
}

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

override string toStringWithTypes() { result = "string" }

override string getAPrimaryQlClass() { result = "StringType" }
}

/**
Expand Down Expand Up @@ -910,7 +942,9 @@ class FunctionPointerType extends Type, Parameterizable, @function_pointer_type
/**
* The `null` type. The type of the `null` literal.
*/
class NullType extends RefType, @null_type { }
class NullType extends RefType, @null_type {
override string getAPrimaryQlClass() { result = "NullType" }
}

/**
* A nullable type, for example `int?`.
Expand Down Expand Up @@ -1124,6 +1158,8 @@ class TupleType extends ValueType, @tuple_type {
final override predicate hasQualifiedName(string qualifier, string name) {
this.getUnderlyingType().hasQualifiedName(qualifier, name)
}

override string getAPrimaryQlClass() { result = "TupleType" }
}

/**
Expand Down
2 changes: 2 additions & 0 deletions csharp/ql/lib/semmle/code/csharp/Variable.qll
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,8 @@ class LocalVariable extends LocalScopeVariable, @local_variable {
override Type getType() { localvars(this, _, _, _, getTypeRef(result), _) }

override Location getALocation() { localvar_location(this, result) }

override string getAPrimaryQlClass() { result = "LocalVariable" }
}

/**
Expand Down
2 changes: 2 additions & 0 deletions csharp/ql/lib/semmle/code/csharp/exprs/Expr.qll
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,8 @@ class DiscardExpr extends Expr, @discard_expr {

private class UnknownExpr extends Expr, @unknown_expr {
override string toString() { result = "Expression" }

override string getAPrimaryQlClass() { result = "UnknownExpr" }
}

/**
Expand Down
4 changes: 4 additions & 0 deletions csharp/ql/lib/semmle/code/csharp/frameworks/System.qll
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ class SystemBooleanStruct extends BoolType {
result.getParameter(1).getType() instanceof BoolType and
result.getReturnType() instanceof BoolType
}

override string getAPrimaryQlClass() { result = "SystemBooleanStruct" }
}

/** Data flow for `System.Boolean`. */
Expand Down Expand Up @@ -1092,6 +1094,8 @@ class SystemIntPtrType extends ValueType {
this = any(SystemNamespace n).getATypeDeclaration() and
this.hasName("IntPtr")
}

override string getAPrimaryQlClass() { result = "SystemIntPtrType" }
}

/** The `System.IDisposable` interface. */
Expand Down
18 changes: 13 additions & 5 deletions csharp/ql/lib/semmle/code/dotnet/Element.qll
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,15 @@ class Element extends @dotnet_element {

/**
* Gets a comma-separated list of the names of the primary CodeQL classes to which this element belongs.
*
* If no primary class can be determined, the result is `"???"`.
*/
final string getPrimaryQlClasses() { result = concat(this.getAPrimaryQlClass(), ",") }
final string getPrimaryQlClasses() {
result = strictconcat(this.getAPrimaryQlClass(), ",")
or
not exists(this.getAPrimaryQlClass()) and
result = "???"
}

/**
* Gets the name of a primary CodeQL class to which this element belongs.
Expand All @@ -53,11 +60,12 @@ class Element extends @dotnet_element {
* which they belong; for example, `AddExpr` is a primary class, but
* `BinaryOperation` is not.
*
* This predicate always has a result. If no primary class can be
* determined, the result is `"???"`. If multiple primary classes match,
* this predicate can have multiple results.
* If no primary classes match, this predicate has no result. If multiple
* primary classes match, this predicate can have multiple results.
*
* See also `getPrimaryQlClasses`, which is better to use in most cases.
*/
string getAPrimaryQlClass() { result = "???" }
string getAPrimaryQlClass() { none() }
}

/** An element that has a name. */
Expand Down
2 changes: 2 additions & 0 deletions csharp/ql/lib/semmle/code/dotnet/Namespace.qll
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class Namespace extends Declaration, @namespace {
final override string getName() { namespaces(this, result) }

final override string getUndecoratedName() { namespaces(this, result) }

override string getAPrimaryQlClass() { result = "Namespace" }
}

/** The global namespace. */
Expand Down
2 changes: 2 additions & 0 deletions csharp/ql/lib/semmle/code/dotnet/Type.qll
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,6 @@ class ArrayType extends ValueOrRefType, @dotnet_array_type {
final override string getLabel() { result = this.getElementType().getLabel() + "[]" }

override string toStringWithTypes() { result = this.getElementType().toStringWithTypes() + "[]" }

override string getAPrimaryQlClass() { result = "ArrayType" }
}