r371660 - [analyzer] NFC: Move resetDiagnosticLocationToMainFile() to BugReporter.

Artem Dergachev via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 11 13:54:24 PDT 2019


Author: dergachev
Date: Wed Sep 11 13:54:24 2019
New Revision: 371660

URL: http://llvm.org/viewvc/llvm-project?rev=371660&view=rev
Log:
[analyzer] NFC: Move resetDiagnosticLocationToMainFile() to BugReporter.

This method of PathDiagnostic is a part of Static Analyzer's particular
path diagnostic construction scheme. As such, it doesn't belong to
the PathDiagnostic class, but to the Analyzer.

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

Modified:
    cfe/trunk/include/clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h
    cfe/trunk/lib/StaticAnalyzer/Core/BugReporter.cpp
    cfe/trunk/lib/StaticAnalyzer/Core/PathDiagnostic.cpp

Modified: cfe/trunk/include/clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h?rev=371660&r1=371659&r2=371660&view=diff
==============================================================================
--- cfe/trunk/include/clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h (original)
+++ cfe/trunk/include/clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h Wed Sep 11 13:54:24 2019
@@ -792,11 +792,6 @@ public:
     VerboseDesc += S;
   }
 
-  /// If the last piece of the report point to the header file, resets
-  /// the location of the report to be the last location in the main source
-  /// file.
-  void resetDiagnosticLocationToMainFile();
-
   StringRef getVerboseDescription() const { return VerboseDesc; }
 
   StringRef getShortDescription() const {
@@ -807,11 +802,6 @@ public:
   StringRef getBugType() const { return BugType; }
   StringRef getCategory() const { return Category; }
 
-  /// Return the semantic context where an issue occurred.  If the
-  /// issue occurs along a path, this represents the "central" area
-  /// where the bug manifests.
-  const Decl *getDeclWithIssue() const { return DeclWithIssue; }
-
   using meta_iterator = std::deque<std::string>::const_iterator;
 
   meta_iterator meta_begin() const { return OtherDesc.begin(); }
@@ -826,10 +816,23 @@ public:
     return *ExecutedLines;
   }
 
+  /// Return the semantic context where an issue occurred.  If the
+  /// issue occurs along a path, this represents the "central" area
+  /// where the bug manifests.
+  const Decl *getDeclWithIssue() const { return DeclWithIssue; }
+
+  void setDeclWithIssue(const Decl *D) {
+    DeclWithIssue = D;
+  }
+
   PathDiagnosticLocation getLocation() const {
     return Loc;
   }
 
+  void setLocation(PathDiagnosticLocation NewLoc) {
+    Loc = NewLoc;
+  }
+
   /// Get the location on which the report should be uniqued.
   PathDiagnosticLocation getUniqueingLoc() const {
     return UniqueingLoc;

Modified: cfe/trunk/lib/StaticAnalyzer/Core/BugReporter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/BugReporter.cpp?rev=371660&r1=371659&r2=371660&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Core/BugReporter.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Core/BugReporter.cpp Wed Sep 11 13:54:24 2019
@@ -3125,6 +3125,71 @@ BugReporter::generateDiagnosticForConsum
   return Out;
 }
 
+static PathDiagnosticCallPiece *
+getFirstStackedCallToHeaderFile(PathDiagnosticCallPiece *CP,
+                                const SourceManager &SMgr) {
+  SourceLocation CallLoc = CP->callEnter.asLocation();
+
+  // If the call is within a macro, don't do anything (for now).
+  if (CallLoc.isMacroID())
+    return nullptr;
+
+  assert(AnalysisManager::isInCodeFile(CallLoc, SMgr) &&
+         "The call piece should not be in a header file.");
+
+  // Check if CP represents a path through a function outside of the main file.
+  if (!AnalysisManager::isInCodeFile(CP->callEnterWithin.asLocation(), SMgr))
+    return CP;
+
+  const PathPieces &Path = CP->path;
+  if (Path.empty())
+    return nullptr;
+
+  // Check if the last piece in the callee path is a call to a function outside
+  // of the main file.
+  if (auto *CPInner = dyn_cast<PathDiagnosticCallPiece>(Path.back().get()))
+    return getFirstStackedCallToHeaderFile(CPInner, SMgr);
+
+  // Otherwise, the last piece is in the main file.
+  return nullptr;
+}
+
+static void resetDiagnosticLocationToMainFile(PathDiagnostic &PD) {
+  if (PD.path.empty())
+    return;
+
+  PathDiagnosticPiece *LastP = PD.path.back().get();
+  assert(LastP);
+  const SourceManager &SMgr = LastP->getLocation().getManager();
+
+  // We only need to check if the report ends inside headers, if the last piece
+  // is a call piece.
+  if (auto *CP = dyn_cast<PathDiagnosticCallPiece>(LastP)) {
+    CP = getFirstStackedCallToHeaderFile(CP, SMgr);
+    if (CP) {
+      // Mark the piece.
+       CP->setAsLastInMainSourceFile();
+
+      // Update the path diagnostic message.
+      const auto *ND = dyn_cast<NamedDecl>(CP->getCallee());
+      if (ND) {
+        SmallString<200> buf;
+        llvm::raw_svector_ostream os(buf);
+        os << " (within a call to '" << ND->getDeclName() << "')";
+        PD.appendToDesc(os.str());
+      }
+
+      // Reset the report containing declaration and location.
+      PD.setDeclWithIssue(CP->getCaller());
+      PD.setLocation(CP->getLocation());
+
+      return;
+    }
+  }
+}
+
+
+
 std::unique_ptr<DiagnosticForConsumerMapTy>
 PathSensitiveBugReporter::generateDiagnosticForConsumerMap(
     BugReport *exampleReport, ArrayRef<PathDiagnosticConsumer *> consumers,
@@ -3159,7 +3224,7 @@ PathSensitiveBugReporter::generateDiagno
   const AnalyzerOptions &Opts = getAnalyzerOptions();
   for (auto const &P : *Out)
     if (Opts.ShouldReportIssuesInMainSourceFile && !Opts.AnalyzeAll)
-      P.second->resetDiagnosticLocationToMainFile();
+      resetDiagnosticLocationToMainFile(*P.second);
 
   return Out;
 }

Modified: cfe/trunk/lib/StaticAnalyzer/Core/PathDiagnostic.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/PathDiagnostic.cpp?rev=371660&r1=371659&r2=371660&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Core/PathDiagnostic.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Core/PathDiagnostic.cpp Wed Sep 11 13:54:24 2019
@@ -29,7 +29,6 @@
 #include "clang/Basic/LLVM.h"
 #include "clang/Basic/SourceLocation.h"
 #include "clang/Basic/SourceManager.h"
-#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/FoldingSet.h"
 #include "llvm/ADT/None.h"
@@ -130,69 +129,6 @@ PathDiagnostic::PathDiagnostic(
       UniqueingDecl(DeclToUnique), ExecutedLines(std::move(ExecutedLines)),
       path(pathImpl) {}
 
-static PathDiagnosticCallPiece *
-getFirstStackedCallToHeaderFile(PathDiagnosticCallPiece *CP,
-                                const SourceManager &SMgr) {
-  SourceLocation CallLoc = CP->callEnter.asLocation();
-
-  // If the call is within a macro, don't do anything (for now).
-  if (CallLoc.isMacroID())
-    return nullptr;
-
-  assert(AnalysisManager::isInCodeFile(CallLoc, SMgr) &&
-         "The call piece should not be in a header file.");
-
-  // Check if CP represents a path through a function outside of the main file.
-  if (!AnalysisManager::isInCodeFile(CP->callEnterWithin.asLocation(), SMgr))
-    return CP;
-
-  const PathPieces &Path = CP->path;
-  if (Path.empty())
-    return nullptr;
-
-  // Check if the last piece in the callee path is a call to a function outside
-  // of the main file.
-  if (auto *CPInner = dyn_cast<PathDiagnosticCallPiece>(Path.back().get()))
-    return getFirstStackedCallToHeaderFile(CPInner, SMgr);
-
-  // Otherwise, the last piece is in the main file.
-  return nullptr;
-}
-
-void PathDiagnostic::resetDiagnosticLocationToMainFile() {
-  if (path.empty())
-    return;
-
-  PathDiagnosticPiece *LastP = path.back().get();
-  assert(LastP);
-  const SourceManager &SMgr = LastP->getLocation().getManager();
-
-  // We only need to check if the report ends inside headers, if the last piece
-  // is a call piece.
-  if (auto *CP = dyn_cast<PathDiagnosticCallPiece>(LastP)) {
-    CP = getFirstStackedCallToHeaderFile(CP, SMgr);
-    if (CP) {
-      // Mark the piece.
-       CP->setAsLastInMainSourceFile();
-
-      // Update the path diagnostic message.
-      const auto *ND = dyn_cast<NamedDecl>(CP->getCallee());
-      if (ND) {
-        SmallString<200> buf;
-        llvm::raw_svector_ostream os(buf);
-        os << " (within a call to '" << ND->getDeclName() << "')";
-        appendToDesc(os.str());
-      }
-
-      // Reset the report containing declaration and location.
-      DeclWithIssue = CP->getCaller();
-      Loc = CP->getLocation();
-
-      return;
-    }
-  }
-}
-
 void PathDiagnosticConsumer::anchor() {}
 
 PathDiagnosticConsumer::~PathDiagnosticConsumer() {




More information about the cfe-commits mailing list