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 | |
Zhongxing Xu | 97ab394 | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 36 | Stmt *AnalysisContext::getBody() { |
Ted Kremenek | 2376002 | 2009-08-21 23:58:43 +0000 | [diff] [blame] | 37 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
Zhongxing Xu | 97ab394 | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 38 | return FD->getBody(); |
Ted Kremenek | 2376002 | 2009-08-21 23:58:43 +0000 | [diff] [blame] | 39 | else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Zhongxing Xu | 97ab394 | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 40 | return MD->getBody(); |
| 41 | |
Mike Stump | 87a05f1 | 2009-07-31 01:10:29 +0000 | [diff] [blame] | 42 | llvm::llvm_unreachable("unknown code decl"); |
Zhongxing Xu | 97ab394 | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Ted Kremenek | 82cd37c | 2009-08-21 23:25:54 +0000 | [diff] [blame] | 45 | const ImplicitParamDecl *AnalysisContext::getSelfDecl() const { |
| 46 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 47 | return MD->getSelfDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 48 | |
Ted Kremenek | 82cd37c | 2009-08-21 23:25:54 +0000 | [diff] [blame] | 49 | return NULL; |
| 50 | } |
| 51 | |
Zhongxing Xu | 97ab394 | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 52 | CFG *AnalysisContext::getCFG() { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 53 | if (!cfg) |
Zhongxing Xu | 97ab394 | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 54 | cfg = CFG::buildCFG(getBody(), &D->getASTContext()); |
| 55 | return cfg; |
| 56 | } |
| 57 | |
| 58 | ParentMap &AnalysisContext::getParentMap() { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 59 | if (!PM) |
Zhongxing Xu | 97ab394 | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 60 | PM = new ParentMap(getBody()); |
| 61 | return *PM; |
| 62 | } |
| 63 | |
| 64 | LiveVariables *AnalysisContext::getLiveVariables() { |
| 65 | if (!liveness) { |
| 66 | CFG *c = getCFG(); |
| 67 | if (!c) |
| 68 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 69 | |
Zhongxing Xu | 97ab394 | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 70 | liveness = new LiveVariables(D->getASTContext(), *c); |
| 71 | liveness->runOnCFG(*c); |
| 72 | liveness->runOnAllBlocks(*c, 0, true); |
| 73 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 74 | |
Zhongxing Xu | 97ab394 | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 75 | return liveness; |
| 76 | } |
| 77 | |
Ted Kremenek | 2376002 | 2009-08-21 23:58:43 +0000 | [diff] [blame] | 78 | AnalysisContext *AnalysisContextManager::getContext(const Decl *D) { |
| 79 | AnalysisContext *&AC = Contexts[D]; |
| 80 | if (!AC) |
| 81 | AC = new AnalysisContext(D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 82 | |
Ted Kremenek | 2376002 | 2009-08-21 23:58:43 +0000 | [diff] [blame] | 83 | return AC; |
Zhongxing Xu | 97ab394 | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 84 | } |
Zhongxing Xu | 18c7c06 | 2009-08-03 07:23:22 +0000 | [diff] [blame] | 85 | |
| 86 | void LocationContext::Profile(llvm::FoldingSetNodeID &ID, ContextKind k, |
Ted Kremenek | 54c809b | 2009-08-21 23:39:58 +0000 | [diff] [blame] | 87 | AnalysisContext *ctx, |
| 88 | const LocationContext *parent) { |
Zhongxing Xu | 18c7c06 | 2009-08-03 07:23:22 +0000 | [diff] [blame] | 89 | ID.AddInteger(k); |
| 90 | ID.AddPointer(ctx); |
| 91 | ID.AddPointer(parent); |
| 92 | } |
| 93 | |
| 94 | void StackFrameContext::Profile(llvm::FoldingSetNodeID &ID,AnalysisContext *ctx, |
Ted Kremenek | 54c809b | 2009-08-21 23:39:58 +0000 | [diff] [blame] | 95 | const LocationContext *parent, const Stmt *s) { |
Zhongxing Xu | 18c7c06 | 2009-08-03 07:23:22 +0000 | [diff] [blame] | 96 | LocationContext::Profile(ID, StackFrame, ctx, parent); |
| 97 | ID.AddPointer(s); |
| 98 | } |
| 99 | |
| 100 | void ScopeContext::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, Scope, ctx, parent); |
| 103 | ID.AddPointer(s); |
| 104 | } |
| 105 | |
Ted Kremenek | 54c809b | 2009-08-21 23:39:58 +0000 | [diff] [blame] | 106 | StackFrameContext* |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 107 | LocationContextManager::getStackFrame(AnalysisContext *ctx, |
Ted Kremenek | 54c809b | 2009-08-21 23:39:58 +0000 | [diff] [blame] | 108 | const LocationContext *parent, |
| 109 | const Stmt *s) { |
Zhongxing Xu | 18c7c06 | 2009-08-03 07:23:22 +0000 | [diff] [blame] | 110 | llvm::FoldingSetNodeID ID; |
| 111 | StackFrameContext::Profile(ID, ctx, parent, s); |
| 112 | void *InsertPos; |
| 113 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 114 | StackFrameContext *f = |
Zhongxing Xu | 18c7c06 | 2009-08-03 07:23:22 +0000 | [diff] [blame] | 115 | cast_or_null<StackFrameContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos)); |
| 116 | if (!f) { |
| 117 | f = new StackFrameContext(ctx, parent, s); |
| 118 | Contexts.InsertNode(f, InsertPos); |
| 119 | } |
| 120 | return f; |
| 121 | } |
| 122 | |
| 123 | ScopeContext *LocationContextManager::getScope(AnalysisContext *ctx, |
Ted Kremenek | 54c809b | 2009-08-21 23:39:58 +0000 | [diff] [blame] | 124 | const LocationContext *parent, |
| 125 | const Stmt *s) { |
Zhongxing Xu | 18c7c06 | 2009-08-03 07:23:22 +0000 | [diff] [blame] | 126 | llvm::FoldingSetNodeID ID; |
| 127 | ScopeContext::Profile(ID, ctx, parent, s); |
| 128 | void *InsertPos; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 129 | |
Zhongxing Xu | 18c7c06 | 2009-08-03 07:23:22 +0000 | [diff] [blame] | 130 | ScopeContext *scope = |
| 131 | cast_or_null<ScopeContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos)); |
| 132 | |
| 133 | if (!scope) { |
| 134 | scope = new ScopeContext(ctx, parent, s); |
| 135 | Contexts.InsertNode(scope, InsertPos); |
| 136 | } |
| 137 | return scope; |
| 138 | } |