[PATCH] D77551: [analyzer] Fix NSErrorChecker false positives

Artem Dergachev via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 6 10:52:11 PDT 2020


This revision was automatically updated to reflect the committed changes.
Closed by commit rG9b1e4a8218b7: [analyzer] Fix NSErrorChecker false positives on constructors. (authored by dergachev.a).

Changed prior to commit:
  https://reviews.llvm.org/D77551?vs=255337&id=255407#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77551/new/

https://reviews.llvm.org/D77551

Files:
  clang/lib/StaticAnalyzer/Checkers/NSErrorChecker.cpp
  clang/test/Analysis/SpecialFunctionsCFError.cpp


Index: clang/test/Analysis/SpecialFunctionsCFError.cpp
===================================================================
--- /dev/null
+++ clang/test/Analysis/SpecialFunctionsCFError.cpp
@@ -0,0 +1,28 @@
+// RUN: %clang_analyze_cc1 -analyzer-checker=core,osx.coreFoundation.CFError \
+// RUN:   -verify %s
+
+typedef unsigned long size_t;
+struct __CFError {};
+typedef struct __CFError *CFErrorRef;
+void *malloc(size_t);
+
+class Foo {
+public:
+  Foo(CFErrorRef *error) {} // no-warning
+
+  void operator delete(void *pointer, CFErrorRef *error) { // no-warning
+    return;
+  }
+
+  void operator delete[](void *pointer, CFErrorRef *error) { // no-warning
+    return;
+  }
+
+  // Check that we report warnings for operators when it can be useful
+  void operator()(CFErrorRef *error) {} // expected-warning {{Function accepting CFErrorRef* should have a non-void return value to indicate whether or not an error occurred}}
+};
+
+// Check that global delete operator is not bothered as well
+void operator delete(void *pointer, CFErrorRef *error) { // no-warning
+  return;
+}
Index: clang/lib/StaticAnalyzer/Checkers/NSErrorChecker.cpp
===================================================================
--- clang/lib/StaticAnalyzer/Checkers/NSErrorChecker.cpp
+++ clang/lib/StaticAnalyzer/Checkers/NSErrorChecker.cpp
@@ -95,6 +95,15 @@
 };
 }
 
+static bool hasReservedReturnType(const FunctionDecl *D) {
+  if (isa<CXXConstructorDecl>(D))
+    return true;
+
+  // operators delete and delete[] are required to have 'void' return type
+  auto OperatorKind = D->getOverloadedOperator();
+  return OperatorKind == OO_Delete || OperatorKind == OO_Array_Delete;
+}
+
 void CFErrorFunctionChecker::checkASTDecl(const FunctionDecl *D,
                                         AnalysisManager &mgr,
                                         BugReporter &BR) const {
@@ -102,6 +111,8 @@
     return;
   if (!D->getReturnType()->isVoidType())
     return;
+  if (hasReservedReturnType(D))
+    return;
 
   if (!II)
     II = &D->getASTContext().Idents.get("CFErrorRef");


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D77551.255407.patch
Type: text/x-patch
Size: 2075 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200406/56cb1933/attachment.bin>


More information about the cfe-commits mailing list