mirror of
https://github.com/capstone-engine/llvm-capstone.git
synced 2024-12-12 01:18:53 +00:00
[Sema] -Wzero-as-null-pointer-constant: don't warn for system macros other than NULL.
Summary: The warning was initially introduced in D32914 by @thakis, and the concerns were raised there, and later in rL302247 and PR33771. I do believe that it makes sense to relax the diagnostic e.g. in this case, when the expression originates from the system header, which can not be modified. This prevents adoption for the diagnostic for codebases which use pthreads (`PTHREAD_MUTEX_INITIALIZER`), gtest, etc. As @malcolm.parsons suggests, it *may* make sense to also not warn for the template types, but it is not obvious to me how to do that in here. Though, it still makes sense to complain about `NULL` macro. While there, add more tests. Reviewers: dblaikie, thakis, rsmith, rjmccall, aaron.ballman Reviewed By: thakis Subscribers: Rakete1111, hans, cfe-commits, thakis, malcolm.parsons Tags: #clang Differential Revision: https://reviews.llvm.org/D38954 llvm-svn: 316662
This commit is contained in:
parent
83454aaec4
commit
809df34efc
@ -91,6 +91,9 @@ Improvements to Clang's diagnostics
|
||||
offset is nonzero. It also now warns about arithmetic on a null pointer
|
||||
treated as a cast from integer to pointer (GNU extension).
|
||||
|
||||
- ``-Wzero-as-null-pointer-constant`` was adjusted not to warn on null pointer
|
||||
constants that originate from system macros, except ``NULL`` macro.
|
||||
|
||||
Non-comprehensive list of changes in this release
|
||||
-------------------------------------------------
|
||||
|
||||
|
@ -436,12 +436,24 @@ void Sema::diagnoseNullableToNonnullConversion(QualType DstType,
|
||||
}
|
||||
|
||||
void Sema::diagnoseZeroToNullptrConversion(CastKind Kind, const Expr* E) {
|
||||
if (Diags.isIgnored(diag::warn_zero_as_null_pointer_constant,
|
||||
E->getLocStart()))
|
||||
return;
|
||||
// nullptr only exists from C++11 on, so don't warn on its absence earlier.
|
||||
if (!getLangOpts().CPlusPlus11)
|
||||
return;
|
||||
|
||||
if (Kind != CK_NullToPointer && Kind != CK_NullToMemberPointer)
|
||||
return;
|
||||
if (E->IgnoreParenImpCasts()->getType()->isNullPtrType())
|
||||
return;
|
||||
// nullptr only exists from C++11 on, so don't warn on its absence earlier.
|
||||
if (!getLangOpts().CPlusPlus11)
|
||||
|
||||
// If it is a macro from system header, and if the macro name is not "NULL",
|
||||
// do not warn.
|
||||
SourceLocation MaybeMacroLoc = E->getLocStart();
|
||||
if (Diags.getSuppressSystemWarnings() &&
|
||||
SourceMgr.isInSystemMacro(MaybeMacroLoc) &&
|
||||
!findMacroSpelling(MaybeMacroLoc, "NULL"))
|
||||
return;
|
||||
|
||||
Diag(E->getLocStart(), diag::warn_zero_as_null_pointer_constant)
|
||||
|
3
clang/test/SemaCXX/Inputs/warn-zero-nullptr.h
Normal file
3
clang/test/SemaCXX/Inputs/warn-zero-nullptr.h
Normal file
@ -0,0 +1,3 @@
|
||||
#define NULL (0)
|
||||
#define SYSTEM_MACRO (0)
|
||||
#define OTHER_SYSTEM_MACRO (NULL)
|
@ -1,4 +1,10 @@
|
||||
// RUN: %clang_cc1 -fsyntax-only -verify %s -Wzero-as-null-pointer-constant -std=c++11
|
||||
// RUN: %clang_cc1 -fsyntax-only -verify %s -isystem %S/Inputs -Wzero-as-null-pointer-constant -std=c++11
|
||||
// RUN: %clang_cc1 -fsyntax-only -verify %s -isystem %S/Inputs -DSYSTEM_WARNINGS -Wzero-as-null-pointer-constant -Wsystem-headers -std=c++11
|
||||
|
||||
#include <warn-zero-nullptr.h>
|
||||
|
||||
#define MACRO (0)
|
||||
#define MCRO(x) (x)
|
||||
|
||||
struct S {};
|
||||
|
||||
@ -15,8 +21,12 @@ void* p2 = __null; // expected-warning{{zero as null pointer constant}}
|
||||
void (*fp2)() = __null; // expected-warning{{zero as null pointer constant}}
|
||||
int (S::*mp2) = __null; // expected-warning{{zero as null pointer constant}}
|
||||
|
||||
void f0(void* v = 0); // expected-warning{{zero as null pointer constant}}
|
||||
void f1(void* v);
|
||||
void f0(void* v = MACRO); // expected-warning{{zero as null pointer constant}}
|
||||
void f1(void* v = NULL); // expected-warning{{zero as null pointer constant}}
|
||||
void f2(void* v = MCRO(0)); // expected-warning{{zero as null pointer constant}}
|
||||
void f3(void* v = MCRO(NULL)); // expected-warning{{zero as null pointer constant}}
|
||||
void f4(void* v = 0); // expected-warning{{zero as null pointer constant}}
|
||||
void f5(void* v);
|
||||
|
||||
void g() {
|
||||
f1(0); // expected-warning{{zero as null pointer constant}}
|
||||
@ -32,3 +42,51 @@ struct A { operator int*() { return nullptr; } };
|
||||
void func() { if (nullptr == A()) {} }
|
||||
void func2() { if ((nullptr) == A()) {} }
|
||||
}
|
||||
|
||||
template <typename T> void TmplFunc0(T var) {}
|
||||
void Func0Test() {
|
||||
TmplFunc0<int>(0);
|
||||
TmplFunc0<int*>(0); // expected-warning {{zero as null pointer constant}}
|
||||
TmplFunc0<void*>(0); // expected-warning {{zero as null pointer constant}}
|
||||
}
|
||||
|
||||
// FIXME: this one probably should not warn.
|
||||
template <typename T> void TmplFunc1(int a, T default_value = 0) {} // expected-warning{{zero as null pointer constant}} expected-warning{{zero as null pointer constant}}
|
||||
void FuncTest() {
|
||||
TmplFunc1<int>(0);
|
||||
TmplFunc1<int*>(0); // expected-note {{in instantiation of default function argument expression for 'TmplFunc1<int *>' required here}}
|
||||
TmplFunc1<void*>(0); // expected-note {{in instantiation of default function argument expression for 'TmplFunc1<void *>' required here}}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
class TemplateClass0 {
|
||||
public:
|
||||
explicit TemplateClass0(T var) {}
|
||||
};
|
||||
void TemplateClass0Test() {
|
||||
TemplateClass0<int> a(0);
|
||||
TemplateClass0<int*> b(0); // expected-warning {{zero as null pointer constant}}
|
||||
TemplateClass0<void*> c(0); // expected-warning {{zero as null pointer constant}}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
class TemplateClass1 {
|
||||
public:
|
||||
// FIXME: this one should *NOT* warn.
|
||||
explicit TemplateClass1(int a, T default_value = 0) {} // expected-warning{{zero as null pointer constant}} expected-warning{{zero as null pointer constant}}
|
||||
};
|
||||
void IgnoreSubstTemplateType1() {
|
||||
TemplateClass1<int> a(1);
|
||||
TemplateClass1<int*> b(1); // expected-note {{in instantiation of default function argument expression for 'TemplateClass1<int *>' required here}}
|
||||
TemplateClass1<void*> c(1); // expected-note {{in instantiation of default function argument expression for 'TemplateClass1<void *>' required here}}
|
||||
}
|
||||
|
||||
#ifndef SYSTEM_WARNINGS
|
||||
// Do not warn on *any* other macros from system headers, even if they
|
||||
// expand to/their expansion contains NULL.
|
||||
void* sys_init = SYSTEM_MACRO;
|
||||
void* sys_init2 = OTHER_SYSTEM_MACRO;
|
||||
#else
|
||||
void* sys_init = SYSTEM_MACRO; // expected-warning {{zero as null pointer constant}}
|
||||
void* sys_init2 = OTHER_SYSTEM_MACRO; // expected-warning {{zero as null pointer constant}}
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user