Static analysis, this on local calls. Change requests

pull/1/head
Michael Fröwis 8 years ago
parent 7039d85a1f
commit ff3db09669
  1. 14
      src/app/staticanalysis/modules/staticAnalysisCommon.js
  2. 2
      src/app/staticanalysis/modules/thisLocal.js
  3. 10
      test/staticanalysis/staticAnalysisCommon-test.js

@ -45,7 +45,7 @@ var specialVariables = {
// usage of now special variable // usage of now special variable
function isNowAccess (node) { function isNowAccess (node) {
return nodeType(node, nodeTypes.IDENTIFIER) && return nodeType(node, nodeTypes.IDENTIFIER) &&
returnType(node, basicTypes.UINT) && expressionType(node, basicTypes.UINT) &&
name(node, 'now') name(node, 'now')
} }
@ -93,20 +93,20 @@ function isLLDelegatecall (node) {
undefined, basicTypes.ADDRESS, lowLevelCallTypes.DELEGATECALL.ident) undefined, basicTypes.ADDRESS, lowLevelCallTypes.DELEGATECALL.ident)
} }
function isMemberAccess (node, retType, accesser, accesserType, memberName) { function isMemberAccess (node, retType, accessor, accessorType, memberName) {
return nodeType(node, nodeTypes.MEMBERACCESS) && return nodeType(node, nodeTypes.MEMBERACCESS) &&
returnType(node, retType) && expressionType(node, retType) &&
name(node, memberName) && name(node, memberName) &&
nrOfChildren(node, 1) && nrOfChildren(node, 1) &&
name(node.children[0], accesser) && name(node.children[0], accessor) &&
returnType(node.children[0], accesserType) expressionType(node.children[0], accessorType)
} }
function nrOfChildren (node, nr) { function nrOfChildren (node, nr) {
return (node && (nr === undefined || nr === null)) || (node && node.children && node.children.length === nr) return (node && (nr === undefined || nr === null)) || (node && node.children && node.children.length === nr)
} }
function returnType (node, typeRegex) { function expressionType (node, typeRegex) {
return (node && !typeRegex) || (node && node.attributes && new RegExp(typeRegex).test(node.attributes.type)) return (node && !typeRegex) || (node && node.attributes && new RegExp(typeRegex).test(node.attributes.type))
} }
@ -148,7 +148,7 @@ module.exports = {
specialVariables: specialVariables, specialVariables: specialVariables,
helpers: { helpers: {
nrOfChildren: nrOfChildren, nrOfChildren: nrOfChildren,
returnType: returnType, expressionType: expressionType,
nodeType: nodeType, nodeType: nodeType,
name: name, name: name,
buildFunctionSignature: buildFunctionSignature buildFunctionSignature: buildFunctionSignature

@ -15,7 +15,7 @@ thisLocal.prototype.report = function (compilationResults) {
this.warningNowNodes = [] this.warningNowNodes = []
return this.warningNodes.map(function (item, i) { return this.warningNodes.map(function (item, i) {
return { return {
warning: `use of "this" for local functions: never use this to call local functions, it only consumes more gas than normal local calls.`, warning: `Use of "this" for local functions: Never use this to call functions in the same contract, it only consumes more gas than normal local calls.`,
location: item.src, location: item.src,
more: 'http://solidity.readthedocs.io/en/develop/control-structures.html#external-function-calls' more: 'http://solidity.readthedocs.io/en/develop/control-structures.html#external-function-calls'
} }

@ -59,16 +59,16 @@ test('staticAnalysisCommon.helpers.nodeType', function (t) {
lowlevelAccessersCommon(t, common.helpers.nodeType, node) lowlevelAccessersCommon(t, common.helpers.nodeType, node)
}) })
test('staticAnalysisCommon.helpers.returnType', function (t) { test('staticAnalysisCommon.helpers.expressionType', function (t) {
t.plan(9) t.plan(9)
var node = { name: 'Identifier', attributes: { value: 'now', type: 'uint256' } } var node = { name: 'Identifier', attributes: { value: 'now', type: 'uint256' } }
var node2 = { name: 'FunctionCall', attributes: { member_name: 'call', type: 'function () payable returns (bool)' } } var node2 = { name: 'FunctionCall', attributes: { member_name: 'call', type: 'function () payable returns (bool)' } }
t.ok(common.helpers.returnType(node, common.basicTypes.UINT), 'should work for ident') t.ok(common.helpers.expressionType(node, common.basicTypes.UINT), 'should work for ident')
t.ok(common.helpers.returnType(node2, utils.escapeRegExp(common.basicFunctionTypes.CALL)), 'should work for funcall') t.ok(common.helpers.expressionType(node2, utils.escapeRegExp(common.basicFunctionTypes.CALL)), 'should work for funcall')
t.ok(common.helpers.returnType(node2, '^function \\('), 'regex should work') t.ok(common.helpers.expressionType(node2, '^function \\('), 'regex should work')
lowlevelAccessersCommon(t, common.helpers.returnType, node) lowlevelAccessersCommon(t, common.helpers.expressionType, node)
}) })
test('staticAnalysisCommon.helpers.nrOfChildren', function (t) { test('staticAnalysisCommon.helpers.nrOfChildren', function (t) {

Loading…
Cancel
Save