r368735 - [analyzer][NFC] Refactoring BugReporter.cpp P4.: If it can be const, make it const

Kristof Umann via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 13 11:48:08 PDT 2019


Author: szelethus
Date: Tue Aug 13 11:48:08 2019
New Revision: 368735

URL: http://llvm.org/viewvc/llvm-project?rev=368735&view=rev
Log:
[analyzer][NFC] Refactoring BugReporter.cpp P4.: If it can be const, make it const

When I'm new to a file/codebase, I personally find C++'s strong static type
system to be a great aid. BugReporter.cpp is still painful to read however:
function calls are made with mile long parameter lists, seemingly all of them
taken with a non-const reference/pointer. This patch fixes nothing but this:
make a few things const, and hammer it until it compiles.

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

Modified:
    cfe/trunk/include/clang/Analysis/AnalysisDeclContext.h
    cfe/trunk/include/clang/StaticAnalyzer/Core/BugReporter/BugReporter.h
    cfe/trunk/include/clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitors.h
    cfe/trunk/include/clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h
    cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h
    cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h
    cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h
    cfe/trunk/lib/StaticAnalyzer/Checkers/MallocChecker.cpp
    cfe/trunk/lib/StaticAnalyzer/Checkers/ObjCUnusedIVarsChecker.cpp
    cfe/trunk/lib/StaticAnalyzer/Checkers/RetainCountChecker/RetainCountDiagnostics.cpp
    cfe/trunk/lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp
    cfe/trunk/lib/StaticAnalyzer/Core/BugReporter.cpp
    cfe/trunk/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp
    cfe/trunk/lib/StaticAnalyzer/Core/CallEvent.cpp
    cfe/trunk/lib/StaticAnalyzer/Core/ExplodedGraph.cpp

Modified: cfe/trunk/include/clang/Analysis/AnalysisDeclContext.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Analysis/AnalysisDeclContext.h?rev=368735&r1=368734&r2=368735&view=diff
==============================================================================
--- cfe/trunk/include/clang/Analysis/AnalysisDeclContext.h (original)
+++ cfe/trunk/include/clang/Analysis/AnalysisDeclContext.h Tue Aug 13 11:48:08 2019
@@ -257,7 +257,7 @@ public:
     return getAnalysisDeclContext()->getAnalysis<T>();
   }
 
-  ParentMap &getParentMap() const {
+  const ParentMap &getParentMap() const {
     return getAnalysisDeclContext()->getParentMap();
   }
 

Modified: cfe/trunk/include/clang/StaticAnalyzer/Core/BugReporter/BugReporter.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/StaticAnalyzer/Core/BugReporter/BugReporter.h?rev=368735&r1=368734&r2=368735&view=diff
==============================================================================
--- cfe/trunk/include/clang/StaticAnalyzer/Core/BugReporter/BugReporter.h (original)
+++ cfe/trunk/include/clang/StaticAnalyzer/Core/BugReporter/BugReporter.h Tue Aug 13 11:48:08 2019
@@ -217,10 +217,10 @@ public:
   void markInteresting(SVal V);
   void markInteresting(const LocationContext *LC);
 
-  bool isInteresting(SymbolRef sym);
-  bool isInteresting(const MemRegion *R);
-  bool isInteresting(SVal V);
-  bool isInteresting(const LocationContext *LC);
+  bool isInteresting(SymbolRef sym) const;
+  bool isInteresting(const MemRegion *R) const;
+  bool isInteresting(SVal V) const;
+  bool isInteresting(const LocationContext *LC) const;
 
   /// Returns whether or not this report should be considered valid.
   ///
@@ -469,9 +469,9 @@ public:
 
   ASTContext &getContext() { return D.getASTContext(); }
 
-  SourceManager &getSourceManager() { return D.getSourceManager(); }
+  const SourceManager &getSourceManager() { return D.getSourceManager(); }
 
-  AnalyzerOptions &getAnalyzerOptions() { return D.getAnalyzerOptions(); }
+  const AnalyzerOptions &getAnalyzerOptions() { return D.getAnalyzerOptions(); }
 
   virtual std::unique_ptr<DiagnosticForConsumerMapTy>
   generatePathDiagnostics(ArrayRef<PathDiagnosticConsumer *> consumers,
@@ -519,12 +519,14 @@ public:
 
   /// getGraph - Get the exploded graph created by the analysis engine
   ///  for the analyzed method or function.
-  ExplodedGraph &getGraph();
+  const ExplodedGraph &getGraph() const;
 
   /// getStateManager - Return the state manager used by the analysis
   ///  engine.
   ProgramStateManager &getStateManager();
 
+  ProgramStateManager &getStateManager() const;
+
   /// \p bugReports A set of bug reports within a *single* equivalence class
   ///
   /// \return A mapping from consumers to the corresponding diagnostics.
@@ -566,25 +568,25 @@ public:
 
   GRBugReporter& getBugReporter() { return BR; }
 
-  ExplodedGraph &getGraph() { return BR.getGraph(); }
+  const ExplodedGraph &getGraph() const { return BR.getGraph(); }
 
-  ProgramStateManager& getStateManager() {
+  ProgramStateManager& getStateManager() const {
     return BR.getStateManager();
   }
 
-  SValBuilder &getSValBuilder() {
+  SValBuilder &getSValBuilder() const {
     return getStateManager().getSValBuilder();
   }
 
-  ASTContext &getASTContext() {
+  ASTContext &getASTContext() const {
     return BR.getContext();
   }
 
-  SourceManager& getSourceManager() {
+  const SourceManager& getSourceManager() const {
     return BR.getSourceManager();
   }
 
-  AnalyzerOptions &getAnalyzerOptions() {
+  const AnalyzerOptions &getAnalyzerOptions() const {
     return BR.getAnalyzerOptions();
   }
 

Modified: cfe/trunk/include/clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitors.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitors.h?rev=368735&r1=368734&r2=368735&view=diff
==============================================================================
--- cfe/trunk/include/clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitors.h (original)
+++ cfe/trunk/include/clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitors.h Tue Aug 13 11:48:08 2019
@@ -74,7 +74,8 @@ public:
   /// NOTE that this function can be implemented on at most one used visitor,
   /// and otherwise it crahes at runtime.
   virtual PathDiagnosticPieceRef
-  getEndPath(BugReporterContext &BRC, const ExplodedNode *N, BugReport &BR);
+  getEndPath(BugReporterContext &BRC, const ExplodedNode *N,
+             BugReport &BR);
 
   virtual void Profile(llvm::FoldingSetNodeID &ID) const = 0;
 

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=368735&r1=368734&r2=368735&view=diff
==============================================================================
--- cfe/trunk/include/clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h (original)
+++ cfe/trunk/include/clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h Tue Aug 13 11:48:08 2019
@@ -919,7 +919,6 @@ public:
 };
 
 } // namespace ento
-
 } // namespace clang
 
 #endif // LLVM_CLANG_STATICANALYZER_CORE_BUGREPORTER_PATHDIAGNOSTIC_H

Modified: cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h?rev=368735&r1=368734&r2=368735&view=diff
==============================================================================
--- cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h (original)
+++ cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h Tue Aug 13 11:48:08 2019
@@ -103,7 +103,7 @@ public:
     return Eng.getBugReporter();
   }
 
-  SourceManager &getSourceManager() {
+  const SourceManager &getSourceManager() {
     return getBugReporter().getSourceManager();
   }
 

Modified: cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h?rev=368735&r1=368734&r2=368735&view=diff
==============================================================================
--- cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h (original)
+++ cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h Tue Aug 13 11:48:08 2019
@@ -153,7 +153,11 @@ public:
 
   CFG &getCFG() const { return *getLocationContext()->getCFG(); }
 
-  ParentMap &getParentMap() const {return getLocationContext()->getParentMap();}
+  const CFGBlock *getCFGBlock() const;
+
+  const ParentMap &getParentMap() const {
+    return getLocationContext()->getParentMap();
+  }
 
   template <typename T>
   T &getAnalysis() const {

Modified: cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h?rev=368735&r1=368734&r2=368735&view=diff
==============================================================================
--- cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h (original)
+++ cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h Tue Aug 13 11:48:08 2019
@@ -507,6 +507,10 @@ public:
     return *svalBuilder;
   }
 
+  const SValBuilder &getSValBuilder() const {
+    return *svalBuilder;
+  }
+
   SymbolManager &getSymbolManager() {
     return svalBuilder->getSymbolManager();
   }

Modified: cfe/trunk/lib/StaticAnalyzer/Checkers/MallocChecker.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Checkers/MallocChecker.cpp?rev=368735&r1=368734&r2=368735&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Checkers/MallocChecker.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Checkers/MallocChecker.cpp Tue Aug 13 11:48:08 2019
@@ -1086,7 +1086,7 @@ void MallocChecker::processNewAllocation
   if (!isStandardNewDelete(NE->getOperatorNew(), C.getASTContext()))
     return;
 
-  ParentMap &PM = C.getLocationContext()->getParentMap();
+  const ParentMap &PM = C.getLocationContext()->getParentMap();
   if (!PM.isConsumedExpr(NE) && treatUnusedNewEscaped(NE))
     return;
 

Modified: cfe/trunk/lib/StaticAnalyzer/Checkers/ObjCUnusedIVarsChecker.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Checkers/ObjCUnusedIVarsChecker.cpp?rev=368735&r1=368734&r2=368735&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Checkers/ObjCUnusedIVarsChecker.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Checkers/ObjCUnusedIVarsChecker.cpp Tue Aug 13 11:48:08 2019
@@ -94,7 +94,7 @@ static void Scan(IvarUsageMap& M, const
 }
 
 static void Scan(IvarUsageMap &M, const DeclContext *C, const FileID FID,
-                 SourceManager &SM) {
+                 const SourceManager &SM) {
   for (const auto *I : C->decls())
     if (const auto *FD = dyn_cast<FunctionDecl>(I)) {
       SourceLocation L = FD->getBeginLoc();
@@ -148,7 +148,7 @@ static void checkObjCUnusedIvar(const Ob
   // FIXME: In the future hopefully we can just use the lexical DeclContext
   // to go from the ObjCImplementationDecl to the lexically "nested"
   // C functions.
-  SourceManager &SM = BR.getSourceManager();
+  const SourceManager &SM = BR.getSourceManager();
   Scan(M, D->getDeclContext(), SM.getFileID(D->getLocation()), SM);
 
   // Find ivars that are unused.

Modified: cfe/trunk/lib/StaticAnalyzer/Checkers/RetainCountChecker/RetainCountDiagnostics.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Checkers/RetainCountChecker/RetainCountDiagnostics.cpp?rev=368735&r1=368734&r2=368735&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Checkers/RetainCountChecker/RetainCountDiagnostics.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Checkers/RetainCountChecker/RetainCountDiagnostics.cpp Tue Aug 13 11:48:08 2019
@@ -736,7 +736,7 @@ RefLeakReportVisitor::getEndPath(BugRepo
   const MemRegion* FirstBinding = AllocI.R;
   BR.markInteresting(AllocI.InterestingMethodContext);
 
-  SourceManager& SM = BRC.getSourceManager();
+  const SourceManager& SM = BRC.getSourceManager();
 
   // Compute an actual location for the leak.  Sometimes a leak doesn't
   // occur at an actual statement (e.g., transition between blocks; end

Modified: cfe/trunk/lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp?rev=368735&r1=368734&r2=368735&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp Tue Aug 13 11:48:08 2019
@@ -55,7 +55,7 @@ void UnreachableCodeChecker::checkEndAna
 
   const Decl *D = nullptr;
   CFG *C = nullptr;
-  ParentMap *PM = nullptr;
+  const ParentMap *PM = nullptr;
   const LocationContext *LC = nullptr;
   // Iterate over ExplodedGraph
   for (ExplodedGraph::node_iterator I = G.nodes_begin(), E = G.nodes_end();

Modified: cfe/trunk/lib/StaticAnalyzer/Core/BugReporter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/BugReporter.cpp?rev=368735&r1=368734&r2=368735&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Core/BugReporter.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Core/BugReporter.cpp Tue Aug 13 11:48:08 2019
@@ -190,8 +190,8 @@ using LocationContextMap =
 /// Recursively scan through a path and prune out calls and macros pieces
 /// that aren't needed.  Return true if afterwards the path contains
 /// "interesting stuff" which means it shouldn't be pruned from the parent path.
-static bool removeUnneededCalls(PathPieces &pieces, BugReport *R,
-                                LocationContextMap &LCM,
+static bool removeUnneededCalls(PathPieces &pieces, const BugReport *R,
+                                const LocationContextMap &LCM,
                                 bool IsInteresting = false) {
   bool containsSomethingInteresting = IsInteresting;
   const unsigned N = pieces.size();
@@ -208,7 +208,7 @@ static bool removeUnneededCalls(PathPiec
         // Check if the location context is interesting.
         assert(LCM.count(&call.path));
         if (!removeUnneededCalls(call.path, R, LCM,
-                                 R->isInteresting(LCM[&call.path])))
+                                 R->isInteresting(LCM.lookup(&call.path))))
           continue;
 
         containsSomethingInteresting = true;
@@ -353,33 +353,33 @@ namespace {
 
 class PathDiagnosticBuilder : public BugReporterContext {
   BugReport *R;
-  PathDiagnosticConsumer *PDC;
+  const PathDiagnosticConsumer *PDC;
 
 public:
   const LocationContext *LC;
 
   PathDiagnosticBuilder(GRBugReporter &br,
                         BugReport *r, InterExplodedGraphMap &Backmap,
-                        PathDiagnosticConsumer *pdc)
+                        const PathDiagnosticConsumer *pdc)
       : BugReporterContext(br, Backmap), R(r), PDC(pdc),
         LC(r->getErrorNode()->getLocationContext()) {}
 
-  PathDiagnosticLocation ExecutionContinues(const ExplodedNode *N);
+  PathDiagnosticLocation ExecutionContinues(const ExplodedNode *N) const;
 
   PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream &os,
-                                            const ExplodedNode *N);
+                                            const ExplodedNode *N) const;
 
   BugReport *getBugReport() { return R; }
 
-  Decl const &getCodeDecl() { return R->getErrorNode()->getCodeDecl(); }
+  const Decl &getCodeDecl() const { return R->getErrorNode()->getCodeDecl(); }
 
-  ParentMap& getParentMap() { return LC->getParentMap(); }
+  const ParentMap& getParentMap() const { return LC->getParentMap(); }
 
-  const Stmt *getParent(const Stmt *S) {
+  const Stmt *getParent(const Stmt *S) const {
     return getParentMap().getParent(S);
   }
 
-  PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S);
+  PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S) const;
 
   PathDiagnosticConsumer::PathGenerationScheme getGenerationScheme() const {
     return PDC ? PDC->getGenerationScheme() : PathDiagnosticConsumer::Minimal;
@@ -393,7 +393,7 @@ public:
 } // namespace
 
 PathDiagnosticLocation
-PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode *N) {
+PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode *N) const {
   if (const Stmt *S = PathDiagnosticLocation::getNextStmt(N))
     return PathDiagnosticLocation(S, getSourceManager(), LC);
 
@@ -403,7 +403,7 @@ PathDiagnosticBuilder::ExecutionContinue
 
 PathDiagnosticLocation
 PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream &os,
-                                          const ExplodedNode *N) {
+                                          const ExplodedNode *N) const {
   // Slow, but probably doesn't matter.
   if (os.str().empty())
     os << ' ';
@@ -454,8 +454,9 @@ static const Stmt *getEnclosingParent(co
 }
 
 static PathDiagnosticLocation
-getEnclosingStmtLocation(const Stmt *S, SourceManager &SMgr, const ParentMap &P,
-                         const LocationContext *LC, bool allowNestedContexts) {
+getEnclosingStmtLocation(const Stmt *S, const SourceManager &SMgr,
+                         const ParentMap &P, const LocationContext *LC,
+                         bool allowNestedContexts) {
   if (!S)
     return {};
 
@@ -521,7 +522,7 @@ getEnclosingStmtLocation(const Stmt *S,
 }
 
 PathDiagnosticLocation
-PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
+PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) const {
   assert(S && "Null Stmt passed to getEnclosingStmtLocation");
   return ::getEnclosingStmtLocation(S, getSourceManager(), getParentMap(), LC,
                                     /*allowNestedContexts=*/false);
@@ -535,7 +536,7 @@ using StackDiagPair =
 using StackDiagVector = SmallVector<StackDiagPair, 6>;
 
 static void updateStackPiecesWithMessage(PathDiagnosticPiece &P,
-                                         StackDiagVector &CallStack) {
+                                         const StackDiagVector &CallStack) {
   // If the piece contains a special message, add it to all the call
   // pieces on the active stack.
   if (auto *ep = dyn_cast<PathDiagnosticEventPiece>(&P)) {
@@ -563,7 +564,7 @@ std::shared_ptr<PathDiagnosticControlFlo
   const CFGBlock *Dst,
   const SourceManager &SM,
   const LocationContext *LC,
-  PathDiagnosticBuilder &PDB,
+  const PathDiagnosticBuilder &PDB,
   PathDiagnosticLocation &Start
   ) {
   // Figure out what case arm we took.
@@ -622,7 +623,7 @@ std::shared_ptr<PathDiagnosticControlFlo
 
 std::shared_ptr<PathDiagnosticControlFlowPiece> generateDiagForGotoOP(
   const Stmt *S,
-  PathDiagnosticBuilder &PDB,
+  const PathDiagnosticBuilder &PDB,
   PathDiagnosticLocation &Start) {
     std::string sbuf;
     llvm::raw_string_ostream os(sbuf);
@@ -633,13 +634,10 @@ std::shared_ptr<PathDiagnosticControlFlo
 }
 
 std::shared_ptr<PathDiagnosticControlFlowPiece> generateDiagForBinaryOP(
-                                                 const ExplodedNode *N,
-                                                 const Stmt *T,
-                                                 const CFGBlock *Src,
-                                                 const CFGBlock *Dst,
-                                                 const SourceManager &SM,
-                                                 PathDiagnosticBuilder &PDB,
-                                                 const LocationContext *LC) {
+    const ExplodedNode *N, const Stmt *T, const CFGBlock *Src,
+    const CFGBlock *Dst, const SourceManager &SM,
+    const PathDiagnosticBuilder &PDB, const LocationContext *LC) {
+
   const auto *B = cast<BinaryOperator>(T);
   std::string sbuf;
   llvm::raw_string_ostream os(sbuf);
@@ -682,7 +680,7 @@ std::shared_ptr<PathDiagnosticControlFlo
 
 void generateMinimalDiagForBlockEdge(const ExplodedNode *N, BlockEdge BE,
                                      const SourceManager &SM,
-                                     PathDiagnosticBuilder &PDB,
+                                     const PathDiagnosticBuilder &PDB,
                                      PathDiagnostic &PD) {
   const LocationContext *LC = N->getLocationContext();
   const CFGBlock *Src = BE.getSrc();
@@ -916,7 +914,8 @@ static bool isJumpToFalseBranch(const Bl
   return (*(Src->succ_begin()+1) == BE->getDst());
 }
 
-static bool isContainedByStmt(ParentMap &PM, const Stmt *S, const Stmt *SubS) {
+static bool isContainedByStmt(const ParentMap &PM, const Stmt *S,
+                              const Stmt *SubS) {
   while (SubS) {
     if (SubS == S)
       return true;
@@ -925,7 +924,7 @@ static bool isContainedByStmt(ParentMap
   return false;
 }
 
-static const Stmt *getStmtBeforeCond(ParentMap &PM, const Stmt *Term,
+static const Stmt *getStmtBeforeCond(const ParentMap &PM, const Stmt *Term,
                                      const ExplodedNode *N) {
   while (N) {
     Optional<StmtPoint> SP = N->getLocation().getAs<StmtPoint>();
@@ -939,7 +938,7 @@ static const Stmt *getStmtBeforeCond(Par
   return nullptr;
 }
 
-static bool isInLoopBody(ParentMap &PM, const Stmt *S, const Stmt *Term) {
+static bool isInLoopBody(const ParentMap &PM, const Stmt *S, const Stmt *Term) {
   const Stmt *LoopBody = nullptr;
   switch (Term->getStmtClass()) {
     case Stmt::CXXForRangeStmtClass: {
@@ -1007,15 +1006,14 @@ static const Stmt *getTerminatorConditio
   return S;
 }
 
-static const char StrEnteringLoop[] = "Entering loop body";
-static const char StrLoopBodyZero[] = "Loop body executed 0 times";
-static const char StrLoopRangeEmpty[] =
-  "Loop body skipped when range is empty";
-static const char StrLoopCollectionEmpty[] =
-  "Loop body skipped when collection is empty";
+llvm::StringLiteral StrEnteringLoop = "Entering loop body";
+llvm::StringLiteral StrLoopBodyZero = "Loop body executed 0 times";
+llvm::StringLiteral StrLoopRangeEmpty = "Loop body skipped when range is empty";
+llvm::StringLiteral StrLoopCollectionEmpty =
+    "Loop body skipped when collection is empty";
 
 static std::unique_ptr<FilesToLineNumsMap>
-findExecutedLines(SourceManager &SM, const ExplodedNode *N);
+findExecutedLines(const SourceManager &SM, const ExplodedNode *N);
 
 /// Generate diagnostics for the node \p N,
 /// and write it into \p PD.
@@ -1210,7 +1208,7 @@ static void generatePathDiagnosticsForNo
     }
 
     const CFGBlock *BSrc = BE->getSrc();
-    ParentMap &PM = PDB.getParentMap();
+    const ParentMap &PM = PDB.getParentMap();
 
     if (const Stmt *Term = BSrc->getTerminatorStmt()) {
       // Are we jumping past the loop body without ever executing the
@@ -1220,7 +1218,7 @@ static void generatePathDiagnosticsForNo
         bool IsInLoopBody =
           isInLoopBody(PM, getStmtBeforeCond(PM, TermCond, N), Term);
 
-        const char *str = nullptr;
+        StringRef str;
 
         if (isJumpToFalseBranch(&*BE)) {
           if (!IsInLoopBody) {
@@ -1236,7 +1234,7 @@ static void generatePathDiagnosticsForNo
           str = StrEnteringLoop;
         }
 
-        if (str) {
+        if (!str.empty()) {
           PathDiagnosticLocation L(TermCond ? TermCond : Term, SM, PDB.LC);
           auto PE = std::make_shared<PathDiagnosticEventPiece>(L, str);
           PE->setPrunable(true);
@@ -1254,7 +1252,7 @@ static void generatePathDiagnosticsForNo
 }
 
 static std::unique_ptr<PathDiagnostic>
-generateEmptyDiagnosticForReport(BugReport *R, SourceManager &SM) {
+generateEmptyDiagnosticForReport(const BugReport *R, const SourceManager &SM) {
   const BugType &BT = R->getBugType();
   return llvm::make_unique<PathDiagnostic>(
       R->getBugType().getCheckName(), R->getDeclWithIssue(),
@@ -1342,7 +1340,7 @@ using OptimizedCallsSet = llvm::DenseSet
 /// This avoids a "swoosh" effect, where an edge from a top-level statement A
 /// points to a sub-expression B.1 that's not at the start of B. In these cases,
 /// we'd like to see an edge from A to B, then another one from B to B.1.
-static void addContextEdges(PathPieces &pieces, SourceManager &SM,
+static void addContextEdges(PathPieces &pieces, const SourceManager &SM,
                             const ParentMap &PM, const LocationContext *LCtx) {
   PathPieces::iterator Prev = pieces.end();
   for (PathPieces::iterator I = pieces.begin(), E = Prev; I != E;
@@ -1493,7 +1491,7 @@ static void simplifySimpleBranches(PathP
 /// If the locations in the range are not on the same line, returns None.
 ///
 /// Note that this does not do a precise user-visible character or column count.
-static Optional<size_t> getLengthOnSingleLine(SourceManager &SM,
+static Optional<size_t> getLengthOnSingleLine(const SourceManager &SM,
                                               SourceRange Range) {
   SourceRange ExpansionRange(SM.getExpansionLoc(Range.getBegin()),
                              SM.getExpansionRange(Range.getEnd()).getEnd());
@@ -1523,7 +1521,7 @@ static Optional<size_t> getLengthOnSingl
 }
 
 /// \sa getLengthOnSingleLine(SourceManager, SourceRange)
-static Optional<size_t> getLengthOnSingleLine(SourceManager &SM,
+static Optional<size_t> getLengthOnSingleLine(const SourceManager &SM,
                                               const Stmt *S) {
   return getLengthOnSingleLine(SM, S->getSourceRange());
 }
@@ -1544,7 +1542,7 @@ static Optional<size_t> getLengthOnSingl
 /// - if there is an inlined call between the edges instead of a single event.
 /// - if the whole statement is large enough that having subexpression arrows
 ///   might be helpful.
-static void removeContextCycles(PathPieces &Path, SourceManager &SM) {
+static void removeContextCycles(PathPieces &Path, const SourceManager &SM) {
   for (PathPieces::iterator I = Path.begin(), E = Path.end(); I != E; ) {
     // Pattern match the current piece and its successor.
     const auto *PieceI = dyn_cast<PathDiagnosticControlFlowPiece>(I->get());
@@ -1599,7 +1597,7 @@ static void removeContextCycles(PathPiec
 }
 
 /// Return true if X is contained by Y.
-static bool lexicalContains(ParentMap &PM, const Stmt *X, const Stmt *Y) {
+static bool lexicalContains(const ParentMap &PM, const Stmt *X, const Stmt *Y) {
   while (X) {
     if (X == Y)
       return true;
@@ -1609,8 +1607,8 @@ static bool lexicalContains(ParentMap &P
 }
 
 // Remove short edges on the same line less than 3 columns in difference.
-static void removePunyEdges(PathPieces &path, SourceManager &SM,
-                            ParentMap &PM) {
+static void removePunyEdges(PathPieces &path, const SourceManager &SM,
+                            const ParentMap &PM) {
   bool erased = false;
 
   for (PathPieces::iterator I = path.begin(), E = path.end(); I != E;
@@ -1685,13 +1683,13 @@ static void removeIdenticalEvents(PathPi
   }
 }
 
-static bool optimizeEdges(PathPieces &path, SourceManager &SM,
+static bool optimizeEdges(PathPieces &path, const SourceManager &SM,
                           OptimizedCallsSet &OCS,
-                          LocationContextMap &LCM) {
+                          const LocationContextMap &LCM) {
   bool hasChanges = false;
-  const LocationContext *LC = LCM[&path];
+  const LocationContext *LC = LCM.lookup(&path);
   assert(LC);
-  ParentMap &PM = LC->getParentMap();
+  const ParentMap &PM = LC->getParentMap();
 
   for (PathPieces::iterator I = path.begin(), E = path.end(); I != E; ) {
     // Optimize subpaths.
@@ -1866,14 +1864,15 @@ static bool optimizeEdges(PathPieces &pa
 /// statement had an invalid source location), this function does nothing.
 // FIXME: We should just generate invalid edges anyway and have the optimizer
 // deal with them.
-static void dropFunctionEntryEdge(PathPieces &Path, LocationContextMap &LCM,
-                                  SourceManager &SM) {
+static void dropFunctionEntryEdge(PathPieces &Path,
+                                  const LocationContextMap &LCM,
+                                  const SourceManager &SM) {
   const auto *FirstEdge =
       dyn_cast<PathDiagnosticControlFlowPiece>(Path.front().get());
   if (!FirstEdge)
     return;
 
-  const Decl *D = LCM[&Path]->getDecl();
+  const Decl *D = LCM.lookup(&Path)->getDecl();
   PathDiagnosticLocation EntryLoc = PathDiagnosticLocation::createBegin(D, SM);
   if (FirstEdge->getStartLocation() != EntryLoc)
     return;
@@ -1919,9 +1918,9 @@ static std::unique_ptr<PathDiagnostic> g
 
   bool GenerateDiagnostics = (ActiveScheme != PathDiagnosticConsumer::None);
   bool AddPathEdges = (ActiveScheme == PathDiagnosticConsumer::Extensive);
-  SourceManager &SM = PDB.getSourceManager();
-  BugReport *R = PDB.getBugReport();
-  AnalyzerOptions &Opts = PDB.getBugReporter().getAnalyzerOptions();
+  const SourceManager &SM = PDB.getSourceManager();
+  const BugReport *R = PDB.getBugReport();
+  const AnalyzerOptions &Opts = PDB.getBugReporter().getAnalyzerOptions();
   StackDiagVector CallStack;
   InterestingExprs IE;
   LocationContextMap LCM;
@@ -1934,7 +1933,8 @@ static std::unique_ptr<PathDiagnostic> g
       assert(!EndNotes->second.empty());
       LastPiece = EndNotes->second[0];
     } else {
-      LastPiece = BugReporterVisitor::getDefaultEndPath(PDB, ErrorNode, *R);
+      LastPiece = BugReporterVisitor::getDefaultEndPath(PDB, ErrorNode,
+                                                        *PDB.getBugReport());
     }
     PD->setEndOfPath(LastPiece);
   }
@@ -1959,8 +1959,7 @@ static std::unique_ptr<PathDiagnostic> g
     for (const PathDiagnosticPieceRef &Note : VisitorNotes->second) {
       llvm::FoldingSetNodeID ID;
       Note->Profile(ID);
-      auto P = DeduplicationSet.insert(ID);
-      if (!P.second)
+      if (!DeduplicationSet.insert(ID).second)
         continue;
 
       if (AddPathEdges)
@@ -2122,11 +2121,11 @@ void BugReport::markInteresting(const Lo
   InterestingLocationContexts.insert(LC);
 }
 
-bool BugReport::isInteresting(SVal V) {
+bool BugReport::isInteresting(SVal V)  const {
   return isInteresting(V.getAsRegion()) || isInteresting(V.getAsSymbol());
 }
 
-bool BugReport::isInteresting(SymbolRef sym) {
+bool BugReport::isInteresting(SymbolRef sym)  const {
   if (!sym)
     return false;
   // We don't currently consider metadata symbols to be interesting
@@ -2134,7 +2133,7 @@ bool BugReport::isInteresting(SymbolRef
   return InterestingSymbols.count(sym);
 }
 
-bool BugReport::isInteresting(const MemRegion *R) {
+bool BugReport::isInteresting(const MemRegion *R)  const {
   if (!R)
     return false;
   R = R->getBaseRegion();
@@ -2146,7 +2145,7 @@ bool BugReport::isInteresting(const MemR
   return false;
 }
 
-bool BugReport::isInteresting(const LocationContext *LC) {
+bool BugReport::isInteresting(const LocationContext *LC)  const {
   if (!LC)
     return false;
   return InterestingLocationContexts.count(LC);
@@ -2202,11 +2201,14 @@ PathDiagnosticLocation BugReport::getLoc
 // Methods for BugReporter and subclasses.
 //===----------------------------------------------------------------------===//
 
-ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
+const ExplodedGraph &GRBugReporter::getGraph() const { return Eng.getGraph(); }
 
 ProgramStateManager&
 GRBugReporter::getStateManager() { return Eng.getStateManager(); }
 
+ProgramStateManager&
+GRBugReporter::getStateManager() const { return Eng.getStateManager(); }
+
 BugReporter::~BugReporter() {
   FlushReports();
 
@@ -2984,7 +2986,7 @@ void BugReporter::FlushReport(BugReportE
 /// Insert all lines participating in the function signature \p Signature
 /// into \p ExecutedLines.
 static void populateExecutedLinesWithFunctionSignature(
-    const Decl *Signature, SourceManager &SM,
+    const Decl *Signature, const SourceManager &SM,
     FilesToLineNumsMap &ExecutedLines) {
   SourceRange SignatureSourceRange;
   const Stmt* Body = Signature->getBody();
@@ -3009,7 +3011,7 @@ static void populateExecutedLinesWithFun
 }
 
 static void populateExecutedLinesWithStmt(
-    const Stmt *S, SourceManager &SM,
+    const Stmt *S, const SourceManager &SM,
     FilesToLineNumsMap &ExecutedLines) {
   SourceLocation Loc = S->getSourceRange().getBegin();
   if (!Loc.isValid())
@@ -3023,7 +3025,7 @@ static void populateExecutedLinesWithStm
 /// \return all executed lines including function signatures on the path
 /// starting from \p N.
 static std::unique_ptr<FilesToLineNumsMap>
-findExecutedLines(SourceManager &SM, const ExplodedNode *N) {
+findExecutedLines(const SourceManager &SM, const ExplodedNode *N) {
   auto ExecutedLines = llvm::make_unique<FilesToLineNumsMap>();
 
   while (N) {
@@ -3087,7 +3089,7 @@ BugReporter::generateDiagnosticForConsum
 
   // Examine the report and see if the last piece is in a header. Reset the
   // report location to the last piece in the main source file.
-  AnalyzerOptions &Opts = getAnalyzerOptions();
+  const AnalyzerOptions &Opts = getAnalyzerOptions();
   for (auto const &P : *Out)
     if (Opts.ShouldReportIssuesInMainSourceFile && !Opts.AnalyzeAll)
       P.second->resetDiagnosticLocationToMainFile();

Modified: cfe/trunk/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp?rev=368735&r1=368734&r2=368735&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp Tue Aug 13 11:48:08 2019
@@ -2250,7 +2250,7 @@ bool ConditionBRVisitor::patternMatch(co
     SourceLocation BeginLoc = OriginalExpr->getBeginLoc();
     SourceLocation EndLoc = OriginalExpr->getEndLoc();
     if (BeginLoc.isMacroID() && EndLoc.isMacroID()) {
-      SourceManager &SM = BRC.getSourceManager();
+      const SourceManager &SM = BRC.getSourceManager();
       const LangOptions &LO = BRC.getASTContext().getLangOpts();
       if (Lexer::isAtStartOfMacroExpansion(BeginLoc, SM, LO) &&
           Lexer::isAtEndOfMacroExpansion(EndLoc, SM, LO)) {
@@ -2587,7 +2587,7 @@ void LikelyFalsePositiveSuppressionBRVis
     BugReporterContext &BRC, const ExplodedNode *N, BugReport &BR) {
   // Here we suppress false positives coming from system headers. This list is
   // based on known issues.
-  AnalyzerOptions &Options = BRC.getAnalyzerOptions();
+  const AnalyzerOptions &Options = BRC.getAnalyzerOptions();
   const Decl *D = N->getLocationContext()->getDecl();
 
   if (AnalysisDeclContext::isInStdNamespace(D)) {
@@ -2654,7 +2654,7 @@ void LikelyFalsePositiveSuppressionBRVis
 
   // Skip reports within the sys/queue.h macros as we do not have the ability to
   // reason about data structure shapes.
-  SourceManager &SM = BRC.getSourceManager();
+  const SourceManager &SM = BRC.getSourceManager();
   FullSourceLoc Loc = BR.getLocation(SM).asLocation();
   while (Loc.isMacroID()) {
     Loc = Loc.getSpellingLoc();

Modified: cfe/trunk/lib/StaticAnalyzer/Core/CallEvent.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/CallEvent.cpp?rev=368735&r1=368734&r2=368735&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Core/CallEvent.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Core/CallEvent.cpp Tue Aug 13 11:48:08 2019
@@ -1035,7 +1035,7 @@ getSyntacticFromForPseudoObjectExpr(cons
 ObjCMessageKind ObjCMethodCall::getMessageKind() const {
   if (!Data) {
     // Find the parent, ignoring implicit casts.
-    ParentMap &PM = getLocationContext()->getParentMap();
+    const ParentMap &PM = getLocationContext()->getParentMap();
     const Stmt *S = PM.getParentIgnoreParenCasts(getOriginExpr());
 
     // Check if parent is a PseudoObjectExpr.

Modified: cfe/trunk/lib/StaticAnalyzer/Core/ExplodedGraph.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/ExplodedGraph.cpp?rev=368735&r1=368734&r2=368735&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Core/ExplodedGraph.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Core/ExplodedGraph.cpp Tue Aug 13 11:48:08 2019
@@ -134,7 +134,7 @@ bool ExplodedGraph::shouldCollect(const
   // Do not collect nodes for non-consumed Stmt or Expr to ensure precise
   // diagnostic generation; specifically, so that we could anchor arrows
   // pointing to the beginning of statements (as written in code).
-  ParentMap &PM = progPoint.getLocationContext()->getParentMap();
+  const ParentMap &PM = progPoint.getLocationContext()->getParentMap();
   if (!PM.isConsumedExpr(Ex))
     return false;
 




More information about the cfe-commits mailing list