Fix unused static symbols so they're not dropped by clang

Message ID CAENS6Es8jHe=xp1Ca0WiZ2dg2JNqJ5JYLP89CCBGWWqPQ20rrQ@mail.gmail.com
State Committed
Headers

Commit Message

David Blaikie April 11, 2014, 6:51 a.m. UTC
  Several tests used file-static functions and variables that were not
referenced by the code. Even at -O0, clang omits these entities at the
frontend so the tests fail.

Since it doesn't look like these tests needed this functionality for
what they were testing, I've modified the variables/functions to
either be non-static, or marked them with __attribute__((used)).

If it's preferred that I use the attribute more pervasively, rather
than just making the entities non-static, I can provide a patch for
that (or some other preferred solution). There's certainly precedent
for both (non-static entities and __attribute__((used)) in the
testsuite already and much more of the former than the latter).

I have commit-after-review access, so just looking for sign-off here.

Thanks,
- David
commit d72b042cbd4f0af6d2e4ac6901445677b93f77ee
Author: David Blaikie <dblaikie@gmail.com>
Date:   Thu Apr 10 23:45:28 2014 -0700

    Ensure unreferenced static symbols aren't omitted by clang (either marking them __attribute__((used)) or making them non-static)
    
    gdb/testsuite/
           * gdb.base/catch-syscall.c: Make unreferenced statics non-static to
           ensure clang would not discard them.
           * gdb.base/gdbvars.c: Ditto.
           * gdb.base/memattr.c: Ditto.
           * gdb.base/whatis.c: Ditto.
           * gdb.python/py-prettyprint.c: Ditto.
           * gdb.trace/actions.c: Ditto.
           * gdb.cp/ptype-cv-cp.cc: Mark unused global const int as used to
           ensure clang would not discard it.
  

Comments

Andrew Pinski April 11, 2014, 7:03 a.m. UTC | #1
> On Apr 10, 2014, at 11:51 PM, David Blaikie <dblaikie@gmail.com> wrote:
> 
> Several tests used file-static functions and variables that were not
> referenced by the code. Even at -O0, clang omits these entities at the
> frontend so the tests fail.

I think clang should change here rather than the testsuite of gdb. Unused static functions make sense to be kept around at -O0 for debugging reasons. 

Thanks,
Andrew


> 
> Since it doesn't look like these tests needed this functionality for
> what they were testing, I've modified the variables/functions to
> either be non-static, or marked them with __attribute__((used)).
> 
> If it's preferred that I use the attribute more pervasively, rather
> than just making the entities non-static, I can provide a patch for
> that (or some other preferred solution). There's certainly precedent
> for both (non-static entities and __attribute__((used)) in the
> testsuite already and much more of the former than the latter).
> 
> I have commit-after-review access, so just looking for sign-off here.
> 
> Thanks,
> - David
> <unused.diff>
  
Doug Evans April 11, 2014, 6:23 p.m. UTC | #2
On Thu, Apr 10, 2014 at 11:51 PM, David Blaikie <dblaikie@gmail.com> wrote:
> Several tests used file-static functions and variables that were not
> referenced by the code. Even at -O0, clang omits these entities at the
> frontend so the tests fail.
>
> Since it doesn't look like these tests needed this functionality for
> what they were testing, I've modified the variables/functions to
> either be non-static, or marked them with __attribute__((used)).
>
> If it's preferred that I use the attribute more pervasively, rather
> than just making the entities non-static, I can provide a patch for
> that (or some other preferred solution). There's certainly precedent
> for both (non-static entities and __attribute__((used)) in the
> testsuite already and much more of the former than the latter).
>
> I have commit-after-review access, so just looking for sign-off here.

Yikes.

This is becoming more and more painful (not your fault of course!).
I can imagine this being a never ending source of regressions.

Does clang perchance have a -O0-and-yes-I-really-mean-O0 option?
  
Doug Evans April 11, 2014, 7:32 p.m. UTC | #3
On Fri, Apr 11, 2014 at 11:23 AM, Doug Evans <dje@google.com> wrote:
> On Thu, Apr 10, 2014 at 11:51 PM, David Blaikie <dblaikie@gmail.com> wrote:
>> Several tests used file-static functions and variables that were not
>> referenced by the code. Even at -O0, clang omits these entities at the
>> frontend so the tests fail.
>>
>> Since it doesn't look like these tests needed this functionality for
>> what they were testing, I've modified the variables/functions to
>> either be non-static, or marked them with __attribute__((used)).
>>
>> If it's preferred that I use the attribute more pervasively, rather
>> than just making the entities non-static, I can provide a patch for
>> that (or some other preferred solution). There's certainly precedent
>> for both (non-static entities and __attribute__((used)) in the
>> testsuite already and much more of the former than the latter).
>>
>> I have commit-after-review access, so just looking for sign-off here.
>
> Yikes.
>
> This is becoming more and more painful (not your fault of course!).
> I can imagine this being a never ending source of regressions.
>
> Does clang perchance have a -O0-and-yes-I-really-mean-O0 option?

Failing that,

making the entries non-static without adding a comment to explain why
things are the way they are will leave things in a more fragile state,
and if we're going to add a comment we might just as well use an
attribute throughout I guess.
However using the attribute is, technically, more complicated than
that because we shouldn't unnecessarily break testing with other
compilers.
That suggests putting the attribute in a macro in a header protected
by appropriate #ifdefs.
The testsuite doesn't yet have a single location for such headers
(testsuite/include or some such, though there is already testsuite/lib
(cough) but if it's just for the one header ...).
  
David Blaikie April 11, 2014, 8:16 p.m. UTC | #4
On Fri, Apr 11, 2014 at 12:32 PM, Doug Evans <dje@google.com> wrote:
> On Fri, Apr 11, 2014 at 11:23 AM, Doug Evans <dje@google.com> wrote:
>> On Thu, Apr 10, 2014 at 11:51 PM, David Blaikie <dblaikie@gmail.com> wrote:
>>> Several tests used file-static functions and variables that were not
>>> referenced by the code. Even at -O0, clang omits these entities at the
>>> frontend so the tests fail.
>>>
>>> Since it doesn't look like these tests needed this functionality for
>>> what they were testing, I've modified the variables/functions to
>>> either be non-static, or marked them with __attribute__((used)).
>>>
>>> If it's preferred that I use the attribute more pervasively, rather
>>> than just making the entities non-static, I can provide a patch for
>>> that (or some other preferred solution). There's certainly precedent
>>> for both (non-static entities and __attribute__((used)) in the
>>> testsuite already and much more of the former than the latter).
>>>
>>> I have commit-after-review access, so just looking for sign-off here.
>>
>> Yikes.
>>
>> This is becoming more and more painful (not your fault of course!).
>> I can imagine this being a never ending source of regressions.
>>
>> Does clang perchance have a -O0-and-yes-I-really-mean-O0 option?
>
> Failing that,
>
> making the entries non-static without adding a comment to explain why
> things are the way they are will leave things in a more fragile state,

If people only ever test with GCC, yes. Though to a degree I'm happy
enough carrying the burden of providing patches to cleanup test cases
people commit that break clang. We're going to do this anyway for
other sources of breakage, I don't /think/ this particular kind of
breakage would be especially more egregious (possibly more common, but
providing a patch every few months doesn't sound like the end of the
world to me)

Though I agree that it's slightly subtle to make them non-static with
no comment.

> and if we're going to add a comment we might just as well use an
> attribute throughout I guess.
> However using the attribute is, technically, more complicated than
> that because we shouldn't unnecessarily break testing with other
> compilers.

While some test cases use #ifdefs, there are several test cases that
already use __attribute__((used)) unconditionally... so I'm not sure
if there's a problem adding more. But perhaps I misunderstand the
priority/need here.

> That suggests putting the attribute in a macro in a header protected
> by appropriate #ifdefs.
> The testsuite doesn't yet have a single location for such headers
> (testsuite/include or some such, though there is already testsuite/lib
> (cough) but if it's just for the one header ...).
  
David Blaikie April 11, 2014, 8:17 p.m. UTC | #5
On Fri, Apr 11, 2014 at 11:23 AM, Doug Evans <dje@google.com> wrote:
> On Thu, Apr 10, 2014 at 11:51 PM, David Blaikie <dblaikie@gmail.com> wrote:
>> Several tests used file-static functions and variables that were not
>> referenced by the code. Even at -O0, clang omits these entities at the
>> frontend so the tests fail.
>>
>> Since it doesn't look like these tests needed this functionality for
>> what they were testing, I've modified the variables/functions to
>> either be non-static, or marked them with __attribute__((used)).
>>
>> If it's preferred that I use the attribute more pervasively, rather
>> than just making the entities non-static, I can provide a patch for
>> that (or some other preferred solution). There's certainly precedent
>> for both (non-static entities and __attribute__((used)) in the
>> testsuite already and much more of the former than the latter).
>>
>> I have commit-after-review access, so just looking for sign-off here.
>
> Yikes.
>
> This is becoming more and more painful (not your fault of course!).
> I can imagine this being a never ending source of regressions.
>
> Does clang perchance have a -O0-and-yes-I-really-mean-O0 option?

Sort of. It does have -femit-all-decls, which, though poorly named,
causes clang to produce definitions for unused static entities and
even unused inline functions (which GCC doesn't do). I'm not sure
where we'd build in passing that flag from the GCC test suite. I'd
rather not have "clang passes the test suite only if you pass this
carefully chosen set of flags".
  
Andrew Pinski April 12, 2014, 4 a.m. UTC | #6
On Fri, Apr 11, 2014 at 1:17 PM, David Blaikie <dblaikie@gmail.com> wrote:
> On Fri, Apr 11, 2014 at 11:23 AM, Doug Evans <dje@google.com> wrote:
>> On Thu, Apr 10, 2014 at 11:51 PM, David Blaikie <dblaikie@gmail.com> wrote:
>>> Several tests used file-static functions and variables that were not
>>> referenced by the code. Even at -O0, clang omits these entities at the
>>> frontend so the tests fail.
>>>
>>> Since it doesn't look like these tests needed this functionality for
>>> what they were testing, I've modified the variables/functions to
>>> either be non-static, or marked them with __attribute__((used)).
>>>
>>> If it's preferred that I use the attribute more pervasively, rather
>>> than just making the entities non-static, I can provide a patch for
>>> that (or some other preferred solution). There's certainly precedent
>>> for both (non-static entities and __attribute__((used)) in the
>>> testsuite already and much more of the former than the latter).
>>>
>>> I have commit-after-review access, so just looking for sign-off here.
>>
>> Yikes.
>>
>> This is becoming more and more painful (not your fault of course!).
>> I can imagine this being a never ending source of regressions.
>>
>> Does clang perchance have a -O0-and-yes-I-really-mean-O0 option?
>
> Sort of. It does have -femit-all-decls, which, though poorly named,
> causes clang to produce definitions for unused static entities and
> even unused inline functions (which GCC doesn't do).

By default GCC does not keep unused inline functions but there is an
option for that -fkeep-inline-functions.


Thanks,
Andrew Pinski

> I'm not sure
> where we'd build in passing that flag from the GCC test suite. I'd
> rather not have "clang passes the test suite only if you pass this
> carefully chosen set of flags".
  
David Blaikie April 13, 2014, 7:11 a.m. UTC | #7
On Fri, Apr 11, 2014 at 9:00 PM, Andrew Pinski <pinskia@gmail.com> wrote:
> On Fri, Apr 11, 2014 at 1:17 PM, David Blaikie <dblaikie@gmail.com> wrote:
>> On Fri, Apr 11, 2014 at 11:23 AM, Doug Evans <dje@google.com> wrote:
>>> On Thu, Apr 10, 2014 at 11:51 PM, David Blaikie <dblaikie@gmail.com> wrote:
>>>> Several tests used file-static functions and variables that were not
>>>> referenced by the code. Even at -O0, clang omits these entities at the
>>>> frontend so the tests fail.
>>>>
>>>> Since it doesn't look like these tests needed this functionality for
>>>> what they were testing, I've modified the variables/functions to
>>>> either be non-static, or marked them with __attribute__((used)).
>>>>
>>>> If it's preferred that I use the attribute more pervasively, rather
>>>> than just making the entities non-static, I can provide a patch for
>>>> that (or some other preferred solution). There's certainly precedent
>>>> for both (non-static entities and __attribute__((used)) in the
>>>> testsuite already and much more of the former than the latter).
>>>>
>>>> I have commit-after-review access, so just looking for sign-off here.
>>>
>>> Yikes.
>>>
>>> This is becoming more and more painful (not your fault of course!).
>>> I can imagine this being a never ending source of regressions.
>>>
>>> Does clang perchance have a -O0-and-yes-I-really-mean-O0 option?
>>
>> Sort of. It does have -femit-all-decls, which, though poorly named,
>> causes clang to produce definitions for unused static entities and
>> even unused inline functions (which GCC doesn't do).
>
> By default GCC does not keep unused inline functions but there is an
> option for that -fkeep-inline-functions.

Ah, good to know.

My point was that the GDB test suite passes without enabling that flag
for GCC and I think that's somewhat akin to having the suite passable
without having to add -femit-all-decls for Clang. I realize, of
course, that most GDB developers won't be running the test suite with
Clang, but I'm happy to contribute patches when this comes up from
time to time. It's certainly not a pervasive habit across the test
suite to keep everything static - just this handful of tests happen to
do it.

But I'm open to whatever you folks think is the best approach - if
that means Clang only passes the suite when passing particular flags,
so be it. Perhaps there'd be a way we could build that knowledge into
the testsuite itself so that GDB developers who want to use Clang
don't have to duplicate those details locally.

- David
  
Doug Evans April 14, 2014, 10:56 p.m. UTC | #8
On Sun, Apr 13, 2014 at 12:11 AM, David Blaikie <dblaikie@gmail.com> wrote:
> On Fri, Apr 11, 2014 at 9:00 PM, Andrew Pinski <pinskia@gmail.com> wrote:
>> On Fri, Apr 11, 2014 at 1:17 PM, David Blaikie <dblaikie@gmail.com> wrote:
>>> On Fri, Apr 11, 2014 at 11:23 AM, Doug Evans <dje@google.com> wrote:
>>>> On Thu, Apr 10, 2014 at 11:51 PM, David Blaikie <dblaikie@gmail.com> wrote:
>>>>> Several tests used file-static functions and variables that were not
>>>>> referenced by the code. Even at -O0, clang omits these entities at the
>>>>> frontend so the tests fail.
>>>>>
>>>>> Since it doesn't look like these tests needed this functionality for
>>>>> what they were testing, I've modified the variables/functions to
>>>>> either be non-static, or marked them with __attribute__((used)).
>>>>>
>>>>> If it's preferred that I use the attribute more pervasively, rather
>>>>> than just making the entities non-static, I can provide a patch for
>>>>> that (or some other preferred solution). There's certainly precedent
>>>>> for both (non-static entities and __attribute__((used)) in the
>>>>> testsuite already and much more of the former than the latter).
>>>>>
>>>>> I have commit-after-review access, so just looking for sign-off here.
>>>>
>>>> Yikes.
>>>>
>>>> This is becoming more and more painful (not your fault of course!).
>>>> I can imagine this being a never ending source of regressions.
>>>>
>>>> Does clang perchance have a -O0-and-yes-I-really-mean-O0 option?
>>>
>>> Sort of. It does have -femit-all-decls, which, though poorly named,
>>> causes clang to produce definitions for unused static entities and
>>> even unused inline functions (which GCC doesn't do).
>>
>> By default GCC does not keep unused inline functions but there is an
>> option for that -fkeep-inline-functions.
>
> Ah, good to know.
>
> My point was that the GDB test suite passes without enabling that flag
> for GCC and I think that's somewhat akin to having the suite passable
> without having to add -femit-all-decls for Clang. I realize, of
> course, that most GDB developers won't be running the test suite with
> Clang, but I'm happy to contribute patches when this comes up from
> time to time. It's certainly not a pervasive habit across the test
> suite to keep everything static - just this handful of tests happen to
> do it.
>
> But I'm open to whatever you folks think is the best approach - if
> that means Clang only passes the suite when passing particular flags,
> so be it. Perhaps there'd be a way we could build that knowledge into
> the testsuite itself so that GDB developers who want to use Clang
> don't have to duplicate those details locally.

I don't have a strong preference other than trying to keep things maintainable.

Maybe it would be enough to document the issue in the testsuite coding
standards section of the manual.  This is a really subtle portability
issue though ... *something* in the code would be nice.
  
David Blaikie April 15, 2014, 3:24 a.m. UTC | #9
On Mon, Apr 14, 2014 at 3:56 PM, Doug Evans <dje@google.com> wrote:
> On Sun, Apr 13, 2014 at 12:11 AM, David Blaikie <dblaikie@gmail.com> wrote:
>> On Fri, Apr 11, 2014 at 9:00 PM, Andrew Pinski <pinskia@gmail.com> wrote:
>>> On Fri, Apr 11, 2014 at 1:17 PM, David Blaikie <dblaikie@gmail.com> wrote:
>>>> On Fri, Apr 11, 2014 at 11:23 AM, Doug Evans <dje@google.com> wrote:
>>>>> On Thu, Apr 10, 2014 at 11:51 PM, David Blaikie <dblaikie@gmail.com> wrote:
>>>>>> Several tests used file-static functions and variables that were not
>>>>>> referenced by the code. Even at -O0, clang omits these entities at the
>>>>>> frontend so the tests fail.
>>>>>>
>>>>>> Since it doesn't look like these tests needed this functionality for
>>>>>> what they were testing, I've modified the variables/functions to
>>>>>> either be non-static, or marked them with __attribute__((used)).
>>>>>>
>>>>>> If it's preferred that I use the attribute more pervasively, rather
>>>>>> than just making the entities non-static, I can provide a patch for
>>>>>> that (or some other preferred solution). There's certainly precedent
>>>>>> for both (non-static entities and __attribute__((used)) in the
>>>>>> testsuite already and much more of the former than the latter).
>>>>>>
>>>>>> I have commit-after-review access, so just looking for sign-off here.
>>>>>
>>>>> Yikes.
>>>>>
>>>>> This is becoming more and more painful (not your fault of course!).
>>>>> I can imagine this being a never ending source of regressions.
>>>>>
>>>>> Does clang perchance have a -O0-and-yes-I-really-mean-O0 option?
>>>>
>>>> Sort of. It does have -femit-all-decls, which, though poorly named,
>>>> causes clang to produce definitions for unused static entities and
>>>> even unused inline functions (which GCC doesn't do).
>>>
>>> By default GCC does not keep unused inline functions but there is an
>>> option for that -fkeep-inline-functions.
>>
>> Ah, good to know.
>>
>> My point was that the GDB test suite passes without enabling that flag
>> for GCC and I think that's somewhat akin to having the suite passable
>> without having to add -femit-all-decls for Clang. I realize, of
>> course, that most GDB developers won't be running the test suite with
>> Clang, but I'm happy to contribute patches when this comes up from
>> time to time. It's certainly not a pervasive habit across the test
>> suite to keep everything static - just this handful of tests happen to
>> do it.
>>
>> But I'm open to whatever you folks think is the best approach - if
>> that means Clang only passes the suite when passing particular flags,
>> so be it. Perhaps there'd be a way we could build that knowledge into
>> the testsuite itself so that GDB developers who want to use Clang
>> don't have to duplicate those details locally.
>
> I don't have a strong preference other than trying to keep things maintainable.
>
> Maybe it would be enough to document the issue in the testsuite coding
> standards section of the manual.  This is a really subtle portability
> issue though ... *something* in the code would be nice.

Given that there are, I assume, many test cases that use unused
non-static functions, the functions after my patch will look just like
those. It'd be weird to comment some but not all of them.

But my initial plan had been to put __attribute__((used))
everywhere... I could still do that, if preferred, but I assume it'll
be woefully inconsistent/arbitrary with some tests using "static
__attribute__((used))" and others using non-static functions anyway. I
suppose the presence of a smattering of static+attribute cases would
remind people to do this in cases where they want/need to make the
entity static, but I'm not sure how effective this would be.

So:

1) Use non-static entities (patch already provided)
2) Use __attribute__((used)) (macro'd at the start of each file? in a
common header? protected under #ifdefs or not (there seem to be a
variety of attributes and gnu-isms not protected by #ifdefs, and some
that are)?)
3) Require Clang run the test suite with non-default flags.
  -> preferably with some auto-detection in the test suite to add
those flags whenever running with clang

Are there other options to consider? (I suppose comments rather than
attributes (2) would be an alternative - "this thing is non-static so
Clang will preserve it")
  
Doug Evans April 23, 2014, 9:50 p.m. UTC | #10
David Blaikie writes:
 > On Mon, Apr 14, 2014 at 3:56 PM, Doug Evans <dje@google.com> wrote:
 > > On Sun, Apr 13, 2014 at 12:11 AM, David Blaikie <dblaikie@gmail.com> wrote:
 > >> On Fri, Apr 11, 2014 at 9:00 PM, Andrew Pinski <pinskia@gmail.com> wrote:
 > >>> On Fri, Apr 11, 2014 at 1:17 PM, David Blaikie <dblaikie@gmail.com> wrote:
 > >>>> On Fri, Apr 11, 2014 at 11:23 AM, Doug Evans <dje@google.com> wrote:
 > >>>>> On Thu, Apr 10, 2014 at 11:51 PM, David Blaikie <dblaikie@gmail.com> wrote:
 > >>>>>> Several tests used file-static functions and variables that were not
 > >>>>>> referenced by the code. Even at -O0, clang omits these entities at the
 > >>>>>> frontend so the tests fail.
 > >>>>>>
 > >>>>>> Since it doesn't look like these tests needed this functionality for
 > >>>>>> what they were testing, I've modified the variables/functions to
 > >>>>>> either be non-static, or marked them with __attribute__((used)).
 > >>>>>>
 > >>>>>> If it's preferred that I use the attribute more pervasively, rather
 > >>>>>> than just making the entities non-static, I can provide a patch for
 > >>>>>> that (or some other preferred solution). There's certainly precedent
 > >>>>>> for both (non-static entities and __attribute__((used)) in the
 > >>>>>> testsuite already and much more of the former than the latter).
 > >>>>>>
 > >>>>>> I have commit-after-review access, so just looking for sign-off here.
 > >>>>>
 > >>>>> Yikes.
 > >>>>>
 > >>>>> This is becoming more and more painful (not your fault of course!).
 > >>>>> I can imagine this being a never ending source of regressions.
 > >>>>>
 > >>>>> Does clang perchance have a -O0-and-yes-I-really-mean-O0 option?
 > >>>>
 > >>>> Sort of. It does have -femit-all-decls, which, though poorly named,
 > >>>> causes clang to produce definitions for unused static entities and
 > >>>> even unused inline functions (which GCC doesn't do).
 > >>>
 > >>> By default GCC does not keep unused inline functions but there is an
 > >>> option for that -fkeep-inline-functions.
 > >>
 > >> Ah, good to know.
 > >>
 > >> My point was that the GDB test suite passes without enabling that flag
 > >> for GCC and I think that's somewhat akin to having the suite passable
 > >> without having to add -femit-all-decls for Clang. I realize, of
 > >> course, that most GDB developers won't be running the test suite with
 > >> Clang, but I'm happy to contribute patches when this comes up from
 > >> time to time. It's certainly not a pervasive habit across the test
 > >> suite to keep everything static - just this handful of tests happen to
 > >> do it.
 > >>
 > >> But I'm open to whatever you folks think is the best approach - if
 > >> that means Clang only passes the suite when passing particular flags,
 > >> so be it. Perhaps there'd be a way we could build that knowledge into
 > >> the testsuite itself so that GDB developers who want to use Clang
 > >> don't have to duplicate those details locally.
 > >
 > > I don't have a strong preference other than trying to keep things maintainable.
 > >
 > > Maybe it would be enough to document the issue in the testsuite coding
 > > standards section of the manual.  This is a really subtle portability
 > > issue though ... *something* in the code would be nice.
 > 
 > Given that there are, I assume, many test cases that use unused
 > non-static functions, the functions after my patch will look just like
 > those. It'd be weird to comment some but not all of them.
 > 
 > But my initial plan had been to put __attribute__((used))
 > everywhere... I could still do that, if preferred, but I assume it'll
 > be woefully inconsistent/arbitrary with some tests using "static
 > __attribute__((used))" and others using non-static functions anyway. I
 > suppose the presence of a smattering of static+attribute cases would
 > remind people to do this in cases where they want/need to make the
 > entity static, but I'm not sure how effective this would be.
 > 
 > So:
 > 
 > 1) Use non-static entities (patch already provided)
 > 2) Use __attribute__((used)) (macro'd at the start of each file? in a
 > common header? protected under #ifdefs or not (there seem to be a
 > variety of attributes and gnu-isms not protected by #ifdefs, and some
 > that are)?)
 > 3) Require Clang run the test suite with non-default flags.
 >   -> preferably with some auto-detection in the test suite to add
 > those flags whenever running with clang
 > 
 > Are there other options to consider? (I suppose comments rather than
 > attributes (2) would be an alternative - "this thing is non-static so
 > Clang will preserve it")

Let's go with (1) but add something to the wiki documenting the issue.

https://sourceware.org/gdb/wiki/Internals%20GDB-Testsuite-Coding-Standards

I'll do the latter.
  
David Blaikie April 25, 2014, 5:35 a.m. UTC | #11
On Wed, Apr 23, 2014 at 2:50 PM, Doug Evans <dje@google.com> wrote:
> David Blaikie writes:
>  > On Mon, Apr 14, 2014 at 3:56 PM, Doug Evans <dje@google.com> wrote:
>  > > On Sun, Apr 13, 2014 at 12:11 AM, David Blaikie <dblaikie@gmail.com> wrote:
>  > >> On Fri, Apr 11, 2014 at 9:00 PM, Andrew Pinski <pinskia@gmail.com> wrote:
>  > >>> On Fri, Apr 11, 2014 at 1:17 PM, David Blaikie <dblaikie@gmail.com> wrote:
>  > >>>> On Fri, Apr 11, 2014 at 11:23 AM, Doug Evans <dje@google.com> wrote:
>  > >>>>> On Thu, Apr 10, 2014 at 11:51 PM, David Blaikie <dblaikie@gmail.com> wrote:
>  > >>>>>> Several tests used file-static functions and variables that were not
>  > >>>>>> referenced by the code. Even at -O0, clang omits these entities at the
>  > >>>>>> frontend so the tests fail.
>  > >>>>>>
>  > >>>>>> Since it doesn't look like these tests needed this functionality for
>  > >>>>>> what they were testing, I've modified the variables/functions to
>  > >>>>>> either be non-static, or marked them with __attribute__((used)).
>  > >>>>>>
>  > >>>>>> If it's preferred that I use the attribute more pervasively, rather
>  > >>>>>> than just making the entities non-static, I can provide a patch for
>  > >>>>>> that (or some other preferred solution). There's certainly precedent
>  > >>>>>> for both (non-static entities and __attribute__((used)) in the
>  > >>>>>> testsuite already and much more of the former than the latter).
>  > >>>>>>
>  > >>>>>> I have commit-after-review access, so just looking for sign-off here.
>  > >>>>>
>  > >>>>> Yikes.
>  > >>>>>
>  > >>>>> This is becoming more and more painful (not your fault of course!).
>  > >>>>> I can imagine this being a never ending source of regressions.
>  > >>>>>
>  > >>>>> Does clang perchance have a -O0-and-yes-I-really-mean-O0 option?
>  > >>>>
>  > >>>> Sort of. It does have -femit-all-decls, which, though poorly named,
>  > >>>> causes clang to produce definitions for unused static entities and
>  > >>>> even unused inline functions (which GCC doesn't do).
>  > >>>
>  > >>> By default GCC does not keep unused inline functions but there is an
>  > >>> option for that -fkeep-inline-functions.
>  > >>
>  > >> Ah, good to know.
>  > >>
>  > >> My point was that the GDB test suite passes without enabling that flag
>  > >> for GCC and I think that's somewhat akin to having the suite passable
>  > >> without having to add -femit-all-decls for Clang. I realize, of
>  > >> course, that most GDB developers won't be running the test suite with
>  > >> Clang, but I'm happy to contribute patches when this comes up from
>  > >> time to time. It's certainly not a pervasive habit across the test
>  > >> suite to keep everything static - just this handful of tests happen to
>  > >> do it.
>  > >>
>  > >> But I'm open to whatever you folks think is the best approach - if
>  > >> that means Clang only passes the suite when passing particular flags,
>  > >> so be it. Perhaps there'd be a way we could build that knowledge into
>  > >> the testsuite itself so that GDB developers who want to use Clang
>  > >> don't have to duplicate those details locally.
>  > >
>  > > I don't have a strong preference other than trying to keep things maintainable.
>  > >
>  > > Maybe it would be enough to document the issue in the testsuite coding
>  > > standards section of the manual.  This is a really subtle portability
>  > > issue though ... *something* in the code would be nice.
>  >
>  > Given that there are, I assume, many test cases that use unused
>  > non-static functions, the functions after my patch will look just like
>  > those. It'd be weird to comment some but not all of them.
>  >
>  > But my initial plan had been to put __attribute__((used))
>  > everywhere... I could still do that, if preferred, but I assume it'll
>  > be woefully inconsistent/arbitrary with some tests using "static
>  > __attribute__((used))" and others using non-static functions anyway. I
>  > suppose the presence of a smattering of static+attribute cases would
>  > remind people to do this in cases where they want/need to make the
>  > entity static, but I'm not sure how effective this would be.
>  >
>  > So:
>  >
>  > 1) Use non-static entities (patch already provided)
>  > 2) Use __attribute__((used)) (macro'd at the start of each file? in a
>  > common header? protected under #ifdefs or not (there seem to be a
>  > variety of attributes and gnu-isms not protected by #ifdefs, and some
>  > that are)?)
>  > 3) Require Clang run the test suite with non-default flags.
>  >   -> preferably with some auto-detection in the test suite to add
>  > those flags whenever running with clang
>  >
>  > Are there other options to consider? (I suppose comments rather than
>  > attributes (2) would be an alternative - "this thing is non-static so
>  > Clang will preserve it")
>
> Let's go with (1) but add something to the wiki documenting the issue.

Works for me.

Committed in 22842ff63e28b86e0cd40a87186757b2525578f4

> https://sourceware.org/gdb/wiki/Internals%20GDB-Testsuite-Coding-Standards
>
> I'll do the latter.

Thanks a bunch - let me know when you do, I'd be happy to review it.

- David
  

Patch

diff --git gdb/testsuite/ChangeLog gdb/testsuite/ChangeLog
index 12ed4f9..623831a 100644
--- gdb/testsuite/ChangeLog
+++ gdb/testsuite/ChangeLog
@@ -1,3 +1,15 @@ 
+2014-04-10  David Blaikie  <dblaikie@gmail.com>
+
+	* gdb.base/catch-syscall.c: Make unreferenced statics non-static to
+	ensure clang would not discard them. 
+	* gdb.base/gdbvars.c: Ditto.
+	* gdb.base/memattr.c: Ditto.
+	* gdb.base/whatis.c: Ditto.
+	* gdb.python/py-prettyprint.c: Ditto.
+	* gdb.trace/actions.c: Ditto.
+	* gdb.cp/ptype-cv-cp.cc: Mark unused global const int as used to
+	ensure clang would not discard it.
+
 2014-04-10  Pedro Alves  <palves@redhat.com>
 
 	* gdb.base/cond-eval-mode.c: New file.
diff --git gdb/testsuite/gdb.base/catch-syscall.c gdb/testsuite/gdb.base/catch-syscall.c
index aa5727a..ea33b93 100644
--- gdb/testsuite/gdb.base/catch-syscall.c
+++ gdb/testsuite/gdb.base/catch-syscall.c
@@ -14,16 +14,16 @@ 
 
 /* These are the syscalls numbers used by the test.  */
 
-static int close_syscall = SYS_close;
-static int chroot_syscall = SYS_chroot;
+int close_syscall = SYS_close;
+int chroot_syscall = SYS_chroot;
 /* GDB had a bug where it couldn't catch syscall number 0 (PR 16297).
    In most GNU/Linux architectures, syscall number 0 is
    restart_syscall, which can't be called from userspace.  However,
    the "read" syscall is zero on x86_64.  */
-static int read_syscall = SYS_read;
-static int pipe_syscall = SYS_pipe;
-static int write_syscall = SYS_write;
-static int exit_group_syscall = SYS_exit_group;
+int read_syscall = SYS_read;
+int pipe_syscall = SYS_pipe;
+int write_syscall = SYS_write;
+int exit_group_syscall = SYS_exit_group;
 
 int
 main (void)
diff --git gdb/testsuite/gdb.base/gdbvars.c gdb/testsuite/gdb.base/gdbvars.c
index 352a76b..46fa84b 100644
--- gdb/testsuite/gdb.base/gdbvars.c
+++ gdb/testsuite/gdb.base/gdbvars.c
@@ -4,12 +4,12 @@  typedef void *ptr;
 
 ptr p = &p;
 
-static void
+void
 foo_void (void)
 {
 }
 
-static int
+int
 foo_int (void)
 {
   return 0;
diff --git gdb/testsuite/gdb.base/memattr.c gdb/testsuite/gdb.base/memattr.c
index 74b2d61..62868b6 100644
--- gdb/testsuite/gdb.base/memattr.c
+++ gdb/testsuite/gdb.base/memattr.c
@@ -16,11 +16,11 @@ 
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #define MEMSIZE 64
-static int mem1[MEMSIZE] = {111, 222, 333, 444, 555};
-static int mem2[MEMSIZE];
-static int mem3[MEMSIZE];
-static int mem4[MEMSIZE];
-static int mem5[MEMSIZE];
+int mem1[MEMSIZE] = {111, 222, 333, 444, 555};
+int mem2[MEMSIZE];
+int mem3[MEMSIZE];
+int mem4[MEMSIZE];
+int mem5[MEMSIZE];
 
 int main()
 {
diff --git gdb/testsuite/gdb.base/whatis.c gdb/testsuite/gdb.base/whatis.c
index a1a3188..bcda5fd 100644
--- gdb/testsuite/gdb.base/whatis.c
+++ gdb/testsuite/gdb.base/whatis.c
@@ -88,14 +88,14 @@  double		v_double_array[2];
    a special case kludge in GDB (Unix system include files like to define
    caddr_t), but for a variety of types.  */
 typedef char *char_addr;
-static char_addr a_char_addr;
+char_addr a_char_addr;
 typedef unsigned short *ushort_addr;
-static ushort_addr a_ushort_addr;
+ushort_addr a_ushort_addr;
 typedef signed long *slong_addr;
-static slong_addr a_slong_addr;
+slong_addr a_slong_addr;
 #ifndef NO_LONG_LONG
 typedef signed long long *slong_long_addr;
-static slong_long_addr a_slong_long_addr;
+slong_long_addr a_slong_long_addr;
 #endif
 
 char		*v_char_pointer;
diff --git gdb/testsuite/gdb.cp/ptype-cv-cp.cc gdb/testsuite/gdb.cp/ptype-cv-cp.cc
index 6546f68..add4021 100644
--- gdb/testsuite/gdb.cp/ptype-cv-cp.cc
+++ gdb/testsuite/gdb.cp/ptype-cv-cp.cc
@@ -22,7 +22,7 @@  typedef volatile const_my_int volatile_const_my_int;
 typedef const volatile_my_int const_volatile_my_int;
 
 my_int v_my_int (0);
-const_my_int v_const_my_int (1);
+__attribute__((used)) const_my_int v_const_my_int (1);
 volatile_my_int v_volatile_my_int (2);
 const_volatile_my_int v_const_volatile_my_int (3);
 volatile_const_my_int v_volatile_const_my_int (4);
diff --git gdb/testsuite/gdb.python/py-prettyprint.c gdb/testsuite/gdb.python/py-prettyprint.c
index 0fd05f5..817bf33 100644
--- gdb/testsuite/gdb.python/py-prettyprint.c
+++ gdb/testsuite/gdb.python/py-prettyprint.c
@@ -230,7 +230,7 @@  struct nullstr
 struct string_repr string_1 = { { "one" } };
 struct string_repr string_2 = { { "two" } };
 
-static int
+int
 eval_func (int p1, int p2, int p3, int p4, int p5, int p6, int p7, int p8)
 {
   return p1;
diff --git gdb/testsuite/gdb.trace/actions.c gdb/testsuite/gdb.trace/actions.c
index 04c69f2..497d04d 100644
--- gdb/testsuite/gdb.trace/actions.c
+++ gdb/testsuite/gdb.trace/actions.c
@@ -116,7 +116,7 @@  unsigned long   gdb_c_test( unsigned long *parm )
    return ( (unsigned long) 0 );
 }
 
-static void gdb_asm_test (void)
+void gdb_asm_test (void)
 {
 }