From ab961c560a235c89588375dfcb0ef4597e460ee6 Mon Sep 17 00:00:00 2001 From: George Karpenkov Date: Sat, 15 Sep 2018 02:03:36 +0000 Subject: [PATCH] [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 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@342314 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Analysis/AnalysisDeclContext.h | 29 ++++++++++++++------ lib/Analysis/AnalysisDeclContext.cpp | 6 ++-- lib/StaticAnalyzer/Core/Environment.cpp | 4 +-- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/include/clang/Analysis/AnalysisDeclContext.h b/include/clang/Analysis/AnalysisDeclContext.h index dead34e7e9..490d2ce346 100644 --- a/include/clang/Analysis/AnalysisDeclContext.h +++ b/include/clang/Analysis/AnalysisDeclContext.h @@ -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 LocationContext { 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 LocationContext { 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 LocationContext { 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 Contexts; + /// ID used for generating a new location context. + int64_t NewID = 0; + public: ~LocationContextManager(); diff --git a/lib/Analysis/AnalysisDeclContext.cpp b/lib/Analysis/AnalysisDeclContext.cpp index 693fe066e1..30160bc239 100644 --- a/lib/Analysis/AnalysisDeclContext.cpp +++ b/lib/Analysis/AnalysisDeclContext.cpp @@ -385,7 +385,7 @@ LocationContextManager::getLocationContext(AnalysisDeclContext *ctx, LOC *L = cast_or_null(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(AnalysisDeclContext *ctx, auto *L = cast_or_null(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::getBlockInvocationContext(AnalysisDeclContext *ctx, cast_or_null(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; diff --git a/lib/StaticAnalyzer/Core/Environment.cpp b/lib/StaticAnalyzer/Core/Environment.cpp index 5f1a37c8d6..4238ceb4ee 100644 --- a/lib/StaticAnalyzer/Core/Environment.cpp +++ b/lib/StaticAnalyzer/Core/Environment.cpp @@ -235,8 +235,8 @@ void Environment::print(raw_ostream &Out, const char *NL, 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; } -- 2.40.0