https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69125

            Bug ID: 69125
           Summary: [6 regression] Please don't print "‘visibility’
                    attribute ignored" when setting hidden visibility for
                    anonymous namespace classes
           Product: gcc
           Version: 6.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: thiago at kde dot org
  Target Milestone: ---

The following code compiled without warnings in GCC < 6 and now prints a
warning:

$ cat /tmp/test.cpp 
#define Q_OBJECT \
    __attribute__((visibility("hidden"))) static void qt_static_metacall();

namespace {
class A
{
    Q_OBJECT
};
}

$ ~/gcc6/bin/gcc -O2 -c /tmp/test.cpp 
/tmp/test.cpp:2:74: warning: ‘visibility’ attribute ignored [-Wattributes]
     __attribute__((visibility("hidden"))) static void qt_static_metacall();
                                                                          ^

/tmp/test.cpp:7:5: note: in expansion of macro ‘Q_OBJECT’
     Q_OBJECT
     ^~~~~~~~

IIUC, classes in anonymous namespaces are getting internal linkage, which means
the visibility attribute is superfluous.

But the Q_OBJECT macro needs to have that __attribute__. We would rather not
pessimise all public classes to suppress this warning.

Alternatively, please provide a way to suppress the warning in the macro. I
tried using _Pragma but the warning remains:

#define Q_OBJECT \
    _Pragma("GCC diagnostic ignored \"-Wattributes\"") \
    __attribute__((visibility("hidden"))) static void qt_static_metacall();

Note that with this definition, the preprocessed source does not produce the
warning:

$ ~/gcc6/bin/gcc -E /tmp/test.cpp | ~/gcc6/bin/gcc -O0 -c -xc++ -

Reply via email to