JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.
If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.
If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.
| CodeSonar® 9.0p0 Hot Tips | CONFIDENTIAL | CodeSecure Inc |
Operations and supporting types for ASTs (cs_ast).
ASTs are available for C/C++ and binary analyses only. For C# and Java analyses, plug-ins that rely on AST properties and relationships will generally not produce useful information.| #define CS_AST_H | ||
| #define cs_ast_dig(ast, out_field, ...) | cs_ast_dig(ast, out_field, __VA_ARGS__, csao_null) | This helps prevent bugs where the client neglects to null terminate the argument list (having two null terminators isn't harmful). |
| #define CS_AST_NULL | (cs_ast_get_null()) | The null cs_ast. |
| #define CS_AST_INL_H |
| Definition | typedef enum cs_ast_field_type cs_ast_field_type |
|---|---|
| Notes | Used in cs_ast_field structs to indicate the type of the value stored in the field. |
| Definition | typedef struct cs_ast_enum_value cs_ast_enum_value |
|---|---|
| Notes | Structure for representing an element of an AST helper enumeration. |
| Definition | typedef struct cs_ast_field cs_ast_field |
|---|---|
| Notes | Structure for representing a field of an abstract syntax tree. |
| Definition | typedef struct cs_ast_binding cs_ast_binding |
|---|---|
| Notes | A binding from one pattern variable to a cs_ast_field.
A list of these is returned by the pattern-matching function cs_ast_match(). |
| Definition | typedef struct cs_ast_pattern cs_ast_pattern |
|---|---|
| Notes | A pattern for matching against ASTs and fields.
Create with cs_ast_pattern_compile(). Used by cs_ast_match() |
| Definition | typedef csuint32 cs_ast_traverse_flags |
|---|---|
| Notes | Specifies what kind of traversal a cs_ast_traverse_iter will carry out.
Used by cs_ast_iter_first(). Values for cs_ast_traverse_flags are defined in an anonymous enum, which this documentation will refer to as anonymous_cs_ast_traverse_flags_enum. |
| Definition | typedef csuint32 cs_ast_traverse_directives |
|---|---|
| Notes | Specifies behavior at a given node in a traversal with a cs_ast_traverse_iter.
Only meaningful for preorder traversals. Used by cs_ast_iter_next(). Values for cs_ast_traverse_directives are defined in an anonymous enum, which this documentation will refer to as anonymous_cs_ast_traverse_directives_enum. |
| Definition | typedef struct cs_ast_traverse_frame cs_ast_traverse_frame |
|---|---|
| Notes | Structure for managing the nodes yet to be visited in a cs_ast traversal. |
| Definition | typedef struct cs_ast_traverse_iter cs_ast_traverse_iter |
|---|---|
| Notes | An iterator over an abstract syntax tree. |
| Definition | typedef enum {
|
||
|---|---|---|---|
| Notes | This is an anonymous enumeration containing some values for cs_ast_class - the anonymous_csac_ast_root_enum label is provided only for ease of reference within this documentation.
CSAC = CodeSonar/CodeSurfer AST Class |
| Definition | typedef enum {
|
||
|---|---|---|---|
| Notes | This is an anonymous enumeration: the anonymous_csaf_base_families_enum label is provided only for ease of reference within this documentation. |
| Definition | typedef enum {
|
||
|---|---|---|---|
| Notes | This is an anonymous enumeration containing some values for cs_ast_ordinal - the anonymous_csao_null_enum label is provided only for ease of reference within this documentation.
Note that zero is a special-case AST ordinal and is not used to identify an AST field. CSAO = CodeSonar/CodeSurfer AST Ordinal |
| Definition | typedef enum {
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Notes | Used in cs_ast_field structs to indicate the type of the value stored in the field. |
| Definition | typedef enum {
|
||||||||
|---|---|---|---|---|---|---|---|---|---|
| Notes | This is an anonymous enumeration containing values for cs_ast_traverse_flags - the anonymous_cs_ast_traverse_flags_enum label is provided only for ease of reference within this documentation. |
| Definition | typedef enum {
|
||||||
|---|---|---|---|---|---|---|---|
| Notes | This is an anonymous enumeration containing values for cs_ast_traverse_directives - the anonymous_cs_ast_traverse_directives_enum label is provided only for ease of reference within this documentation. |
| struct cs_ast_enum_value |
| struct cs_ast_field |
| union cs_ast_field::cs_ast_field_variant |
| struct cs_ast_binding |
| struct cs_ast_traverse_iter |
| cs_result | cs_ast_class_lookup ( cs_const_string class_name, cs_ast_class * out_ast_class )
Get the cs_ast_class with a specified name.
|
| cs_result | cs_ast_ordinal_lookup ( cs_const_string class_name, cs_ast_ordinal * out_ast_ordinal )
Get the cs_ast_ordinal with a specified name.
|
| cs_result | cs_ast_family_lookup ( cs_const_string family_name, cs_ast_family * out_ast_family )
Get the cs_ast_family with a specified name.
|
| cs_result | cs_ast_get_all_classes ( cs_ast_class * out_classes, cs_size_t capacity_bytes, cs_size_t * bytes_needed )
Get a list of all AST classes (cs_ast_class).
|
| cs_result | cs_ast_get_all_ordinals ( cs_ast_ordinal * out_ordinals, cs_size_t capacity_bytes, cs_size_t * bytes_needed )
Get a list of all AST ordinals (cs_ast_ordinal).
|
| cs_result | cs_ast_get_all_family_names ( cs_const_string * out_families, cs_size_t capacity_bytes, cs_size_t * bytes_needed )
Get a list of all names of AST families (cs_ast_family).
|
| cs_ast_class | cs_ast_class_superclass ( cs_ast_class sub )
Get the immediate superclass of a cs_ast_class.
|
| cs_result | cs_ast_get_field ( cs_ast ast, cs_ast_ordinal ord, cs_ast_field * out_field )
Get a specified field of a cs_ast.
|
| cs_result | cs_ast_children ( cs_ast ast, cs_ast_field * out_children, cs_size_t capacity_bytes, cs_size_t * bytes_needed )
Get all children of a cs_ast.
|
| cs_result | cs_ast_attributes ( cs_ast ast, cs_ast_field * out_attributes, cs_size_t capacity_bytes, cs_size_t * bytes_needed )
Get all attributes of a cs_ast.
|
| cs_result | cs_ast_fields ( cs_ast ast, cs_ast_field * out_fields, cs_size_t capacity_bytes, cs_size_t * bytes_needed )
Get all fields (that is, all children and all attributes) of a cs_ast.
|
| cs_result | cs_ast_fields_split ( cs_ast ast, cs_ast_field * out_fields, cs_size_t capacity_bytes, cs_size_t * bytes_needed, cs_size_t * children_bytes_needed )
Like cs_ast_fields(), but with an additional output parameter that allows the caller to distinguish children from attributes.
|
| cs_result | cs_ast_pretty_print ( cs_ast ast, cs_string out_string, cs_size_t capacity_bytes, cs_size_t * bytes_needed )
Get a pretty-printed version of a cs_ast.
|
| cs_const_string | cs_ast_dump ( cs_ast ast, cs_size_t attribute_depth )
Get an ASCII art tree rendering of a cs_ast.
|
| cs_result | cs_ast_create ( cs_ast * out_ast, cs_ast_class ast_class, cs_size_t num_fields, const cs_ast_field * ast_fields )
Create a new cs_ast with the specified fields.
|
| cs_result | cs_ast_close ( cs_ast ast )
Dispose of a cs_ast (but don't free any of its fields).
|
| cs_const_string | cs_ast_class_name ( cs_ast_class c )
Get a string representation of a cs_ast_class name.
|
| cs_const_string | cs_ast_ordinal_name ( cs_ast_ordinal ord )
Get a string representation of a cs_ast_ordinal.
|
| cs_const_string | cs_ast_field_type_name ( cs_ast_field_type ft )
Get a string representation of a cs_ast_field_type.
|
| int | cs_ast_compare ( cs_ast ast_a, cs_ast ast_b )
Compare two cs_ast objects, with consistent results across all analysis processes for a single analysis.
|
| cs_hash_t | cs_ast_hash ( cs_ast ast )
Get a hash value for a cs_ast.
|
| int | cs_ast_field_fast_compare ( cs_ast_field a, cs_ast_field b )
If the ast field is ast-typed, uses cs_ast_fast_compare.
|
| int | cs_ast_field_compare ( cs_ast_field a, cs_ast_field b )
Compare two cs_ast_field objects, with consistent results across all analysis processes for a single analysis.
|
| cs_hash_t | cs_ast_field_hash ( cs_ast_field f )
Get a hash value for a cs_ast_field.
|
| cs_hash_t | cs_ast_field_fast_hash ( cs_ast_field f )
If the ast field is ast-typed, uses cs_ast_fast_hash(), otherwise behaves just like cs_ast_field_hash().
|
| int | cs_ast_stable_compare ( cs_ast ast_a, cs_ast ast_b )
Compare two cs_ast values, with stable results across sufficiently-similar analyses.
|
| int | cs_ast_field_stable_compare ( cs_ast_field field_a, cs_ast_field field_b )
Compare two cs_ast_field values, with stable results across sufficiently-similar analyses.
|
| cs_result | cs_ast_pattern_compile ( cs_const_string pat_spec, cs_ast_pattern ** out_pat, cs_const_string * err, cs_const_string * err_location, ... )
Compile a cs_const_string representation into a cs_ast_pattern.
|
| void | cs_ast_pattern_incr ( cs_ast_pattern * pat )
Increment reference count of pat.
|
| cs_result | cs_ast_match ( cs_ast v, cs_ast_pattern * p, cs_ast_binding * out_bindings, cs_size_t capacity_bytes, cs_size_t * bytes_needed )
Check whether a cs_ast matches a specified cs_ast_pattern.
|
| cs_result | cs_ast_pattern_close ( cs_ast_pattern * p )
Dispose of a cs_ast_pattern.
|
| cs_result | cs_ast_iter_first ( cs_ast root_ast, cs_ast_traverse_flags trav_flags, cs_ast * ast, cs_ast_traverse_iter * iter )
Set up a cs_ast for traversal, retrieving the first cs_ast according to the traversal specified and opening a cs_ast_traverse_iter for the rest of the tree.
|
| cs_result | cs_ast_iter_next ( cs_ast_traverse_directives directives, cs_ast * ast, cs_ast_traverse_iter * iter )
Retrieve the next cs_ast from a cs_ast_traverse_iter.
|
| cs_result | cs_ast_iter_close ( cs_ast_traverse_iter * iter )
Close a cs_ast_traverse_iter iterator.
|
| cs_result | cs_ast_dig ( cs_ast ast, cs_ast_field * out_field, ... )
Quickly dig deep into an abstract syntax tree to recover a field.
|
| const char * | cs_ast_string ( cs_ast ast )
Get a pretty-printed version of a cs_ast.
|
| cs_boolean | cs_ast_class_is_subclass ( cs_ast_class sub, cs_ast_class sup )
Check: is one cs_ast_class a subclass of another?
|
| cs_ast_class | cs_ast_get_class ( cs_ast ast )
Get the cs_ast_class to which a cs_ast belongs.
|
| cs_boolean | cs_ast_is_a ( cs_ast ast, cs_ast_class cls )
Check: is a cs_ast an instance of the specified cs_ast_class?
|
| int | cs_ast_fast_compare ( cs_ast ast1, cs_ast ast2 )
Get the relative ordering of two cs_ast objects within the "fast AST compare" ordering.
|
| cs_boolean | cs_ast_equal ( cs_ast ast1, cs_ast ast2 )
Check: do two cs_ast objects refer to the very same node?
|
| cs_hash_t | cs_ast_fast_hash ( cs_ast ast )
Get a "fast hash" value for a cs_ast.
|
| cs_boolean | cs_ast_is_null ( cs_ast ast )
Check: is a cs_ast null?
|
| cs_boolean | cs_ast_field_equal ( cs_ast_field a, cs_ast_field b )
Check: do two cs_ast_field objects have the same type and value?
|
| cs_boolean | cs_ast_field_is_null ( cs_ast_field fld )
Check whether the type of a cs_ast_field is csft_null.
|
| Parameters |
|
||||||
|---|---|---|---|---|---|---|---|
| Returns |
A cs_result:
|
| Parameters |
|
||||||
|---|---|---|---|---|---|---|---|
| Returns |
A cs_result:
|
| Parameters |
|
||||||
|---|---|---|---|---|---|---|---|
| Returns |
A cs_result:
|
| Parameters |
|
|||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Returns |
A cs_result:
|
|||||||||
| Notes | This retrieves every class possible, even abstract ones.
Call this function with out_classes NULL and capacity_bytes 0 (zero) to determine the memory needed to hold all cs_ast_class values. |
| Parameters |
|
|||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Returns |
A cs_result:
|
|||||||||
| Notes | This retrieves every ordinal possible.
Call this function with out_ordinals NULL and capacity_bytes 0 (zero) to determine the memory needed to hold all cs_ast_ordinal values. |
| Parameters |
|
|||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Returns |
A cs_result:
|
|||||||||
| Notes | This retrieves every family name possible.
Call this function with out_families NULL and capacity_bytes 0 (zero) to determine the memory needed to hold all cs_ast_family values. |
| Parameters |
|
|||
|---|---|---|---|---|
| Returns | The cs_ast_class that is the immediate superclass of sub. If sub is at the top of the class inheritance tree, return sub. |
| Parameters |
|
|||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Returns |
A cs_result:
|
| Parameters |
|
||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Returns |
A cs_result:
|
||||||||||||
| Notes | Call this function with out_children NULL and capacity_bytes 0 (zero) to determine the memory needed to hold all children of ast. |
| Parameters |
|
||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Returns |
A cs_result:
|
||||||||||||
| Notes | Call this function with out_attributes NULL and capacity_bytes 0 (zero) to determine the memory needed to hold all attributes of ast. |
| Parameters |
|
||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Returns |
A cs_result:
|
||||||||||||
| Notes | Call this function with out_fields NULL and capacity_bytes 0 (zero) to determine the memory needed to hold all fields of ast. |
| Parameters |
|
|||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Returns | A cs_result, as for cs_ast_fields(). | |||||||||||||||
| Notes | Call this function with out_fields NULL and capacity_bytes 0 (zero) to determine the memory needed to hold all fields of ast. |
| Parameters |
|
||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Returns |
A cs_result:
|
||||||||||||
| Notes | Call this function with out_string NULL and capacity_bytes 0 (zero) to determine the memory needed to hold the pretty-printed version of ast. |
| Parameters |
|
||||||
|---|---|---|---|---|---|---|---|
| Returns | A pointer to an internal buffer, which will remain valid until the next call to this function. |
| Parameters |
|
||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Returns |
A cs_result:
|
| Parameters |
|
|||
|---|---|---|---|---|
| Returns |
A cs_result:
|
| Parameters |
|
|||
|---|---|---|---|---|
| Returns | The name associated with c, as a cs_const_string. |
| Parameters |
|
|||
|---|---|---|---|---|
| Returns | if ord<0, (ie, corresponds to an enum symbol) the name associated with ord; otherwise NULL. |
| Parameters |
|
|||
|---|---|---|---|---|
| Returns | if ft corresponds to a valid field type, the name associated with ft; otherwise NULL. |
| Parameters |
|
||||||
|---|---|---|---|---|---|---|---|
| Returns |
An integer N, such that:
|
||||||
| Notes | This function is provided so cs_ast objects can be stored in ordered containers. It is not based on any user-perceivable ordering.
For fixed cs_ast values a and b in a single analysis, cs_ast_compare(a,b) will always return the same value. However, cs_ast_compare() will not necessarily return this same value when called on the corresponding cs_ast values a' and b' in a different analysis. Function cs_ast_stable_compare() provides some degree of stability across analyses, although restrictions still apply. If you do not need comparison results to be stable across different analyses, use cs_ast_compare() or cs_ast_fast_compare(): they have better performance.
|
| Parameters |
|
|||
|---|---|---|---|---|
| Returns | The hash value for ast, as a cs_hash_t. | |||
| Notes | For fixed cs_ast a in a single analysis, cs_ast_hash(a) will always return the same value. However, cs_ast_hash() will not necessarily return this same value when called on the corresponding cs_ast value a' in a different analysis.
If you only need hash values to be consistent within a single analysis process (not across different processes in the same analysis), you can use cs_ast_fast_hash(). |
| Notes | Otherwise behaves just like cs_ast_field_compare. |
|---|
| Notes | The ordinals, types, and payloads get compared. |
|---|
| Parameters |
|
||||||
|---|---|---|---|---|---|---|---|
| Returns |
An integer N, such that:
|
||||||
| Time-Complexity | O(s) where s is the sum of the number of transitive children and attributes in ast_a and ast_b and the sum of the lengths of strings that are transitively children or attributes of either ast. It can be the total number of nodes in the two compilations in the worst case. | ||||||
| Notes | The comparison is stable in the following sense. Suppose there are two analyses A1 and A2 generated with exactly the same inputs (including identical analyzed code, underlying build commands and ordering, command line and configuration settings, increment order and contents). Let a1 and b1 be two cs_ast values in A1, and a2 and b2 be the cs_ast values in A2 that correspond to a1 and b1 respectively. Then cs_ast_stable_compare(a1,b1)==cs_ast_stable_compare(a2,b2).
If you do not need comparison results to be stable across different analyses, use one of the following: they have better performance.
|
| Parameters |
|
||||||
|---|---|---|---|---|---|---|---|
| Returns |
An integer N, such that:
|
||||||
| Time-Complexity | O(s) where s is the sum of the number of transitive children and attributes in field_a and field_b and the sum of the lengths of strings that are transitively children or attributes of either AST. | ||||||
| Notes | The comparison is stable in the following sense. Suppose there are two analyses A1 and A2 generated with exactly the same inputs (including identical analyzed code, underlying build commands and ordering, command line and configuration settings, increment order and contents). Let a1 and b1 be two cs_ast_field values in A1, and a2 and b2 be the cs_ast_field values in A2 that correspond to a1 and b1 respectively. Then cs_ast_field_stable_compare(a1,b1)==cs_ast_field_stable_compare(a2,b2). |
| Parameters |
|
|||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Returns |
A cs_result:
|
|||||||||||||||
| Notes | For more information, see AST Patterns. |
| Parameters |
|
|||
|---|---|---|---|---|
| Notes | cs_ast_pattern_close() will only free the pattern when the reference count reaches 0. Freshly compiled patterns have a reference count of 1. |
| Parameters |
|
|||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Returns |
A cs_result:
|
|||||||||||||||
| Notes | Call this function with out_bindings NULL and capacity_bytes 0 (zero) to determine
|
| Parameters |
|
|||
|---|---|---|---|---|
| Returns | CS_SUCCESS on success. |
| Parameters |
|
||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Returns |
A cs_result:
|
||||||||||||
| Notes | Retrieve the first cs_ast from the tree rooted at root_ast. This function opens iter, which is required by cs_ast_iter_next(). Use cs_ast_iter_close() to close iter. |
| Parameters |
|
|||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Returns |
A cs_result:
|
|||||||||
| Notes | Retrieve the next cs_ast from a cs_ast_traverse_iter. If iter is at the end of its iteration, it is closed. |
| Parameters |
|
|||
|---|---|---|---|---|
| Returns | CS_SUCCESS on success. |
| Parameters |
|
|||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Returns |
A cs_result:
|
|||||||||
| Notes | On success, out_field is set as follows:
For example, suppose ast1 is a c:= AST representing the statement a = b + c. Then to retrieve the AST for c (by wrapping it in c_field), you could call: cs_ast_dig(ast1, &c_field, 2, 2, csao_null) |
| Parameters |
|
|||
|---|---|---|---|---|
| Returns | A pointer to a buffer containing a pretty-printed version of ast (as a char*). The buffer is only valid until another API function is invoked. | |||
| Notes | Do not modify or free the returned buffer.
This function entails less user overhead than cs_ast_pretty_print(), and so can be more convenient to use; the tradeoff is that the returned value is not under user control and has a constrained lifetime. |
| Parameters |
|
||||||
|---|---|---|---|---|---|---|---|
| Returns | cs_true if sub is a subclass of sup, cs_false otherwise. The subclass relationship is transitive and reflexive |
| Parameters |
|
|||
|---|---|---|---|---|
| Returns | The most specific cs_ast_class to which ast belongs. |
| Parameters |
|
||||||
|---|---|---|---|---|---|---|---|
| Returns | cs_true if ast is an instance of cls, or of a subclass of cls, cs_false otherwise. |
| Parameters |
|
||||||
|---|---|---|---|---|---|---|---|
| Returns |
An integer N, such that:
|
||||||
| Notes | The "fast AST compare" ordering is a total ordering over cs_ast objects that is provided for performance only. It is not stable from process to process, even on the same (ie, not rebuilt) project.
|
| Parameters |
|
|||
|---|---|---|---|---|
| Returns | The hash value for ast, as a cs_hash_t. | |||
| Notes | The "fast-hash" function on cs_ast is provided for performance only. It is not stable from process to process, even on the same (ie, not rebuilt) project. For consistent results across all processes for a single analysis, use cs_ast_hash(). |
| Parameters |
|
||||||
|---|---|---|---|---|---|---|---|
| Returns | cs_true if a and b have the same type and value, cs_false otherwise. | ||||||
| Notes | If both arguments are csft_ast typed, then returns the same value as cs_ast_equal(). |