[clang-tools-extra] r258714 - 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.
Aaron Ballman via cfe-commits
cfe-commits at lists.llvm.org
Mon Jan 25 12:00:53 PST 2016
Author: aaronballman
Date: Mon Jan 25 14:00:53 2016
New Revision: 258714
URL: http://llvm.org/viewvc/llvm-project?rev=258714&view=rev
Log:
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.
Modified:
clang-tools-extra/trunk/clang-tidy/misc/AssertSideEffectCheck.cpp
clang-tools-extra/trunk/test/clang-tidy/misc-assert-side-effect.cpp
Modified: clang-tools-extra/trunk/clang-tidy/misc/AssertSideEffectCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/AssertSideEffectCheck.cpp?rev=258714&r1=258713&r2=258714&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/misc/AssertSideEffectCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/misc/AssertSideEffectCheck.cpp Mon Jan 25 14:00:53 2016
@@ -83,11 +83,18 @@ void AssertSideEffectCheck::storeOptions
}
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);
}
Modified: clang-tools-extra/trunk/test/clang-tidy/misc-assert-side-effect.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/misc-assert-side-effect.cpp?rev=258714&r1=258713&r2=258714&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/misc-assert-side-effect.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/misc-assert-side-effect.cpp Mon Jan 25 14:00:53 2016
@@ -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;
}
More information about the cfe-commits
mailing list