Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 316230

Summary: Need representation of gcc attributes in AST
Product: [Tools] CDT Reporter: Elena Laskavaia <elaskavaia.cdt>
Component: cdt-parserAssignee: Sergey Prigogin <eclipse.sprigogin>
Status: RESOLVED FIXED QA Contact: Mike Kucera <mikekucera>
Severity: normal    
Priority: P3 CC: eclipse.sprigogin, paedu.hofer, yevshif
Version: 7.0Flags: eclipse.sprigogin: review? (mschorn.eclipse)
Target Milestone: 8.1.0   
Hardware: PC   
OS: Windows 7   
Whiteboard:
Bug Depends on:    
Bug Blocks: 316076, 330635    

Description Elena Laskavaia CLA 2010-06-08 20:30:03 EDT
For purposes of static analysis (see bug 316076) we need support of
gcc attributes in AST. I.e. API to query the attributes of functions and variables. For example

void f() __attribute__((noreturn));

bool test() {
  f();
}

I want to know that f has attribute "noreturn".
Comment 1 Markus Schorn CLA 2010-06-09 05:50:02 EDT
C++0x specifies support for c++-attributes (with a different syntax). We need to model the AST along this specification and can (that's my hope) treat the __attribute__ syntax as a special case.
See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2761.pdf
Comment 2 Sergey Prigogin CLA 2012-04-09 14:47:00 EDT
The proposed fix is in https://git.eclipse.org/r/#/c/5554/. Markus could you please review IASTGCCAttribute and parser changes. Thanks.
Comment 3 Markus Schorn CLA 2012-04-11 04:48:57 EDT
(In reply to comment #2)
> The proposed fix is in https://git.eclipse.org/r/#/c/5554/. Markus could you
> please review IASTGCCAttribute and parser changes. Thanks.

I have some recommondations: 
1) The new standard for c++ defines attributes that are more general than your interface (they allow for a balanced token sequence rather than an expression). We should model the attributes of the new standard. Gcc attributes are a special case of those. 
2) I am uncertain on whether the name of an attribute shall be represented with an IASTName. A simple string might be sufficient. 
3) Attributes can be used in quite a lot of places (the new standard provides a definition for c++). I suggest to introduce a new interface (IASTAttributeOwner) similar to IASTImplicitNameOwner, which can be implemented by IASTDeclarator but also by ICPPASTNamespace, .... It'd be certainly best to provide the attributes for all places where they can be used. 
4)It'd be good to support the new c++11 syntax for attributes, however this may be a separate effort.
Comment 4 Sergey Prigogin CLA 2012-04-12 21:53:50 EDT
(In reply to comment #3)
I've updated the proposed fix in Gerrit (https://git.eclipse.org/r/#/c/5554/).
> 1) The new standard for c++ defines attributes that are more general than your
> interface (they allow for a balanced token sequence rather than an expression).
> We should model the attributes of the new standard. Gcc attributes are a
> special case of those. 

Done.

> 2) I am uncertain on whether the name of an attribute shall be represented with
> an IASTName. A simple string might be sufficient. 

Changed to char[]. Initially I got confused by qualified attribute names.

> 3) Attributes can be used in quite a lot of places (the new standard provides a
> definition for c++). I suggest to introduce a new interface
> (IASTAttributeOwner) similar to IASTImplicitNameOwner, which can be implemented
> by IASTDeclarator but also by ICPPASTNamespace, .... It'd be certainly best to
> provide the attributes for all places where they can be used. 

I've added IASTAttributeOwner interface and ASTAttributeOwner abstract class to make implementation of the interface easier. My attempt to add attributes to all places where they are allowed encountered some difficulties:

1. There seem to be differences between places where GCC-style and C++11-style attributes are allowed. In the GCC case it's not always clear what the attribute applies to.
2. Current structure of the parser code makes it difficult to propagate attributes from the place where they are parsed to the place where they can be applied to their owner node.
3. I'm not sure how to treat attributes in CASTAmbiguousStatement, CPPASTAmbiguousStatement and CPPASTAmbiguousDeclaration classes.
4. It's not clear how to order visiting of attributes relative to their sibling nodes. I'm currently visiting them before their siblings, which matches the C++11 syntax, but it means that nodes are not visited in a monotonic offset order when GCC attributes are present.

> 4)It'd be good to support the new c++11 syntax for attributes, however this
> may be a separate effort.

This is indeed a separate effort. The library of GCC 4.6 doesn't yet use C++11-style attributes.
Comment 5 Sergey Prigogin CLA 2012-04-13 15:17:09 EDT
Fixed in master.