[cfe-dev] Attribute parsing at end of translation unit?

Richard Smith richard at metafoo.co.uk
Tue Dec 11 14:44:38 PST 2012


On Mon, Dec 10, 2012 at 3:07 PM, Delesley Hutchins <delesley at google.com> wrote:
> I am considering changing the way in which thread safety attributes
> are parsed, but I wanted to get some feedback.
>
> Thread safety analysis currently parses attributes at the end of the
> class where they occur, just like method bodies.  However, this
> strategy can lead to situations where there is no valid way to
> annotate a piece of code.  Here's a toy example:
>
>
> class Graph;
>
> class Node {
> public:
>   // error: Graph has incomplete type.
>   void reachable(Graph* g, int nodeID) SHARED_LOCKS_REQUIRED(g->mu_);
>
> private:
>   // error: Graph has incomplete type.
>   std::vector<int> edges_ GUARDED_BY(&Graph::mu_);
> };
>
>
> class Graph {
>   friend class Node;
>
>   Mutex mu_;
>   std::vector<Node> nodes_ GUARDED_BY(mu_);
> };
>
>
> There's a circular dependency here between Graph and Node.  You could
> try to resolve the dependency by refactoring, but the usual ways of
> breaking dependencies in C++ don't work with attributes.  The body of
> a method can be defined outside of its class, but the attribute can't;
> there's no way to redeclare a method (or a data member) with a new
> attribute.

We could allow out-of-line declarations for members if they contain
any thread-safety attributes.



More information about the cfe-dev mailing list