diff options
author | Szabolcs Sipos <szabolcs.sipos@ericsson.com> | 2015-05-29 09:49:59 +0000 |
---|---|---|
committer | Szabolcs Sipos <szabolcs.sipos@ericsson.com> | 2015-05-29 09:49:59 +0000 |
commit | 43a298cb36f8e05e9335bd3deb3214ef5bb99df8 (patch) | |
tree | 1e9c0ae7f5ca8c40e623b4a84aecc6056ab84421 | |
parent | e4770da766d675917b315145e1c54165ea73f9fb (diff) | |
download | bcm5719-llvm-43a298cb36f8e05e9335bd3deb3214ef5bb99df8.tar.gz bcm5719-llvm-43a298cb36f8e05e9335bd3deb3214ef5bb99df8.zip |
[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
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 9d21b499ee9..fa589a4c759 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<CallExpr>(E)) { bool Result = CheckFunctionCalls; - if (const auto *FuncDecl = CExpr->getDirectCallee()) - if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(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<CXXMethodDecl>(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 ed9367c8a21..1b6b8971c88 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 73eb8b4ce7b..b2454191128 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 8375531e2b2..b2c72ea8e80 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; } |