#pragma diagnostic ignored and template code

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

#pragma diagnostic ignored and template code

Luc Bourhis
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

==== main.cpp ====

#include "warning.hpp"
#include <iostream>

int main() {
  std::cout << foo(1u) << std::endl;
  return 0;
}

================

clang ignores the #pragma's completely:

~> clang++ -o main main.cpp        
In file included from main.cpp:1:
./warning.hpp:6:9: warning: comparison of unsigned expression < 0 is always false
      [-Wtautological-compare]
  if (x < T(0)) return -x;
      ~ ^ ~~~~
main.cpp:5:16: note: in instantiation of function template specialization 'foo<unsigned int>' requested
      here
  std::cout << foo(1u) << std::endl;
               ^
1 warning generated.


Is that the intended behaviour?

Luc Bourhis



_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev

smime.p7s (4K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: #pragma diagnostic ignored and template code

Luc Bourhis
I cat and then pasted the wrong code, sorry!

> Hi,
>
> consider the trivial source code
>
==== warning.hpp ====

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wtautological-compare"

template <class T>
T foo(T x) {
  if (x < T(0)) return -x;
  return x;
}

#pragma clang diagnostic pop

>
> ==== main.cpp ====
>
> #include "warning.hpp"
> #include <iostream>
>
> int main() {
>  std::cout << foo(1u) << std::endl;
>  return 0;
> }
>
> ================
>
> clang ignores the #pragma's completely:
>
> ~> clang++ -o main main.cpp        
> In file included from main.cpp:1:
> ./warning.hpp:6:9: warning: comparison of unsigned expression < 0 is always false
>      [-Wtautological-compare]
>  if (x < T(0)) return -x;
>      ~ ^ ~~~~
> main.cpp:5:16: note: in instantiation of function template specialization 'foo<unsigned int>' requested
>      here
>  std::cout << foo(1u) << std::endl;
>               ^
> 1 warning generated.
>
>
> Is that the intended behaviour?
>
> Luc Bourhis
>
>

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev

smime.p7s (4K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: #pragma diagnostic ignored and template code

Chris Lattner
In reply to this post by Luc Bourhis

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.

-Chris
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: #pragma diagnostic ignored and template code

Douglas Gregor

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.

        - Doug
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: #pragma diagnostic ignored and template code

Louis Gerbarg
On Thu, Sep 9, 2010 at 4:40 PM, Douglas Gregor <[hidden email]> 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.

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.

Louis

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: #pragma diagnostic ignored and template code

Chris Lattner
On Sep 10, 2010, at 4:09 AM, Louis Gerbarg wrote:
>> 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:

It is completely unrelated, it uses different infrastructure.

-Chris


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: #pragma diagnostic ignored and template code

Douglas Gregor
In reply to this post by Louis Gerbarg

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

> On Thu, Sep 9, 2010 at 4:40 PM, Douglas Gregor <[hidden email]> 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
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: #pragma diagnostic ignored and template code

Luc Bourhis
In reply to this post by Chris Lattner
> No, it's a bug.

In which case,

http://llvm.org/bugs/show_bug.cgi?id=8143

Luc



_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev

smime.p7s (4K) Download Attachment