r342314 - [analyzer] Generate and use stable identifiers for LocationContext

George Karpenkov via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 14 19:03:36 PDT 2018


Author: george.karpenkov
Date: Fri Sep 14 19:03:36 2018
New Revision: 342314

URL: http://llvm.org/viewvc/llvm-project?rev=342314&view=rev
Log:
[analyzer] Generate and use stable identifiers for LocationContext

Those are not created in the allocator.
Since they are created fairly rarely, a counter overhead should not
affect the memory consumption.

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

Modified:
    cfe/trunk/include/clang/Analysis/AnalysisDeclContext.h
    cfe/trunk/lib/Analysis/AnalysisDeclContext.cpp
    cfe/trunk/lib/StaticAnalyzer/Core/Environment.cpp

Modified: cfe/trunk/include/clang/Analysis/AnalysisDeclContext.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Analysis/AnalysisDeclContext.h?rev=342314&r1=342313&r2=342314&view=diff
==============================================================================
--- cfe/trunk/include/clang/Analysis/AnalysisDeclContext.h (original)
+++ cfe/trunk/include/clang/Analysis/AnalysisDeclContext.h Fri Sep 14 19:03:36 2018
@@ -227,17 +227,23 @@ private:
   AnalysisDeclContext *Ctx;
 
   const LocationContext *Parent;
+  int64_t ID;
 
 protected:
   LocationContext(ContextKind k, AnalysisDeclContext *ctx,
-                  const LocationContext *parent)
-      : Kind(k), Ctx(ctx), Parent(parent) {}
+                  const LocationContext *parent,
+                  int64_t ID)
+      : Kind(k), Ctx(ctx), Parent(parent), ID(ID) {}
 
 public:
   virtual ~LocationContext();
 
   ContextKind getKind() const { return Kind; }
 
+  int64_t getID() const {
+    return ID;
+  }
+
   AnalysisDeclContext *getAnalysisDeclContext() const { return Ctx; }
 
   const LocationContext *getParent() const { return Parent; }
@@ -297,8 +303,9 @@ class StackFrameContext : public Locatio
 
   StackFrameContext(AnalysisDeclContext *ctx, const LocationContext *parent,
                     const Stmt *s, const CFGBlock *blk,
-                    unsigned idx)
-      : LocationContext(StackFrame, ctx, parent), CallSite(s),
+                    unsigned idx,
+                    int64_t ID)
+      : LocationContext(StackFrame, ctx, parent, ID), CallSite(s),
         Block(blk), Index(idx) {}
 
 public:
@@ -334,8 +341,8 @@ class ScopeContext : public LocationCont
   const Stmt *Enter;
 
   ScopeContext(AnalysisDeclContext *ctx, const LocationContext *parent,
-               const Stmt *s)
-      : LocationContext(Scope, ctx, parent), Enter(s) {}
+               const Stmt *s, int64_t ID)
+      : LocationContext(Scope, ctx, parent, ID), Enter(s) {}
 
 public:
   ~ScopeContext() override = default;
@@ -361,9 +368,10 @@ class BlockInvocationContext : public Lo
   const void *ContextData;
 
   BlockInvocationContext(AnalysisDeclContext *ctx,
-                         const LocationContext *parent,
-                         const BlockDecl *bd, const void *contextData)
-      : LocationContext(Block, ctx, parent), BD(bd), ContextData(contextData) {}
+                         const LocationContext *parent, const BlockDecl *bd,
+                         const void *contextData, int64_t ID)
+      : LocationContext(Block, ctx, parent, ID), BD(bd),
+        ContextData(contextData) {}
 
 public:
   ~BlockInvocationContext() override = default;
@@ -389,6 +397,9 @@ public:
 class LocationContextManager {
   llvm::FoldingSet<LocationContext> Contexts;
 
+  /// ID used for generating a new location context.
+  int64_t NewID = 0;
+
 public:
   ~LocationContextManager();
 

Modified: cfe/trunk/lib/Analysis/AnalysisDeclContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/AnalysisDeclContext.cpp?rev=342314&r1=342313&r2=342314&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/AnalysisDeclContext.cpp (original)
+++ cfe/trunk/lib/Analysis/AnalysisDeclContext.cpp Fri Sep 14 19:03:36 2018
@@ -385,7 +385,7 @@ LocationContextManager::getLocationConte
   LOC *L = cast_or_null<LOC>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
 
   if (!L) {
-    L = new LOC(ctx, parent, d);
+    L = new LOC(ctx, parent, d, ++NewID);
     Contexts.InsertNode(L, InsertPos);
   }
   return L;
@@ -402,7 +402,7 @@ LocationContextManager::getStackFrame(An
   auto *L =
    cast_or_null<StackFrameContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
   if (!L) {
-    L = new StackFrameContext(ctx, parent, s, blk, idx);
+    L = new StackFrameContext(ctx, parent, s, blk, idx, ++NewID);
     Contexts.InsertNode(L, InsertPos);
   }
   return L;
@@ -427,7 +427,7 @@ LocationContextManager::getBlockInvocati
     cast_or_null<BlockInvocationContext>(Contexts.FindNodeOrInsertPos(ID,
                                                                     InsertPos));
   if (!L) {
-    L = new BlockInvocationContext(ctx, parent, BD, ContextData);
+    L = new BlockInvocationContext(ctx, parent, BD, ContextData, ++NewID);
     Contexts.InsertNode(L, InsertPos);
   }
   return L;

Modified: cfe/trunk/lib/StaticAnalyzer/Core/Environment.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/Environment.cpp?rev=342314&r1=342313&r2=342314&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Core/Environment.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Core/Environment.cpp Fri Sep 14 19:03:36 2018
@@ -235,8 +235,8 @@ void Environment::print(raw_ostream &Out
       const Stmt *S = I.first.getStmt();
       assert(S != nullptr && "Expected non-null Stmt");
 
-      Out << "(LC" << (const void *)LC << ", S" << S->getID(Context) << " <"
-          << (const void *)S << "> ) ";
+      Out << "(LC " << LC->getID() << " <" << (const void *)LC << ">, S "
+          << S->getID(Context) << " <" << (const void *)S << ">) ";
       S->printPretty(Out, /*Helper=*/nullptr, PP);
       Out << " : " << I.second << NL;
     }




More information about the cfe-commits mailing list