[PATCH] [clang-tidy] Fix for Bug 23161
Szabolcs Sipos
szabolcs.sipos at ericsson.com
Thu Apr 9 07:39:15 PDT 2015
The new fix handles the case when NULL is defined as ((T)0), where T is an arbitrary type.
Please tell me, if you think that T should be restricted to void*.
http://reviews.llvm.org/D8920
Files:
clang-tidy/misc/StaticAssertCheck.cpp
test/clang-tidy/misc-static-assert.cpp
Index: clang-tidy/misc/StaticAssertCheck.cpp
===================================================================
--- clang-tidy/misc/StaticAssertCheck.cpp
+++ clang-tidy/misc/StaticAssertCheck.cpp
@@ -27,15 +27,17 @@
: ClangTidyCheck(Name, Context) {}
void StaticAssertCheck::registerMatchers(MatchFinder *Finder) {
- auto IsAlwaysFalse = ignoringParenImpCasts(
- anyOf(boolLiteral(equals(false)).bind("isAlwaysFalse"),
- integerLiteral(equals(0)).bind("isAlwaysFalse")));
+ auto IsAlwaysFalse = expr(ignoringParenImpCasts(
+ expr(anyOf(boolLiteral(equals(false)), integerLiteral(equals(0)),
+ nullPtrLiteralExpr())).bind("isAlwaysFalse")));
+ auto IsAlwaysFalseWithCast = ignoringParenImpCasts(anyOf(IsAlwaysFalse,
+ cStyleCastExpr(hasDescendant(IsAlwaysFalse))));
auto AssertExprRoot = anyOf(
binaryOperator(
- hasOperatorName("&&"),
+ anyOf(hasOperatorName("&&"), hasOperatorName("==")),
hasEitherOperand(ignoringImpCasts(stringLiteral().bind("assertMSG"))),
- anyOf(binaryOperator(hasEitherOperand(IsAlwaysFalse)), anything()))
- .bind("assertExprRoot"),
+ anyOf(binaryOperator(hasEitherOperand(IsAlwaysFalseWithCast)),
+ anything())).bind("assertExprRoot"),
IsAlwaysFalse);
auto Condition = expr(anyOf(
expr(ignoringParenCasts(anyOf(
@@ -83,7 +85,8 @@
StringRef FalseMacroName =
Lexer::getImmediateMacroName(FalseLiteralLoc, SM, Opts);
- if (FalseMacroName.compare_lower("false") == 0)
+ if (FalseMacroName.compare_lower("false") == 0 ||
+ FalseMacroName.compare_lower("null") == 0)
return;
}
Index: test/clang-tidy/misc-static-assert.cpp
===================================================================
--- test/clang-tidy/misc-static-assert.cpp
+++ test/clang-tidy/misc-static-assert.cpp
@@ -15,6 +15,8 @@
#define False false
#define FALSE 0
+#define NULL ((void*)0)
+
#define my_macro() assert(0 == 1)
// CHECK-FIXES: #define my_macro() assert(0 == 1)
@@ -78,6 +80,18 @@
assert(false && "Don't report me!");
// CHECK-FIXES: {{^ }}assert(false && "Don't report me!");
+ assert(NULL && "Don't report me!");
+ // CHECK-FIXES: {{^ }}assert(NULL && "Don't report me!");
+
+ assert(NULL == "Don't report me!");
+ // CHECK-FIXES: {{^ }}assert(NULL == "Don't report me!");
+
+ assert("Don't report me!" == NULL);
+ // CHECK-FIXES: {{^ }}assert("Don't report me!" == NULL);
+
+ assert(0 == "Don't report me!");
+ // CHECK-FIXES: {{^ }}assert(0 == "Don't report me!");
+
assert(ZERO_MACRO && "Report me!");
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be
// CHECK-FIXES: {{^ }}static_assert(ZERO_MACRO , "Report me!");
EMAIL PREFERENCES
http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D8920.23489.patch
Type: text/x-patch
Size: 2763 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150409/a07b8164/attachment.bin>
More information about the cfe-commits
mailing list