class symbol_set

A set of symbols ( symbol ).

The symbol_set class corresponds to the ABS_LOC_SET abstraction.

Return and/or parameter type for various queries on point , procedure , project , symbol .

Use a symbol_set_iterator to iterate over a symbol_set . For example:

for e in my_symbol_set:
    print('symbol: ', e)

symbol_set Details

class cs.symbol_set

A set of symbols ( symbol ).

__init__()

Construct an empty set.

>>> symbol_set()
<cs.symbol_set {}>
__init__(v)

Construct a set from a list of elements.

Parameters:v (iterable of symbol) – The set elements.
>>> cu = next(c for c in project.current().compunits()
...             if c.name().endswith('apitest.cpp'))
>>> proc = next(p for p in cu.procedures() if p.name()=='bar')
>>> pt = next(p for p in proc.points()
...             if p.get_kind()==point_kind.EXPRESSION
...             and p.get_ast()[1].get_class()==ast_class.NC_POINTEREXPR)
>>> print(pt)
[expression] *p = x
>>> pt_used = symbol_set(pt.csonar_enum_used())
>>> for sym in iter(pt_used):            # iteration managed by symbol_set_iterator.__iter__()
...                                      # and symbol_set_iterator.__next__()
...    print(sym.file_line(), sym.name())
...
(<cs.sfileinst C:\alex\test\apitest.cpp>, 14) x
(<cs.sfileinst C:\alex\test\apitest.cpp>, 15) p
__init__(other)

Copy constructor.

Parameters:other (symbol_set) –
>>> sym = next(project.current().symbols())
>>> ss = symbol_set([sym])
>>> ss2 = symbol_set(ss)
>>> ss2
<cs.symbol_set {<cs.symbol #File_Initialization>}>
__init__(k)

Construct an empty set of the specified kind.

Parameters:k (set_kind) – The set kind.
Raises:result.ERROR_CODESURFER_ONLY if k is set_kind.TRIE, which is only available in CodeSurfer.
>>> symbol_set(set_kind.BIT_VECTOR)
<cs.symbol_set {}>
__contains__(elt)

Check: does the set contain the specified element?

Parameters:elt (symbol) – The element to check.
Return type:bool
Returns:True if elt is present in the set, False otherwise.
>>> syms = project.current().symbols()
>>> ss = symbol_set([next(syms)])
>>> ss
<cs.symbol_set {<cs.symbol #File_Initialization>}>
>>> sym = next(syms)
>>> sym
<cs.symbol foo>
>>> sym in ss
False
__eq__(__y)

Equality operator for symbol_set .

Parameters:__y (symbol_set) – The set to compare against.
Return type:bool
Returns:True if self equal to __y, False otherwise.
>>> syms = project.current().symbols()
>>> ss1 = symbol_set([next(syms)])
>>> ss1
<cs.symbol_set {<cs.symbol #File_Initialization>}>
>>> ss2 = symbol_set()
>>> ss2
<cs.symbol_set {}>
>>> ss1 == ss2
False
__hash__()

Get a hash of the set.

Return type:int
Returns:The hash value for the set.
>>> syms = project.current().symbols()
>>> ss1 = symbol_set([next(syms)])
>>> ss1
<cs.symbol_set {<cs.symbol #File_Initialization>}>
>>> hash(ss1)
378599093
__iter__()

Get an iterator over the set elements.

Return type:symbol_set_iterator
Returns:An iterator over the set elements.
>>> cu = next(c for c in project.current().compunits()
...             if c.name().endswith('apitest.cpp'))
>>> proc = next(p for p in cu.procedures() if p.name()=='bar')
>>> pt = next(p for p in proc.points()
...             if p.get_kind()==point_kind.EXPRESSION
...             and p.get_ast()[1].get_class()==ast_class.NC_POINTEREXPR)
>>> print(pt)
[expression] *p = x
>>> pt_used = symbol_set(pt.csonar_enum_used())
>>> for sym in pt_used:                  # iteration managed by symbol_set.__iter__()
...                                      # and symbol_set_iterator.__next__()
...    print(sym.file_line(), sym.name())
...
(<cs.sfileinst C:\alex\test\apitest.cpp>, 14) x
(<cs.sfileinst C:\alex\test\apitest.cpp>, 15) p
__len__()

Get the set cardinality.

Return type:int
Returns:The number of elements in the set.
>>> syms = project.current().symbols()
>>> ss1 = symbol_set([next(syms)])
>>> ss1
<cs.symbol_set {<cs.symbol #File_Initialization>}>
>>> len(ss1)
1
__ne__(__y)

Inequality operator for symbol_set .

Parameters:__y (symbol_set) – The set to compare against.
Return type:bool
Returns:False if self equal to __y according to symbol_set.__eq__(), True otherwise.
>>> syms = project.current().symbols()
>>> ss1 = symbol_set([next(syms)])
>>> ss1
<cs.symbol_set {<cs.symbol #File_Initialization>}>
>>> ss2 = symbol_set()
>>> ss2
<cs.symbol_set {}>
>>> ss1 != ss2
True
__repr__()

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

Return type:str
>>> syms = project.current().symbols()
>>> ss1 = symbol_set([next(syms)])
>>> ss1
<cs.symbol_set {<cs.symbol #File_Initialization>}>
>>> repr(ss1)
'<cs.symbol_set {<cs.symbol #File_Initialization>}>'
__str__()

Get a simple string representation of a set object.

Return type:str
>>> syms = project.current().symbols()
>>> ss1 = symbol_set([next(syms)])
>>> ss1
<cs.symbol_set {<cs.symbol #File_Initialization>}>
>>> str(ss1)
'{<cs.symbol #File_Initialization>}'
add(v)

Add an element to the set.

Parameters:v (symbol) – The element to add.
Return type:bool
Returns:True if v was not already present in the set, False if it was already present.
  • Side effects: Modifies self.
>>> syms = project.current().symbols()
>>> ss = symbol_set([next(syms)])
>>> ss
<cs.symbol_set {<cs.symbol #File_Initialization>}>
>>> ss.add(next(syms))
True
>>> ss
<cs.symbol_set {<cs.symbol foo>, <cs.symbol #File_Initialization>}>
clear()

Remove all elements from the set (that is, make it empty).

Return type:NoneType
  • Side effects: Modifies self.
>>> ss = symbol_set([next(syms)])
>>> syms = project.current().symbols()
>>> ss = symbol_set([next(syms)])
>>> ss
<cs.symbol_set {<cs.symbol #File_Initialization>}>
>>> ss.clear()
>>> ss
<cs.symbol_set {}>
difference(b)

Set difference.

Parameters:b (symbol_set) – The set to subtract from self.
Return type:symbol_set
Returns:A set containing self - b.

The returned set contains the elements in self that are not in b.

>>> cu = next(c for c in project.current().compunits())
>>> proc = next(p for p in cu.procedures() if p.name()=='bar')
>>> proclocals = symbol_set([s for s in proc.local_symbols()
...                          if s.get_kind()==symbol_kind.USER])
>>> print(', '.join([str(s) for s in proclocals]))
i, j, k, x, p
>>> pt = next(p for p in proc.points()
...              if p.get_kind()==point_kind.EXPRESSION
...              and p.get_ast()[1].get_class()==ast_class.NC_POINTEREXPR)
>>> pt_used = symbol_set(pt.csonar_enum_used())
>>> print(', '.join([str(s) for s in pt_used]))
x, p
>>> proclocals.difference(pt_used)
<cs.symbol_set {<cs.symbol i>, <cs.symbol j>, <cs.symbol k>}>
discard(v)

Remove an element from the set.

Parameters:v (symbol) – The element to remove.
Return type:NoneType
Returns:The number of elements deleted:
  • 1 if v was present in the set.
  • 0 if v was not present in the set.
  • Side effects: Modifies self.
>>> syms = project.current().symbols()
>>> s1 = next(syms)
>>> ss = symbol_set([s1])
>>> ss
<cs.symbol_set {<cs.symbol #File_Initialization>}>
>>> ss.discard(s1)
>>> ss
<cs.symbol_set {}>
empty()

Check: is the set empty?

Return type:bool
Returns:True if the set is empty, False otherwise.
>>> v0 = symbol_set()
>>> v0.empty()
True
intersect(b)

Set intersection.

Parameters:b (symbol_set) – The other operand for the intersection operation.
Return type:symbol_set
Returns:A set containing self INTERSECT b.

The returned set contains the elements that are in both self and b.

>>> syms = project.current().symbols()
>>> s1 = next(syms)
>>> ss = symbol_set([s1])
>>> ss2 = ss
>>> ss2.add(next(syms))
True
>>> ss1
<cs.symbol_set {<cs.symbol #File_Initialization>}>
>>> ss2
<cs.symbol_set {<cs.symbol foo>, <cs.symbol #File_Initialization>}>
>>> ss1.intersect(ss2)
<cs.symbol_set {<cs.symbol #File_Initialization>}>
kind()

Get the kind of a set.

Return type:set_kind
Returns:The set kind ( set_kind ).
Raises:result.ELEMENT_NOT_PRESENT if the set was constructed with the default constructor and no elements have been added yet.
>>> ss = symbol_set([next(project.current().symbols())])
>>> ss.kind()
<cs.set_kind fast-vector>
remove(v)

Remove an element from the set.

Parameters:v (symbol) – The element to remove.
Return type:NoneType
Raises:KeyError
  • Side effects: Modifies self.
>>> syms = project.current().symbols()
>>> s = next(syms)
>>> symset = symbol_set([s])
>>> symset
<cs.symbol_set {<cs.symbol #File_Initialization>}>
>>> symset.remove(s)
>>> symset
<cs.symbol_set {}>
>>> symset.remove(s)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Program Files\CodeSecure\CodeSonar/codesonar/src/api/python\cs.py", line 6898, in remove
raise KeyError(v)
KeyError: <cs.symbol #File_Initialization>
to_vector()

Get a list of all the elements in the set.

Return type:[symbol]
Returns:A list containing all the elements in the set.
>>> ss = symbol_set([next(project.current().symbols())])
>>> ss.to_vector()
(<cs.symbol #File_Initialization>,)
union(b)

Set union.

Parameters:b (symbol_set) – The other operand for the union operation.
Return type:symbol_set
Returns:A set containing self UNION b.

The returned set contains the elements that are in self, b, or both.

>>> syms = project.current().symbols()
>>> ss1 = symbol_set([next(syms)])
>>> ss1
<cs.symbol_set {<cs.symbol #File_Initialization>}>
>>> ss2 = symbol_set([next(syms)])
>>> ss2
<cs.symbol_set {<cs.symbol foo>}>
>>> ss1.union(ss2)
<cs.symbol_set {<cs.symbol foo>, <cs.symbol #File_Initialization>}>
union_p(b)

Update a set to the union of itself and another set.

Parameters:b (symbol_set) – The set whose elements are to be added.
Return type:NoneType
  • Side effects: Modifies self.

self is updated to contain self UNION b.

>>> syms = project.current().symbols()
>>> ss1 = symbol_set([next(syms)])
>>> ss1
<cs.symbol_set {<cs.symbol #File_Initialization>}>
>>> ss2 = symbol_set([next(syms)])
>>> ss2
<cs.symbol_set {<cs.symbol foo>}>
>>> ss1.union_p(ss2)
>>> ss1
<cs.symbol_set {<cs.symbol foo>, <cs.symbol #File_Initialization>}>