[clang-tools-extra] b92a39a - [clang-tidy] bugprone-assert-side-effect: Warn on NSAssert by default.

Artem Dergachev via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 28 22:32:02 PST 2021


Author: Artem Dergachev
Date: 2021-01-28T22:31:49-08:00
New Revision: b92a39ac1319c796777bca19a3af2856acbc69c1

URL: https://github.com/llvm/llvm-project/commit/b92a39ac1319c796777bca19a3af2856acbc69c1
DIFF: https://github.com/llvm/llvm-project/commit/b92a39ac1319c796777bca19a3af2856acbc69c1.diff

LOG: [clang-tidy] bugprone-assert-side-effect: Warn on NSAssert by default.

NSAssert and NSCAssert are Objective-C Foundation's standard assert macros.

Differential Revision: https://reviews.llvm.org/D95519

Added: 
    clang-tools-extra/test/clang-tidy/checkers/bugprone-assert-side-effect.m

Modified: 
    clang-tools-extra/clang-tidy/bugprone/AssertSideEffectCheck.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/bugprone/AssertSideEffectCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/AssertSideEffectCheck.cpp
index 0d8834e4135f..4e2359ff4f67 100644
--- a/clang-tools-extra/clang-tidy/bugprone/AssertSideEffectCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/AssertSideEffectCheck.cpp
@@ -72,7 +72,8 @@ AssertSideEffectCheck::AssertSideEffectCheck(StringRef Name,
                                              ClangTidyContext *Context)
     : ClangTidyCheck(Name, Context),
       CheckFunctionCalls(Options.get("CheckFunctionCalls", false)),
-      RawAssertList(Options.get("AssertMacros", "assert")) {
+      RawAssertList(Options.get("AssertMacros",
+                                "assert,NSAssert,NSCAssert")) {
   StringRef(RawAssertList).split(AssertMacros, ",", -1, false);
 }
 

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone-assert-side-effect.m b/clang-tools-extra/test/clang-tidy/checkers/bugprone-assert-side-effect.m
new file mode 100644
index 000000000000..bca79565cea0
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone-assert-side-effect.m
@@ -0,0 +1,53 @@
+// RUN: %check_clang_tidy %s bugprone-assert-side-effect %t
+
+int abort();
+
+ at interface NSObject
+ at end
+
+ at interface NSString
+ at end
+
+ at interface NSAssertionHandler
++ (NSAssertionHandler *)currentHandler;
+- handleFailureInMethod:(SEL)cmd object:(NSObject *)obj desc:(NSString *)desc;
+- handleFailureInFunction:(NSString *)desc;
+ at end
+
+#ifndef NDEBUG
+#define NSAssert(condition, description, ...)                                    \
+  do {                                                                           \
+    if (__builtin_expect(!(condition), 0)) {                                     \
+      [[NSAssertionHandler currentHandler] handleFailureInMethod:_cmd            \
+                                                          object:self            \
+                                                            desc:(description)]; \
+    }                                                                            \
+  } while (0);
+#define NSCAssert(condition, description, ...)                                     \
+  do {                                                                             \
+    if (__builtin_expect(!(condition), 0)) {                                       \
+      [[NSAssertionHandler currentHandler] handleFailureInFunction:(description)]; \
+    }                                                                              \
+  } while (0);
+#else
+#define NSAssert(condition, description, ...) do {} while (0)
+#define NSCAssert(condition, description, ...) do {} while (0)
+#endif
+
+ at interface I : NSObject
+- (void)foo;
+ at end
+
+ at implementation I
+- (void)foo {
+  int x = 0;
+  NSAssert((++x) == 1, @"Ugh.");
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: side effect in NSAssert() condition discarded in release builds [bugprone-assert-side-effect]
+}
+ at end
+
+void foo() {
+  int x = 0;
+  NSCAssert((++x) == 1, @"Ugh.");
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: side effect in NSCAssert() condition discarded in release builds [bugprone-assert-side-effect]
+}


        


More information about the cfe-commits mailing list