]> granicus.if.org Git - clang/commitdiff
Only take NULL macros instead of all macros into account for -Wnull-conversion.
authorRichard Trieu <rtrieu@google.com>
Sat, 9 Jan 2016 01:10:17 +0000 (01:10 +0000)
committerRichard Trieu <rtrieu@google.com>
Sat, 9 Jan 2016 01:10:17 +0000 (01:10 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@257240 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaChecking.cpp
test/SemaCXX/conversion.cpp

index 0c4b59e3ce1f62568cc283c951d32f87dc94d4e5..98c9ceb1d65ef93576a22aa753c8225ea84480e5 100644 (file)
@@ -7066,8 +7066,12 @@ static void DiagnoseNullConversion(Sema &S, Expr *E, QualType T,
 
   // __null is usually wrapped in a macro.  Go up a macro if that is the case.
   if (NullKind == Expr::NPCK_GNUNull) {
-    if (Loc.isMacroID())
-      Loc = S.SourceMgr.getImmediateExpansionRange(Loc).first;
+    if (Loc.isMacroID()) {
+      StringRef MacroName =
+          Lexer::getImmediateMacroName(Loc, S.SourceMgr, S.getLangOpts());
+      if (MacroName == "NULL")
+        Loc = S.SourceMgr.getImmediateExpansionRange(Loc).first;
+    }
   }
 
   // Only warn if the null and context location are in the same macro expansion.
index 2c83147ff4c480b00425d50d50f98a2284f4d32c..4c4089c6aae773840122e33d755fabe0c5331246 100644 (file)
@@ -208,3 +208,23 @@ namespace test9 {
     return EXIT();
   }
 }
+
+// Test NULL macro inside a macro has same warnings nullptr inside a macro.
+namespace test10 {
+#define test1(cond) \
+      ((cond) ? nullptr : NULL)
+#define test2(cond) \
+      ((cond) ? NULL : nullptr)
+
+#define assert(cond) \
+      ((cond) ? foo() : bar())
+  void foo();
+  void bar();
+
+  void run(int x) {
+    if (test1(x)) {}
+    if (test2(x)) {}
+    assert(test1(x));
+    assert(test2(x));
+  }
+}