[cfe-commits] [PATCH][PR11329][v14] Generalize -Wempty-body: warn when statement body is empty

Dmitri Gribenko gribozavr at gmail.com
Sun Feb 5 09:39:01 PST 2012


Hello,

Attached is a patch that implements enhancement proposed in PR11329.

The discussion was a bit lengthy, so I'll repeat all design decisions
to make it easy to follow for reviewers.

Thanks to the reviewers' comments, I've implemented the following
heuristic that reduces false positives: for and while with null
statement as a body should be followed by a CompoundStmt in order to
emit a warning:

   for (int i = 0; i < n; i++);
   {
       a(i);
   }

But applying only the heuristic explained above limits the warning to
for(...);{...} cases.  The second heuristic is to emit a warning also
if for/while have less indentation than the next statement, e. g.:

   for (int i = 0; i < n; i++);
       a(i);

As suggested by Argyrios, we optimize for the common case where there
are no statements with null bodies.  We implement:
* a stack of "CompoundScopeInfo"s, helper functions to push/pop them,
ActOn{Start,Finish}OfCompoundStatement actions;
* a RAII object CompoundScopeRAII to call ActOn{Start,Finish} actions;
* a flag in CompoundScopeInfo that records if there were any
statements null bodies.

I also check if the warning is actually enabled before doing costly checks.

Since this is a syntactic check we don't need to repeatedly emit the
diagnostic for all template instantiations.  I check
Sema::CurrentInstantiationScope == NULL (is this the correct
approach?).

Tests from test/SemaCXX/if-empty-body.cpp are merged with new tests in
test/SemaCXX/warn-empty-body.cpp.

The patch is the same as v13, but rebased to current trunk.

Dmitri Gribenko

-- 
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: generalize-warn-empty-body-v14.patch
Type: text/x-diff
Size: 35125 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20120205/ca379af9/attachment.patch>


More information about the cfe-commits mailing list