[cfe-dev] Possible incompatible gcc attribute parsing with GCC

Xiaolong Tang xiaolong.snake at gmail.com
Fri Jul 1 05:37:40 PDT 2011


Hi Eli, 

> >
> > Hi all,
> >
> > I am trying to see how far clang supports GNU attributes. It seems to
> > me that clang does not support declare attributes for members of
> > templates. Following is what I found:
> >
> > I use a very simple test code (in template_attribute.cpp):
> >
> >  template<typename T> class vector {
> >  private:
> >    T rep[10];
> >  public:
> >    T top() { return rep[0]; }
> >  };
> >
> >  template<typename T> T vector<T>::top() __attribute__ ((pure));
> >
> >  int main()
> >  {
> >    vector<int> a;
> >    int r = a.top();
> >    int t = a.top();
> >    return r + t;
> >  }
> >
> > With this command:
> >
> >  g++ -O2 -cse -fno-inline -fdump-tree-all-all template_attribute.cpp
> >
> > the above code gets compiled and optimized (CSE works here).
> >
> > However, trying to compile the code with clang generates the following
> > message:
> >
> >  16:15:55->clang++ -c template_attribute.cpp
> >
> >  template_attribute.cpp:9:35: error: out-of-line declaration of a member must be
> >        a definition [-Wout-of-line-declaration]
> >  template<typename T> T vector<T>::top() __attribute__ ((pure));
> >                         ~~~~~~~~~~~^
> >  template_attribute.cpp:9:35: warning: attribute declaration must precede
> >        definition
> >  template_attribute.cpp:5:5: note: previous definition is here
> >    T top() { return rep[0]; }
> >      ^
> >  1 warning and 1 error generated.
> >
> > PS: clang version 2.9 (trunk 126848)
> 
> Like the error says, your template declaration is at the very least
> strange... the more usual way to do what you're trying to do is to
> change the definition to "__attribute__ ((pure)) T top() { return
> rep[0]; }".
> 


Maybe I am abusing the attributes construct. However, we may need the
ability to specify additional attributes for procedures outside the
definition of classes in some cases, e.g., when the user who declares
attributes for a class's procedures is not the class designer.

Best, 
Xiaolong




More information about the cfe-dev mailing list