[clang] badba51 - [analyzer] NonnullGlobalConstants: Add support for kCFNull.

Artem Dergachev via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 18 12:08:25 PST 2019


Author: Artem Dergachev
Date: 2019-12-18T12:08:15-08:00
New Revision: badba5118ff5cc6d61aeca6ee2dc2ead5bb5286f

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

LOG: [analyzer] NonnullGlobalConstants: Add support for kCFNull.

It's a singleton in CoreFoundation that always contains a non-null CFNullRef.

Added: 
    

Modified: 
    clang/lib/StaticAnalyzer/Checkers/NonnullGlobalConstantsChecker.cpp
    clang/test/Analysis/nonnull-global-constants.mm

Removed: 
    


################################################################################
diff  --git a/clang/lib/StaticAnalyzer/Checkers/NonnullGlobalConstantsChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/NonnullGlobalConstantsChecker.cpp
index 43dbe57b8432..6efba433eed2 100644
--- a/clang/lib/StaticAnalyzer/Checkers/NonnullGlobalConstantsChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/NonnullGlobalConstantsChecker.cpp
@@ -36,6 +36,7 @@ class NonnullGlobalConstantsChecker : public Checker<check::Location> {
   mutable IdentifierInfo *NSStringII = nullptr;
   mutable IdentifierInfo *CFStringRefII = nullptr;
   mutable IdentifierInfo *CFBooleanRefII = nullptr;
+  mutable IdentifierInfo *CFNullRefII = nullptr;
 
 public:
   NonnullGlobalConstantsChecker() {}
@@ -61,6 +62,7 @@ void NonnullGlobalConstantsChecker::initIdentifierInfo(ASTContext &Ctx) const {
   NSStringII = &Ctx.Idents.get("NSString");
   CFStringRefII = &Ctx.Idents.get("CFStringRef");
   CFBooleanRefII = &Ctx.Idents.get("CFBooleanRef");
+  CFNullRefII = &Ctx.Idents.get("CFNullRef");
 }
 
 /// Add an assumption that const string-like globals are non-null.
@@ -136,7 +138,7 @@ bool NonnullGlobalConstantsChecker::isNonnullType(QualType Ty) const {
       T->getInterfaceDecl()->getIdentifier() == NSStringII;
   } else if (auto *T = dyn_cast<TypedefType>(Ty)) {
     IdentifierInfo* II = T->getDecl()->getIdentifier();
-    return II == CFStringRefII || II == CFBooleanRefII;
+    return II == CFStringRefII || II == CFBooleanRefII || II == CFNullRefII;
   }
   return false;
 }

diff  --git a/clang/test/Analysis/nonnull-global-constants.mm b/clang/test/Analysis/nonnull-global-constants.mm
index 9e1a588ba47a..8c174c48b30d 100644
--- a/clang/test/Analysis/nonnull-global-constants.mm
+++ b/clang/test/Analysis/nonnull-global-constants.mm
@@ -7,7 +7,11 @@
 
 @class NSString;
 typedef const struct __CFString *CFStringRef;
-typedef const struct __CFBoolean * CFBooleanRef;
+typedef const struct __CFBoolean *CFBooleanRef;
+
+#define CF_BRIDGED_TYPE(T) __attribute__((objc_bridge(T)))
+typedef const struct CF_BRIDGED_TYPE(NSNull) __CFNull *CFNullRef;
+extern const CFNullRef kCFNull;
 
 // Global NSString* is non-null.
 extern NSString *const StringConstGlobal;
@@ -113,3 +117,7 @@ void testNonnullNonconstCFString() {
 void testNonnullNonnullCFString() {
   clang_analyzer_eval(str4); // expected-warning{{TRUE}}
 }
+
+void test_kCFNull() {
+  clang_analyzer_eval(kCFNull); // expected-warning{{TRUE}}
+}


        


More information about the cfe-commits mailing list