OikawaKirie created this revision.
OikawaKirie added reviewers: NoQ, steakhal, vsavchenko, ASDenysPetrov.
OikawaKirie added a project: clang.
Herald added subscribers: manas, martong, dkrupp, donat.nagy, Szelethus, 
mikhail.ramalho, a.sidorin, szepet, baloghadamsoftware, xazax.hun.
OikawaKirie requested review of this revision.
Herald added a subscriber: cfe-commits.

In function `BugReporter::FlushReport`, function 
`PathSensitiveBugReporter::generatePathDiagnostics` will re-select one report 
from the equivalence class to construct the `PathDiagnostic`. It makes the 
report used to generate `PathDiagnostic` may be different from the one used in 
function `BugReporter::FlushReport`. Changes to the bug report via a 
`BugReporterVisitor` may not be reflected when generating report notes or 
fix-hints.

This patch tries to fix this problem by resetting the pointer to the example 
report selected in function `BugReporter::FlushReport` with the one used to 
generate `PathDiagnostic`.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D115716

Files:
  clang/include/clang/StaticAnalyzer/Core/BugReporter/BugReporter.h
  clang/lib/StaticAnalyzer/Core/BugReporter.cpp
  clang/test/Analysis/keychainAPI.m
  clang/test/Analysis/malloc-plist.c
  clang/test/Analysis/stream.c

Index: clang/test/Analysis/stream.c
===================================================================
--- clang/test/Analysis/stream.c
+++ clang/test/Analysis/stream.c
@@ -261,9 +261,7 @@
   if (!F1)
     return;
   if (Test == 1) {
-    return; // no warning
+    return; // expected-warning {{Opened stream never closed. Potential resource leak}}
   }
   rewind(F1);
-} // expected-warning {{Opened stream never closed. Potential resource leak}}
-// FIXME: This warning should be placed at the `return` above.
-// See https://reviews.llvm.org/D83120 about details.
+} // no warning
Index: clang/test/Analysis/malloc-plist.c
===================================================================
--- clang/test/Analysis/malloc-plist.c
+++ clang/test/Analysis/malloc-plist.c
@@ -135,8 +135,8 @@
 static void function_with_leak3(int y) {
     char *x = (char*)malloc(12);
     if (y)
-        y++;
-}//expected-warning{{Potential leak}}
+        y++;//expected-warning{{Potential leak}}
+}
 void use_function_with_leak3(int y) {
     function_with_leak3(y);
 }
Index: clang/test/Analysis/keychainAPI.m
===================================================================
--- clang/test/Analysis/keychainAPI.m
+++ clang/test/Analysis/keychainAPI.m
@@ -402,10 +402,10 @@
     OSStatus st = 0;
     void *outData = my_AllocateReturn(&st); 
     if (x) {
-      consumeChar(*(char*)outData); // expected-warning{{Allocated data is not released:}}
+      consumeChar(*(char*)outData);
       return;
     } else {
-      consumeChar(*(char*)outData);
+      consumeChar(*(char*)outData); // expected-warning{{Allocated data is not released:}}
     }
     return;
 }
Index: clang/lib/StaticAnalyzer/Core/BugReporter.cpp
===================================================================
--- clang/lib/StaticAnalyzer/Core/BugReporter.cpp
+++ clang/lib/StaticAnalyzer/Core/BugReporter.cpp
@@ -244,6 +244,9 @@
   std::unique_ptr<PathDiagnostic>
   generate(const PathDiagnosticConsumer *PDC) const;
 
+  /// Get the bug report used to generate the PathDiagnostic.
+  const PathSensitiveBugReport *getBugReport() const { return R; }
+
 private:
   void updateStackPiecesWithMessage(PathDiagnosticPieceRef P,
                                     const CallWithEntryStack &CallStack) const;
@@ -271,8 +274,6 @@
   PathDiagnosticLocation
   ExecutionContinues(llvm::raw_string_ostream &os,
                      const PathDiagnosticConstruct &C) const;
-
-  const PathSensitiveBugReport *getBugReport() const { return R; }
 };
 
 } // namespace
@@ -2874,6 +2875,7 @@
 
 std::unique_ptr<DiagnosticForConsumerMapTy>
 PathSensitiveBugReporter::generatePathDiagnostics(
+    const BugReport *&exampleReport,
     ArrayRef<PathDiagnosticConsumer *> consumers,
     ArrayRef<PathSensitiveBugReport *> &bugReports) {
   assert(!bugReports.empty());
@@ -2889,6 +2891,7 @@
         (*Out)[PC] = std::move(PD);
       }
     }
+    exampleReport = PDB->getBugReport();
   }
 
   return Out;
@@ -3062,7 +3065,7 @@
 
 void BugReporter::FlushReport(BugReportEquivClass& EQ) {
   SmallVector<BugReport*, 10> bugReports;
-  BugReport *report = findReportInEquivalenceClass(EQ, bugReports);
+  const BugReport *report = findReportInEquivalenceClass(EQ, bugReports);
   if (!report)
     return;
 
@@ -3200,7 +3203,8 @@
 
 std::unique_ptr<DiagnosticForConsumerMapTy>
 BugReporter::generateDiagnosticForConsumerMap(
-    BugReport *exampleReport, ArrayRef<PathDiagnosticConsumer *> consumers,
+    const BugReport *&exampleReport,
+    ArrayRef<PathDiagnosticConsumer *> consumers,
     ArrayRef<BugReport *> bugReports) {
   auto *basicReport = cast<BasicBugReport>(exampleReport);
   auto Out = std::make_unique<DiagnosticForConsumerMapTy>();
@@ -3272,11 +3276,10 @@
   }
 }
 
-
-
 std::unique_ptr<DiagnosticForConsumerMapTy>
 PathSensitiveBugReporter::generateDiagnosticForConsumerMap(
-    BugReport *exampleReport, ArrayRef<PathDiagnosticConsumer *> consumers,
+    const BugReport *&exampleReport,
+    ArrayRef<PathDiagnosticConsumer *> consumers,
     ArrayRef<BugReport *> bugReports) {
   std::vector<BasicBugReport *> BasicBugReports;
   std::vector<PathSensitiveBugReport *> PathSensitiveBugReports;
@@ -3296,7 +3299,7 @@
       reinterpret_cast<PathSensitiveBugReport *const *>(&*bugReports.begin()),
       reinterpret_cast<PathSensitiveBugReport *const *>(&*bugReports.end()));
   std::unique_ptr<DiagnosticForConsumerMapTy> Out = generatePathDiagnostics(
-      consumers, convertedArrayOfReports);
+      exampleReport, consumers, convertedArrayOfReports);
 
   if (Out->empty())
     return Out;
Index: clang/include/clang/StaticAnalyzer/Core/BugReporter/BugReporter.h
===================================================================
--- clang/include/clang/StaticAnalyzer/Core/BugReporter/BugReporter.h
+++ clang/include/clang/StaticAnalyzer/Core/BugReporter/BugReporter.h
@@ -210,7 +210,7 @@
     Notes.push_back(std::move(P));
   }
 
-  ArrayRef<std::shared_ptr<PathDiagnosticNotePiece>> getNotes() {
+  ArrayRef<std::shared_ptr<PathDiagnosticNotePiece>> getNotes() const {
     return Notes;
   }
 
@@ -657,7 +657,7 @@
 protected:
   /// Generate the diagnostics for the given bug report.
   virtual std::unique_ptr<DiagnosticForConsumerMapTy>
-  generateDiagnosticForConsumerMap(BugReport *exampleReport,
+  generateDiagnosticForConsumerMap(const BugReport *&exampleReport,
                                    ArrayRef<PathDiagnosticConsumer *> consumers,
                                    ArrayRef<BugReport *> bugReports);
 };
@@ -672,9 +672,10 @@
 
   /// Generate the diagnostics for the given bug report.
   std::unique_ptr<DiagnosticForConsumerMapTy>
-  generateDiagnosticForConsumerMap(BugReport *exampleReport,
+  generateDiagnosticForConsumerMap(const BugReport *&exampleReport,
                                    ArrayRef<PathDiagnosticConsumer *> consumers,
                                    ArrayRef<BugReport *> bugReports) override;
+
 public:
   PathSensitiveBugReporter(BugReporterData& d, ExprEngine& eng)
       : BugReporter(d), Eng(eng) {}
@@ -692,9 +693,10 @@
   /// \return A mapping from consumers to the corresponding diagnostics.
   /// Iterates through the bug reports within a single equivalence class,
   /// stops at a first non-invalidated report.
-  std::unique_ptr<DiagnosticForConsumerMapTy> generatePathDiagnostics(
-      ArrayRef<PathDiagnosticConsumer *> consumers,
-      ArrayRef<PathSensitiveBugReport *> &bugReports);
+  std::unique_ptr<DiagnosticForConsumerMapTy>
+  generatePathDiagnostics(const BugReport *&exampleReport,
+                          ArrayRef<PathDiagnosticConsumer *> consumers,
+                          ArrayRef<PathSensitiveBugReport *> &bugReports);
 
   void emitReport(std::unique_ptr<BugReport> R) override;
 };
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to