[cfe-commits] r152940 - in /cfe/trunk: lib/Sema/SemaChecking.cpp test/SemaCXX/conversion.cpp

David Blaikie dblaikie at gmail.com
Fri Mar 16 13:30:12 PDT 2012


Author: dblaikie
Date: Fri Mar 16 15:30:12 2012
New Revision: 152940

URL: http://llvm.org/viewvc/llvm-project?rev=152940&view=rev
Log:
Suppress macro expansion of NULL in NULL warnings.

For "int i = NULL;" we would produce:

null.cpp:5:11: warning: implicit conversion of NULL constant to integer [-Wconversion]
  int i = NULL;
      ~   ^~~~
null.cpp:1:14: note: expanded from macro 'NULL'
\#define NULL __null
              ^~~~~~

But we really shouldn't trace that macro expansion back into the header, yet we
still want macro back traces for code like this:

\#define FOO NULL
int i = FOO;

or

\#define FOO int i = NULL;
FOO

While providing appropriate tagging at different levels of the expansion, etc.

The included test case exercises these cases & does some basic validation (to
ensure we don't have macro expansion notes where we shouldn't, and do where we
should) - but doesn't go as far as to validate the source location/ranges
used in those notes and warnings.

Modified:
    cfe/trunk/lib/Sema/SemaChecking.cpp
    cfe/trunk/test/SemaCXX/conversion.cpp

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=152940&r1=152939&r2=152940&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Fri Mar 16 15:30:12 2012
@@ -4075,8 +4075,11 @@
 
   if ((E->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull)
            == Expr::NPCK_GNUNull) && Target->isIntegerType()) {
-    S.Diag(E->getExprLoc(), diag::warn_impcast_null_pointer_to_integer)
-        << T << E->getSourceRange() << clang::SourceRange(CC);
+    SourceLocation Loc = E->getSourceRange().getBegin();
+    if (Loc.isMacroID())
+      Loc = S.SourceMgr.getImmediateExpansionRange(Loc).first;
+    S.Diag(Loc, diag::warn_impcast_null_pointer_to_integer)
+        << T << Loc << clang::SourceRange(CC);
     return;
   }
 

Modified: cfe/trunk/test/SemaCXX/conversion.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/conversion.cpp?rev=152940&r1=152939&r2=152940&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/conversion.cpp (original)
+++ cfe/trunk/test/SemaCXX/conversion.cpp Fri Mar 16 15:30:12 2012
@@ -1,4 +1,5 @@
 // RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -Wconversion -verify %s
+// RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -Wconversion %s 2>&1 | FileCheck %s
 
 #include <stddef.h>
 
@@ -68,4 +69,15 @@
   char ch = NULL; // expected-warning {{implicit conversion of NULL constant to 'char'}}
   unsigned char uch = NULL; // expected-warning {{implicit conversion of NULL constant to 'unsigned char'}}
   short sh = NULL; // expected-warning {{implicit conversion of NULL constant to 'short'}}
+
+  // Use FileCheck to ensure we don't get any unnecessary macro-expansion notes 
+  // (that don't appear as 'real' notes & can't be seen/tested by -verify)
+  // CHECK-NOT: note:
+  // CHECK: note: expanded from macro 'FNULL'
+#define FNULL NULL
+  int a2 = FNULL; // expected-warning {{implicit conversion of NULL constant to 'int'}}
+  // CHECK-NOT: note:
+  // CHECK: note: expanded from macro 'FINIT'
+#define FINIT int a3 = NULL;
+  FINIT // expected-warning {{implicit conversion of NULL constant to 'int'}}
 }





More information about the cfe-commits mailing list