[cfe-dev] #pragma diagnostic ignored and template code

Douglas Gregor dgregor at apple.com
Fri Sep 10 13:26:22 PDT 2010


On Sep 10, 2010, at 4:09 AM, Louis Gerbarg wrote:

> On Thu, Sep 9, 2010 at 4:40 PM, Douglas Gregor <dgregor at apple.com> wrote:
>> 
>> On Sep 9, 2010, at 11:29 AM, Chris Lattner wrote:
>> 
>>> 
>>> On Sep 9, 2010, at 9:55 AM, Luc Bourhis wrote:
>>> 
>>>> Hi,
>>>> 
>>>> consider the trivial source code
>>>> 
>>>> ==== warning.cpp ====
>>>> 
>>>> #pragma clang diagnostic push
>>>> #pragma clang diagnostic ignored "-Wtautological-compare"
>>>> 
>>>> unsigned foo(unsigned x) {
>>>> if (x < 0) return -x;
>>>> return x;
>>>> }
>>>> 
>>>> #pragma clang diagnostic pop
>>> ...
>>>> Is that the intended behaviour?
>>> 
>>> No, it's a bug.  A similar problem occurs with inline methods in classes:
>>> 
>>> class foo {
>>>  #pragma clang diagnostic push
>>>  #pragma clang diagnostic ignored "-Wtautological-compare"
>>> 
>>>  void bar() {
>>>    ...
>>>  }
>>> 
>>>  #pragma .. pop
>>> };
>>> 
>>> This is certainly a bug, but it's not trivial to fix.
>> 
>> FWIW, the fix would require us to keep track of the source ranges covering each push/pop. Then, when determining whether to suppress a diagnostic during template instantiation/inline member processing/etc., we would perform a binary search in those source ranges to determine the active pragmas, diagnostic flags, etc.
> 
> How does this effect similiar pragmas, like align? For example:
> 
> #pragma pack(push,1)
> 
> unsigned foo(unsigned x) {
> if (x < 0) return -x;
> return x;
> }
> 
> #pragma pack(pop)
> 
> If that works correctly it seems like we could use the same
> infrastructure for storing the alignment info during template
> instantiation. If it doesn't work, then it would probably make some
> sense to figure out how to solve them both generically, since not
> honoring the alignment info could be a pretty serious issue for some
> codebases.

It's different infrastructure, but both would need similar updates; perhaps a generic infrastructure could work here.

> Also, is there any chance this could be worked around by using
> _Pragma()? That would be the solution in a C macro, but I am unsure
> what the semantics of _Pragma() with respect to template instantiation
> are.

_Pragma() won't help at all. Use attributes instead.

	- Doug



More information about the cfe-dev mailing list