Go 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 Go library provides access to Go coding constructs.
This document provides information about the patterns and functions provided by the Go 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 `Go`.
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 Go struct or interface.
classDefinition produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
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 |
Describes a function definition.
functionDefinition produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
body | statement | A blockStatement that is the function’s body. |
formalParameterLists | list<symbol> | A list of parameterSymbol objects, one for each parameter to the function. |
functionSymbol | symbol | The functionSymbol object that represents this function. |
Inherits properties from:
Describes a function or a statically declared variable.
functionOrStaticVariableDefinition produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
allCode | set<astnode> | A list of all the nodes within the function. For a variable, this is usually just the variable’s initialization. |
location | sourceloc | The function’s location in the code. This value is used in error reports. |
paths | executionPaths | The execution paths used for path-sensitive analysis. |
Matches symbols for variables that are declared as const.
staticVariableDefinition produces a record that contains the following property:
Name | Type | Description |
---|---|---|
initializer | initializer? | The initializer for this variable; null if no initializer exists. |
variable | symbol | A staticVariableSymbol that represents this variable. |
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 type casts in Go.
The following values are defined:
Name | Description |
---|---|
`dynamic` | A dynamic cast |
`explicit` | An explicit cast |
`implicit` | An implicit cast |
In Go an explicit type cast, or conversion, appears similar to a function call. For example, consider the following declarations:
... then to obtain a value for mean based on sum and count, the Go code would first have to convert the integers to 32-bit floating-point values, as follows:
Represents whether a floating-point value is real or complex.
The following values are defined:
Name | Description |
---|---|
`float` | A real number, in floating-point format |
`complex` | The imaginary component of a complex number, in floating-point format |
Represents either a simple or an enhanced for loop.
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 |
Represents various kinds of integer values.
The following values are defined:
Name | Description |
---|---|
`bool` | A Boolean value |
`byte` | A single byte |
`int` | A full integer |
`int8` | An 8-bit signed integer |
`int16` | A 16-bit signed integer |
`int32` | A 32-bit signed integer |
`int64` | A 64-bit signed integer |
`uint8` | An 8-bit unsigned integer |
`uint16` | A 16-bit unsigned integer |
`uint32` | A 32-bit unsigned integer |
`uint64` | A 64-bit unsigned integer |
`uintptr` | An unsigned integer pointer value |
`rune` | A 32-bit signed integer: Can be used to represent a Unicode character. |
Represents the scope of a label.
The following values are defined:
Name | Description |
---|---|
`unscoped` | The label is within a local scope. |
`scoped` | The label appears with a struct or an interface. |
Represents the scope of a variable declaration.
The following values are defined:
Name | Description |
---|---|
`local` | The declaration is in a local scope. |
`static` | The declaration appears with a struct or an interface. |
Represents the scope of a variable.
The following values are defined:
Name | Description |
---|---|
`local` | The variable is local to the function, struct, or interface. |
`static` | The variable is static and 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 definitions of all interface and struct types declared in the current Go 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 staticaly 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 Go code.
allFunctionAndStaticVariableCode
Includes all function definitions in the current Go code.
allFunctionAndStaticVariableDefinitions
Includes all statically declared variables in the current Go code.
allFunctionAndStaticVariableCode
Includes all definitions of statically declared variables in the current Go code.
allFunctionAndStaticVariableDefinitions
Includes all source files in the current project that are coded in the Go 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 both simple and enhanced for loops.
This pattern only matches nodes of type statement.
This pattern does not expose any new properties.
Inherits properties from:
The following pattern uses allLoops to determine whether the simpleStatement is a loop:
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, forLoop, forLoopSimple
Matches individual case statements.
This pattern does not match the default statement. See the 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, switch. |
Inherits properties from:
The following example matches continue statements within a switch:
labeledContinueStatement, allLoops, forLoop, forLoopSimple
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 defer statements.
This pattern only matches nodes of type statement.
This pattern does not expose any new properties.
Inherits properties from:
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 fallthrough statements.
This pattern only matches nodes of type statement.
This pattern does not expose any new properties.
Inherits properties from:
This pattern matches both simple and enhanced for loops.
The condition of a simple for loop is a for clause; for example, i := 0; i < 10; i++.
The condition of an enhanced for loop is either a Boolean expression, or it is a range clause that tells the loop to iterate over all the entries in an object such as an array, a string, a map, and so on.
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 ForLoopKind. |
Inherits properties from:
Matches any simple for loop:
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 IncDec statements: that is, statements that use either the incrementOperator, ++, or the decrementOperator, --.
This pattern only matches nodes of type statement.
incDecStatement produces a record that contains the following property:
Name | Type | Description |
---|---|---|
expression | expression | The expression; for example, a function call, an assignment, or (frequently) a post clause in a simple for loop. |
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, for 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, forLoop, forLoopSimple
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 |
---|---|---|
kind | enum labelScopeKind | The scope of the label: `unscoped` if the label is inside a local scope; `scoped` if the label appears within a struct or an interface. See labelScopeKind. |
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 statements that use a locally declared type.
This pattern only matches nodes of type statement.
locallyDeclaredTypeStatement produces a record that contains the following property:
Name | Type | Description |
---|---|---|
declaredClass | expression | The expression, such as a function call or an assignment. |
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 select statements.
This pattern only matches nodes of type statement.
selectStatement produces a record that contains the following property:
Name | Type | Description |
---|---|---|
branchStatement | statement | The body of the select statement, including all the statements contained in its case and default clauses. |
The following pattern matches select statements that have a default clause:
caseStatement, defaultStatement
Matches send statements.
This pattern only matches nodes of type statement.
sendStatement produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
lhsExpression | expression | The left-hand side of the Send expression. |
rhsExpression | expression | The right-hand side of the Send expression. |
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
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 |
variableDeclarationKind | enum | Either `unscoped` or `scoped` |
variable | symbol | The identifier of the variable being declared |
Inherits properties from:
The following pattern matches all variables declared with the type int:
These patterns match various types of values in the target code.
Matches any Go language type.
This pattern only matches nodes of type type.
This pattern does not expose any new properties.
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.
boolType 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 all struct and interface types.
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: either `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 interface types:
Matches the Go types float and complex.
This pattern only matches nodes of type type.
floatType produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
kind | enum floatKind | The kind of floating-point value: either `float` or `complex`. See floatKind. |
alignmentInBytes | int | The alignment of the type, in bytes |
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 float:
Matches function types.
This pattern only matches nodes of type type.
functionType produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
declaredThrownTypeList | list<type> | The types the function is declared to throw |
hasVariableArity | bool | true if the function uses variable arity; that is, a variable number of potential arguments |
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 all integer types that Go recognizes.
This pattern only matches nodes of type type.
integertype produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
alignmentInBytes | int | The alignment of the type, in bytes |
kind | enum intKind | The kind of integer type: See 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 uint8 type:
Matches reference types.
A reference type represents an object being passed by reference; for example, as an argument to a function.
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 pattern matches all references to a structure named MyStruct:
Matches the string type.
This pattern only matches nodes of type type.
This pattern does not expose any new properties.
The following pattern matches an expression whose result is a string type:
Patterns in this section belong to the expression class. They match expressions in the target code.
Many statements in Go 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 Go type of the expression |
isParenthesized | bool | Whether there are parenthesis around this expression |
Matches locations where an expression references a variable.
This pattern only matches nodes of type expression.
anyTypeVariableReference produces a record that contains the following property:
Name | Type | Description |
---|---|---|
variable | symbol | The symbol that represents the variable |
Inherits properties from:
The following pattern matches all references to local variables:
Matches expressions that reference an attribute. For example, townStructure.street.
This pattern only matches nodes of type expression.
attributeReference produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
primaryExpression | expression | The base (the left-hand side) of the attribute-reference expression |
propertyExpression | expression | The property to access (the right-hand side) of the attribute-reference expression |
Inherits properties from:
The following pattern matches an attribute-reference expression that accesses a property named street:
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:
Matches calls to built-in functions.
This pattern only matches nodes of type expression.
builtinFunctionExpression produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
args | list<expression> | A list that contains the arguments passed to the function |
name | string | The name of the built-in function |
Inherits properties from:
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 calls to dynamic functions.
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 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:
Given the following code:
... the following pattern would match the access to the MyInt field:
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 locations where an expression references a variable.
This pattern only matches nodes of type expression.
functionVariableReference produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
qualifiedName | string | The name of the variable, including any scope information |
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 local variables:
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:
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 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 |
---|---|---|
qualifiedName | string | The name of the variable, with scope information. |
scope | enum variableScopeEnum | The scope of the variable: either `local` or `static` (available globally). See variableScopeEnum. |
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 local variables:
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.
boolLiteral 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 true Boolean values:
Matches struct literals.
This pattern only matches nodes of type expression.
compositeLiteral produces a record that contains the following property:
Name | Type | Description |
---|---|---|
targetType | classType | The type of the struct object. |
The following pattern matches literal struct objects whose name is Example:
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 the imaginary part of a complex number.
This pattern only matches nodes of type expression.
imaginaryLiteral produces a record that contains the following property:
Name | Type | Description |
---|---|---|
valueString | string | The value of the imaginary component of the complex number, represented as a string. |
Inherits properties from:
Given the following snippet of source code:
... the following CodeXM pattern would match it:
Matches integer literals; that is, all possible literal integer values.
This pattern only matches nodes of type expression.
integerLiteral produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
base | enum | One of `binary`, `decimal`, `octal`, or `hexadecimal`. |
intKind | enum | The kind of the integer type: See intKind. |
value | int | The value of the integer literal |
Inherits properties from:
The following pattern matches only long integer literals:
Matches literal map expressions.
This pattern only matches nodes of type expression.
This pattern does not expose any new properties.
Inherits properties from:
Matches all nil 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 nil; for example, a = nil:
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 address-of operations.
In Go, the address-of operation applies only to integers.
This pattern only matches nodes of type expression.
addressOf produces a record that contains the following property:
Name | Type | Description |
---|---|---|
kind | enum | Always `int` |
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 assignmentOperatorCompound | 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 nil literal:
assignmentOperatorCompound, assignmentOperator
Matches all possible binary operations in Go.
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 ( * ):
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 |
---|---|---|
castKind | enum | The kind of cast represented: `explicit`, `implicit`, or `dynamic`. See castKind. |
operandExpression | expression | The expression being cast |
Inherits properties from:
The following CodeXM pattern matches all kinds of casts (implicit, explicit, dynamic) to type int:
Matches all decrement operators.
This pattern only matches nodes of type expression.
decrementOperator produces a record that contains the following property:
Name | Type | Description |
---|---|---|
operandExpression | expression | The expression the decrement operator is being applied to |
Inherits properties from:
The following pattern matches when a decrement operator is used to update a for loop:
Matches all increment operators.
This pattern only matches nodes of type expression.
incrementOperator produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
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 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 struct Example:
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. They match initializers in the target code.
In many cases, an initializer can appear to be 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 the initializations of linear arrays that use a list enclosed in curly braces.
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 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:
This pattern would match the following Go initialization:
The following pattern matches all initializations done using binary operations:
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:
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 parameter to a function.
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 the entity 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 Go 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 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 |
---|---|---|
isTransient | bool | true if the field is declared transient |
isVolatile | bool | true if the field is declared volatile |
ownerClass | classType | The owner class for the field symbol |
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 the field name Unleaded:
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 |
---|---|---|
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 |
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 |
isStaticMethod | bool | true if the function is a static method |
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 function calls to method 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 |
---|---|---|
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 |
---|---|---|
extra | extra | Extra information used for defect reporting |
ownerClass | classType | The owner class for the parameter 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 whose type is referenceType:
Matches symbols for variables declared as const.
This pattern only matches nodes of type symbol.
staticVariableSymbol produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
ownerClass | classType | The owner class for the static variable symbol |
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 |
---|---|---|
qualifiedName | string | The name of the variable, including any scope information |
scope | variableScopeKind | Either `local` for local variables, or `static` for statically defined variables |
simpleName | string | The name of the variable, without scope information |
Inherits properties from:
staticVariableSymbol, localVariableSymbol, parameterSymbol
These are records that can be used within CodeXM patterns to match declarations.
Represents a declaration.
declaration produces a record that contains the following properties:
Name | Type | Description |
---|---|---|
initializer | initializer? | An initializer; null if there is none |
kind | enum variableDeclarationKind | The kind of declaration: `local` if the scope of the variable is local; `static` if the variable is declared within a struct or an interface. See variableDeclarationKind. |
variable | symbol | The symbol variable |
The following pattern matches the declaration of an array whose name is perYear:
The Go 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 definitions of struct, interface, function (func), or static variable objects.
Returns the definition of a specified class type, if that definition is available to analysis.
Name | Type | Description |
---|---|---|
classTypeResult | classType | The type of class whose definition you want to obtain. |
return value | classDefinition | Returns the class definition if one is available; returns null, otherwise. |
Pattern functions return patterns that you can use in your CodeXM searches, just as you use patterns provided by the language library.
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: