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

Delesley Hutchins delesley at google.com
Tue Dec 11 14:55:20 PST 2012


Does gcc accept out-of-line declarations of all members?  What are the
restrictions?

On Tue, Dec 11, 2012 at 2:54 PM, Richard Smith <richard at metafoo.co.uk> wrote:
> On Tue, Dec 11, 2012 at 2:52 PM, Delesley Hutchins <delesley at google.com> wrote:
>> But out-of-line declarations are not standard C++, so you wouldn't be
>> able to compile that code with any other compiler, right?
>
> Right, you'd need to remove those declarations in other build
> configurations. GCC accepts them with -fpermissive, FWIW.
>
>> On Tue, Dec 11, 2012 at 2:44 PM, Richard Smith <richard at metafoo.co.uk> wrote:
>>> 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.
>>
>>
>>
>> --
>> DeLesley Hutchins | Software Engineer | delesley at google.com | 505-206-0315



-- 
DeLesley Hutchins | Software Engineer | delesley at google.com | 505-206-0315



More information about the cfe-dev mailing list