Exceptions declaration and static analysis

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi all,

I'm toying with the idea to write a plugin to statically check if a function
throws an undocumented exception.

In C++ the list of exceptions that a function can throw is not part of the
function declaration:

int g(int);

int f(int index) {
    return g(index) + 1;
}

int g(int x) {
    if (x > 10) {
        throw std::out_of_range{""};
    }
    return x;
}

Only the `g()` documentation can tell us that an `out_of_range` exception may
be thrown.

I'm wondering if a custom attribute and a static analyzer can help us; for
example:

int g(int) [[t::throw(std::out_of_range)]];

int f(int index) {
    return g(index) + 1;
}

A static analyzer should be able to detect the problem and emit a warning.
We can either wrap the `g()` call into a try/catch or annotate the
function `f`:

int f(int index) {
    try {
        return g(index) + 1;
    }
    catch(std::out_of_range const&) {
        return 0;
    }
}

int f(int index) [[t::throw(std::out_of_range)]] {
    ...
}

I've spent the last days studying the plugin architecture and the gcc AST,
writing some demos and then restart studying the AST (and again, and again)!

My first attempt hooks the PLUGIN_PRE_GENERICE event and search the function
body for throw_expr nodes, decodes theirs types (following the subtree of
compound and cleanup expressions) and takes note of it.

Obviously the devil is in the details and here there are a tons of details!

So this is a good time to ask for criticism and advice.

Is this a totally waste of time and energy? Or it could be useful (even if not
100% accurate)?

Is `pre_generice` the right event for this work?

Do you have any advices?


Thanks
David





[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux