eth-brownie / brownie

A Python-based development and testing framework for smart contracts targeting the Ethereum Virtual Machine.
https://eth-brownie.readthedocs.io
MIT License
2.66k stars 551 forks source link

Show solidity compiler warnings #889

Open jeffywu opened 3 years ago

jeffywu commented 3 years ago

Overview

I'm experimenting with brownie and the SMT checker and one thing I'm noticing is that compiler warnings don't show (SMT checker errors seem to be returned as warnings rather than errors). It looks like this is occuring on these lines: https://github.com/iamdefinitelyahuman/py-solc-x/blob/master/solcx/main.py#L378

Not sure what happens to the return value after here: https://github.com/eth-brownie/brownie/blob/master/brownie/project/compiler/solidity.py#L64

But adding these lines to the above code:

...
  try:
        output = solcx.compile_standard(input_json, allow_paths=allow_paths,)
        print('compile standard output', output['errors'])
        import pprint 
        pp = pprint.PrettyPrinter(indent=4)
        pp.pprint(output['errors'])
        return output
    except solcx.exceptions.SolcError as e:
        raise CompilerError(e, "solc")
...

Gives me some values where the severity = 'warning':

[   {   'component': 'general',
        'errorCode': '3805',
        'formattedMessage': 'Warning: This is a pre-release compiler version, '
                            'please do not use it in production.\n',
        'message': 'This is a pre-release compiler version, please do not use '
                   'it in production.',
        'severity': 'warning',
        'type': 'Warning'},
    {   'component': 'general',
        'errorCode': '1878',
        'formattedMessage': 'contracts/Test.sol: Warning: SPDX license '
                            'identifier not provided in source file. Before '
                            'publishing, consider adding a comment containing '
                            '"SPDX-License-Identifier: <SPDX-License>" to each '
                            'source file. Use "SPDX-License-Identifier: '
                            'UNLICENSED" for non-open-source code. Please see '
                            'https://spdx.org for more information.\n',
        'message': 'SPDX license identifier not provided in source file. '
                   'Before publishing, consider adding a comment containing '
                   '"SPDX-License-Identifier: <SPDX-License>" to each source '
                   'file. Use "SPDX-License-Identifier: UNLICENSED" for '
                   'non-open-source code. Please see https://spdx.org for more '
                   'information.',
        'severity': 'warning',
        'sourceLocation': {   'end': -1,
                              'file': 'contracts/Test.sol',
                              'start': -1},
        'type': 'Warning'},
    {   'component': 'general',
        'errorCode': '2018',
        'formattedMessage': 'contracts/Test.sol:6:5: Warning: Function state '
                            'mutability can be restricted to pure\n'
                            '    function smtTest(uint256 a) external {\n'
                            '    ^ (Relevant source part starts here and spans '
                            'across multiple lines).\n',
        'message': 'Function state mutability can be restricted to pure',
        'severity': 'warning',
        'sourceLocation': {   'end': 175,
                              'file': 'contracts/Test.sol',
                              'start': 83},
        'type': 'Warning'},
    {   'component': 'general',
        'errorCode': '6328',
        'formattedMessage': 'contracts/Test.sol:8:9: Warning: CHC: Assertion '
                            'violation happens here.\n'
                            '        assert(a < 9);\n'
                            '        ^-----------^\n'
                            'Counterexample:\n'
                            '\n'
                            'a = 11\n'
                            '\n'
                            '\n'
                            'Transaction trace:\n'
                            'constructor()\n'
                            'smtTest(11)\n',
        'message': 'CHC: Assertion violation happens here.',
        'secondarySourceLocations': [   {   'message': 'Counterexample:\n'
                                                       '\n'
                                                       'a = 11\n'
                                                       '\n'
                                                       '\n'
                                                       'Transaction trace:\n'
                                                       'constructor()\n'
                                                       'smtTest(11)'}],
        'severity': 'warning',
        'sourceLocation': {   'end': 168,
                              'file': 'contracts/Test.sol',
                              'start': 155},
        'type': 'Warning'}]

Would be nice to have an option to print compiler warnings and maybe fail on them. Happy to work on a PR if someone gives me some pointers on the best place to add such code.

Specification

Basically a cli flag and perhaps a config file parameter to show warnings and fail on warnings, maybe allowing the user to set the severity filter in solcx 🤷🏽 https://github.com/iamdefinitelyahuman/py-solc-x/blob/master/solcx/main.py#L378

iamdefinitelyahuman commented 3 years ago

This seems like a useful feature, and shouldn't be that hard to implement :+1:

cameel commented 3 years ago

Just a heads up for when someone picks this issue to work on it: the Solidity compiler will soon get a new severity level (info) and SMTChecker will probably switch to using that for some of its messages (https://github.com/ethereum/solidity/issues/11508).