C♯ Library Reference
Copyright © 2020 Synopsys, Inc. All rights reserved worldwide.
CodeXM is a domain-specific language that allows you to create custom code checkers. CodeXM checkers analyze source code for specific coding patterns, and report these defects with messages that you provide. Like other Coverity checkers, the defects are displayed in Coverity Connect. They can be archived with the cov-commit-defect
feature.
The CodeXM C# library provides access to C# coding constructs.
This document provides information about the patterns and functions provided by the C# library. To learn how to get started with CodeXM, please see Learning to Write CodeXM Checkers.
Describing CodeXM is a bit different from describing other languages. Most programming books talk about one language only. To describe CodeXM, we have to talk about CodeXM code itself, and the code of the language it is inspecting—the target language.
This reference shows all code in monospace
type.
CodeXM code appears in shades of green.
Target code (whatever the language) appears in shades of orange.
Reminder: By default, many browsers do not print out background colors.
The type of certain pattern properties is said to be nullable. This means that the property might return a value, or it might not. If it does not, its value matches the CodeXM keyword null. In the CodeXM documents, a nullable type is indicated by the name of the type the property might return, followed by a question mark; for example, int?.
In checker code, a nullable type requires some special handling in order to avoid the error of referencing the null. The Handling Null Values section of Learning to Write CodeXM Checkers describes how to do this.
In your CodeXM source, begin with an include declaration that names the library target language: include `C#`.
Specifying the target language makes the library’s special patterns and functions available within this CodeXM file. These patterns and functions are the subject of this reference.
The language specification also causes the checkers defined in your CodeXM file to be applied only to the target source code in your code base.
Class definitions match class entities in the abstract representation of the target code, and provide properties that describe the classes that the library supports.
Every target-code object that Coverity inspects is a node in an abstract syntax tree. For a brief overview of how Coverity uses these syntax trees, see How Does Coverity Analysis Work?.
Every astnode has the following properties:
Name | Type | Description |
---|---|---|
location | sourceloc | The location of this code construct in the source files |
children | list<astnode> | A list of child nodes that are sub-parts of this code construct |
parent | astnode? | This node’s parent node, if there is one; null if there is no parent. |
macrosExpandedFrom | list<macroinfo> | If the code is produced by macro expansion, this list contains the macros that generated that expansion. Otherwise, this list is empty. |
implicit | bool | Indicates whether the node is the result of compiler intervention, as opposed to being explicitly stated in the source code. For example, if ( x ) in the source code is interpreted as if ( x != 0 ) by the compiler. The not-equals binary operator and the literal constant zero are implicit. |
The astnode elements are further classified as statement, expression, initializer and ctorinit (constructor initializer), and declaration elements (declaration elements declare symbols). Certain kinds of elements have their own set of properties in addition to the astnode properties shown here.
Describes a C# class, interface, enum, or code attribute.
classDefinition produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
attributes | list<codeAttribute> | Any attributes specified for this class |
declaredType | classType | The type of the class. |
fieldList | list<fieldSymbol> | A list of fieldSymbols, one for each non-static field in the class |
location | sourceloc | The location of this class in the source code |
memberFunctionList | list<functionSymbol> | A list of functionSymbols, one for each non-static member function |
parentList | list<classParent> | A list of parent classes |
staticFieldList | list<staticVariableSymbol> | A list of staticVariableSymbol objects, one for each static field in the class |
staticMemberFunctionList | list<functionSymbol> | A list of all the functionSymbol objects for each static member function |
findBaseClass | function<testType> | A function that invokes a callback function, which can be used as a predicate to find a particular base class—in other words, to test whether the class is a parent of the current class. See the section, “The Base Class Properties”, below. |
findMatchingBaseClass | function<testType> | A function that invokes a pattern, which can be used as a predicate to find a particular base class—in other words, to test whether the class is a parent of the current class. See the section “The Base Class Properties”, below. |
The function provided by the findBaseClass property has this form:
The function provided by the findMatchingBaseClass property has this form:
The findBaseClass call detects whether a base class is accepted by the callback function. If the callback returns a non-null value, then findBaseClass returns that value (which matches NonNull). If the callback does not return such a non-null value on any base class, findBaseClass returns null.
The findMatchingBaseClass call uses a pattern rather than a callback function, but it returns either a matching value or null, just as findBaseClass does.
Because both these function calls accomplish the same thing, for the most part which one you choose to use is up to you. The pattern form can be easier, and less lengthy, to code (see the examples that follow); on the other hand, a callback function can encode tests that a pattern cannot.
The following “Examples” section shows checkers that use these properties.
This first example uses findBaseClass. If findBaseClass can locate a class whose identifier (base.identifier) is "A", it returns the object for that class. Otherwise, the return value is null.
The second example uses findMatchingBaseClass:
Describes a C# parent class.
classParent produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
classType | classType | The type of the parent class |
isVirtual | bool | true if the class is virtual |
Describes a function definition.
functionDefinition produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
body | statement | A blockStatement of the function body. |
constructorInitializerList | list<ctor>? | A list of the constructors that are called in the initialization of this function |
formalParameterList | list<symbol> | A list of parameterSymbol objects, one for each argument to the function |
functionSymbol | symbol | The functionSymbol that represents this function |
Inherits properties from:
Describes a function or statically declared variable.
functionOrStaticVariableDefinition produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
allCode | set<astnode> | All the nodes within a function. For a variable, this is usually just its initialization. |
location | sourceloc | The location in the source code. This is used for defect reporting. |
paths | executionPaths | The execution paths used for path-sensitive analysis |
Describes a variable that is declared static within a class.
staticVariableDefinition produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
initializer | initializer? | The initializer for this variable; null if one does not exist |
variable | symbol | A staticVariableSymbol that represents this variable |
Inherits properties from:
Attribute classes match attribute constructs in the target code. Attributes add metadata that can be used to help manage code, specify run-time behavior, and for a variety of other purposes.
When used with Coverity, in particular, an attribute can specify how Coverity Analysis should treat an object; for example, a call to a particular function.
Describes an attribute argument, whether positional or named.
This pattern does not expose any new properties.
Describes a named attribute argument.
attributeNamedArgument produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
name | string | The name of the argument |
value | expression | The value of the named argument |
Inherits properties from:
Describes the positional arguments in an attribute.
attributePositionalArguments produces a record that contains the following property:
Name | Type | Description |
---|---|---|
arguments | list<expression> | The positional arguments to the attribute |
constructorFunction | functionSymbol | The symbol of the constructor function that retrieves the positional arguments |
Inherits properties from:
These are enumerations that are used by the language library, and that are available to your own CodeXM code.
Specifies whether an assignment is simple (assignment only) or compound (incorporating an additional operation such as plus, +, or minus, -).
The following values are defined:
Name | Description |
---|---|
`simple` | A simple assignment; for example, a = b |
`compound` | A compound assignment; for example, a += b |
assignmentOperator, assignmentOperatorCompound, assignmentOperatorSimple
Represents the various kinds of casts that C# supports.
The following values are defined:
Name | Description |
---|---|
`checkedExplicit` | An explicit checked cast |
`checkedImplicit` | An implicit checked cast |
`dynamic` | A dynamic cast |
`explicit` | An explicit cast; for example, (int) a |
`implicit` | An implicit cast |
`uncheckedFromGenerics` | An unchecked cast from generics |
castOperator, castOperatorExplicit, castOperatorImplicit
Represents the kinds of cvModifier known to C# (that is, const or volatile).
The following values are defined:
Name | Description |
---|---|
`const` | constant |
`volatile` | volatile |
Represents the kinds of floating-point types known to C#.
The following values are defined:
Name | Description |
---|---|
`decimal` | A 128-bit number with 28–29 significat digits |
`double` | A double-precision floating-point number |
`float` | A single-precision floating-point number |
Represents either simple or enhanced for loops.
A simple for loop is of the form:
An enhanced for loop is of the form:
The following values are defined:
Name | Description |
---|---|
`enhanced` | An enhanced for loop |
`simple` | A simple for loop |
forLoop, forLoopEnhanced, forLoopSimple
Represents the integer types known to C#.
The following values are defined:
Name | Description |
---|---|
`byte` | Unsigned 8-bit integer |
`char` | Unicode 16-bit character |
`int` | Signed 32-bit integer |
`long` | Signed 64-bit integer |
`sbyte` | Signed 8-bit integer |
`short` | Signed 16-bit integer |
`uint` | Unsigned 32-bit integer |
`ulong` | Unsigned 64-bit integer |
`ushort` | Unsigned 16-bit integer |
addressOf, integralLiteral, integralType
Describes how a variable has been declared.
The following values are defined:
Name | Description |
---|---|
`local` | The variable declaration is local to a function or a class. |
`static` | The variable declaration is static and the variable is available globally. |
`tryResource` | The variable is declared in a block that belongs to a try statement. |
variableDeclaration, variableReference, propertyReference
Represents the scope of a variable symbol.
The following values are defined:
Name | Description |
---|---|
`local` | Scope is local to a function or a class |
`static` | Scope is static, variable is available globally |
The sets described in this section can help narrow the search of your checker. Typically they are used in for loop constructions such as the following:
Includes the definition of all class, interface, and struct types in the current C# code.
Includes the Abstract Syntax Tree (AST) nodes in all functions, and all statically declared variables. This set is the union of allFunctionCode and allStaticVariableCode.
Includes all function and statically declared variable definitions. This set is the union of allFunctionDefinitions and allStaticVariableDefinitions.
Includes all the Abstract Syntax Tree (AST) nodes in all the functions of the current C# code.
allFunctionAndStaticVariableCode, allFunctionsAndGlobalVariableCode
Includes all function definitions in the current C# code.
allFunctionAndStaticVariableDefinitions
Matches all the Abstract Syntax Tree (AST) nodes in all the functions and global variable initializers of the current C# code.
Includes all statically declared variables in the current C# code.
allFunctionAndStaticVariableCode
Includes all definitions of statically declared variables in the current C# code.
allFunctionAndStaticVariableDefinitions
Includes all source files in the current project that are coded in the C# language.
These patterns, organized by category, match the constructs that might be found in the target source code.
Patterns in this section belong to the statement class. They match executable statements in the target code.
Matches all loop kinds.
The pattern matches for (simple/enhanced), while and do ... while loop statements. For specific patterns, with more details about each kind of loop, refer to forLoopSimple, forLoopEnhanced, whileLoop, and doWhileLoop.
This pattern does not expose any new properties.
Inherits properties from:
The following pattern uses allLoops to determine whether the simpleStatement is a loop:
forLoopSimple, forLoopEnhanced, forLoop, whileLoop, doWhileLoop
Matches statements contained in curly braces ( { } ).
Block statements contain one or more statements, enclosed in curly braces.
This pattern only matches nodes of type statement.
blockStatement produces a record that contains the following property:
Name | Type | Description |
---|---|---|
containedStatements | list<statement> | The statements contained in the block |
Inherits properties from:
The following pattern finds blockStatement entities that contain only the empty statement:
Matches break statements.
This does not match a break that has a label. See labeledBreakStatement.
This pattern only matches nodes of type statement.
breakStatement produces a record that contains the following property:
Name | Type | Description |
---|---|---|
controlStatement | statement | The flow control statement within which the break occurs; for example, while or switch |
Inherits properties from:
The following example matches break statements within a switch:
switchStatement, whileLoop, doWhileLoop, forLoop, forLoopSimple, forLoopEnhanced
Matches individual case statements.
This pattern does not match the default statement. See defaultStatement pattern.
This pattern only matches nodes of type statement.
caseStatement produces a record that contains the following property:
Name | Type | Description |
---|---|---|
valueExpression | expression | The value associated with the case |
Inherits properties from:
The following CodeXM pattern matches all case statements that have a case for the integer value 1:
defaultStatement, switchStatement
Matches continue statements.
This pattern only matches nodes of type statement.
continueStatement produces a record that contains the following property:
Name | Type | Description |
---|---|---|
controlStatement | statement | The flow-control statement within which the continue occurs. For example, while or switch. |
Inherits properties from:
The following example matches continue statements within a while loop:
labeledContinueStatement, whileLoop, doWhileLoop, forLoop, forLoopSimple, forLoopEnhanced
Matches the default case in switch statements.
This pattern only matches nodes of type statement.
This pattern does not expose any new properties.
Inherits properties from:
The following pattern matches switch statements that have a default clause:
caseStatement, switchStatement
Matches do ... while loops.
This pattern only matches nodes of type statement.
doWhileLoop produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
bodyStatement | statement | The body of the loop |
conditionExpression | expression | The condition expression of the loop |
Inherits properties from:
The following pattern matches locations where a do ... while loop has a Boolean constant value as its condition:
whileLoop, forLoop, forLoopSimple, forLoopEnhanced
Matches empty statements.
An empty statement can be a line with no executable code that is terminated by a semicolon; non-executable code enclosed by curly braces; or an implied branch of an if statement.
The following are examples of emptyStatement situations:
This pattern only matches nodes of type statement.
This pattern does not expose any new properties.
Inherits properties from:
Matches fixed statements.
This pattern only matches nodes of type statement.
fixedStatement produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
bodyStatement | statement | The contained statement |
declarations | declaration | Declaration of the fixed variables |
Inherits properties from:
The following pattern matches fixed statements that are empty:
Matches both simple and enhanced for loops.
To match specific kinds of for loops, and to get greater detail about them, refer to forLoopSimple and forLoopEnhanced.
This pattern only matches nodes of type statement.
forLoop produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
body | statement | The body of the for loop |
kind | enum forLoopKind | The kind of for loop: either `simple` or `enhanced`; see ForLoopKinde |
Inherits properties from:
Matches any simple for loop:
forLoopSimple, forLoopEnhanced
Matches enhanced for loops of the form foreach (int i in numbers).
Matches only nodes of type statement.
forLoopEnhanced produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
bodyStatement | statement | The body of the loop |
containerExpression | expression | The container being iterated |
kind | enum ForLoopKind | Always `enhanced`; see ForLoopKind |
loopVariable | localVariableSymbol | The iterator for the loop |
Inherits properties from:
The following pattern finds all enhanced for loops over an array of integers:
Matches simple for loops of the form for (int i = 0; i < 10; i++).
This pattern only matches nodes of type statement.
forLoopSimple produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
bodyStatement | statement | The body of the loop |
conditionDeclaration | variableDeclaration? | If a variable is declared in the second clause of the loop, it is represented here; otherwise this field is null |
conditionExpression | statement | The conditional clause for the loop |
initializationStatement | statement | The initialization clause for the loop |
kind | enum ForLoopKind | Always `simple`; see ForLoopKind |
updateStatement | statement | The update clause of the loop |
Inherits properties from:
The following pattern matches a simple for loop that uses a postfix increment expression to update its iterations:
Matches goto statements.
This pattern only matches nodes of type statement.
goToStatement produces a record that contains the following property:
Name | Type | Description |
---|---|---|
labelStatement | statement | The statement to go to |
Inherits properties from:
Matches entire if statements, including their condition expressions and their true and false branches.
This pattern only matches nodes of type statement.
ifStatement produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
conditionDeclaration | variableDeclaration? | The variable declared in the condition, if one exists; null otherwise |
conditionExpression | expression | The condition of the if statement |
falseStatement | statement? | The false branch of the if statement, or null if no false branch exists |
trueStatement | statement | The true branch of the if statement |
Inherits properties from:
The following pattern matches if statements that have no else clause:
Matches break statements that target a label.
This pattern only matches nodes of type statement.
labeledBreakStatement produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
controlStatement | statement | The flow-of-control statement within which the break occurs; for example, while or switch |
target | statement | The label to break to |
Inherits properties from:
The following pattern matches a break statement that targets the label outer:
labelStatement, breakStatement, switchStatement, whileLoop, doWhileLoop, forLoop, forLoopSimple, forLoopEnhanced
Matches continue statements that target a label.
This pattern only matches nodes of type statement.
labeledContinueStatement produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
controlStatement | statement | The flow-of-control statement within which the continue occurs; for example, while or switch |
target | statement | The label to target |
Inherits properties from:
The following pattern matches a continue statement that targets the label outer:
Matches statements that have a label.
This pattern only matches nodes of type statement.
labelStatement produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
nameString | string | The name of the label |
targetStatement | statement | The statement to which this label is assigned |
Inherits properties from:
The following pattern finds all continue statements that target the label outer:
labeledContinueStatement, labeledBreakStatement
Matches both simple, void return statements, and return <expression> returns.
This pattern only matches nodes of type statement.
returnStatement produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
isVoid | bool | true if the return does not have an associated expression |
returnedExpression | expression? | The expression returned, if one is specified; null, otherwise |
Inherits properties from:
The following pattern matches return statements that are void (that is, that do not return a value):
Matches individual executable statements.
The term simple—as opposed to compound—means that the these statements are not flow-of-control statements. Simple statements include assignments, function calls, and function returns. Variable declarations (variableDeclaration) are not considered statements.
This pattern only matches nodes of type statement.
simpleStatement produces a record that contains the following property:
Name | Type | Description |
---|---|---|
expression | expression | The expression to match, such as a function call or an assignment |
Inherits properties from:
The following pattern matches a function call:
Matches entire switch statements, including all the statements contained in their case and default clauses.
This pattern only matches nodes of type statement.
switchStatement produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
bodyStatement | statement | The body of the switch statement |
caseList | list<statement> | A list of the case names for the switch statement |
conditionDeclaration | variableDeclaration? | If a variable is declared in the conditionExpression, it is identified here; if it is not, this field is null |
conditionExpression | expression | The condition for the switch statement |
Inherits properties from:
The following pattern matches switch statements that have a default clause:
caseStatement, defaultStatement
Matches synchronized blocks.
This pattern only matches nodes of type statement.
synchronizedStatement produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
bodyStatement | statement | The statement block |
lockExpression | expression | The monitor object of the statement |
Inherits properties from:
This pattern matches all synchronized blocks that use an expression of type class MyLock as the monitor:
Matches throw statements.
This pattern only matches nodes of type statement.
throwStatement produces a record that contains the following property:
Name | Type | Description |
---|---|---|
conditionExpression | expression | The expression that is thrown when an exception occurs |
Inherits properties from:
The following pattern matches when an expression of the type class MyException is thrown:
Matches try statements, including their initial block, catch blocks, and finally block (if present).
This pattern only matches nodes of type statement.
tryStatement produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
bodyStatement | statement | The body of the initial try block |
catchBlockList | list<handler> | The bodies of the catch blocks |
finallyStatement | statement? | The body of the finally block, it if exists; null, otherwise |
resourcesList | list<declaration> | A list of resources declared in a try-with-resource try. |
Inherits properties from:
The following pattern matches handlers for Exception. It uses the tryStatement pattern’s catchBlockList property, which provides a list of handlers:
Matches unsafe blocks.
This pattern only matches nodes of type statement.
unsafeStatement produces a record that contains the following property:
Name | Type | Description |
---|---|---|
bodyStatement | statement | The body statement contained by unsafe |
Inherits properties from:
The following pattern matches unsafe statements that are empty:
Matches standard while loops.
This pattern matches only while loops, and does not match do ... while or for loops.
This pattern only matches nodes of type statement.
whileLoop produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
bodyStatement | statement | The body of the loop |
conditionDeclaration | variableDeclaration? | If a variable is declared in the condition expression, it is represented here; if it is not, this field is null |
conditionExpression | expression | The expression in the condition of the loop |
Inherits properties from:
The following pattern matches locations where a while loop has a Boolean constant value as its condition:
doWhileLoop, forLoop, forLoopSimple, forLoopEnhanced
Patterns in this section belong to the declaration class. They match object declarations in the target code.
Matches all kinds of variable declarations.
variableDeclaration produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
initializer | initializer? | The initializer of the variable, if it exists |
kind | enum variabelDeclarationKind | Either `local` or `static`; see variableDeclarationKind |
variable | symbol | The identifier of the variable being declared |
Inherits properties from:
The following pattern matches all variables declared with the type char:
These patterns match various types of values in the target code.
Matches all array types.
This pattern only matches nodes of type type.
arrayType produces a record that contains the following property:
Name | Type | Description |
---|---|---|
elementType | type | The type of the elements in the array |
The following pattern finds all arrays whose elements are any type of integer:
Matches the bool type.
This pattern only matches nodes of type type.
booleanType produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
alignmentInBytes | int | The alignment of the type, in bytes |
sizeInBits | int | The size of the Boolean, in bits |
sizeInBytes | int | The size of the Boolean, in bytes |
The following pattern matches any expression of the type bool:
Matches the char type.
This pattern only matches nodes of type type.
charType produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
alignmentInBytes | int | The alignment of the type, in bytes |
sizeInBits | int | The total size of the char, in bits |
sizeInBytes | int | The size of the char, in bytes |
The following pattern matches any expression of the type char:
Matches all kinds of C# class types.
This pattern matches the following kinds of C# classes:
This pattern only matches nodes of type type.
classType produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
extra | extra | Extra information used for defect reporting |
extraWithoutScope | extra | Extra information used for defect reporting. This version of the extra information omits the scope details. |
isAnonymous | bool | true if the class is anonymous |
isComplete | bool | true if the class is complete. |
kind | enum | The kind of class, one of `class`, `delegate`, `enum`, `interface`, or `struct` |
location | location | The location information of the class |
qualifiedName | string | The name of the class including the class’s package |
scopeList | list<string> | The scope of the class. This is the elements of the qualified name broken up into a list. |
simpleName | string | The name of the class, without the package prefix |
The following pattern matches all delegate classes:
Matches const or volatile modified types in C#.
This pattern only matches nodes of type type.
cvModifiedType produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
cvFlags | list<cvModifierKind> | A list of the modifier types applied to the matched type |
toType | enum cvModifierKind | The type of the object being modified. |
The following pattern:
... matches const char.
This pattern can be used to inspect target code such as the following:
Matches all kinds of C# enum types.
This pattern only matches nodes of type type.
enumType produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
alignmentInBytes | int | The alignment of the enum, in bytes |
extra | extra | Extra information used for defect reporting |
extraWithoutScope | extra | Extra information used for defect reporting. This version of the extra information omits the scope details. |
location | location | The location of the enum |
qualifiedName | string | The name of the enum including its package |
scopeList | list<string> | The scope of the enum. This is the elements of the qualified name broken up into a list. |
simpleName | string | The name of the enum, without the package prefix |
sizeInBytes | int | The size of the enum, in bytes |
The following CodeXM pattern matches all enum classes:
Matches C# floating-point types.
This pattern only matches nodes of type type.
floatType produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
alignmentInBytes | int | The alignment of the type, in bytes |
kind | enum floatKind | The kind of floating-point type. See floatKind. |
sizeInBits | int | The total size of the float, in bits |
sizeInBytes | int | The size of the float, in bytes |
Note: The representation of floating types is implementation-defined, so CodeXM does not show how bits are apportioned between the exponent and the mantissa.
The following pattern matches any expression that has the type double:
Matches function types.
This pattern only matches nodes of type type.
functionType produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
isStatic | bool | true if the function is declared as static |
parameterTypeList | list<type> | A list of all the parameter types for the function |
returnType | type | The return type of the function |
The following pattern matches all function types that return an int:
Matches C# generic types.
This pattern only matches nodes of type type.
genericType produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
genericClass | type | The generic type |
isNullable | bool | true if this a nullable type |
typeParameters | list<typeParameterType> | The type parameters |
The following CodeXM pattern matches all nullable types:
Matches all C# integer types.
This pattern only matches nodes of type type.
integralType produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
alignmentInBytes | int | The alignment of the type, in bytes |
kind | Subset of enum intKind | The type of integer: `int`, `short`, `long`, or `byte`; see also intKind |
sizeInBits | int | The size of the type, in bits |
sizeInBytes | int | The size of the type, in bytes |
The following pattern uses the kind property to match a long type:
Matches C# nullable types.
This pattern only matches nodes of type type.
nullableType produces a record that contains the following property:
Name | Type | Description |
---|---|---|
valueType | type | The underlying type that can be nulled |
The following pattern matches all nullable char instances:
Matches a C# out parameter.
This pattern only matches nodes of type type.
outParamReferenceType produces a record that contains the following property:
Name | Type | Description |
---|---|---|
toType | type | The type the reference refers to |
The following pattern:
... matches integer out parameter types; for example:
Matches C# pointer types.
C# recognizes pointer types only in unsafe mode.
This pattern only matches nodes of type type.
pointerType produces a record that contains the following property:
Name | Type | Description |
---|---|---|
toType | type | The type of the object pointed to |
The following pattern matches all pointers to a class named MyClass:
Matches C# reference (ref) parameter types.
Reference types are used when something is passed as a reference (as opposed to by value); for example, in a function call such as myFun(ref count).
This pattern only matches nodes of type type.
referenceParamType produces a record that contains the following property:
Name | Type | Description |
---|---|---|
toType | type | The type the reference refers to |
The following pattern matches all reference types to a class named MyClass:
Here is another example of using ref:
Matches C# reference types.
A C# reference type can be declared as a class, interface>, or delegate; the dynamic, object, and string types are inherently references.
This pattern only matches nodes of type type.
referenceType produces a record that contains the following property:
Name | Type | Description |
---|---|---|
toType | type | The type the reference refers to |
The following CodeXM pattern matches all references to a class named MyClass:
outParamReferenceType referenceParamType
Matches C# System.Tuple class types.
This pattern only matches nodes of type type.
tupleClassType produces a record that contains the following property:
Name | Type | Description |
---|---|---|
componentTypes | list<type> | The types of the tuple’s components |
The following CodeXM pattern matches all instances of Tuple<char,bool>:
Matches all C# parameter types.
This pattern only matches nodes of type type.
typeParameterType produces a record that contains the following property:
Name | Type | Description |
---|---|---|
name | string | The name of the parameter type |
The following CodeXM pattern matches all parameters whose type is Element:
Matches types that have been defined with the using keyword.
This pattern only matches nodes of type type.
usingType produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
alias | string | The (mangled) name of the type being defined |
id | symbol | The identifier of the type being defined |
targetType | type | The type being associated with the name |
Matches any C# System.ValueTuple type.
This pattern only matches nodes of type type.
valueTupleType produces a record that contains the following property:
Name | Type | Description |
---|---|---|
componentTypes | list<type> | The types of the tuple’s components |
The following CodeXM pattern matches all instances of ValueTuple<char,bool>:
Matches the void type.
This pattern only matches nodes of type type.
This pattern does not expose any new properties.
Patterns in this section belong to the expression class. They match expressions in the target code.
Many statements in C# code contain expressions. The patterns in this section match specific kinds of expressions.
Please be aware: The patterns shown in the “Literals” and “Operators” sections are expressions, too.
For example, in the following snippet of target code:
... we see a pair of if statements. As described in the previous section, the ifStatement pattern detects either instance.
But each statement has a condition: namely, the part enclosed by the parentheses that follow the keyword if. Both conditions in this example are expressions. The first is simply a variable reference. The second is more complicated: It is made up of a binary operator (specifically, the equality operator) with operands (which are themselves expressions) appearing on either side. The left-hand operand is a function call, and the right-hand operand is a variable reference.
You could match the first of these conditions by using the expression pattern variableReference (which also matches the right-hand side of the second condition), and you could match the second of these conditions by using the expression pattern binaryOperator.
To inspect a complete condition expression, look at the .conditionExpression property of the ifStatement pattern.
In addition to the properties specific to each pattern, and the properties inherited from astnode, all expression patterns have the following properties:
Name | Type | Description |
---|---|---|
type | type | The C# type of the expression |
isParenthesized | bool | Whether there are parenthesis around this expression |
Matches the use of __arglist.
In C#, you can use __arglist as an argument to a new ArgIterator object. This allows you to later call that object using __arglist(args) and specify an arbitrary number of arguments in place of "args".
In other words, __arglist behaves somewhat like the ... construct for “tuple” arguments in C++.
Here is an example of using __arglist in a new declaration:
This pattern only matches nodes of type expression.
argList produces a record that contains the following property:
Name | Type | Description |
---|---|---|
type | type | The type of the expression |
Inherits properties from:
Matches all expressions, such as array.length, that get the length of an array.
This pattern only matches nodes of type expression.
arrayLength produces a record that contains the following property:
Name | Type | Description |
---|---|---|
array | expression | The array to get the length of |
Inherits properties from:
The following pattern finds all array length expressions for integer arrays:
Matches box expressions: both boxing a value and unboxing a reference.
This pattern only matches nodes of type expression.
boxExpression produces a record that contains the following property:
Name | Type | Description |
---|---|---|
expression | expression | The expression being boxed or unboxed |
Inherits properties from:
The following pattern matches all unboxing expressions that directly call a function:
Matches checked blocks.
This pattern only matches nodes of type statement.
checkedBlock produces a record that contains the following property:
Name | Type | Description |
---|---|---|
bodyStatement | statement | The statements contained in the block |
Inherits properties from:
The following pattern matches checked statements that are empty:
checkedexpression uncheckedexpression uncheckedBlock
Matches checked expressions.
This pattern only matches nodes of type expression.
checkedExpression produces a record that contains the following property:
Name | Type | Description |
---|---|---|
expression | expression | The expression being checked |
Inherits properties from:
The following pattern matches all checked expressions on integer types:
checkedexpression, uncheckedexpression, checkedBlock
Matches deconstruction expressions.
This pattern only matches nodes of type expression.
deconstructionExpression produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
sourceExpression | expression | The source of the deconstruction |
targetStatements | list<statement> | The targets of the deconstruction |
Inherits properties from:
The following pattern matches the deconstruction of value tuples:
Matches dynamic casts.
This pattern only matches nodes of type expression.
dynamicCastExpression produces a record that contains the following property:
Name | Type | Description |
---|---|---|
expression | expression | The expression being cast |
Inherits properties from:
The following pattern matches casts to short:
castOperatorImplicit, castOperator
Matches dynamic function calls.
This pattern only matches nodes of type expression.
dynamicFunctionCall produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
callKind | DynamicFuncKind | One of `DFK_METHOD`, `DFK_INVOKE`, `DFK_PROPERTY`, `DFK_INDEXER`, `DFK_OPERATOR`, or `DFK_CONVERSION` |
name | string | The name of the function |
Inherits properties from:
The following pattern matches calls to the dynamic function named example():
Matches expressions that reference an enum.
This pattern only matches nodes of type expression.
enumReference produces a record that contains the following property:
Name | Type | Description |
---|---|---|
enumVariableSymbol | symbol | The symbol that refers to this enum |
Inherits properties from:
The following pattern matches enum instances that have the identifer Example:
Matches C# query-expression patterns.
This pattern matches C# patterns (not to be confused with CodeXM patterns). In C#, a query-expression pattern matches other portions of the C# code.
expressionPattern produces a record that contains the following property:
Name | Type | Description |
---|---|---|
expression | expression | The C# expression used for matching |
Inherits properties from:
The following pattern matches an expression that contains the C# pattern match expression is null:
patternMatchExpression, namedPattern, isPattern
Matches all expression trees.
This pattern only matches nodes of type expression.
This pattern does not expose any new properties.
Inherits properties from:
The following pattern matches only expression trees whose root is a binary operator:
Matches expressions that access fields.
This pattern only matches nodes of type expression.
fieldAccess produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
field | symbol | The symbol of the field being accessed |
objectExpression | expression | The object that owns the field being accessed |
Inherits properties from:
The following pattern matches all accesses to public fields:
Matches expressions that reference fields.
This pattern only matches nodes of type expression.
fieldReference produces a record that contains the following property:
Name | Type | Description |
---|---|---|
fieldSymbol | symbol | The field symbol |
Inherits properties from:
The following pattern matches when a field example is referenced:
Matches calls to functions.
This pattern only matches nodes of type expression.
functionCall a record that contains the following properties:
Name | Type | Description |
---|---|---|
argumentList | list<expression> | A list of the arguments passed to the function |
calledExpression | expression | The expression of the function being called |
calledFunction | functionSymbol | The symbol of the function being called |
isNonStaticMethod | bool | true if the function being called is a non-static method |
resolvedCallees | list<symbol> | A list of the callees |
Inherits properties from:
The following pattern finds function calls whose arguments are not variable references:
Matches expressions that reference functions.
This pattern only matches nodes of type expression.
functionReference produces a record that contains the following property:
Name | Type | Description |
---|---|---|
functionSymbol | symbol | The symbol that represents the function |
Inherits properties from:
The following pattern matches calls to a function named example():
Matches the C# instanceof() function.
instanceOf produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
expression | expression | The expression being examined |
referenceType | type | The type the expression is being compared with |
Inherits properties from:
The following pattern matches all calls to instanceof() for the class type Example:
Matches C# patterns that perform dynamic type checks.
In C#, you can used an is pattern (not to be confused with a CodeXM pattern) to perform a dynamic type check. The C# pattern can have an optional subpattern: If the type check succeeds, the subpattern is matched against the expression. This pattern is usually followed by a namedPattern.
For example, the following C# snippet matches the variable o against the type int. If the variable is not an integer, the function containing this statement returns:
isPattern produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
subPattern | pattern | The subpattern to match if the type check succeeds |
type | type | The type to match against |
Inherits properties from:
The following pattern matches an expression that contains the C# pattern-match expression is int:
patternMatchExpression, namedPattern, expressionPattern
Matches expressions that use the C# keyword ref to make references.
This pattern only matches nodes of type expression.
makeRefExpression produces a record that contains the following property:
Name | Type | Description |
---|---|---|
expression | expression | The expression being used |
Inherits properties from:
The following pattern matches all make reference expressions within functions:
Matches expressions that use a multi-subscript operator on a multidimensional array.
This pattern only matches nodes of type expression.
multiSubscriptReference produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
arrayExpression | expression | The array the subscript is being used on |
indices | list<expression> | The values inside the brackets ( [ ] ). |
Inherits properties from:
The following pattern matches all two-dimensional arrays:
Matches C# named patterns that bind names to values.
In C#, you can used a named pattern (not to be confused with a CodeXM pattern) to bind a name to a value: usually this follows an isPattern expression, as in the following example:
namedPattern produces a record that contains the following property:
Name | Type | Description |
---|---|---|
variable | variable | The variable the value is being bound to |
Inherits properties from:
The following pattern matches an expression that contains the C# pattern-match expression is int i:
patternMatchExpression, expressionPattern, isPattern
Matches expressions that reference nullable values.
This pattern only matches nodes of type expression.
nullableGetValue produces a record that contains the following property:
Name | Type | Description |
---|---|---|
from | expression | The expression that resulted in a nullable |
Inherits properties from:
The following pattern matches when references of nullables result from a division:
Matches C# pattern-match expressions.
This pattern matches a C# pattern-match expression (not to be confused with a CodeXM pattern). Pattern matching can be used in if and switch statements.
patternMatchExpression produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
expression | expression | The expression to be matched by the pattern |
matchPattern | pattern | The pattern to match |
Inherits properties from:
The following pattern matches an expression that contains the C# pattern-match expression is int:
namedPattern, expressionPattern, isPattern
Matches expressions that reference a C# property.
This pattern only matches nodes of type expression.
propertyReference produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
isFinal | bool | true if this variable is declared as final |
qualifiedName | string | The name of the variable, with scope information |
scope | enum variableDeclarationKind | The scope of the variable: one of `static`, `local`, or `tryResource`; see variableDeclarationKind |
simpleName | string | The name of the variable, without scope information. |
variable | symbol | The symbol that represents this variable |
Inherits properties from:
The following CodeXM matches all variable references to final static variables:
Matches all property updates.
This pattern only matches nodes of type expression.
propertyUpdate produces a record that contains the following property:
Name | Type | Description |
---|---|---|
setterCall | functionSymbol | The setter to be called |
Inherits properties from:
Matches locations where a reference-type expression has been dereferenced.
This pattern only matches nodes of type expression.
referenceDereference produces a record that contains the following property:
Name | Type | Description |
---|---|---|
referencedExpression | expression | The expression being dereferenced |
Inherits properties from:
For the following C# function:
... you might use the following pattern to match the dereferences of the parameter o:
Matches expressions that make references by using the __reftype construct.
The following code is an example of using __reftype:
This pattern only matches nodes of type expression.
refTypeExpression produces a record that contains the following property:
Name | Type | Description |
---|---|---|
expression | expression | The expression being used |
Inherits properties from:
The following pattern matches all make-reference-type expressions from function calls:
Matches expressions that make references by using the __refvalue construct.
The following code is an example of using __refvalue:
This pattern only matches nodes of type expression.
refValueExpression produces a record that contains the following property:
Name | Type | Description |
---|---|---|
expression | expression | The expression being used |
Inherits properties from:
The following pattern matches all make-reference-value expressions from function calls:
Matches stack allocations made by using the keyword stackalloc.
This pattern only matches nodes of type expression.
stackAllocation produces a record that contains the following property:
Name | Type | Description |
---|---|---|
type | expression | The expression being used |
Inherits properties from:
The following pattern matches all stackalloc allocations of more than 100 objects:
Matches uses of a subscript to find a value in an array.
This pattern only matches nodes of type expression.
subscriptReference produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
arrayExpression | expression | The array the subscript is being used on |
indexExpression | expression | The value inside the brackets ( [ ] ) |
Inherits properties from:
The following pattern matches where a variable is used as an index to look up a value in an array:
Matches all locations where a temporary variable is constructed.
This pattern only matches nodes of type expression.
temporaryConstruction produces a record that contains the following property:
Name | Type | Description |
---|---|---|
expression | expression | How the temporary variable was constructed |
Inherits properties from:
The following pattern finds all assignments from temporary variables:
Matches all locations where a temporary object is constructed.
This pattern only matches nodes of type expression.
This pattern does not expose any new properties.
Inherits properties from:
The following pattern matches temporary constructions of collections:
Matches references to compiler-generated temporary variables.
This pattern does not expose any new properties.
Note: The real name of a compiler-generated variable is not accessable.
Inherits properties from:
Matches statements in unchecked blocks.
This pattern only matches nodes of type statement.
uncheckedBlock produces a record that contains the following property:
Name | Type | Description |
---|---|---|
checked | statement | The statement |
Inherits properties from:
The following pattern matches unchecked statements that are empty:
checkedexpression, uncheckedexpression, checkedBlock
Matches unchecked expressions.
This pattern only matches nodes of type expression.
uncheckedExpression produces a record that contains the following property:
Name | Type | Description |
---|---|---|
expression | expression | The expression |
Inherits properties from:
The following pattern matches all unchecked expression on integer types:
checkedexpression uncheckedexpression uncheckedBlock
Matches where variables are referenced in expressions.
This pattern only matches nodes of type expression.
variableReference produces a record that contains the following properties by variableReference:
Name | Type | Description |
---|---|---|
isFinal | bool | true if this variable is declared as final |
qualifiedName | string | The name of the variable, with scope information. |
scope | enum variableDeclarationKind | The scope of the variable: one of `static`, `local`, or `tryResource`; see variableDeclarationKind |
simpleName | string | The name of the variable, without scope information. |
variable | symbol | The symbol that represents this variable |
Inherits properties from:
The following pattern matches all references to final static variables:
Matches yield return expressions.
This pattern only matches nodes of type expression.
yieldExpression produces a record that contains the following property:
Name | Type | Description |
---|---|---|
expression | expression | The expression being yielded |
Inherits properties from:
The following pattern matches all yield return expressions that return integral types:
These patterns match literal values in the target code.
Matches Boolean literals: that is, either true or false.
This pattern only matches nodes of type expression.
booleanLiteral produces a record that contains the following property:
Name | Type | Description |
---|---|---|
value | enum | `true` or `false` |
Inherits properties from:
The following pattern matches all literal Boolean values:
Matches character literals.
This pattern only matches nodes of type expression.
characterLiteral produces a record that contains the following property:
Name | Type | Description |
---|---|---|
value | int | The value of the character |
Inherits properties from:
The following pattern matches all 'a' characters:
Matches class literals.
This pattern only matches nodes of type expression.
classLiteral produces a record that contains the following property:
Name | Type | Description |
---|---|---|
targetType | type | The type of the class |
Inherits properties from:
To match literals for the class Example, you might use the following pattern:
Matches enum literals.
This pattern only matches nodes of type expression.
enumLiteral produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
type | type | The underlying type of the enum |
value | int | The underlying value |
Inherits properties from:
The following pattern matches only enum literal values whose underlying type is byte:
Matches floating-point literals.
This pattern only matches nodes of type expression.
floatLiteral produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
isHexFloat | bool | true if this float is represented using hexadecimal numerals |
suffix | enum? | The suffix of the literal: either `f` or null. |
valueString | string | The value of the float, represented as a string |
Inherits properties from:
The following pattern:
... matches this target-code expression:
Matches integer literals; that is, all literals of the type int, short, long, or byte.
This pattern only matches nodes of type expression.
integralLiteral produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
base | enum | One of `binary`, `decimal`, `octal`, or `hexadecimal`. |
kind | Subset of enum intKind | One of `short`, `byte`, `int`, or `long`; see also intKind |
value | int | The value of the integer literal |
Inherits properties from:
The following pattern matches only long integer literals:
Matches all null literals.
This pattern only matches nodes of type expression.
This pattern does not expose any new properties.
Inherits properties from:
The following pattern finds all assignments to null; for example, a = null:
Matches all string literals.
This pattern only matches nodes of type expression.
stringLiteral produces a record that contains the following property:
Name | Type | Description |
---|---|---|
valueString | string | The value of the string literal. |
Inherits properties from:
The following pattern finds assignments from the string literal "Example":
These patterns match operators in the target code.
Matches instances of the address-of ( & ) operator.
This pattern only matches nodes of type expression.
addressOf produces a record that contains the following property.
Name | Type | Description |
---|---|---|
kind | enum intKind | The type to find the address of: one of `bool`, `byte`, `char`, `decimal`, `double`, `dynamic`, `enum`, `float`, `int`, `object`, `sbyte`, `short`, `string`, `uint`, `ulong`, or `ushort`; see intKind |
Inherits properties from:
The following pattern matches operations that obtain the address of int variables:
Matches all forms of the assignment operator where it occurs.
This pattern does not match variable declarations where the variable is initialized with a value (see variableDeclaration).
This general pattern matches both simple (a = b) and compound (a += b) forms of assignments.
This pattern only matches nodes of type expression.
assignmentOperator produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
kind | enum assignKind | The kind of assignment: either `simple` or `compound`; see assignKind |
operator | enum | The operator used, one of `=`, `+=`, `-=`, `*=`, `/=`, `|=`, `&=` `%=`, `^=`, `>>=`, or `<<=` |
sourceExpression | expression | The expression on the right-hand side of the assignment operator |
targetExpression | expression | The expression on the left-hand side of the assignment operator |
Inherits properties from:
The following pattern matches any assignment where the source of the assignment is a cast:
assignmentOperatorCompound, assignmentOperatorSimple
Matches only compound assignment operators such as a += b.
This pattern only matches nodes of type expression.
assignmentOperatorCompound produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
kind | enum assignKind | Always `compound`; see assignKind |
operator | enum | The operator used: one of `+=`, `-=`, `*=`, `/=`, `|=`, `&=`, `%=`, `^=`, `>>=`, or `<<=` |
sourceExpression | expression | The expression on the right-hand side of the assignment operator |
targetExpression | expression | The expression on the left-hand side of the assignment operator |
Inherits properties from:
The following pattern matches the += operator:
assignmentOperatorSimple, assignmentOperator
Matches only simple assignments such as a = 1.
Even though variable declarations look similar to assignment operators, this pattern does not match variable declarations. See variableDeclaration.
This pattern only matches nodes of type expression.
assignmentOperatorSimple produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
kind | enum assignKind | Always `simple`; see assignKind |
operator | enum | Always `=` |
sourceExpression | expression | The expression on the right-hand side of the assignment operator |
targetExpression | expression | The expression on the left-hand side of the assignment operator |
Inherits properties from:
The following pattern matches all simple assignments to a null literal:
assignmentOperatorCompound, assignmentOperator
Matches await operators.
This pattern only matches nodes of type expression.
awaitOperator produces a record that contains the following property:
Name | Type | Description |
---|---|---|
operandExpression | expression | The thing being thrown by the operator |
Inherits properties from:
The following pattern find all await occurrences in a function named example():
Matches all possible C# binary operators.
This pattern only matches nodes of type expression.
binaryOperator produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
isImplicit | bool | true if the operator is implicit |
lhsExpression | expression | The expression on the left-hand side of the operator |
operator | enum | The operator this pattern represents: one of `==`, `!=`, `<`, `>`, `<=`, `>=`, `*`, `/`, `%`, `+`, `-`, `<<`, `>>`, `&`, `^`, `|`, `&&`, `||`, `,`, or `=` |
rhsExpression | expression | The expression on the right-hand side of the operator |
Inherits properties from:
When pattern matching, be aware that either or both operands of a binary operator can themselves be binary operators that represent operations to be completed before the matched operation is performed.
For example, a + b * c is understood as a + (b * c) due to operator precedence. This is matched as a binary addition that has a right-hand operand of b * c. The value of the subexpression must be computed before the addition is performed.
On the other hand, (a + b) * c is matched as a binary multiplication. The left-hand operand, a + b, must be computed before it is multiplied by c.
The following illustration shows these two situations:
In both these cases, a binaryOperator { .operand == `*` } pattern matches some part of the expression. In the left-hand case, it matches b * c. In the right-hand case, it matches the entire expression.
The following pattern matches only multiplication ( * ):
unaryOperator nullableBinaryOperator
Matches all kinds of casts.
This pattern only matches nodes of type expression.
castOperator produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
kind | enum castKind (see below) | The kind of cast represented |
operandExpression | expression | The expression being cast |
These are the possible values of the kind property (see also castKind):
Name | Description |
---|---|
`checkedExplicit` | An explicit checked cast |
`checkedImplicit` | An implicit checked cast |
`dynamic` | A dynamic cast |
`explicit` | An explicit cast; for example, (int) a |
`implicit` | An implicit cast |
`uncheckedFromGenerics` | An unchecked cast from generics |
Inherits properties from:
The following CodeXM pattern matches all kinds of casts (implicit, explicit, checked) to type int:
Matches explicit casts.
This pattern only matches nodes of type expression.
castOperatorExplicit produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
kind | enum castKind | Always `explicit`. See castKind. |
operandExpression | expression | The expression being cast |
Inherits properties from:
For the following snippet of C#:
... the following pattern matches casts to short, so it would match the preceding snippet:
castOperatorImplicit, castOperator
Matches implicit casts; that is, casts that are not explicitly stated but are performed by the C# compiler.
This pattern only matches nodes of type expression.
castOperatorImplicit produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
kind | enum castKind | Always `implicit`. See castKind. |
operandExpression | expression | The expression being cast |
Inherits properties from:
In the following snippet of C#:
... there is an implicit cast between the int and the long. To detect such a cast, you could use the following pattern:
castOperator, castOperatorExplicit
Matches the conditional operator ? :, sometimes called the “ternary&Cx201d; operator.
This pattern only matches nodes of type expression.
conditionalOperator produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
conditionExpression | expression | The expression that is the condition of the operator (the first argument) |
falseExpression | expression | The expression on the false side of the operator (the third argument) |
trueExpression | expression | The expression on the true side of the operator (the second argument) |
Inherits properties from:
The following pattern matches all conditional operators whose condition uses a binary operator:
Matches all decrement operators, both prefix and postfix.
This pattern only matches nodes of type expression.
decrementOperator produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
kind | enum | Either `prefix` or `postfix` |
operandExpression | expression | The expression the decrement operator is being applied to |
Inherits properties from:
The following pattern matches when a postfix decrement operator is used to update a for loop:
Matches all increment operators, both prefix and postfix.
This pattern only matches nodes of type expression.
incrementOperator produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
kind | enum | Either `prefix` or `postfix` |
operandExpression | expression | The expression the increment operator is being applied to |
Inherits properties from:
The following example CodeXM matches when a prefix increment operator is used to update a for loop:
Matches the C# new operator.
This pattern only matches nodes of type expression.
newOperator produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
initializer | initializer | The expression evaluated to determine the initial condition |
objectType | type | The type of the object being created |
Inherits properties from:
The following pattern matches all new operations that create an object of type class Example:
Matches all possible C# binary operations with nullable values (all of these are comparisons).
This pattern only matches nodes of type expression.
nullableBinaryOperator produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
isImplicit | bool | true if the operator is implicit |
lhsExpression | expression | The expression on the left-hand side of the operator |
operator | enum | The operator this pattern represents: one of `==`, `!=`, `&`, `^`, or `|` |
rhsExpression | expression | The expression on the right-hand side of the operator |
Inherits properties from:
The following pattern matches only a comparison for equality:
Matches the null-coalescing operator ??.
This pattern only matches nodes of type expression.
nullCoalescingOperator produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
nonNullExpression | expression | The value to be coalesced. |
nullExpression | expression | The value to use if the nullable value is actually null |
Inherits properties from:
The following pattern matches only null-coalescing of Boolean values:
Matches all size-of operations.
This pattern only matches nodes of type expression.
This pattern does not expose any new properties.
Inherits properties from:
The following pattern finds calls to sizeof() when the type is int:
The pattern would locate the following call:
Matches instances of the throw operator.
This pattern only matches nodes of type expression.
throwOperator produces a record that contains the following property:
Name | Type | Description |
---|---|---|
operandExpression | expression | The expression being thrown by the operator |
Inherits properties from:
The following pattern finds all throws of type Exception:
Matches unary operators—those operators that have only one operand.
This pattern only matches nodes of type expression.
unaryOperator produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
operandExpression | expression | The expression the operation is performed on |
operator | enum | The unary operator this pattern represents: one of `+`, `-`, `!`, or `~` (tilde for bitwise negation) |
Inherits properties from:
The folowing pattern matches any use of the unary plus ( + ) operator:
Patterns in this section belong to the initializer class—or the ctorinit class, in the case of the constructor initializers. They match initializers in the target code.
In many cases, an initializer can look the same as an assigment statement. However, many languages treat initialization as its own, special case. The CodeXM language libraries treat initialization in this way, as well.
Matches collection initializers.
Collection initializers let you specify one or more element initializers when you initialize a collection type that implements IEnumerable and has an add method. For example:
This pattern only matches nodes of type addMemberInitializer, assignmentMemberInitializer, or nestedMemberInitializer.
addMemberInitializer produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
addFunction | functionType | The add method used |
arguments | list<expression> | The argument used for add |
typeArguments | list<type>? | The type argument if Generic is involved |
Inherits properties from:
The following pattern matches the C# example shown in the previous section:
objectInitializer, nestedMemberInitializer, assignmentMemberInitializer
Matches multidimensional array initializations.
This pattern only matches nodes of type initializer.
arrayDimensionsInitializer produces a record that contains the following property:
Name | Type | Description |
---|---|---|
dimensions | list<expression> | A list of the dimensions |
Inherits properties from:
The arrayDimensionsInitializer pattern matches code such as the following array initialization:
Matches the initializations of linear arrays that use a list enclosed in braces (curly brackets).
This pattern only matches nodes of type initializer.
arrayInitializer produces a record that contains the following property:
Name | Type | Description |
---|---|---|
variableInitializerList | list<initializer> | A list of the elements in the curly braces |
Inherits properties from:
The arrayInitializer pattern matches code such as the following array initialization:
Matches object initializers that set the value of their members.
Object initializers let you assign values to any accessible fields or properties of an object at creation time. If you use an object initializer, you don’t have to invoke a constructor followed by lines of assignment statements. For example:
This pattern only matches nodes of type addMemberInitializer, assignmentMemberInitializer, or nestedMemberInitializer.
assignmentMemberInitializer produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
expression | expression | The expression for the initial value. |
fieldOrSetter | bool | Whether we are setting via a field (true) or via a getter (false) |
typeArguments | list<type>? | The type argument if Generic is involved; null otherwise |
Inherits properties from:
The following pattern matches the C# example shown in the “Detail” section, above:
objectInitializer, nestedMemberInitializer, addMemberInitializer
Matches initialization calls to the super() function.
This pattern only matches nodes of type constructorInitializer.
baseClassInitializer produces a record that contains the following property:
Name | Type | Description |
---|---|---|
baseClass | type | The classType of the base type being called |
Inherits properties from:
Given the following CodeXM pattern:
... it would match the call to super() in the following C# code:
Matches cases where a constructor is used to initialize an object.
This pattern only matches nodes of type initializer.
constructorInitializer produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
arguments | list<expression> | A list of the arguments passed to the constructor |
constructorFunction | symbol | The symbol that represents the constructor function used |
enclosingClassReference | expression? | Non-null when an inner class is used. For example, in the initializer y.new X() this value would be "y". |
Inherits properties from:
For the following C# initialization:
... the following CodeXM can be used to match it. The example pattern matches a constructor that takes an integer literal as its argument:
Matches simple expression initializers used to initialize scalar objects.
This pattern only matches nodes of type initializer.
expressionInitializer produces a record that contains the following property:
Name | Type | Description |
---|---|---|
expression | expression | The expression that evaluates to the value that is used to initialize the scalar object |
Inherits properties from:
The expressionInitializer pattern would match the following C# initialization:
The following pattern matches all initializations done using binary operations.
Matches locations where a member is being initialized as part of a call to a constructor.
This pattern only matches nodes of type constructorInitializer.
memberInitializer produces a record that contains the following property:
Name | Type | Description |
---|---|---|
field | symbol | The fieldSymbol of the member being initialized |
Inherits properties from:
Matches object initializers that allow further nesting of initializers.
You can apply an initializer to a field or value obtained from a getter, as in the following example:
This pattern only matches nodes of type addMemberInitializer, assignmentMemberInitializer, or nestedMemberInitializer.
nestedMemberInitializer produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
fieldOrGetter | bool | Whether we are setting the value by using a field (true) or by using a getter (false) |
initializer | initializer |
The initializer used.
Note: You can use any kind of initializer, not just member initializers. |
typeArguments | list<type>? | The type arguments if Generic is involved; null otherwise |
Inherits properties from:
The following pattern would match the C# example shown in the previous section:
objectInitializer, addMemberInitializer, assignmentMemberInitializer
Matches object initializers used in variable declarations and temporary constructions.
The constructor is called first, then additional initializers are called. A user can specify a series of field or property assignments. For example:
Also, collections can have multiple values which are all added via calls to collection, as in the following code:
This pattern only matches nodes of type initializer.
objectInitializer produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
initializer | initializer | The constructor used |
memberInitializers | memberInitializer | The member initializer used |
Inherits properties from:
The following pattern matches a collection initializer:
temporaryExpression, temporaryConstruction, variableDeclaration, nestedMemberInitializer, addMemberInitializer, assignmentMemberInitializer
Matches locations where a constructor uses another constructor within the same class.
This pattern only matches nodes of type constructorInitializer.
This pattern does not expose any new properties.
Inherits properties from:
Matches initializations where no value is provided.
This pattern only matches nodes of type initializer.
This pattern does not expose any new properties.
Inherits properties from:
For a class declared as follows:
... the initialization of the field `a` will match zeroInitializer.
These patterns match symbols in the target code.
A symbol is an entity that can have an identifier; for example, a symbol might be a variable, a function, or a field in a record.
In Coverity Analysis, each symbol in a program is its own node in the abstract syntax tree, and is distinct from the identifier that represents it in the source.
In addition to the properties that are specific to each pattern, all symbol patterns have the following properties in common:
Name | Type | Description |
---|---|---|
identifier | string? | The string used as an unqualified name for the symbol; null if there is none |
mangledName | string? | The internal “mangled” name used for the symbol (the mangled name includes type and scope information, to disambiguate this instance of the identifier); null if the mangled name is not available |
location | sourceloc | The location where this symbol was declared |
type | type | The C# type declared for this symbol |
access | enum AccessKeyword | The scoping keywords associated with the symbol, such as `public` or `private` |
is_file_or_readonly | bool | true if the symbol has a final specifier |
scopeList | list<string> | A list of the class names and namespaces that enclose the symbol |
Matches the symbols in an enum.
This pattern only matches nodes of type symbol.
enumeratorSymbol produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
isExplicit | bool | Whether this enum is explicit |
ownerEnumType | type | The owner enum type |
value | int | The integer value of the symbol |
Inherits properties from:
Matches symbols used in enum declarations.
This pattern only matches nodes of type symbol.
enumVariableSymbol produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
parentEnum | classType | The parent enum class of this enum value |
qualifiedName | string | The name of the enum value, including any scope information |
simpleName | string | The name of the enum value, without scope information |
Inherits properties from:
The following pattern finds all uses of enum variables:
Matches field symbols in class declarations.
This pattern only matches nodes of type symbol.
fieldSymbol produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
access | enum | The type of access to the field: `public`, `private`, or `protected` |
attributes | list<codeAttribute> | A list of the code attributes applied to this field |
isFinal | bool | true if the field is declared final |
isTransient | bool | true if the field is declared transient |
isVolatile | bool | true if the field is declared volatile |
ownerClass | classType | The class that this field belongs to |
qualifiedName | string | The name of the field, including any scope information |
simpleName | string | The name of the field, excluding scope information |
Inherits properties from:
The following pattern finds all field accesses to private fields:
Matches the symbols used to declare functions.
This pattern only matches nodes of type symbol.
functionSymbol produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
attributes | list<codeAttribute> | A list of the code attributes applied to this function |
explicitParameterCount | int | The number of explicit parameters this function has |
functionType | functionType | The type of the function |
hasThis | bool | true if the function has the implicit this argument |
isAbstract | bool | true if the function is declared abstract |
isAnonymous | bool | true if the function is a lambda or anonymous method |
isAsync | bool | true if the function is async |
isClassInitializer | bool | true if this function is a class initializer |
isCompilerGenerated | bool | true if the function is compiler-generated |
isConstructor | bool | true if the function is a constructor |
isDestructor | bool | true if the function is a destructor |
isExtension | bool | true if the function is an extension method |
isFinal | bool | true if the function is declared final |
isGeneric | bool | true if the function is generic |
isOverride | bool | true if the function is overriding |
isStaticConstructor | bool | true if the function is a static constructor |
isStaticMethod | bool | true if the function is a static method |
isVirtual | bool | true if the function is declared virtual |
qualifiedName | string | The name of the function, including any scope information |
simpleName | string | The name of the function, without scope information |
Inherits properties from:
The following pattern finds all calls to synchronized functions:
Matches the variable symbols used in variable declarations.
This pattern only matches nodes of type symbol.
localVariableSymbol produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
isFinal | bool | true if they symbol is declared as final |
qualifiedName | string | The name of the variable, including any scope information |
simpleName | string | The name of the variable, without scope information |
Inherits properties from:
The following pattern finds all uses of local variables:
Matches parameter symbols in function declarations.
This pattern only matches nodes of type symbol.
parameterSymbol produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
access | enum | The type of access: `public`, `private`, or `protected` |
extra | extra | Extra information used for defect reporting |
isFinal | bool | true if the parameter is declared as final. |
isThis | bool | true if the parameter is the implicit this parameter |
location | location | The parameter’s location information |
ownerClass | classType | The owner class for the symbol |
position | sourceloc | The position of the parameter in the function |
qualifiedName | string | The name of the class, including scope information |
scopeList | list<string> | The scope of the parameter. This is the elements of the qualified name broken up into a list. |
simpleName | string | The name of the parameter, without scope information |
type | type | The parameter’s type |
Inherits properties from:
The following pattern finds all uses of parameters that are references:
Matches symbols for variables declared as static within a class.
This pattern only matches nodes of type symbol.
staticVariableSymbol produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
attributes | list<codeAttribute> | A list of the code attributes applied to this variable |
isFinal | bool | true if the variable is declared final |
ownerClass | classType | The class this variable is located in |
qualifiedName | string | The name of the variable, including any scope information |
simpleName | string | The name of the variable, without scope information |
Inherits properties from:
The following pattern finds all uses of statically defined variables:
Matches the symbols of all declared variables.
This pattern only matches nodes of type symbol.
variableSymbol produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
isFinal | bool | true if the variable is declared final |
qualifiedName | string | The name of the variable, including any scope information |
variableScopeKind | enum | Either `local` for local variables, or `static` for statically defined variables; see variableScopeKind |
simpleName | string | The name of the variable, without scope information |
Inherits properties from:
staticVariableSymbol, localVariableSymbol, enumVariableSymbol, parameterSymbol
These are records that can be used within CodeXM patterns to match declarations, code attributes, or handlers for try/catch blocks.
Represents a code attribute. Attributes can be placed on classes, class variables, or methods.
Name | Type | Description |
---|---|---|
attributeType | classType | The attribute type |
arguments | list<attributeArgument> | The arguments to the attribute |
This type can be used to inspect attributes such as the [Obsolete] attribute in the following sample code:
classDefinition, staticVariableSymbol, functionSymbol, fieldSymbol
Represents a declaration.
declaration produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
initializer | initializer? | An initializer (possibly null) |
kind | variableDeclarationKind | The kind of declaration |
variable | symbol | The symbol of the variable |
The following pattern matches a try-with-resource statement, using a resource named myResource:
Represents exception handlers in a try/catch block.
handler produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
body | blockStatement | The body of the exception handler |
variable | localVariableSymbol | The variable that represents the exception being caught |
The following CodeXM pattern matches handlers that declare a variable named Exception in catch statements; for example, catch ( Exception ex ):
The C# library provides a number of functions to help you handle, and analyze, the target source code.
See the Common Library Reference for descriptions of some general-purpose functions that are available to all language libraries.
Definition functions retrieve class definitions.
Gets the class definition of a given class type, if it is available to the analysis.
Name | Type | Description |
---|---|---|
classTypeResult | classType | The class type whose definition will be fetched |
return value | classDefinition | Returns the class definition if one is available; otherwise, returns null. |
Type functions handle elaborated types.
Returns the type that a referenceType points to, or the type of the elements in an array.
Name | Type | Description |
---|---|---|
t | type | The type to return the base type of |
return value | type | The base type of the argument |
Using the function hasBaseType() on the target-code array int[] a gives you the result integralType.
A function that removes the reference types around a base type.
It is sometimes useful to strip away the reference types to inspect what the reference is pointing to. If a type other than a referenceType is passed, that same type is returned.
Name | Type | Description |
---|---|---|
t | type | The type to strip reference types from This does not itself necessarily need to be a referenceType. |
return value | type | The type, stripped of any reference types |
For the following C# code:
... the parameter c can be passed as a referenceType. To inspect the type being referenced, you can use the following pattern:
Decomposing functions deal explicitly with managing casts and qualifiers.
This function matches any aliased type (for example, one aliased via a using statement) defined in terms of the parameter given.
Use this function to detect not only a specified type, but any other type defined as an alias of that type.
Name | Type | Description |
---|---|---|
baseType | pattern(type) -> Result | A pattern based on a specific type |
return value | pattern(type) -> Result | A pattern similarly matching the desired type, or any alias |
Assuming that the following type definitions appear in your source code:
... then the use of the following pattern:
... matches the actual type int in your source code, as well as any uses of type myInt, and even myNewInt (or any other alias based on any of these types).
A function that generates a pattern to match arrays of a particular type.
This can be used to construct patterns that match nested structure. å
Name | Type | Description |
---|---|---|
typePattern | pattern | A pattern matching a type |
return value | pattern | A pattern matching an array type |
The following CodeXM snippet returns a pattern, intArray, that matches arrays of type int:
Matches types that have a const qualification.
The pattern produced by this function matches various forms of const qualification of the type described by the typePattern field, including the following kinds of constants:
Name | Type | Description |
---|---|---|
typePattern | pattern(type) -> Result | A pattern matching some simple type |
return value | pattern(type) -> Result | A pattern that matches a const-qualified variation of the given type |
Assuming that the following type definition appears in your source code:
... then the use of this pattern:
... matches the actual type const int in your source code.
cvModifiedType, cvModifierKind
Returns a list of const/volatile qualifiers, including ones that are hidden behind aliases.
Name | Type | Description |
---|---|---|
t | type | The input type |
return value | list<cvModifiedType> | A list of modifiers |
cvModifiedType, cvModifierKind
Strips an outermost box expression, if one is present, and returns the underlying expression.
If the expression consists of sub-expressions which themselves are box expressions, those expressions are also stripped.
Name | Type | Description |
---|---|---|
e | expression | The expression to strip boxes from |
return value | expression | The expression with the boxes removed |
In the following C# snippet:
... to examine the variable on the right-hand side of the assignmentOperatorCompound, the function stripBoxes could be called on the sourceExpression of that assignment expression.
stripCasts, stripCastsAndBoxes
Strips an outermost cast, if one is present, and returns the underlying expression.
If the expression consists of sub-expressions which themselves are being cast, those casts are also stripped.
Name | Type | Description |
---|---|---|
e | expression | The expression to strip casts from |
return value | expression | The expression with casts removed |
To examine the variable i in the assignment below:
... the function stripCasts could be used on the sourceExpression of the assignmentOperator.
stripBoxes, stripCastsAndBoxes
This function strips an outermost cast and box expression, if those are present, and returns the underlying expression.
If the expression consists of sub-expressions which themselves are cast or box expressions, those expressions are also stripped.
Name | Type | Description |
---|---|---|
e | expression | The expression to strip boxes and casts from |
return value | expression | The expression with boxes and casts removed |
See the examples of stripCasts and stripBoxes.
This function decomposes expressions that have implicit casts. In other words, it strips casts that are not explicitly placed in the source code, but rather are implicitly put there by the C# compiler.
Name | Type | Description |
---|---|---|
e | expression | The expression to strip casts from. If a non-cast is passed here it is just returned. |
return value | expression | The expression, free of casts |
When assigning between integer kinds, implicit casts can occur. For example:
To match all assignments from short integers, we might need to use stripImplicitCasts in the following way:
An overridee function returns the symbols that have been used to override a specified function symbol.
Returns all the overridees of a given functionSymbol.
Name | Type | Description |
---|---|---|
f | functionSymbol | The functionSymbol to get the overridees of |
return value | set<functionSymbol> | The overridees of the argument, as a set |
The following CodeXM function determines whether a function overrides a function named example():