Add support to the misc-assert-side-effect check for MSVC-style assert macros, which use !! instead of an if statement or a conditional operator.

llvm-svn: 258714
This commit is contained in:
Aaron Ballman 2016-01-25 20:00:53 +00:00
parent 92f0f7c2f3
commit a5d55a4617
2 changed files with 21 additions and 5 deletions

View File

@ -83,11 +83,18 @@ void AssertSideEffectCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
}
void AssertSideEffectCheck::registerMatchers(MatchFinder *Finder) {
auto ConditionWithSideEffect =
hasCondition(hasDescendant(expr(hasSideEffect(CheckFunctionCalls))));
auto DescendantWithSideEffect =
hasDescendant(expr(hasSideEffect(CheckFunctionCalls)));
auto ConditionWithSideEffect = hasCondition(DescendantWithSideEffect);
Finder->addMatcher(
stmt(anyOf(conditionalOperator(ConditionWithSideEffect),
ifStmt(ConditionWithSideEffect))).bind("condStmt"),
stmt(
anyOf(conditionalOperator(ConditionWithSideEffect),
ifStmt(ConditionWithSideEffect),
unaryOperator(hasOperatorName("!"),
hasUnaryOperand(unaryOperator(
hasOperatorName("!"),
hasUnaryOperand(DescendantWithSideEffect))))))
.bind("condStmt"),
this);
}

View File

@ -1,4 +1,4 @@
// RUN: %check_clang_tidy %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,convoluted_assert'}]}" -- -fexceptions
// RUN: %check_clang_tidy %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,convoluted_assert,msvc_assert'}]}" -- -fexceptions
//===--- assert definition block ------------------------------------------===//
int abort() { return 0; }
@ -35,6 +35,12 @@ void print(...);
#define wrap2(x) wrap1(x)
#define convoluted_assert(x) wrap2(x)
#define msvc_assert(expression) (void)( \
(!!(expression)) || \
(abort(), 0) \
)
//===----------------------------------------------------------------------===//
class MyClass {
@ -101,5 +107,8 @@ int main() {
assert2(1 == 2 - 1);
msvc_assert(mc2 = mc);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found msvc_assert() with side effect
return 0;
}