CodeSonar C++ API
Public Member Functions | Related Symbols | List of all members
cs::ast_pattern Class Reference

An AST Pattern. More...

Public Member Functions

 ast_pattern (const ast_pattern &rhs)
 Copy constructor.
 
 ast_pattern (const std::string &pattern)
 Constructor: given a string representation of a pattern, construct a corresponding ast_pattern.
 
std::string as_repr () const
 Get a representation of a ast_pattern object that includes information useful for debugging.
 
std::string as_string () const
 Get a simple string representation of a ast_pattern object.
 
cs_hash_t hash () const
 Get a hash value for a ast_pattern.
 
bool match (ast node) const
 Check: does the specified ast match the pattern?
 
ast_bindings match_with_bindings (ast node) const
 Get the bindings generated in matching against the specified ast.
 

Related Symbols

(Note that these are not member symbols.)

std::ostream & operator<< (std::ostream &out, const ast_pattern &a)
 
 

Detailed Description

An AST Pattern.

Patterns are constructs that describe ast properties, much like specialized regular expressions for ASTs. They are used for testing whether a specific ast object conforms to those properties ("matches the pattern"), particularly in traversal with an ast_iterator.

A successful match results in an ast_bindings object (analogous to regular expression "tags").

For example, the following code uses an ast_pattern to determine whether an ast object to determine whether or not it represents a cast of a const pointer to a non-const pointer type. The ast class documentation has an example in which the same check is performed by dissecting the ast manually.

"(c:cast"
" :2 (?e"
" :type (c:pointer"
" :pointed-to (?c :is-const #t)))"
" :type (c:pointer"
" :pointed-to (?k :is-const #f)))"
);
// return true iff pt is a cast of a const pointer to a non-const pointer type
bool is_badcast(cs::ast tmpast){
return pattern->match(tmpast);
}
An AST Pattern.
Definition cs_ast_decl.hpp:2298
bool match(ast node) const
Check: does the specified ast match the pattern?
Definition cs_ast_decl.hpp:2389
An Abstract Syntax Tree (AST).
Definition cs_ast_decl.hpp:452

Constructor & Destructor Documentation

◆ ast_pattern() [1/2]

cs::ast_pattern::ast_pattern ( const std::string &  pattern)
inline

Constructor: given a string representation of a pattern, construct a corresponding ast_pattern.

Parameters
[in]patternThe std::string representation of the pattern.
Exceptions
ast_pattern_compilation_errorif pattern is not properly formed, or includes an operation on a value of incorrect type.

For more information, see AST Patterns.

◆ ast_pattern() [2/2]

cs::ast_pattern::ast_pattern ( const ast_pattern rhs)
inline

Copy constructor.

Parameters
[in]rhsThe ast_pattern to copy.

Member Function Documentation

◆ as_repr()

std::string cs::ast_pattern::as_repr ( ) const
inline

Get a representation of a ast_pattern object that includes information useful for debugging.

Returns
The string representation.

◆ as_string()

std::string cs::ast_pattern::as_string ( ) const
inline

Get a simple string representation of a ast_pattern object.

Returns
The string representation.

◆ hash()

cs_hash_t cs::ast_pattern::hash ( ) const
inline

Get a hash value for a ast_pattern.

◆ match()

bool cs::ast_pattern::match ( ast  node) const
inline

Check: does the specified ast match the pattern?

Parameters
nodeThe ast to match.
Returns
  • true if if node matches the pattern
  • false if if node does not match the pattern

◆ match_with_bindings()

ast_bindings cs::ast_pattern::match_with_bindings ( ast  node) const
inline

Get the bindings generated in matching against the specified ast.

Parameters
nodeThe ast to match.
Returns
The ast_bindings generated in matching against node. Use ast_bindings::matched() to check whether the pattern matched.

Friends And Related Symbol Documentation

◆ operator<<()

std::ostream & operator<< ( std::ostream &  out,
const ast_pattern a 
)
related


Print a representation of a ast_pattern object to the specified stream.

Parameters
[in]outThe stream to print to.
[in]aThe ast_pattern object to print.
Returns
void

The documentation for this class was generated from the following file: