From 43a298cb36f8e05e9335bd3deb3214ef5bb99df8 Mon Sep 17 00:00:00 2001 From: Szabolcs Sipos Date: Fri, 29 May 2015 09:49:59 +0000 Subject: [PATCH] [clang-tidy] Fix for llvm.org/PR23355 misc-static-assert and misc-assert-side-effect will handle __builtin_expect based asserts correctly. llvm-svn: 238548 --- clang-tools-extra/clang-tidy/misc/AssertSideEffectCheck.cpp | 8 ++++++-- clang-tools-extra/clang-tidy/misc/StaticAssertCheck.cpp | 11 +++++++---- clang-tools-extra/test/clang-tidy/misc-assert-side-effect.cpp | 8 +++++++- clang-tools-extra/test/clang-tidy/misc-static-assert.cpp | 11 +++++++++++ 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/clang-tools-extra/clang-tidy/misc/AssertSideEffectCheck.cpp b/clang-tools-extra/clang-tidy/misc/AssertSideEffectCheck.cpp index 9d21b49..fa589a4 100644 --- a/clang-tools-extra/clang-tidy/misc/AssertSideEffectCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/AssertSideEffectCheck.cpp @@ -55,9 +55,13 @@ AST_MATCHER_P(Expr, hasSideEffect, bool, CheckFunctionCalls) { if (const auto *CExpr = dyn_cast(E)) { bool Result = CheckFunctionCalls; - if (const auto *FuncDecl = CExpr->getDirectCallee()) - if (const auto *MethodDecl = dyn_cast(FuncDecl)) + if (const auto *FuncDecl = CExpr->getDirectCallee()) { + if (FuncDecl->getDeclName().isIdentifier() && + FuncDecl->getName() == "__builtin_expect") // exceptions come here + Result = false; + else if (const auto *MethodDecl = dyn_cast(FuncDecl)) Result &= !MethodDecl->isConst(); + } return Result; } diff --git a/clang-tools-extra/clang-tidy/misc/StaticAssertCheck.cpp b/clang-tools-extra/clang-tidy/misc/StaticAssertCheck.cpp index ed9367c..1b6b897 100644 --- a/clang-tools-extra/clang-tidy/misc/StaticAssertCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/StaticAssertCheck.cpp @@ -41,15 +41,18 @@ void StaticAssertCheck::registerMatchers(MatchFinder *Finder) { IsAlwaysFalse); auto NonConstexprFunctionCall = callExpr(hasDeclaration(functionDecl(unless(isConstexpr())))); - auto Condition = expr(anyOf( + auto AssertCondition = expr(anyOf( expr(ignoringParenCasts(anyOf( AssertExprRoot, unaryOperator(hasUnaryOperand(ignoringParenCasts(AssertExprRoot)))))), - anything()), unless(findAll(NonConstexprFunctionCall))); + anything()), unless(findAll(NonConstexprFunctionCall))).bind("condition"); + auto Condition = anyOf(ignoringParenImpCasts(callExpr( + hasDeclaration(functionDecl(hasName("__builtin_expect"))), + hasArgument(0, AssertCondition))), AssertCondition); Finder->addMatcher( - stmt(anyOf(conditionalOperator(hasCondition(Condition.bind("condition"))), - ifStmt(hasCondition(Condition.bind("condition")))), + stmt(anyOf(conditionalOperator(hasCondition(Condition)), + ifStmt(hasCondition(Condition))), unless(isInTemplateInstantiation())).bind("condStmt"), this); } diff --git a/clang-tools-extra/test/clang-tidy/misc-assert-side-effect.cpp b/clang-tools-extra/test/clang-tidy/misc-assert-side-effect.cpp index 73eb8b4..b245419 100644 --- a/clang-tools-extra/test/clang-tidy/misc-assert-side-effect.cpp +++ b/clang-tools-extra/test/clang-tidy/misc-assert-side-effect.cpp @@ -1,4 +1,4 @@ -// RUN: $(dirname %s)/check_clang_tidy.sh %s misc-assert-side-effect %t -config="{CheckOptions: [{key: misc-assert-side-effect.CheckFunctionCalls, value: 1}, {key: misc-assert-side-effect.AssertMacros, value: 'assert,my_assert'}]}" -- -fexceptions +// RUN: $(dirname %s)/check_clang_tidy.sh %s misc-assert-side-effect %t -config="{CheckOptions: [{key: misc-assert-side-effect.CheckFunctionCalls, value: 1}, {key: misc-assert-side-effect.AssertMacros, value: 'assert,assert2,my_assert'}]}" -- -fexceptions // REQUIRES: shell //===--- assert definition block ------------------------------------------===// @@ -12,6 +12,10 @@ int abort() { return 0; } (void)abort() #endif +void print(...); +#define assert2(e) (__builtin_expect(!(e), 0) ? \ + print (#e, __FILE__, __LINE__) : (void)0) + #ifdef NDEBUG #define my_assert(x) 1 #else @@ -88,5 +92,7 @@ int main() { assert((throw 1, false)); // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() with side effect + assert2(1 == 2 - 1); + return 0; } diff --git a/clang-tools-extra/test/clang-tidy/misc-static-assert.cpp b/clang-tools-extra/test/clang-tidy/misc-static-assert.cpp index 8375531..b2c72ea 100644 --- a/clang-tools-extra/test/clang-tidy/misc-static-assert.cpp +++ b/clang-tools-extra/test/clang-tidy/misc-static-assert.cpp @@ -10,6 +10,8 @@ void abort() {} abort() #endif +void print(...); + #define ZERO_MACRO 0 #define False false @@ -126,5 +128,14 @@ int main() { assert(strlen("12345") == 5); // CHECK-FIXES: {{^ }}assert(strlen("12345") == 5); +#define assert(e) (__builtin_expect(!(e), 0) ? print (#e, __FILE__, __LINE__) : (void)0) + assert(false); + // CHECK-FIXES: {{^ }}assert(false); + + assert(10 == 5 + 5); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be + // CHECK-FIXES: {{^ }}static_assert(10 == 5 + 5, ""); +#undef assert + return 0; } -- 2.7.4