You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
222 lines
7.3 KiB
222 lines
7.3 KiB
8 years ago
|
const parse = {};
|
||
8 years ago
|
const instrumenter = require('./instrumenter');
|
||
|
|
||
8 years ago
|
// All the functions in this file do is walk the AST and call the instrumenter
|
||
|
// functions where appropriate, which determine where to inject events.
|
||
|
|
||
8 years ago
|
// Assign a dummy function to everything we might see in the AST
|
||
8 years ago
|
['AssemblyAssignment',
|
||
|
'AssemblyItem',
|
||
|
'AssemblyLocalBinding',
|
||
|
'AssignmentExpression',
|
||
8 years ago
|
'BinaryExpression',
|
||
|
'BlockStatement',
|
||
|
'BreakStatement',
|
||
|
'CallExpression',
|
||
|
'ConditionalExpression',
|
||
|
'ContinueStatement',
|
||
|
'ContractStatement',
|
||
|
'DebuggerStatement',
|
||
|
'DeclarativeExpression',
|
||
|
'DeclarativeExpressionList',
|
||
|
'DenominationLiteral',
|
||
|
'DoWhileStatement',
|
||
|
'EmptyStatement',
|
||
|
'EnumDeclaration',
|
||
|
'EventDeclaration',
|
||
|
'ExpressionStatement',
|
||
|
'ForInStatement',
|
||
|
'ForStatement',
|
||
8 years ago
|
'FunctionalAssemblyExpression',
|
||
8 years ago
|
'FunctionDeclaration',
|
||
|
'FunctionName',
|
||
|
'Identifier',
|
||
|
'IfStatement',
|
||
|
'ImportStatement',
|
||
|
'InformalParameter',
|
||
8 years ago
|
'InlineAssemblyBlock',
|
||
8 years ago
|
'InlineAssemblyStatement',
|
||
8 years ago
|
'IsStatement',
|
||
|
'LibraryStatement',
|
||
|
'Literal',
|
||
|
'MappingExpression',
|
||
|
'MemberExpression',
|
||
|
'ModifierArgument',
|
||
|
'ModifierDeclaration',
|
||
|
'ModifierName',
|
||
|
'Modifiers',
|
||
|
'NewExpression',
|
||
|
'PlaceholderStatement',
|
||
|
'PragmaStatement',
|
||
|
'Program',
|
||
|
'ReturnStatement',
|
||
|
'SequenceExpression',
|
||
8 years ago
|
'StateVariableDeclaration',
|
||
8 years ago
|
'StructDeclaration',
|
||
|
'ThisExpression',
|
||
|
'ThrowStatement',
|
||
|
'Type',
|
||
|
'UnaryExpression',
|
||
|
'UpdateExpression',
|
||
|
'UsingStatement',
|
||
|
'VariableDeclaration',
|
||
|
'VariableDeclarationTuple',
|
||
|
'VariableDeclarator',
|
||
|
'WhileStatement'].forEach(key => {
|
||
|
parse[key] = function dummyFunction() {};
|
||
8 years ago
|
});
|
||
8 years ago
|
|
||
8 years ago
|
// Override the dummy functions where we know we can do better.
|
||
8 years ago
|
|
||
8 years ago
|
parse.AssignmentExpression = function parseAssignmentExpression(contract, expression) {
|
||
8 years ago
|
instrumenter.instrumentStatement(contract, expression);
|
||
|
instrumenter.instrumentAssignmentExpression(contract, expression);
|
||
8 years ago
|
};
|
||
|
|
||
8 years ago
|
parse.ConditionalExpression = function parseConditionalExpression(contract, expression) {
|
||
8 years ago
|
instrumenter.instrumentStatement(contract, expression);
|
||
|
instrumenter.instrumentConditionalExpression(contract, expression);
|
||
8 years ago
|
};
|
||
|
|
||
8 years ago
|
parse.Modifiers = function parseModifier(contract, modifiers) {
|
||
8 years ago
|
if (modifiers) {
|
||
|
modifiers.forEach(modifier => {
|
||
8 years ago
|
parse[modifier.type](contract, modifier);
|
||
8 years ago
|
});
|
||
|
}
|
||
|
};
|
||
|
|
||
8 years ago
|
parse.ReturnStatement = function parseReturnStatement(contract, expression) {
|
||
8 years ago
|
instrumenter.instrumentStatement(contract, expression);
|
||
8 years ago
|
};
|
||
|
|
||
8 years ago
|
parse.NewExpression = function parseNewExpression(contract, expression) {
|
||
|
parse[expression.callee.type](contract, expression.callee);
|
||
8 years ago
|
};
|
||
|
|
||
8 years ago
|
parse.MemberExpression = function parseMemberExpression(contract, expression) {
|
||
|
parse[expression.object.type](contract, expression.object);
|
||
8 years ago
|
};
|
||
|
|
||
8 years ago
|
parse.CallExpression = function parseCallExpression(contract, expression) {
|
||
8 years ago
|
// It looks like in any given chain of call expressions, only the head callee is an Identifier
|
||
|
// node ... and we only want to instrument the statement once.
|
||
|
if (expression.callee.type === 'Identifier') {
|
||
|
instrumenter.instrumentStatement(contract, expression);
|
||
8 years ago
|
parse[expression.callee.type](contract, expression.callee);
|
||
|
} else {
|
||
|
parse[expression.callee.type](contract, expression.callee);
|
||
|
}
|
||
8 years ago
|
};
|
||
|
|
||
8 years ago
|
parse.UnaryExpression = function parseUnaryExpression(contract, expression) {
|
||
|
parse[expression.argument.type](contract, expression.argument);
|
||
8 years ago
|
};
|
||
|
|
||
8 years ago
|
parse.ThrowStatement = function parseThrowStatement(contract, expression) {
|
||
8 years ago
|
instrumenter.instrumentStatement(contract, expression);
|
||
8 years ago
|
};
|
||
|
|
||
8 years ago
|
parse.IfStatement = function parseIfStatement(contract, expression) {
|
||
8 years ago
|
instrumenter.instrumentStatement(contract, expression);
|
||
|
instrumenter.instrumentIfStatement(contract, expression);
|
||
8 years ago
|
// We can't instrument
|
||
|
// if (x==1)
|
||
|
//
|
||
|
// So don't count x==1 as a statement - just the if as a whole.
|
||
8 years ago
|
// parse[expression.test.type](contract, expression.test)
|
||
|
parse[expression.consequent.type](contract, expression.consequent);
|
||
8 years ago
|
if (expression.alternate) {
|
||
8 years ago
|
parse[expression.alternate.type](contract, expression.alternate);
|
||
8 years ago
|
}
|
||
|
};
|
||
|
|
||
8 years ago
|
parse.ExpressionStatement = function parseExpressionStatement(contract, content) {
|
||
|
parse[content.expression.type](contract, content.expression);
|
||
8 years ago
|
};
|
||
|
|
||
8 years ago
|
parse.VariableDeclarationTuple = function parseVariableDeclarationTuple(contract, expression) {
|
||
|
parse[expression.init.type](contract, expression.init);
|
||
8 years ago
|
};
|
||
|
|
||
8 years ago
|
parse.BlockStatement = function parseBlockStatement(contract, expression) {
|
||
8 years ago
|
for (let x = 0; x < expression.body.length; x++) {
|
||
8 years ago
|
instrumenter.instrumentLine(contract, expression.body[x]);
|
||
8 years ago
|
parse[expression.body[x].type](contract, expression.body[x]);
|
||
8 years ago
|
}
|
||
|
};
|
||
|
|
||
8 years ago
|
parse.VariableDeclaration = function parseVariableDeclaration(contract, expression) {
|
||
8 years ago
|
instrumenter.instrumentStatement(contract, expression);
|
||
8 years ago
|
if (expression.declarations.length > 1) {
|
||
|
console.log('more than one declaration');
|
||
|
}
|
||
8 years ago
|
parse[expression.declarations[0].id.type](contract, expression.declarations[0].id);
|
||
8 years ago
|
};
|
||
|
|
||
8 years ago
|
parse.UsingStatement = function parseUsingStatement(contract, expression) {
|
||
|
parse[expression.for.type](contract, expression.for);
|
||
8 years ago
|
};
|
||
|
|
||
8 years ago
|
parse.FunctionDeclaration = function parseFunctionDeclaration(contract, expression) {
|
||
|
parse.Modifiers(contract, expression.modifiers);
|
||
8 years ago
|
if (expression.body) {
|
||
8 years ago
|
instrumenter.instrumentFunctionDeclaration(contract, expression);
|
||
8 years ago
|
parse[expression.body.type](contract, expression.body);
|
||
8 years ago
|
}
|
||
|
};
|
||
|
|
||
8 years ago
|
parse.ContractOrLibraryStatement = function parseContractOrLibraryStatement(contract, expression) {
|
||
8 years ago
|
// Inject our coverage event if we're covering
|
||
8 years ago
|
// This is harder because of where .start and .end represent, and how documented comments are validated
|
||
|
// by solc upon compilation. From the start of this contract statement, find the first '{', and inject
|
||
|
// there.
|
||
|
const injectionPoint = expression.start + contract.instrumented.slice(expression.start).indexOf('{') + 2;
|
||
|
if (contract.injectionPoints[injectionPoint]) {
|
||
|
contract.injectionPoints[expression.start + contract.instrumented.slice(expression.start).indexOf('{') + 2].push({
|
||
|
type: 'eventDefinition',
|
||
|
});
|
||
|
} else {
|
||
|
contract.injectionPoints[expression.start + contract.instrumented.slice(expression.start).indexOf('{') + 2] = [{
|
||
|
type: 'eventDefinition',
|
||
|
}];
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
if (expression.body) {
|
||
|
expression.body.forEach(construct => {
|
||
|
parse[construct.type](contract, construct);
|
||
|
});
|
||
|
}
|
||
8 years ago
|
};
|
||
|
|
||
8 years ago
|
parse.ContractStatement = function ParseContractStatement(contract, expression) {
|
||
|
parse.ContractOrLibraryStatement(contract, expression);
|
||
|
};
|
||
|
|
||
8 years ago
|
parse.LibraryStatement = function parseLibraryStatement(contract, expression) {
|
||
8 years ago
|
parse.ContractOrLibraryStatement(contract, expression);
|
||
8 years ago
|
};
|
||
|
|
||
8 years ago
|
parse.ModifierDeclaration = function parseModifierDeclaration(contract, expression) {
|
||
8 years ago
|
instrumenter.instrumentFunctionDeclaration(contract, expression);
|
||
8 years ago
|
parse[expression.body.type](contract, expression.body);
|
||
8 years ago
|
};
|
||
|
|
||
8 years ago
|
parse.Program = function parseProgram(contract, expression) {
|
||
8 years ago
|
expression.body.forEach(construct => {
|
||
8 years ago
|
parse[construct.type](contract, construct);
|
||
8 years ago
|
});
|
||
|
};
|
||
|
|
||
8 years ago
|
parse.WhileStatement = function parseWhileStatement(contract, expression) {
|
||
8 years ago
|
instrumenter.instrumentStatement(contract, expression);
|
||
8 years ago
|
parse[expression.body.type](contract, expression.body);
|
||
8 years ago
|
};
|
||
|
|
||
8 years ago
|
parse.ForStatement = function parseForStatement(contract, expression) {
|
||
8 years ago
|
instrumenter.instrumentStatement(contract, expression);
|
||
8 years ago
|
parse[expression.body.type](contract, expression.body);
|
||
8 years ago
|
};
|
||
|
|
||
|
module.exports = parse;
|