NoQ created this revision.
NoQ added a reviewer: dcoughlin.
Herald added subscribers: cfe-commits, Charusso, dkrupp, donat.nagy, Szelethus, 
mikhail.ramalho, a.sidorin, szepet, baloghadamsoftware, xazax.hun.
Herald added a project: clang.

IIG is a replacement for MIG in DriverKit: IIG is autogenerating C++ code. 
Suppress dead store warnings on such code, as the tool seems to be producing 
them regularly, and the users of IIG are not in position to address these 
warnings, as they don't control the autogenerated code. IIG-generated code is 
identified by looking at the comments at the top of the file.


Repository:
  rC Clang

https://reviews.llvm.org/D63118

Files:
  clang/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp
  clang/test/Analysis/deadstores-driverkit.cpp
  clang/test/Analysis/os_object_base.h


Index: clang/test/Analysis/os_object_base.h
===================================================================
--- clang/test/Analysis/os_object_base.h
+++ clang/test/Analysis/os_object_base.h
@@ -19,6 +19,9 @@
 
 using size_t = decltype(sizeof(int));
 
+typedef int kern_return_t;
+struct IORPC {};
+
 struct OSMetaClass;
 
 struct OSMetaClassBase {
@@ -37,8 +40,13 @@
 
   virtual void free();
   virtual ~OSMetaClassBase(){};
+
+  kern_return_t Invoke(IORPC invoke);
 };
 
+typedef kern_return_t (*OSDispatchMethod)(OSMetaClassBase *self,
+                                          const IORPC rpc);
+
 struct OSObject : public OSMetaClassBase {
   virtual ~OSObject(){}
 
Index: clang/test/Analysis/deadstores-driverkit.cpp
===================================================================
--- /dev/null
+++ clang/test/Analysis/deadstores-driverkit.cpp
@@ -0,0 +1,24 @@
+/* iig generated from SomethingSomething.iig */
+
+// The comment above is the whole point of the test.
+// That's how the suppression works.
+// It needs to be on the top.
+// Run-lines can wait.
+
+// RUN: %clang_analyze_cc1 -w -triple x86_64-apple-driverkit19.0 \
+// RUN:   -analyzer-checker=deadcode -verify %s
+
+// expected-no-diagnostics
+
+#include "os_object_base.h"
+
+class OSSomething {
+  kern_return_t Invoke(const IORPC);
+  void foo(OSDispatchMethod supermethod) {
+    kern_return_t ret;
+    IORPC rpc;
+    // Test the DriverKit specific suppression in the dead stores checker.
+    if (supermethod) ret = supermethod((OSObject *)this, rpc); // no-warning
+    else             ret = ((OSObject *)this)->Invoke(rpc); // no-warning
+  }
+};
Index: clang/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp
===================================================================
--- clang/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp
+++ clang/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp
@@ -160,6 +160,26 @@
     return InEH->count(D);
   }
 
+  bool isSuppressed(SourceRange R) {
+    SourceManager &SMgr = Ctx.getSourceManager();
+    SourceLocation Loc = R.getBegin();
+    if (!Loc.isValid())
+      return false;
+
+    FileID FID = SMgr.getFileID(Loc);
+    bool Invalid = false;
+    StringRef Data = SMgr.getBufferData(FID, &Invalid);
+    if (Invalid)
+      return false;
+
+    // Files autogenerated by DriverKit IIG contain some dead stores that
+    // we don't want to report.
+    if (Data.startswith("/* iig generated from"))
+      return true;
+
+    return false;
+  }
+
   void Report(const VarDecl *V, DeadStoreKind dsk,
               PathDiagnosticLocation L, SourceRange R) {
     if (Escaped.count(V))
@@ -175,6 +195,9 @@
     if (!reachableCode->isReachable(currentBlock))
       return;
 
+    if (isSuppressed(R))
+      return;
+
     SmallString<64> buf;
     llvm::raw_svector_ostream os(buf);
     const char *BugType = nullptr;


Index: clang/test/Analysis/os_object_base.h
===================================================================
--- clang/test/Analysis/os_object_base.h
+++ clang/test/Analysis/os_object_base.h
@@ -19,6 +19,9 @@
 
 using size_t = decltype(sizeof(int));
 
+typedef int kern_return_t;
+struct IORPC {};
+
 struct OSMetaClass;
 
 struct OSMetaClassBase {
@@ -37,8 +40,13 @@
 
   virtual void free();
   virtual ~OSMetaClassBase(){};
+
+  kern_return_t Invoke(IORPC invoke);
 };
 
+typedef kern_return_t (*OSDispatchMethod)(OSMetaClassBase *self,
+                                          const IORPC rpc);
+
 struct OSObject : public OSMetaClassBase {
   virtual ~OSObject(){}
 
Index: clang/test/Analysis/deadstores-driverkit.cpp
===================================================================
--- /dev/null
+++ clang/test/Analysis/deadstores-driverkit.cpp
@@ -0,0 +1,24 @@
+/* iig generated from SomethingSomething.iig */
+
+// The comment above is the whole point of the test.
+// That's how the suppression works.
+// It needs to be on the top.
+// Run-lines can wait.
+
+// RUN: %clang_analyze_cc1 -w -triple x86_64-apple-driverkit19.0 \
+// RUN:   -analyzer-checker=deadcode -verify %s
+
+// expected-no-diagnostics
+
+#include "os_object_base.h"
+
+class OSSomething {
+  kern_return_t Invoke(const IORPC);
+  void foo(OSDispatchMethod supermethod) {
+    kern_return_t ret;
+    IORPC rpc;
+    // Test the DriverKit specific suppression in the dead stores checker.
+    if (supermethod) ret = supermethod((OSObject *)this, rpc); // no-warning
+    else             ret = ((OSObject *)this)->Invoke(rpc); // no-warning
+  }
+};
Index: clang/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp
===================================================================
--- clang/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp
+++ clang/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp
@@ -160,6 +160,26 @@
     return InEH->count(D);
   }
 
+  bool isSuppressed(SourceRange R) {
+    SourceManager &SMgr = Ctx.getSourceManager();
+    SourceLocation Loc = R.getBegin();
+    if (!Loc.isValid())
+      return false;
+
+    FileID FID = SMgr.getFileID(Loc);
+    bool Invalid = false;
+    StringRef Data = SMgr.getBufferData(FID, &Invalid);
+    if (Invalid)
+      return false;
+
+    // Files autogenerated by DriverKit IIG contain some dead stores that
+    // we don't want to report.
+    if (Data.startswith("/* iig generated from"))
+      return true;
+
+    return false;
+  }
+
   void Report(const VarDecl *V, DeadStoreKind dsk,
               PathDiagnosticLocation L, SourceRange R) {
     if (Escaped.count(V))
@@ -175,6 +195,9 @@
     if (!reachableCode->isReachable(currentBlock))
       return;
 
+    if (isSuppressed(R))
+      return;
+
     SmallString<64> buf;
     llvm::raw_svector_ostream os(buf);
     const char *BugType = nullptr;
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
  • [PATCH] D63118: [analyzer]... Artem Dergachev via Phabricator via cfe-commits

Reply via email to