RedDocMD updated this revision to Diff 327396.
RedDocMD added a comment.

Should not emit note if raw pointer cannot be tracked


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D97183

Files:
  clang/lib/StaticAnalyzer/Checkers/SmartPtrModeling.cpp
  clang/test/Analysis/smart-ptr-text-output.cpp


Index: clang/test/Analysis/smart-ptr-text-output.cpp
===================================================================
--- clang/test/Analysis/smart-ptr-text-output.cpp
+++ clang/test/Analysis/smart-ptr-text-output.cpp
@@ -306,10 +306,18 @@
 };
 
 void derefAfterBranchingOnUnknownInnerPtr(std::unique_ptr<A> P) {
-  A *RP = P.get();
+  A *RP = P.get(); // expected-note {{Obtained null inner pointer from 'P'}}
   if (!RP) { // expected-note {{Assuming 'RP' is null}}
     // expected-note@-1 {{Taking true branch}}
     P->foo(); // expected-warning {{Dereference of null smart pointer 'P' 
[alpha.cplusplus.SmartPtr]}}
     // expected-note@-1{{Dereference of null smart pointer 'P'}}
   }
 }
+
+void getShouldNotAlwaysLeaveANote() {
+       std::unique_ptr<A> P; // expected-note {{Default constructed smart 
pointer 'P' is null}}
+       A *a = P.get(); 
+       P->foo(); // expected-warning {{Dereference of null smart pointer 'P' 
[alpha.cplusplus.SmartPtr]}}
+    // expected-note@-1{{Dereference of null smart pointer 'P'}}
+}
+
Index: clang/lib/StaticAnalyzer/Checkers/SmartPtrModeling.cpp
===================================================================
--- clang/lib/StaticAnalyzer/Checkers/SmartPtrModeling.cpp
+++ clang/lib/StaticAnalyzer/Checkers/SmartPtrModeling.cpp
@@ -457,8 +457,22 @@
 
   State = State->BindExpr(Call.getOriginExpr(), C.getLocationContext(),
                           InnerPointerVal);
-  // TODO: Add NoteTag, for how the raw pointer got using 'get' method.
-  C.addTransition(State);
+
+  C.addTransition(State, C.getNoteTag([ThisRegion, InnerPointerVal,
+                                       State](PathSensitiveBugReport &BR,
+                                              llvm::raw_ostream &OS) {
+    if (&BR.getBugType() != smartptr::getNullDereferenceBugType() ||
+        !BR.isInteresting(ThisRegion))
+      return;
+    if (!State->assume(InnerPointerVal.castAs<DefinedSVal>(), true))
+      return;
+    if (ThisRegion->canPrintPretty()) {
+      OS << "Obtained null inner pointer from";
+      checkAndPrettyPrintRegion(OS, ThisRegion);
+    } else {
+      OS << "Obtained null inner pointer here";
+    }
+  }));
 }
 
 bool SmartPtrModeling::handleAssignOp(const CallEvent &Call,


Index: clang/test/Analysis/smart-ptr-text-output.cpp
===================================================================
--- clang/test/Analysis/smart-ptr-text-output.cpp
+++ clang/test/Analysis/smart-ptr-text-output.cpp
@@ -306,10 +306,18 @@
 };
 
 void derefAfterBranchingOnUnknownInnerPtr(std::unique_ptr<A> P) {
-  A *RP = P.get();
+  A *RP = P.get(); // expected-note {{Obtained null inner pointer from 'P'}}
   if (!RP) { // expected-note {{Assuming 'RP' is null}}
     // expected-note@-1 {{Taking true branch}}
     P->foo(); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}}
     // expected-note@-1{{Dereference of null smart pointer 'P'}}
   }
 }
+
+void getShouldNotAlwaysLeaveANote() {
+	std::unique_ptr<A> P; // expected-note {{Default constructed smart pointer 'P' is null}}
+	A *a = P.get(); 
+	P->foo(); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}}
+    // expected-note@-1{{Dereference of null smart pointer 'P'}}
+}
+
Index: clang/lib/StaticAnalyzer/Checkers/SmartPtrModeling.cpp
===================================================================
--- clang/lib/StaticAnalyzer/Checkers/SmartPtrModeling.cpp
+++ clang/lib/StaticAnalyzer/Checkers/SmartPtrModeling.cpp
@@ -457,8 +457,22 @@
 
   State = State->BindExpr(Call.getOriginExpr(), C.getLocationContext(),
                           InnerPointerVal);
-  // TODO: Add NoteTag, for how the raw pointer got using 'get' method.
-  C.addTransition(State);
+
+  C.addTransition(State, C.getNoteTag([ThisRegion, InnerPointerVal,
+                                       State](PathSensitiveBugReport &BR,
+                                              llvm::raw_ostream &OS) {
+    if (&BR.getBugType() != smartptr::getNullDereferenceBugType() ||
+        !BR.isInteresting(ThisRegion))
+      return;
+    if (!State->assume(InnerPointerVal.castAs<DefinedSVal>(), true))
+      return;
+    if (ThisRegion->canPrintPretty()) {
+      OS << "Obtained null inner pointer from";
+      checkAndPrettyPrintRegion(OS, ThisRegion);
+    } else {
+      OS << "Obtained null inner pointer here";
+    }
+  }));
 }
 
 bool SmartPtrModeling::handleAssignOp(const CallEvent &Call,
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to