Zhongxing Xu | 97ab394 | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 1 | //== AnalysisContext.cpp - Analysis context for Path Sens analysis -*- C++ -*-// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines AnalysisContext, a class that manages the analysis context |
| 11 | // data for path sensitive analysis. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Analysis/PathSensitive/AnalysisContext.h" |
| 16 | #include "clang/Analysis/Analyses/LiveVariables.h" |
| 17 | #include "clang/Analysis/CFG.h" |
| 18 | #include "clang/AST/Decl.h" |
| 19 | #include "clang/AST/DeclObjC.h" |
| 20 | #include "clang/AST/ParentMap.h" |
Mike Stump | 87a05f1 | 2009-07-31 01:10:29 +0000 | [diff] [blame] | 21 | #include "llvm/Support/ErrorHandling.h" |
Zhongxing Xu | 97ab394 | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace clang; |
| 24 | |
| 25 | AnalysisContext::~AnalysisContext() { |
| 26 | delete cfg; |
| 27 | delete liveness; |
| 28 | delete PM; |
| 29 | } |
| 30 | |
Ted Kremenek | 2376002 | 2009-08-21 23:58:43 +0000 | [diff] [blame] | 31 | AnalysisContextManager::~AnalysisContextManager() { |
| 32 | for (ContextMap::iterator I = Contexts.begin(), E = Contexts.end(); I!=E; ++I) |
| 33 | delete I->second; |
| 34 | } |
| 35 | |
Ted Kremenek | 58f5ec7 | 2009-10-20 21:39:41 +0000 | [diff] [blame] | 36 | void AnalysisContextManager::clear() { |
| 37 | for (ContextMap::iterator I = Contexts.begin(), E = Contexts.end(); I!=E; ++I) |
| 38 | delete I->second; |
| 39 | Contexts.clear(); |
| 40 | } |
| 41 | |
Zhongxing Xu | 97ab394 | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 42 | Stmt *AnalysisContext::getBody() { |
Ted Kremenek | 2376002 | 2009-08-21 23:58:43 +0000 | [diff] [blame] | 43 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
Zhongxing Xu | 97ab394 | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 44 | return FD->getBody(); |
Ted Kremenek | 2376002 | 2009-08-21 23:58:43 +0000 | [diff] [blame] | 45 | else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Zhongxing Xu | 97ab394 | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 46 | return MD->getBody(); |
| 47 | |
Mike Stump | 87a05f1 | 2009-07-31 01:10:29 +0000 | [diff] [blame] | 48 | llvm::llvm_unreachable("unknown code decl"); |
Zhongxing Xu | 97ab394 | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Ted Kremenek | 82cd37c | 2009-08-21 23:25:54 +0000 | [diff] [blame] | 51 | const ImplicitParamDecl *AnalysisContext::getSelfDecl() const { |
| 52 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 53 | return MD->getSelfDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 54 | |
Ted Kremenek | 82cd37c | 2009-08-21 23:25:54 +0000 | [diff] [blame] | 55 | return NULL; |
| 56 | } |
| 57 | |
Zhongxing Xu | 97ab394 | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 58 | CFG *AnalysisContext::getCFG() { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 59 | if (!cfg) |
Zhongxing Xu | 97ab394 | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 60 | cfg = CFG::buildCFG(getBody(), &D->getASTContext()); |
| 61 | return cfg; |
| 62 | } |
| 63 | |
| 64 | ParentMap &AnalysisContext::getParentMap() { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 65 | if (!PM) |
Zhongxing Xu | 97ab394 | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 66 | PM = new ParentMap(getBody()); |
| 67 | return *PM; |
| 68 | } |
| 69 | |
| 70 | LiveVariables *AnalysisContext::getLiveVariables() { |
| 71 | if (!liveness) { |
| 72 | CFG *c = getCFG(); |
| 73 | if (!c) |
| 74 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 75 | |
Zhongxing Xu | 97ab394 | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 76 | liveness = new LiveVariables(D->getASTContext(), *c); |
| 77 | liveness->runOnCFG(*c); |
| 78 | liveness->runOnAllBlocks(*c, 0, true); |
| 79 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 80 | |
Zhongxing Xu | 97ab394 | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 81 | return liveness; |
| 82 | } |
| 83 | |
Ted Kremenek | 2376002 | 2009-08-21 23:58:43 +0000 | [diff] [blame] | 84 | AnalysisContext *AnalysisContextManager::getContext(const Decl *D) { |
| 85 | AnalysisContext *&AC = Contexts[D]; |
| 86 | if (!AC) |
| 87 | AC = new AnalysisContext(D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 88 | |
Ted Kremenek | 2376002 | 2009-08-21 23:58:43 +0000 | [diff] [blame] | 89 | return AC; |
Zhongxing Xu | 97ab394 | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 90 | } |
Zhongxing Xu | 18c7c06 | 2009-08-03 07:23:22 +0000 | [diff] [blame] | 91 | |
| 92 | void LocationContext::Profile(llvm::FoldingSetNodeID &ID, ContextKind k, |
Ted Kremenek | 54c809b | 2009-08-21 23:39:58 +0000 | [diff] [blame] | 93 | AnalysisContext *ctx, |
| 94 | const LocationContext *parent) { |
Zhongxing Xu | 18c7c06 | 2009-08-03 07:23:22 +0000 | [diff] [blame] | 95 | ID.AddInteger(k); |
| 96 | ID.AddPointer(ctx); |
| 97 | ID.AddPointer(parent); |
| 98 | } |
| 99 | |
| 100 | void StackFrameContext::Profile(llvm::FoldingSetNodeID &ID,AnalysisContext *ctx, |
Ted Kremenek | 54c809b | 2009-08-21 23:39:58 +0000 | [diff] [blame] | 101 | const LocationContext *parent, const Stmt *s) { |
Zhongxing Xu | 18c7c06 | 2009-08-03 07:23:22 +0000 | [diff] [blame] | 102 | LocationContext::Profile(ID, StackFrame, ctx, parent); |
| 103 | ID.AddPointer(s); |
| 104 | } |
| 105 | |
| 106 | void ScopeContext::Profile(llvm::FoldingSetNodeID &ID, AnalysisContext *ctx, |
Ted Kremenek | 54c809b | 2009-08-21 23:39:58 +0000 | [diff] [blame] | 107 | const LocationContext *parent, const Stmt *s) { |
Zhongxing Xu | 18c7c06 | 2009-08-03 07:23:22 +0000 | [diff] [blame] | 108 | LocationContext::Profile(ID, Scope, ctx, parent); |
| 109 | ID.AddPointer(s); |
| 110 | } |
| 111 | |
Ted Kremenek | 58f5ec7 | 2009-10-20 21:39:41 +0000 | [diff] [blame] | 112 | LocationContextManager::~LocationContextManager() { |
| 113 | clear(); |
| 114 | } |
| 115 | |
| 116 | void LocationContextManager::clear() { |
| 117 | for (llvm::FoldingSet<LocationContext>::iterator I = Contexts.begin(), |
| 118 | E = Contexts.end(); I != E; ) { |
| 119 | LocationContext *LC = &*I; |
| 120 | ++I; |
| 121 | delete LC; |
| 122 | } |
| 123 | |
| 124 | Contexts.clear(); |
| 125 | } |
| 126 | |
Ted Kremenek | 54c809b | 2009-08-21 23:39:58 +0000 | [diff] [blame] | 127 | StackFrameContext* |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 128 | LocationContextManager::getStackFrame(AnalysisContext *ctx, |
Ted Kremenek | 54c809b | 2009-08-21 23:39:58 +0000 | [diff] [blame] | 129 | const LocationContext *parent, |
| 130 | const Stmt *s) { |
Zhongxing Xu | 18c7c06 | 2009-08-03 07:23:22 +0000 | [diff] [blame] | 131 | llvm::FoldingSetNodeID ID; |
| 132 | StackFrameContext::Profile(ID, ctx, parent, s); |
| 133 | void *InsertPos; |
| 134 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 135 | StackFrameContext *f = |
Zhongxing Xu | 18c7c06 | 2009-08-03 07:23:22 +0000 | [diff] [blame] | 136 | cast_or_null<StackFrameContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos)); |
| 137 | if (!f) { |
| 138 | f = new StackFrameContext(ctx, parent, s); |
| 139 | Contexts.InsertNode(f, InsertPos); |
| 140 | } |
| 141 | return f; |
| 142 | } |
| 143 | |
| 144 | ScopeContext *LocationContextManager::getScope(AnalysisContext *ctx, |
Ted Kremenek | 54c809b | 2009-08-21 23:39:58 +0000 | [diff] [blame] | 145 | const LocationContext *parent, |
| 146 | const Stmt *s) { |
Zhongxing Xu | 18c7c06 | 2009-08-03 07:23:22 +0000 | [diff] [blame] | 147 | llvm::FoldingSetNodeID ID; |
| 148 | ScopeContext::Profile(ID, ctx, parent, s); |
| 149 | void *InsertPos; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 150 | |
Zhongxing Xu | 18c7c06 | 2009-08-03 07:23:22 +0000 | [diff] [blame] | 151 | ScopeContext *scope = |
| 152 | cast_or_null<ScopeContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos)); |
| 153 | |
| 154 | if (!scope) { |
| 155 | scope = new ScopeContext(ctx, parent, s); |
| 156 | Contexts.InsertNode(scope, InsertPos); |
| 157 | } |
| 158 | return scope; |
| 159 | } |