[clang-tools-extra] r234596 - [clang-tidy] Fix for llvm.org/PR23161
Szabolcs Sipos
szabolcs.sipos at ericsson.com
Fri Apr 10 06:55:40 PDT 2015
Author: eszasip
Date: Fri Apr 10 08:55:39 2015
New Revision: 234596
URL: http://llvm.org/viewvc/llvm-project?rev=234596&view=rev
Log:
[clang-tidy] Fix for llvm.org/PR23161
The misc-static-assert check will not warn on the followings:
assert("Some message" == NULL);
assert(NULL == "Some message");
Modified:
clang-tools-extra/trunk/clang-tidy/misc/StaticAssertCheck.cpp
clang-tools-extra/trunk/test/clang-tidy/misc-static-assert.cpp
Modified: clang-tools-extra/trunk/clang-tidy/misc/StaticAssertCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/StaticAssertCheck.cpp?rev=234596&r1=234595&r2=234596&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/misc/StaticAssertCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/misc/StaticAssertCheck.cpp Fri Apr 10 08:55:39 2015
@@ -27,15 +27,17 @@ StaticAssertCheck::StaticAssertCheck(Str
: 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(has(IsAlwaysFalse)).bind("castExpr")));
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(
@@ -60,6 +62,7 @@ void StaticAssertCheck::check(const Matc
const auto *AssertMSG = Result.Nodes.getNodeAs<StringLiteral>("assertMSG");
const auto *AssertExprRoot =
Result.Nodes.getNodeAs<BinaryOperator>("assertExprRoot");
+ const auto *CastExpr = Result.Nodes.getNodeAs<CStyleCastExpr>("castExpr");
SourceLocation AssertExpansionLoc = CondStmt->getLocStart();
if (!Opts.CPlusPlus11 || !AssertExpansionLoc.isValid() ||
@@ -75,7 +78,7 @@ void StaticAssertCheck::check(const Matc
return;
// False literal is not the result of macro expansion.
- if (IsAlwaysFalse) {
+ if (IsAlwaysFalse && (!CastExpr || CastExpr->getType()->isPointerType())) {
SourceLocation FalseLiteralLoc =
SM.getImmediateSpellingLoc(IsAlwaysFalse->getExprLoc());
if (!FalseLiteralLoc.isMacroID())
@@ -83,7 +86,8 @@ void StaticAssertCheck::check(const Matc
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;
}
Modified: clang-tools-extra/trunk/test/clang-tidy/misc-static-assert.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/misc-static-assert.cpp?rev=234596&r1=234595&r2=234596&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/misc-static-assert.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/misc-static-assert.cpp Fri Apr 10 08:55:39 2015
@@ -78,6 +78,25 @@ int main() {
assert(false && "Don't report me!");
// CHECK-FIXES: {{^ }}assert(false && "Don't report me!");
+#define NULL ((void*)0)
+ 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!");
+
+#define NULL ((unsigned int)0)
+ assert(NULL && "Report me!");
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be
+ // CHECK-FIXES: {{^ }}static_assert(NULL , "Report me!");
+#undef NULL
+
assert(ZERO_MACRO && "Report me!");
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be
// CHECK-FIXES: {{^ }}static_assert(ZERO_MACRO , "Report me!");
More information about the cfe-commits
mailing list