From 8429681d5759005777475c0faf6d2149e40386ac Mon Sep 17 00:00:00 2001 From: Szabolcs Sipos Date: Fri, 10 Apr 2015 13:55:39 +0000 Subject: [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"); llvm-svn: 234596 --- .../clang-tidy/misc/StaticAssertCheck.cpp | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'clang-tools-extra/clang-tidy/misc/StaticAssertCheck.cpp') diff --git a/clang-tools-extra/clang-tidy/misc/StaticAssertCheck.cpp b/clang-tools-extra/clang-tidy/misc/StaticAssertCheck.cpp index 91451e680ca..c63cd7473ec 100644 --- a/clang-tools-extra/clang-tidy/misc/StaticAssertCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/StaticAssertCheck.cpp @@ -27,15 +27,17 @@ StaticAssertCheck::StaticAssertCheck(StringRef Name, ClangTidyContext *Context) : 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 MatchFinder::MatchResult &Result) { const auto *AssertMSG = Result.Nodes.getNodeAs("assertMSG"); const auto *AssertExprRoot = Result.Nodes.getNodeAs("assertExprRoot"); + const auto *CastExpr = Result.Nodes.getNodeAs("castExpr"); SourceLocation AssertExpansionLoc = CondStmt->getLocStart(); if (!Opts.CPlusPlus11 || !AssertExpansionLoc.isValid() || @@ -75,7 +78,7 @@ void StaticAssertCheck::check(const MatchFinder::MatchResult &Result) { 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 MatchFinder::MatchResult &Result) { 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; } -- cgit v1.2.3