This chapter describes the classes and methods available in the C++test API so that you can add them to your rule definitions in RuleWizard. Some properties are node-type dependent and not all of these properties are accessible. If a property is not accessible, a -1 will be returned if the return type is a number. An empty string will be returned if the return type is a string. Modules Table of Contents |
---|
maxLevel | 2 |
---|
minLevel | 2 |
---|
type | flat |
---|
separator | pipe |
---|
|
nodeproviderClasses: Table of Content Zone |
---|
maxLevel | 3 |
---|
minLevel | 3 |
---|
location | top |
---|
type | flat |
---|
separator | pipe |
---|
| NodeProviderMethods: Table of Content Zone |
---|
maxLevel | 4 |
---|
minLevel | 4 |
---|
location | top |
---|
type | flat |
---|
separator | pipe |
---|
| getPropertystring getProperty(string propertyName)
Returns property as string. Dictionaries: C,C++ and C++Text Return type: string Parameters: string propertyName | Name of property Available values: - "name" ( c++ dictionary , applies to: )
- "filename" ( c++ dictionary , applies to: )
- "pathname" ( c++ dictionary , applies to: )
- "line" ( c++ dictionary , applies to: )
- "name" ( c++text dictionary , applies to: tokens )
- "value" ( c++text dictionary , applies to: tokens )
- "filename" ( c++text dictionary , applies to: tokens )
- "line" ( c++text dictionary , applies to: tokens )
- "startcolumn" ( c++text dictionary , applies to: tokens )
- "endcolumn" ( c++text dictionary , applies to: tokens )
- "lastinline" ( c++text dictionary , applies to: tokens )
- "lastinbody" ( c++text dictionary , applies to: tokens )
|
---|
Example: Code Block |
---|
| def test(node, context):
context.report(node.getProperty("name"))
return 1 |
getColumnint getColumn()
Returns the start column for a node. Dictionaries: C,C++ and C++Text Return type: int Example: Code Block |
---|
| def test(node, context):
context.report(str(node.getColumn()))
return 1 |
getLineint getLine()
Returns the line for a node. Dictionaries: C,C++ and C++Text Return type: int Example: Code Block |
---|
| def test(node, context):
context.report(str(node.getLine()))
return 1 |
reportreport ()
Reports text in C++test style. Dictionaries: C,C++ and C++Text Return type: none Example: Code Block |
---|
| def test(node, context):
node.report("hit")
return 1 |
getElementsEnumeration getElements(string elementName)
Returns enumerations. Dictionaries: C++Text Return type: enumeration Parameters: string elementName | Name of element Available values: - "body" ( c++text dictionary , applies to: tokens )
|
---|
Example: Code Block |
---|
| def test(node, context):
bodyenumeration = node.getElements("body")
c = 0
elem = bodyenumeration.next()
while (elem != None):
c = c + 1
elem = bodyenumeration.next()
context.report("The number of elements in the body: " + str(c))
return 1 |
getEnforcerRuleEnforcerContext getEnforcer()
Returns the enforcer context - RuleEnforcerContext. Dictionaries: C,C++ and C++Text Return type: RuleEnforcerContext Example: Code Block |
---|
| def test(node, context):
ruleenforcercontext = node.getEnforcer()
context.report(ruleenforcercontext.getContextName())
return 1 |
|
|
rulecontextClasses: Table of Content Zone |
---|
maxLevel | 3 |
---|
minLevel | 3 |
---|
location | top |
---|
type | flat |
---|
separator | pipe |
---|
| RuleContextMethods: Table of Content Zone |
---|
maxLevel | 4 |
---|
minLevel | 4 |
---|
location | top |
---|
type | flat |
---|
separator | pipe |
---|
| getContextNamestring getContextName()
Returns the context name. Dictionaries: C,C++ and C++Text Return type: string Example: Code Block |
---|
| def test(node, context):
context.report(context.getContextName())
return 1 |
getParentContextRuleEnforcerContext getParentContext()
Returns the parent context - RuleEnforcerContext. Dictionaries:C,C++ and C++Text Return type: RuleEnforcerContext Example: Code Block |
---|
| def test(node, context):
ruleenforcercontext = context.getParentContext()
context.report(ruleenforcercontext.getContextName())
return 1 |
getListList getList(string collectorName)
Returns a list of collected objects. Dictionaries: C,C++ and C++Text Return type: list Parameters: string collectorName | name of collector |
---|
Example: Code Block |
---|
| # first we have coleced nodes in colector A
def test(node, context):
list = context.getList("A")
output = ""
for it in list:
output = output + it.getProperty("name") + " "
context.report("Elements in the list: " + output)
return 1 |
reportreport ()
Reports text in C++test style. Dictionaries: C,C++ and C++Text Return type: none Example: Code Block |
---|
| def test(node, context):
context.report("hit")
return 1 |
putput(string key, string value)
Saves a key value inside the context. Dictionaries: C,C++ and C++Text Return type: none Parameters: string key | name of key |
---|
string value | key value |
---|
Example: Code Block |
---|
| def test(node, context):
context.put("color","red")
return 1 |
getstring get(string key)
Gets the key value from the context. Dictionaries: C,C++ and C++Text Return type: string Parameters: Example: Code Block |
---|
| def test(node, context):
context.put("color","red")
context.report(context.get("color"))
return 1 |
getKeysList getKeys()
Gets a list of keys stored in the context. Dictionaries: C,C++ and C++Text Return type: list Example: Code Block |
---|
| def test(node, context):
context.put("color","red")
context.put("size","big")
context.report(context.getKeys())
return 1 |
clearclear()
Removes all keys stored in the context. Dictionaries: C,C++ and C++Text Return type: none Example: Code Block |
---|
def test(node, context):
context.put("color","red")
txt1 = context.get("color")
context.clear()
txt2 = context.get("color")
context.report("Before clear: " + txt1 + " After clear: " + str(txt2))
return 1 |
|
|
enforcercontextClasses: Table of Content Zone |
---|
maxLevel | 3 |
---|
minLevel | 3 |
---|
location | top |
---|
type | flat |
---|
separator | pipe |
---|
| RuleEnforcerContextMethods: Table of Content Zone |
---|
maxLevel | 4 |
---|
minLevel | 4 |
---|
location | top |
---|
type | flat |
---|
separator | pipe |
---|
| getContextNamestring getContextName()
Returns context name. Dictionaries: C,C++ and C++Text Return type: string Example: Code Block |
---|
def test(node, context):
enforcercontext = context.getParentContext()
context.report(enforcercontext.getContextName())
return 1 |
executeRuleint executeRule(string textRuleName, NodeProvider node)
Returns 1 if the called text rule returns true. Return type: int Parameters: string textRuleName | Path and name of text rule |
---|
NodeProvider node | Node determines the point where text rule will be invoked |
---|
Example: Code Block |
---|
def test(node, context):
ruleenforcercontext = node.getEnforcer()
#call text rule in the line from node and in default: first column
ret = ruleenforcercontext.executeRule("./my_text_rule.rule", node)
return ret |
executeRuleEx int executeRuleEx(string textRuleName, NodeProvider node, string fileName, int line, int column)
Returns 1 if the called text rule returns true. Dictionaries: C,C++ and C++Text Return type: int Parameters: string textRuleName | Path and name of text rule |
---|
NodeProvider node | Node determines the point where text rule will be invoked |
---|
string fileName | Name of source file |
---|
int line | Starting line number from which the text rule will be invoked. If -1 , the call child rule will be skipped. |
---|
int column | Starting column number from which the text rule will be invoked. If -1 , the call child rule will be skipped. |
---|
Example: Code Block |
---|
def test(node, context):
ruleenforcercontext = node.getEnforcer()
file = node.getProperty("pathname")
line = node.getLine()
colum = node.getColumn()
ret = ruleenforcercontext.executeRuleEx("./txt.rule", node, filename, line, colum)
return ret |
executeRegexpint executeRegexp(string regExp, NodeProvider node)
Calls a regExp on the current source file in the line specified in the node. Dictionaries: C,C++ and C++Text Return type: int Parameters: string regExp | Regular expression string |
---|
NodeProvider node | Node is used to determine the line for which the regExp is called |
---|
Example: Code Block |
---|
def test(node, context):
ruleenforcercontext = node.getEnforcer()
#return 1 if there is cpp style comment in the line from node
ret = ruleenforcercontext.executeRegexp("\/\/",node)
return ret |
executeRegexpExint executeRegexpEx(string regExp, string filename, int start_line, int op_start_column, int op_end_line, int op_end_column)
Calls a regExp on source file $filename in the region defined by $start_line $op_start_column $op_end_lin $op_end_column Dictionaries: C,C++ and C++Text Return type: int Parameters: string regExp | Regular expression string |
---|
string fileName | Name of source file |
---|
int start_line | Starting line number. |
---|
int op_start_column | Starting column number (optional). Available value(s): - "each column number in the given line; default value '-1' means first column in line" ( c++ dictionary , applies to: )
- "each column number in the given line; default value '-1' means first column in line" ( c++text dictionary , applies to: )
|
---|
int op_end_line | Defined end line (optional). Available value(s): - "each line number for the given source file; default value '-1' means the same value like start line" ( c++ dictionary , applies to: )
- "each line number for the given source file; default value '-1' means the same value like start line" ( c++text dictionary , applies to: )
|
---|
int op_end_column | Defined end column (optional). Available value(s): - "each column number in the given line; default value '-1' means last column in line" ( c++ dictionary , applies to: )
- "each column number in the given line; default value '-1' means last column in line" ( c++text dictionary , applies to: )
|
---|
Example: Code Block |
---|
def test(node, context):
ruleenforcercontext = node.getEnforcer()
line = node.getLine()
file = node.getProperty("pathname")
#return 1 if there is cpp style comment in next line given from node
ret = ruleenforcercontext.executeRegexpEx("\/\/", file, line+1)
return ret |
putput(string key, string value)
Saves the key value inside the context. Dictionaries: C,C++ and C++Text Return type: none Parameters: string key | name of the key |
---|
string value | value of the key |
---|
Example: Code Block |
---|
| def test(node, context):
ruleenforcercontext = context.getParentContext()
ruleenforcercontext.put("color","red")
return 1 |
getstring get(string key)
Gets the key value from the context. Dictionaries: C,C++ and C++Text Return type: string Parameters: string key | name of the key |
---|
Example: Code Block |
---|
| def test(node, context):
ruleenforcercontext = context.getParentContext()
ruleenforcercontext.put("color","red")
context.report(ruleenforcercontext.get("color"))
return 1 |
getKeysList getKeys()
Gets a list of keys stored in the context. Dictionaries: C,C++ and C++Text Return type: list Example: Code Block |
---|
| def test(node, context):
ruleenforcercontext = context.getParentContext()
ruleenforcercontext.put("color","red")
ruleenforcercontext.put("size","big")
context.report(ruleenforcercontext.getKeys())
return 1 |
clearclear ()
Removes all keys stored in the context. Dictionaries: C,C++ and C++Text Return type: none Example: Code Block |
---|
| def test(node, context):
ruleenforcercontext = context.getParentContext()
ruleenforcercontext.put("color","red")
txt1 = ruleenforcercontext.get("color")
ruleenforcercontext.clear()
txt2 = ruleenforcercontext.get("color")
context.report("Before clear: " + txt1 + " After clear: " + str(txt2))
return 1 |
|
|
enumerationClasses: Table of Content Zone |
---|
maxLevel | 3 |
---|
minLevel | 3 |
---|
location | top |
---|
type | flat |
---|
separator | pipe |
---|
| EnumerationMethods: Table of Content Zone |
---|
maxLevel | 4 |
---|
minLevel | 4 |
---|
location | top |
---|
type | flat |
---|
separator | pipe |
---|
| nextNodeProvider nerxt()
Returns the next object in the enumeration. Dictionaries: C++Text Return type: NodeProvider Example: Code Block |
---|
| def test(node, context):
bodyenumeration = node.getElements("body")
#call next() in for:
lines =""
for el in bodyenumeration:
lines = lines +str(el.getLine()) + " "
context.report("Lines with elements: " + lines)
return 1 |
|
|
Examples of Python Functions Code Block |
---|
language | cpp |
---|
title | #Displaying messages |
---|
| def reportMsg(node, context):
node.report(repr(node.getLine()))
context.report(repr(node.getLine()))
return 1 |
Code Block |
---|
language | cpp |
---|
title | #First element from collection |
---|
| def cleaning( node, context ):
#cleans the mark
context.put( "a_mark","" )
def firstOnly( node, context ):
#checks if mark has been already set; if not sets it
if (context.get( "a_mark" ) != "a"):
context.put( "a_mark", "a" )
return 1
else:
return 0 |
Code Block |
---|
language | cpp |
---|
title | #Getting all elements from collector |
---|
| def compare( node, context ):
list = context.getList( 'A' )
if (list.size( ) == 0):
return 0
else:
return 1 |
|