[cfe-dev] Pushable #pragma diagnostics

Louis Gerbarg lgerbarg at gmail.com
Sun Jul 5 14:42:27 PDT 2009


I generally compile code with as many warnings enabled as I can, and - 
Werror. Clang is great for that since it has a lot more flexible  
options for handling warnings than gcc. It also adopted gcc's recent  
diagnostic pragmas, which allow one to embed the warning controls  
directly around the relevant code.

Obviously, doing that in a way that is compatible with gcc was  
important, but gcc's pragma is deficient in a few ways. The big one is  
that it has no sort of push/pop semantics, like the pack pragmas. This  
impacts me since I have some warnings I want to disable in a small  
section of a header. Unfortunately it is impossible to set the  
warnings back to the state they were in before that bit of code. It is  
not just a header issue, since realistically people want to change  
command line warnings at various points, but the current pragmas force  
permanent changes in the diagnostic configuration for the entire rest  
of that compilation unit, despite the change usually only being  
relevant for a few lines of code.

With that in mind, I would like to propose an extension to the current  
pragma to add push/pop functionality (similiar to that of pack). There  
are a couple of ways that this could be done. The first code be to  
prefix push onto the existing pragmas:

#pragma clang diagnostic push ignored "-Wmultichar"
char d = 'df'; // no warning.
#pragma clang diagnostic pop

That has the benefit of being concise, but it would mean we could have  
complicated situations like:

#pragma clang diagnostic push ignored "-Wmultichar"
#pragma clang diagnostic warning "-Wall"
char d = 'df'; // no warning.
#pragma clang diagnostic pop

It is unclear what the correct behavior of the pop should be at that  
point. Should pop all the way back to the push, should it attempt to  
regenerate a diagnostic state without the push put with the warning "- 
Wall" ? We could choose something, but it seems to me like it is not  
worth it. Another option to make push and pop explicit it commands:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmultichar"
char d = 'df'; // no warning.
#pragma clang diagnostic pop

The above is slightly less concise in the normal case, but the  
behavior of the pragma in the presence of multiple changes is clear.  
There are some other things that are possible with implicit popping  
and so forth, but they all seem to have complicated behaviors when  
they nest, and in this case I am in favor of simple, if slightly  
verbose.

Implementing that is actually not particularly difficult, I have a  
patch attached for comment.The patch may require some slight cleanups,  
but it works. I also have some test cases on my system I can cleanup  
and send if people like this extension.

I think that I could probably generate some better warnings for  
misuses of the pragma, though what those warnings are depends on what  
the exact extensions are, so if people are happy with the my proposed  
extension I may go through and expand on those.

Louis
-------------- next part --------------
A non-text attachment was scrubbed...
Name: pushable_diagnostics.patch
Type: application/octet-stream
Size: 6334 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20090705/31351b33/attachment.obj>


More information about the cfe-dev mailing list