I'm trying to write a Custom KlocWork checker for c++ however im stuck with an issue:
When we have an expression like this one:
x = y + z;
I want to access the left node which is the variable 'x' but also both variables from the Right node(Left and Right from the RIGHT NODE) i don't know how to access every variable, so far i have this in my checker:
// BinaryExpr [ getOperationCode() = KTC_OPCODE_ASSIGN]
[$exprL:= Left]
[$size1:= $exprL.getTypeSize()]
[$exprR:= Right]
[$exprR.getOperationCode() = KTC_OPCODE_ADD]
Which detects every BinaryExpression with another expression on the Left Node(store in $exprR) but after that i don't know how to access Left and Right childs of $exprR.
Thanks in advance for any help!
There are two nested expressions here, and you want to store the Left node of the assignment and then continue to traverse the AST further using the full pattern to the second binary expression node to get the left and right nodes of the add. For example:
// BinaryExpr [getOperationCode() = KTC_OPCODE_ASSIGN] [$exprOL:= Left]
Here we find and store the left node of the assignment expression.
// BinaryExpr [getOperationCode() = KTC_OPCODE_ASSIGN] [$exprOL:= Left] / Right::BinaryExpr [getOperationCode() = KTC_OPCODE_ADD]
And then we continue on to get the addition expression. Finally, we can grab Left and Right of this expression:
// BinaryExpr [getOperationCode() = KTC_OPCODE_ASSIGN] [$exprOL:= Left] / Right::BinaryExpr [getOperationCode() = KTC_OPCODE_ADD]
[$exprL:= Left]
[$exprR:= Right]
You can use the println() function to test this. So the complete expression
// BinaryExpr [getOperationCode() = KTC_OPCODE_ASSIGN] [$exprOL:= Left] / Right::BinaryExpr [getOperationCode() = KTC_OPCODE_ADD]
[$exprL:= Left]
[$exprR:= Right]
[$exprOL.getName().println()]
[$exprL.getName().println()]
[$exprR.getName().println()]
for the following code:
int func (int x, int y)
{
int local;
local = x;
local = x + y;
local = y - x;
return local;
}
Would print out:
local
x
y