All Projects → nlohmann → exception_reporter

nlohmann / exception_reporter

Licence: MIT License
A tool to collect the exceptions that can reach a C++ function

Programming Languages

python
139335 projects - #7 most used programming language

Exceptions Reporter

This tool tries to answer this r/cpp question for a tool to find out, for a given function in my code base, which exceptions it may encounter. It is a simple Python script processing a callgraph generated by Clang. It is far from being ready or useful, but demonstrates that this approach could be feasible.

Please open an issue for ideas or bugs.

Example

Assume the following C++ program:

#include <map>

using int_map = std::map<int, int>;

int bar(int i, const int_map& m)
{
    if (i == 100)
    {
        throw std::runtime_error("oops");
    }

    try
    {
        return m.at(i);
    }
    catch (std::out_of_range&)
    {
        return -1;
    }
}

int main()
{
    int_map m;
    m[1] = 1;

    if (bar(2, m) == -1)
    {
        return 1;
    }
}

The goal is to find out which exceptions can be encountered in its functions; that is, which try/catch block needs to be written.

# step 1: create a callgraph with clang and demangle its C++ symbols
clang++ -S -emit-llvm examples/example.cpp -o - | opt -print-callgraph 2>&1 | sed "s/'/\"/g" | c++filt -n > callgraph.txt

# step 2: analyze the callgraph
./exeption_reporter.py callgraph.txt

For the example, this is the output:

functions containing a throw:
- bar(int, std::__1::map<int, int, std::__1::less<int>, std::__1::allocator<std::__1::pair<int const, int> > > const&)
- std::__1::__throw_length_error(char const*)
- std::__1::__throw_out_of_range(char const*)

functions that MAY throw std::logic_error:
- std::__1::__throw_out_of_range(char const*)
- bar(int, std::__1::map<int, int, std::__1::less<int>, std::__1::allocator<std::__1::pair<int const, int> > > const&)
- std::__1::__throw_length_error(char const*)

functions that MAY throw std::length_error:
- std::__1::__throw_length_error(char const*)

functions that MAY throw std::out_of_range:
- std::__1::__throw_out_of_range(char const*)
- bar(int, std::__1::map<int, int, std::__1::less<int>, std::__1::allocator<std::__1::pair<int const, int> > > const&)

functions that MAY throw std::runtime_error:
- bar(int, std::__1::map<int, int, std::__1::less<int>, std::__1::allocator<std::__1::pair<int const, int> > > const&)

functions that MAY encounter an exception:
- bar(int, std::__1::map<int, int, std::__1::less<int>, std::__1::allocator<std::__1::pair<int const, int> > > const&)
- std::__1::__throw_length_error(char const*)
- std::__1::__throw_out_of_range(char const*)
- main
- std::__1::allocator<std::__1::__tree_node<std::__1::__value_type<int, int>, void*> >::allocate(unsigned long, void const*)
- std::__1::map<int, int, std::__1::less<int>, std::__1::allocator<std::__1::pair<int const, int> > >::at(int const&) const
- std::__1::allocator_traits<std::__1::allocator<std::__1::__tree_node<std::__1::__value_type<int, int>, void*> > >::allocate(std::__1::allocator<std::__1::__tree_node<std::__1::__value_type<int, int>, void*> >&, unsigned long)
- std::__1::unique_ptr<std::__1::__tree_node<std::__1::__value_type<int, int>, void*>, std::__1::__tree_node_destructor<std::__1::allocator<std::__1::__tree_node<std::__1::__value_type<int, int>, void*> > > > std::__1::__tree<std::__1::__value_type<int, int>, std::__1::__map_value_compare<int, std::__1::__value_type<int, int>, std::__1::less<int>, true>, std::__1::allocator<std::__1::__value_type<int, int> > >::__construct_node<std::__1::piecewise_construct_t const&, std::__1::tuple<int&&>, std::__1::tuple<> >(std::__1::piecewise_construct_t const&&&, std::__1::tuple<int&&>&&, std::__1::tuple<>&&)
- std::__1::pair<std::__1::__tree_iterator<std::__1::__value_type<int, int>, std::__1::__tree_node<std::__1::__value_type<int, int>, void*>*, long>, bool> std::__1::__tree<std::__1::__value_type<int, int>, std::__1::__map_value_compare<int, std::__1::__value_type<int, int>, std::__1::less<int>, true>, std::__1::allocator<std::__1::__value_type<int, int> > >::__emplace_unique_key_args<int, std::__1::piecewise_construct_t const&, std::__1::tuple<int&&>, std::__1::tuple<> >(int const&, std::__1::piecewise_construct_t const&&&, std::__1::tuple<int&&>&&, std::__1::tuple<>&&)
- std::__1::map<int, int, std::__1::less<int>, std::__1::allocator<std::__1::pair<int const, int> > >::operator[](int&&)

functions that MAY encounter a std::logic_error exception:
- std::__1::map<int, int, std::__1::less<int>, std::__1::allocator<std::__1::pair<int const, int> > >::at(int const&) const
- bar(int, std::__1::map<int, int, std::__1::less<int>, std::__1::allocator<std::__1::pair<int const, int> > > const&)
- std::__1::__throw_length_error(char const*)
- std::__1::unique_ptr<std::__1::__tree_node<std::__1::__value_type<int, int>, void*>, std::__1::__tree_node_destructor<std::__1::allocator<std::__1::__tree_node<std::__1::__value_type<int, int>, void*> > > > std::__1::__tree<std::__1::__value_type<int, int>, std::__1::__map_value_compare<int, std::__1::__value_type<int, int>, std::__1::less<int>, true>, std::__1::allocator<std::__1::__value_type<int, int> > >::__construct_node<std::__1::piecewise_construct_t const&, std::__1::tuple<int&&>, std::__1::tuple<> >(std::__1::piecewise_construct_t const&&&, std::__1::tuple<int&&>&&, std::__1::tuple<>&&)
- main
- std::__1::allocator<std::__1::__tree_node<std::__1::__value_type<int, int>, void*> >::allocate(unsigned long, void const*)
- std::__1::__throw_out_of_range(char const*)
- std::__1::allocator_traits<std::__1::allocator<std::__1::__tree_node<std::__1::__value_type<int, int>, void*> > >::allocate(std::__1::allocator<std::__1::__tree_node<std::__1::__value_type<int, int>, void*> >&, unsigned long)
- std::__1::map<int, int, std::__1::less<int>, std::__1::allocator<std::__1::pair<int const, int> > >::operator[](int&&)
- std::__1::pair<std::__1::__tree_iterator<std::__1::__value_type<int, int>, std::__1::__tree_node<std::__1::__value_type<int, int>, void*>*, long>, bool> std::__1::__tree<std::__1::__value_type<int, int>, std::__1::__map_value_compare<int, std::__1::__value_type<int, int>, std::__1::less<int>, true>, std::__1::allocator<std::__1::__value_type<int, int> > >::__emplace_unique_key_args<int, std::__1::piecewise_construct_t const&, std::__1::tuple<int&&>, std::__1::tuple<> >(int const&, std::__1::piecewise_construct_t const&&&, std::__1::tuple<int&&>&&, std::__1::tuple<>&&)

functions that MAY encounter a std::length_error exception:
- std::__1::__throw_length_error(char const*)
- std::__1::unique_ptr<std::__1::__tree_node<std::__1::__value_type<int, int>, void*>, std::__1::__tree_node_destructor<std::__1::allocator<std::__1::__tree_node<std::__1::__value_type<int, int>, void*> > > > std::__1::__tree<std::__1::__value_type<int, int>, std::__1::__map_value_compare<int, std::__1::__value_type<int, int>, std::__1::less<int>, true>, std::__1::allocator<std::__1::__value_type<int, int> > >::__construct_node<std::__1::piecewise_construct_t const&, std::__1::tuple<int&&>, std::__1::tuple<> >(std::__1::piecewise_construct_t const&&&, std::__1::tuple<int&&>&&, std::__1::tuple<>&&)
- main
- std::__1::allocator<std::__1::__tree_node<std::__1::__value_type<int, int>, void*> >::allocate(unsigned long, void const*)
- std::__1::allocator_traits<std::__1::allocator<std::__1::__tree_node<std::__1::__value_type<int, int>, void*> > >::allocate(std::__1::allocator<std::__1::__tree_node<std::__1::__value_type<int, int>, void*> >&, unsigned long)
- std::__1::map<int, int, std::__1::less<int>, std::__1::allocator<std::__1::pair<int const, int> > >::operator[](int&&)
- std::__1::pair<std::__1::__tree_iterator<std::__1::__value_type<int, int>, std::__1::__tree_node<std::__1::__value_type<int, int>, void*>*, long>, bool> std::__1::__tree<std::__1::__value_type<int, int>, std::__1::__map_value_compare<int, std::__1::__value_type<int, int>, std::__1::less<int>, true>, std::__1::allocator<std::__1::__value_type<int, int> > >::__emplace_unique_key_args<int, std::__1::piecewise_construct_t const&, std::__1::tuple<int&&>, std::__1::tuple<> >(int const&, std::__1::piecewise_construct_t const&&&, std::__1::tuple<int&&>&&, std::__1::tuple<>&&)

functions that MAY encounter a std::out_of_range exception:
- std::__1::__throw_out_of_range(char const*)
- std::__1::map<int, int, std::__1::less<int>, std::__1::allocator<std::__1::pair<int const, int> > >::at(int const&) const
- main
- bar(int, std::__1::map<int, int, std::__1::less<int>, std::__1::allocator<std::__1::pair<int const, int> > > const&)

functions that MAY encounter a std::runtime_error exception:
- main
- bar(int, std::__1::map<int, int, std::__1::less<int>, std::__1::allocator<std::__1::pair<int const, int> > > const&)

License

The code is licensed under the MIT License:

Copyright © 2019 Niels Lohmann

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].