r232295 - -Wempty-body: fix false negative triggered by macros

Dmitri Gribenko gribozavr at gmail.com
Sat Mar 14 18:08:23 PDT 2015


Author: gribozavr
Date: Sat Mar 14 20:08:23 2015
New Revision: 232295

URL: http://llvm.org/viewvc/llvm-project?rev=232295&view=rev
Log:
-Wempty-body: fix false negative triggered by macros

When if statement condition ended in a macro:

    if (ptr == NULL);

the check used to consider the definition location of NULL, instead of the
current line.

Patch by Manasij Mukherjee.

Modified:
    cfe/trunk/lib/Sema/SemaChecking.cpp
    cfe/trunk/test/SemaCXX/warn-empty-body.cpp

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=232295&r1=232294&r2=232295&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Sat Mar 14 20:08:23 2015
@@ -8625,7 +8625,7 @@ bool ShouldDiagnoseEmptyStmtBody(const S
 
   // Get line numbers of statement and body.
   bool StmtLineInvalid;
-  unsigned StmtLine = SourceMgr.getSpellingLineNumber(StmtLoc,
+  unsigned StmtLine = SourceMgr.getPresumedLineNumber(StmtLoc,
                                                       &StmtLineInvalid);
   if (StmtLineInvalid)
     return false;

Modified: cfe/trunk/test/SemaCXX/warn-empty-body.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-empty-body.cpp?rev=232295&r1=232294&r2=232295&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-empty-body.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn-empty-body.cpp Sat Mar 14 20:08:23 2015
@@ -4,10 +4,17 @@ void a(int i);
 int b();
 int c();
 
+#define MACRO_A 0
+
 void test1(int x, int y) {
   while(true) {
     if (x); // expected-warning {{if statement has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
 
+    // Check that we handle conditions that start or end with a macro
+    // correctly.
+    if (x == MACRO_A); // expected-warning {{if statement has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
+    if (MACRO_A == x); // expected-warning {{if statement has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}
+
     int i;
     // PR11329
     for (i = 0; i < x; i++); { // expected-warning{{for loop has empty body}} expected-note{{put the semicolon on a separate line to silence this warning}}





More information about the cfe-commits mailing list