Index: clang-tidy/misc/AssertSideEffectCheck.cpp =================================================================== --- clang-tidy/misc/AssertSideEffectCheck.cpp +++ clang-tidy/misc/AssertSideEffectCheck.cpp @@ -55,9 +55,13 @@ 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()) { + const std::string FunctionName = FuncDecl->getNameInfo().getAsString(); + if (FunctionName == "__builtin_expect") // exceptions come here + Result = false; + else if (const auto *MethodDecl = dyn_cast(FuncDecl)) Result &= !MethodDecl->isConst(); + } return Result; } Index: clang-tidy/misc/StaticAssertCheck.cpp =================================================================== --- clang-tidy/misc/StaticAssertCheck.cpp +++ clang-tidy/misc/StaticAssertCheck.cpp @@ -41,15 +41,18 @@ 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); } Index: test/clang-tidy/misc-assert-side-effect.cpp =================================================================== --- test/clang-tidy/misc-assert-side-effect.cpp +++ 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 @@ (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 @@ assert((throw 1, false)); // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() with side effect + assert2(1 == 2 - 1); + return 0; } Index: test/clang-tidy/misc-static-assert.cpp =================================================================== --- test/clang-tidy/misc-static-assert.cpp +++ test/clang-tidy/misc-static-assert.cpp @@ -10,6 +10,8 @@ abort() #endif +void print(...); + #define ZERO_MACRO 0 #define False false @@ -126,5 +128,14 @@ 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; }