[clang] 85d97aa - [analyzer] Support implicit parameter 'self' in path note

Jan Korous via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 21 17:27:24 PDT 2022


Author: Jan Korous
Date: 2022-09-21T17:26:09-07:00
New Revision: 85d97aac80b8e7689a6d957441054aa817bbb192

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

LOG: [analyzer] Support implicit parameter 'self' in path note

showBRParamDiagnostics assumed stores happen only via function parameters while that
can also happen via implicit parameters like 'self' or 'this'.
The regression test caused a failed assert in the original cast to ParmVarDecl.

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

Added: 
    clang/test/Analysis/path-notes-impl-param.m

Modified: 
    clang/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp b/clang/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp
index 3796ad51694ae..4f23af07e3ae4 100644
--- a/clang/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp
+++ b/clang/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp
@@ -1340,13 +1340,12 @@ static void showBRDiagnostics(llvm::raw_svector_ostream &OS, StoreInfo SI) {
 static void showBRParamDiagnostics(llvm::raw_svector_ostream &OS,
                                    StoreInfo SI) {
   const auto *VR = cast<VarRegion>(SI.Dest);
-  const auto *Param = cast<ParmVarDecl>(VR->getDecl());
+  const auto *D = VR->getDecl();
 
   OS << "Passing ";
 
   if (isa<loc::ConcreteInt>(SI.Value)) {
-    OS << (isObjCPointer(Param) ? "nil object reference"
-                                : "null pointer value");
+    OS << (isObjCPointer(D) ? "nil object reference" : "null pointer value");
 
   } else if (SI.Value.isUndef()) {
     OS << "uninitialized value";
@@ -1361,12 +1360,19 @@ static void showBRParamDiagnostics(llvm::raw_svector_ostream &OS,
     OS << "value";
   }
 
-  // Printed parameter indexes are 1-based, not 0-based.
-  unsigned Idx = Param->getFunctionScopeIndex() + 1;
-  OS << " via " << Idx << llvm::getOrdinalSuffix(Idx) << " parameter";
-  if (VR->canPrintPretty()) {
-    OS << " ";
-    VR->printPretty(OS);
+  if (const auto *Param = dyn_cast<ParmVarDecl>(VR->getDecl())) {
+    // Printed parameter indexes are 1-based, not 0-based.
+    unsigned Idx = Param->getFunctionScopeIndex() + 1;
+    OS << " via " << Idx << llvm::getOrdinalSuffix(Idx) << " parameter";
+    if (VR->canPrintPretty()) {
+      OS << " ";
+      VR->printPretty(OS);
+    }
+  } else if (const auto *ImplParam = dyn_cast<ImplicitParamDecl>(D)) {
+    if (ImplParam->getParameterKind() ==
+        ImplicitParamDecl::ImplicitParamKind::ObjCSelf) {
+      OS << " via implicit parameter 'self'";
+    }
   }
 }
 

diff  --git a/clang/test/Analysis/path-notes-impl-param.m b/clang/test/Analysis/path-notes-impl-param.m
new file mode 100644
index 0000000000000..cc1069ab96c36
--- /dev/null
+++ b/clang/test/Analysis/path-notes-impl-param.m
@@ -0,0 +1,31 @@
+// RUN: %clang_analyze_cc1 -analyzer-checker=core,osx.cocoa.RetainCount -analyzer-output=text -verify %s
+
+ at protocol NSObject
+ at end
+
+ at interface NSObject <NSObject> {}
+- (id)init;
++ (id)alloc;
+- (id)autorelease;
+ at end
+
+ at interface Foo : NSObject
+ at property(nonatomic) int bar;
+ at end
+
+ at implementation Foo
+-(int)bar {
+  return 0;
+}
+ at end
+
+int baz() {
+  Foo *f = [Foo alloc];
+  // expected-note at -1 {{'f' initialized here}}
+  // expected-note at -2 {{Method returns an instance of Foo with a +1 retain count}}
+
+  return f.bar;
+  // expected-warning at -1 {{Potential leak of an object stored into 'self' [osx.cocoa.RetainCount]}}
+  // expected-note at -2 {{Passing value via implicit parameter 'self'}}
+  // expected-note at -3 {{Object leaked: object allocated and stored into 'self' is not referenced later in this execution path and has a retain count of +1}}
+}


        


More information about the cfe-commits mailing list