Commit 881f95d1 authored by aniket-engg's avatar aniket-engg

suggested changes

parent 78db7cf7
var name = 'For loop iterates over dynamic array: ' var name = 'For loop iterates over dynamic array: '
var desc = 'The number of \'for\' loop iterations depends on dynamic array\'s size' var desc = 'The number of \'for\' loop iterations depends on dynamic array\'s size'
var categories = require('./categories') var categories = require('./categories')
var common = require('./staticAnalysisCommon') var { isForLoop, isDynamicArrayLengthAccess, isBinaryOperation } = require('./staticAnalysisCommon')
function forLoopIteratesOverDynamicArray () { function forLoopIteratesOverDynamicArray () {
this.relevantNodes = [] this.relevantNodes = []
} }
forLoopIteratesOverDynamicArray.prototype.visit = function (node) { forLoopIteratesOverDynamicArray.prototype.visit = function (node) {
if (common.isForLoop(node) && // is for loop node if (isForLoop(node)) {
(common.isDynamicArrayLengthAccess(node.children[1].children[1]) || // check if for loop condition uses dynamic array length like `i < array.length` // Access 'condition' node of 'for' loop statement
(node.children[1].children[1].children && common.isDynamicArrayLengthAccess(node.children[1].children[1].children[0]))) // or like `i < array.length (operator like -,+,*,/) number` e.g; i < array.length -1 let forLoopConditionNode = node.children[1]
) { // Access right side of condition as its children
let conditionChildrenNode = forLoopConditionNode.children[1]
// Check if it is a binary operation. if yes, check if its children node access length of dynamic array
if (isBinaryOperation(conditionChildrenNode) && isDynamicArrayLengthAccess(conditionChildrenNode.children[0])) {
this.relevantNodes.push(node) this.relevantNodes.push(node)
} else if (isDynamicArrayLengthAccess(conditionChildrenNode)) { // else check if condition node itself access length of dynamic array
this.relevantNodes.push(node)
}
} }
} }
......
...@@ -518,7 +518,7 @@ function isDynamicArrayAccess (node) { ...@@ -518,7 +518,7 @@ function isDynamicArrayAccess (node) {
} }
/** /**
* True if node accesses 'length' member of array * True if node accesses 'length' member of dynamic array
* @node {ASTNode} node to check for * @node {ASTNode} node to check for
* @return {bool} * @return {bool}
*/ */
...@@ -526,7 +526,7 @@ function isDynamicArrayLengthAccess (node) { ...@@ -526,7 +526,7 @@ function isDynamicArrayLengthAccess (node) {
return node && // if node exists return node && // if node exists
nodeType(node, exactMatch(nodeTypes.MEMBERACCESS)) && // is memberAccess Node nodeType(node, exactMatch(nodeTypes.MEMBERACCESS)) && // is memberAccess Node
(node.attributes.member_name === 'length') && // accessing 'length' member (node.attributes.member_name === 'length') && // accessing 'length' member
node.children[0].attributes.type.indexOf('[]') !== -1 // member is accessed from array node.children[0].attributes.type.indexOf('[]') !== -1 // member is accessed from dynamic array, notice [] without any number
} }
/** /**
...@@ -1184,6 +1184,7 @@ module.exports = { ...@@ -1184,6 +1184,7 @@ module.exports = {
isStatement: isStatement, isStatement: isStatement,
isExpressionStatement: isExpressionStatement, isExpressionStatement: isExpressionStatement,
isBlock: isBlock, isBlock: isBlock,
isBinaryOperation: isBinaryOperation,
// #################### Constants // #################### Constants
nodeTypes: nodeTypes, nodeTypes: nodeTypes,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment